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