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