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 com.hp.hpl.jena.rdf.model.Resource;
028
029import org.fcrepo.http.commons.session.SessionFactory;
030import org.fcrepo.kernel.api.models.FedoraResource;
031import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
032import org.fcrepo.kernel.api.services.BinaryService;
033import org.fcrepo.kernel.api.services.NamespaceService;
034import org.fcrepo.kernel.api.services.NodeService;
035import org.fcrepo.kernel.api.services.ContainerService;
036import org.fcrepo.kernel.api.services.VersionService;
037import org.fcrepo.kernel.api.services.functions.HierarchicalIdentifierSupplier;
038import org.fcrepo.kernel.api.services.functions.UniqueValueSupplier;
039
040import org.jvnet.hk2.annotations.Optional;
041
042import com.google.common.eventbus.EventBus;
043
044/**
045 * Superclass for Fedora JAX-RS Resources, providing convenience fields and methods.
046 *
047 * @author ajs6f
048 */
049public class AbstractResource {
050
051    /**
052     * Useful for constructing URLs
053     */
054    @Context
055    protected UriInfo uriInfo;
056
057    /**
058     * For getting user agent
059     */
060    @Context
061    protected HttpHeaders headers;
062
063    @Inject
064    protected SessionFactory sessions;
065
066    /**
067     * The JCR node service
068     */
069    @Inject
070    protected NodeService nodeService;
071
072    /**
073     * The repository object service
074     */
075    @Inject
076    protected ContainerService containerService;
077
078    /**
079     * The bitstream service
080     */
081    @Inject
082    protected BinaryService binaryService;
083
084    /**
085     * The version service
086     */
087    @Inject
088    protected VersionService versionService;
089
090    /**
091     * The namespace service
092     */
093    @Inject
094    protected NamespaceService namespaceService;
095
096    @Inject
097    @Optional
098    protected EventBus eventBus;
099
100    /**
101     * A resource that can mint new Fedora PIDs.
102     */
103    @Inject
104    @Optional
105    protected Supplier<String> pidMinter;
106
107    protected UniqueValueSupplier defaultPidMinter = new DefaultPathMinter();
108
109    /**
110     * Convert a JAX-RS list of PathSegments to a JCR path
111     *
112     * @param idTranslator the id translator
113     * @param originalPath the original path
114     * @return String jcr path
115     */
116    public static final String toPath(final IdentifierConverter<Resource, FedoraResource> idTranslator,
117                                      final String originalPath) {
118
119        final Resource resource = idTranslator.toDomain(originalPath);
120
121        final String path = idTranslator.asString(resource);
122
123        return path.isEmpty() ? "/" : path;
124    }
125
126    private static class DefaultPathMinter implements HierarchicalIdentifierSupplier { }
127
128}