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.services.BinaryService;
032import org.fcrepo.kernel.api.services.NodeService;
033import org.fcrepo.kernel.api.services.ContainerService;
034import org.fcrepo.kernel.api.services.VersionService;
035import org.fcrepo.kernel.api.services.functions.ConfigurableHierarchicalSupplier;
036import org.fcrepo.kernel.api.services.functions.UniqueValueSupplier;
037
038import org.jvnet.hk2.annotations.Optional;
039
040/**
041 * Superclass for Fedora JAX-RS Resources, providing convenience fields and methods.
042 *
043 * @author ajs6f
044 */
045public class AbstractResource {
046
047    /**
048     * Useful for constructing URLs
049     */
050    @Context
051    protected UriInfo uriInfo;
052
053    /**
054     * For getting user agent
055     */
056    @Context
057    protected HttpHeaders headers;
058
059    /**
060     * The JCR node service
061     */
062    @Inject
063    protected NodeService nodeService;
064
065    /**
066     * The repository object service
067     */
068    @Inject
069    protected ContainerService containerService;
070
071    /**
072     * The bitstream service
073     */
074    @Inject
075    protected BinaryService binaryService;
076
077    /**
078     * The version service
079     */
080    @Inject
081    protected VersionService versionService;
082
083    /**
084     * A resource that can mint new Fedora PIDs.
085     */
086    @Inject
087    @Optional
088    protected Supplier<String> pidMinter;
089
090    // Mint non-hierarchical identifiers. To force pairtree creation as default, use
091    //  ConfigurableHierarchicalSupplier(int length, count) instead.
092    protected UniqueValueSupplier defaultPidMinter = new ConfigurableHierarchicalSupplier();
093
094    /**
095     * Convert a JAX-RS list of PathSegments to a JCR path
096     *
097     * @param idTranslator the id translator
098     * @param originalPath the original path
099     * @return String jcr path
100     */
101    public static String toPath(final IdentifierConverter<Resource, FedoraResource> idTranslator,
102                                final String originalPath) {
103
104        final Resource resource = idTranslator.toDomain(originalPath);
105
106        final String path = idTranslator.asString(resource);
107
108        return path.isEmpty() ? "/" : path;
109    }
110}