001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.http.commons;
019
020import java.util.function.Supplier;
021
022import javax.inject.Inject;
023import javax.ws.rs.core.Context;
024import javax.ws.rs.core.HttpHeaders;
025import javax.ws.rs.core.UriInfo;
026
027import org.apache.jena.rdf.model.Resource;
028
029import org.fcrepo.kernel.api.models.FedoraResource;
030import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
031import org.fcrepo.kernel.api.models.ResourceFactory;
032import org.fcrepo.kernel.api.services.TimeMapService;
033import org.fcrepo.kernel.api.services.VersionService;
034import org.fcrepo.kernel.api.services.functions.ConfigurableHierarchicalSupplier;
035import org.fcrepo.kernel.api.services.functions.UniqueValueSupplier;
036
037import org.jvnet.hk2.annotations.Optional;
038
039/**
040 * Superclass for Fedora JAX-RS Resources, providing convenience fields and methods.
041 *
042 * @author ajs6f
043 */
044public class AbstractResource {
045
046    /**
047     * Useful for constructing URLs
048     */
049    @Context
050    protected UriInfo uriInfo;
051
052    /**
053     * For getting user agent
054     */
055    @Context
056    protected HttpHeaders headers;
057
058    @Inject
059    protected ResourceFactory resourceFactory;
060
061    /**
062     * The version service
063     */
064    @Inject
065    protected VersionService versionService;
066
067    /**
068     * The timemap service
069     */
070    @Inject
071    protected TimeMapService timeMapService;
072
073    /**
074     * A resource that can mint new Fedora PIDs.
075     */
076    @Inject
077    @Optional
078    protected Supplier<String> pidMinter;
079
080    // Mint non-hierarchical identifiers. To force pairtree creation as default, use
081    //  ConfigurableHierarchicalSupplier(int length, count) instead.
082    protected UniqueValueSupplier defaultPidMinter = new ConfigurableHierarchicalSupplier();
083
084    /**
085     * Convert a JAX-RS list of PathSegments to a JCR path
086     *
087     * @param idTranslator the id translator
088     * @param originalPath the original path
089     * @return String jcr path
090     */
091    public static String toPath(final IdentifierConverter<Resource, FedoraResource> idTranslator,
092                                final String originalPath) {
093
094        final Resource resource = idTranslator.toDomain(originalPath);
095
096        final String path = idTranslator.asString(resource);
097
098        return path.isEmpty() ? "/" : path;
099    }
100}