001/*
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.http.commons;
017
018import java.util.function.Supplier;
019
020import javax.inject.Inject;
021import javax.ws.rs.core.Context;
022import javax.ws.rs.core.HttpHeaders;
023import javax.ws.rs.core.UriInfo;
024
025import com.hp.hpl.jena.rdf.model.Resource;
026
027import org.fcrepo.http.commons.session.SessionFactory;
028import org.fcrepo.kernel.api.models.FedoraResource;
029import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
030import org.fcrepo.kernel.api.services.BinaryService;
031import org.fcrepo.kernel.api.services.NodeService;
032import org.fcrepo.kernel.api.services.ContainerService;
033import org.fcrepo.kernel.api.services.VersionService;
034import org.fcrepo.kernel.api.services.functions.HierarchicalIdentifierSupplier;
035import org.fcrepo.kernel.api.services.functions.UniqueValueSupplier;
036
037import org.jvnet.hk2.annotations.Optional;
038
039import com.google.common.eventbus.EventBus;
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    @Inject
061    protected SessionFactory sessions;
062
063    /**
064     * The JCR node service
065     */
066    @Inject
067    protected NodeService nodeService;
068
069    /**
070     * The repository object service
071     */
072    @Inject
073    protected ContainerService containerService;
074
075    /**
076     * The bitstream service
077     */
078    @Inject
079    protected BinaryService binaryService;
080
081    /**
082     * The version service
083     */
084    @Inject
085    protected VersionService versionService;
086
087    @Inject
088    @Optional
089    protected EventBus eventBus;
090
091    /**
092     * A resource that can mint new Fedora PIDs.
093     */
094    @Inject
095    @Optional
096    protected Supplier<String> pidMinter;
097
098    protected UniqueValueSupplier defaultPidMinter = new DefaultPathMinter();
099
100    /**
101     * Convert a JAX-RS list of PathSegments to a JCR path
102     *
103     * @param idTranslator the id translator
104     * @param originalPath the original path
105     * @return String jcr path
106     */
107    public static final String toPath(final IdentifierConverter<Resource, FedoraResource> idTranslator,
108                                      final String originalPath) {
109
110        final Resource resource = idTranslator.toDomain(originalPath);
111
112        final String path = idTranslator.asString(resource);
113
114        return path.isEmpty() ? "/" : path;
115    }
116
117    private static class DefaultPathMinter implements HierarchicalIdentifierSupplier { }
118
119}