001/**
002 * Copyright 2014 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.api;
017
018
019import static javax.ws.rs.core.MediaType.APPLICATION_XHTML_XML;
020import static javax.ws.rs.core.MediaType.APPLICATION_XML;
021import static javax.ws.rs.core.MediaType.TEXT_HTML;
022import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
023import static javax.ws.rs.core.Response.noContent;
024import static org.fcrepo.http.commons.domain.RDFMediaType.JSON_LD;
025import static org.fcrepo.http.commons.domain.RDFMediaType.N3;
026import static org.fcrepo.http.commons.domain.RDFMediaType.N3_ALT2;
027import static org.fcrepo.http.commons.domain.RDFMediaType.NTRIPLES;
028import static org.fcrepo.http.commons.domain.RDFMediaType.RDF_XML;
029import static org.fcrepo.http.commons.domain.RDFMediaType.TURTLE;
030import static org.fcrepo.http.commons.domain.RDFMediaType.TURTLE_X;
031import static org.fcrepo.kernel.FedoraJcrTypes.FCR_VERSIONS;
032import static org.slf4j.LoggerFactory.getLogger;
033
034import java.io.IOException;
035
036import javax.annotation.PostConstruct;
037import javax.inject.Inject;
038import javax.jcr.RepositoryException;
039import javax.jcr.Session;
040import javax.ws.rs.DELETE;
041import javax.ws.rs.GET;
042import javax.ws.rs.HeaderParam;
043import javax.ws.rs.Path;
044import javax.ws.rs.PathParam;
045import javax.ws.rs.Produces;
046import javax.ws.rs.core.Response;
047
048import org.fcrepo.http.commons.domain.PATCH;
049import org.fcrepo.kernel.models.FedoraBinary;
050import org.fcrepo.kernel.models.FedoraResource;
051import org.fcrepo.kernel.utils.iterators.RdfStream;
052import org.slf4j.Logger;
053import org.springframework.context.annotation.Scope;
054
055import com.google.common.annotations.VisibleForTesting;
056
057/**
058 * Endpoint for managing versions of nodes
059 *
060 * @author awoods
061 */
062@Scope("request")
063@Path("/{path: .*}/fcr:versions/{labelAndOptionalPathIntoVersion: .*}")
064public class FedoraVersions extends ContentExposingResource {
065
066    @Inject
067    protected Session session;
068
069    private static final Logger LOGGER = getLogger(FedoraVersions.class);
070
071    @PathParam("path") protected String externalPath;
072
073    @PathParam("labelAndOptionalPathIntoVersion") protected String pathListIntoVersion;
074
075    protected String path;
076    protected String label;
077    protected String pathIntoVersion;
078
079    protected FedoraResource resource;
080    protected FedoraResource baseResource;
081
082    /**
083     * Default JAX-RS entry point
084     */
085    public FedoraVersions() {
086        super();
087    }
088
089    /**
090     * Create a new FedoraNodes instance for a given path
091     * @param path
092     */
093    @VisibleForTesting
094    public FedoraVersions(final String path, final String label, final String pathIntoVersion) {
095        this.path = path;
096        this.label = label;
097        this.pathIntoVersion = pathIntoVersion;
098    }
099
100    @PostConstruct
101    private void postConstruct() {
102        this.path = externalPath + "/" + FCR_VERSIONS + "/" + pathListIntoVersion;
103        this.label = pathListIntoVersion.split("/", 2)[0];
104    }
105
106    /**
107     * Reverts the resource at the given path to the version specified by
108     * the label.
109     * @return response
110     * @throws RepositoryException
111     */
112    @PATCH
113    public Response revertToVersion() throws RepositoryException {
114        LOGGER.info("Reverting {} to version {}.", path,
115                label);
116        versionService.revertToVersion(session, unversionedResourcePath(), label);
117        return noContent().build();
118    }
119
120    /**
121     * Removes the version specified by the label.
122     * @return 204 No Content
123     * @throws RepositoryException
124    **/
125    @DELETE
126    public Response removeVersion() throws RepositoryException {
127        LOGGER.info("Removing {} version {}.", path, label);
128        versionService.removeVersion(session, unversionedResourcePath(), label);
129        return noContent().build();
130    }
131
132    /**
133     * Retrieve a version of an object.  The path structure is as follows
134     * (though these URLs are returned from getVersionList and need not be
135     * constructed manually):
136     * /versionable-node/fcr:versions/label/path/to/any/copied/unversionable/nodes
137     * @return the version of the object as RDF in the requested format
138     */
139    @GET
140    @Produces({TURTLE + ";qs=10", JSON_LD + ";qs=8",
141            N3, N3_ALT2, RDF_XML, NTRIPLES, APPLICATION_XML, TEXT_PLAIN, TURTLE_X,
142            TEXT_HTML, APPLICATION_XHTML_XML, "*/*"})
143    public Response getVersion(@HeaderParam("Range") final String rangeValue) throws IOException {
144        LOGGER.trace("Getting version profile for: {} at version: {}", path,
145                label);
146        checkCacheControlHeaders(request, servletResponse, resource(), session);
147        final RdfStream rdfStream = new RdfStream().session(session).topic(
148                translator().reverse().convert(resource()).asNode());
149        return getContent(rangeValue, rdfStream);
150    }
151
152    protected String unversionedResourcePath() throws RepositoryException {
153
154        if (baseResource == null) {
155            baseResource = getResourceFromPath(externalPath);
156            if ( baseResource instanceof FedoraBinary ) {
157                baseResource = ((FedoraBinary)baseResource).getDescription();
158            }
159        }
160
161        return baseResource.getPath();
162    }
163
164    @Override
165    protected FedoraResource resource() {
166
167        if (resource == null) {
168            resource = getResourceFromPath(path);
169        }
170
171        return resource;
172    }
173
174    @Override
175    protected void addResourceHttpHeaders(final FedoraResource resource) {
176        // no-op
177    }
178
179    @Override
180    protected String externalPath() {
181        return externalPath;
182    }
183
184
185    @Override
186    protected Session session() {
187        return session;
188    }
189}