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;
031//import org.fcrepo.kernel.services.RepositoryService;
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 * Abstract superclass for Fedora JAX-RS Resources, providing convenience fields
041 * and methods.
042 *
043 * @author ajs6f
044 */
045public abstract 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    @Autowired
061    protected SessionFactory sessions;
062
063    /**
064     * The fcrepo node service
065     */
066    @Autowired
067    protected NodeService nodeService;
068
069    /**
070     * The fcrepo object service
071     */
072    @Autowired
073    protected ContainerService containerService;
074
075    /**
076     * The fcrepo datastream service
077     */
078    @Autowired
079    protected BinaryService binaryService;
080
081    /**
082     * The fcrepo version service
083     */
084    @Autowired
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    @Autowired
095    protected PidMinter pidMinter;
096
097    /**
098     * Convert a JAX-RS list of PathSegments to a JCR path
099     *
100     * @param idTranslator
101     * @param originalPath
102     * @return String jcr path
103     */
104    public static final String toPath(final IdentifierConverter<Resource, FedoraResource> idTranslator,
105                                      final String originalPath) {
106
107        final Resource resource = idTranslator.toDomain(originalPath);
108
109        final String path = idTranslator.asString(resource);
110
111        if (path.isEmpty()) {
112            return "/";
113        }
114        return path;
115    }
116
117}