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 javax.inject.Inject;
019import javax.ws.rs.core.Context;
020import javax.ws.rs.core.HttpHeaders;
021import javax.ws.rs.core.UriInfo;
022
023import com.hp.hpl.jena.rdf.model.Resource;
024
025import org.fcrepo.http.commons.session.SessionFactory;
026import org.fcrepo.kernel.models.FedoraResource;
027import org.fcrepo.kernel.identifiers.IdentifierConverter;
028import org.fcrepo.kernel.identifiers.PidMinter;
029import org.fcrepo.kernel.services.BinaryService;
030import org.fcrepo.kernel.services.NodeService;
031import org.fcrepo.kernel.services.ContainerService;
032import org.fcrepo.kernel.services.VersionService;
033import org.jvnet.hk2.annotations.Optional;
034import org.slf4j.bridge.SLF4JBridgeHandler;
035import org.springframework.beans.factory.annotation.Autowired;
036
037import com.google.common.eventbus.EventBus;
038
039/**
040 * Superclass for Fedora JAX-RS Resources, providing convenience fields
041 * 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    @Autowired
067    protected SessionFactory sessions;
068
069    /**
070     * The fcrepo node service
071     */
072    @Autowired
073    protected NodeService nodeService;
074
075    /**
076     * The fcrepo object service
077     */
078    @Autowired
079    protected ContainerService containerService;
080
081    /**
082     * The fcrepo datastream service
083     */
084    @Autowired
085    protected BinaryService binaryService;
086
087    /**
088     * The fcrepo version service
089     */
090    @Autowired
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    @Autowired
101    protected PidMinter 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        if (path.isEmpty()) {
118            return "/";
119        }
120        return path;
121    }
122
123}