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