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