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.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.api.FedoraTypes.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.api.models.FedoraBinary;
050import org.fcrepo.kernel.api.models.FedoraResource;
051import org.fcrepo.kernel.api.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 * @author ajs6f
062 */
063@Scope("request")
064@Path("/{path: .*}/fcr:versions/{labelAndOptionalPathIntoVersion: .*}")
065public class FedoraVersions extends ContentExposingResource {
066
067    @Inject
068    protected Session session;
069
070    private static final Logger LOGGER = getLogger(FedoraVersions.class);
071
072    @PathParam("path") protected String externalPath;
073
074    @PathParam("labelAndOptionalPathIntoVersion") protected String pathListIntoVersion;
075
076    protected String path;
077    protected String label;
078    protected String pathIntoVersion;
079
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 the path
092     * @param label the label
093     * @param pathIntoVersion the string value of pathIntoVersion
094     */
095    @VisibleForTesting
096    public FedoraVersions(final String path, final String label, final String pathIntoVersion) {
097        this.path = path;
098        this.label = label;
099        this.pathIntoVersion = pathIntoVersion;
100    }
101
102    @PostConstruct
103    private void postConstruct() {
104        this.path = externalPath + "/" + FCR_VERSIONS + "/" + pathListIntoVersion;
105        this.label = pathListIntoVersion.split("/", 2)[0];
106    }
107
108    /**
109     * Reverts the resource at the given path to the version specified by
110     * the label.
111     * @return response
112     * @throws RepositoryException if repository exception occurred
113     */
114    @PATCH
115    public Response revertToVersion() throws RepositoryException {
116        LOGGER.info("Reverting {} to version {}.", path,
117                label);
118        versionService.revertToVersion(session, unversionedResourcePath(), label);
119        return noContent().build();
120    }
121
122    /**
123     * Removes the version specified by the label.
124     * @return 204 No Content
125     * @throws RepositoryException if repository exception occurred
126    **/
127    @DELETE
128    public Response removeVersion() throws RepositoryException {
129        LOGGER.info("Removing {} version {}.", path, label);
130        versionService.removeVersion(session, unversionedResourcePath(), label);
131        return noContent().build();
132    }
133
134    /**
135     * Retrieve a version of an object.  The path structure is as follows
136     * (though these URLs are returned from getVersionList and need not be
137     * constructed manually):
138     * /versionable-node/fcr:versions/label/path/to/any/copied/unversionable/nodes
139     * @param rangeValue the range value
140     * @throws IOException if IO exception occurred
141     * @return the version of the object as RDF in the requested format
142     */
143    @GET
144    @Produces({TURTLE + ";qs=10", JSON_LD + ";qs=8",
145            N3, N3_ALT2, RDF_XML, NTRIPLES, APPLICATION_XML, TEXT_PLAIN, TURTLE_X,
146            TEXT_HTML, APPLICATION_XHTML_XML, "*/*"})
147    public Response getVersion(@HeaderParam("Range") final String rangeValue) throws IOException {
148        LOGGER.trace("Getting version profile for: {} at version: {}", path,
149                label);
150        checkCacheControlHeaders(request, servletResponse, resource(), session);
151        final RdfStream rdfStream = new RdfStream().session(session).topic(
152                translator().reverse().convert(resource()).asNode());
153        return getContent(rangeValue, rdfStream);
154    }
155
156    protected String unversionedResourcePath() {
157
158        if (baseResource == null) {
159            baseResource = getResourceFromPath(externalPath);
160            if ( baseResource instanceof FedoraBinary ) {
161                baseResource = ((FedoraBinary)baseResource).getDescription();
162            }
163        }
164
165        return baseResource.getPath();
166    }
167
168    @Override
169    protected FedoraResource resource() {
170
171        if (resource == null) {
172            resource = getResourceFromPath(path);
173        }
174
175        return resource;
176    }
177
178    @Override
179    protected void addResourceHttpHeaders(final FedoraResource resource) {
180        // no-op
181    }
182
183    @Override
184    protected String externalPath() {
185        return externalPath;
186    }
187
188
189    @Override
190    protected Session session() {
191        return session;
192    }
193}