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
018import static javax.ws.rs.core.MediaType.APPLICATION_XHTML_XML;
019import static javax.ws.rs.core.MediaType.APPLICATION_XML;
020import static javax.ws.rs.core.MediaType.TEXT_HTML;
021import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
022import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
023import static javax.ws.rs.core.Response.created;
024import static javax.ws.rs.core.Response.noContent;
025import static javax.ws.rs.core.Response.status;
026import static org.apache.commons.lang3.StringUtils.isBlank;
027import static org.fcrepo.http.commons.domain.RDFMediaType.JSON_LD;
028import static org.fcrepo.http.commons.domain.RDFMediaType.N3;
029import static org.fcrepo.http.commons.domain.RDFMediaType.N3_ALT2;
030import static org.fcrepo.http.commons.domain.RDFMediaType.NTRIPLES;
031import static org.fcrepo.http.commons.domain.RDFMediaType.RDF_XML;
032import static org.fcrepo.http.commons.domain.RDFMediaType.TURTLE;
033import static org.fcrepo.http.commons.domain.RDFMediaType.TURTLE_X;
034import static org.fcrepo.kernel.modeshape.identifiers.NodeResourceConverter.nodeToResource;
035import static org.slf4j.LoggerFactory.getLogger;
036
037import java.net.URI;
038
039import javax.inject.Inject;
040import javax.jcr.RepositoryException;
041import javax.jcr.Session;
042import javax.servlet.http.HttpServletResponse;
043import javax.ws.rs.DELETE;
044import javax.ws.rs.GET;
045import javax.ws.rs.HeaderParam;
046import javax.ws.rs.POST;
047import javax.ws.rs.PUT;
048import javax.ws.rs.Path;
049import javax.ws.rs.PathParam;
050import javax.ws.rs.Produces;
051import javax.ws.rs.core.Context;
052import javax.ws.rs.core.Request;
053import javax.ws.rs.core.Response;
054import javax.ws.rs.core.UriInfo;
055
056import org.fcrepo.http.commons.responses.HtmlTemplate;
057import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
058import org.fcrepo.kernel.api.exception.RepositoryVersionRuntimeException;
059import org.fcrepo.kernel.api.models.FedoraResource;
060import org.fcrepo.kernel.api.utils.iterators.RdfStream;
061import org.fcrepo.kernel.modeshape.rdf.impl.VersionsRdfContext;
062import org.slf4j.Logger;
063import org.springframework.context.annotation.Scope;
064
065import com.google.common.annotations.VisibleForTesting;
066
067/**
068 * @author cabeer
069 * @since 9/25/14
070 */
071@Scope("request")
072@Path("/{path: .*}/fcr:versions")
073public class FedoraVersioning extends FedoraBaseResource {
074
075    @Inject
076    protected Session session;
077
078    private static final Logger LOGGER = getLogger(FedoraVersioning.class);
079
080
081    @Context protected Request request;
082    @Context protected HttpServletResponse servletResponse;
083    @Context protected UriInfo uriInfo;
084
085    @PathParam("path") protected String externalPath;
086
087    protected FedoraResource resource;
088
089
090    /**
091     * Default JAX-RS entry point
092     */
093    public FedoraVersioning() {
094        super();
095    }
096
097    /**
098     * Create a new FedoraNodes instance for a given path
099     * @param externalPath the external path
100     */
101    @VisibleForTesting
102    public FedoraVersioning(final String externalPath) {
103        this.externalPath = externalPath;
104    }
105
106
107    /**
108     * Enable versioning
109     * @return the response
110     */
111    @PUT
112    public Response enableVersioning() {
113        LOGGER.info("Enable versioning for '{}'", externalPath);
114        resource().enableVersioning();
115
116        try {
117            session.save();
118        } catch (final RepositoryException e) {
119            throw new RepositoryRuntimeException(e);
120        }
121        return created(uriInfo.getRequestUri()).build();
122    }
123
124    /**
125     * Disable versioning
126     * @return the response
127     */
128    @DELETE
129    public Response disableVersioning() {
130        LOGGER.info("Disable versioning for '{}'", externalPath);
131        resource().disableVersioning();
132
133        try {
134            session.save();
135        } catch (final RepositoryException e) {
136            throw new RepositoryRuntimeException(e);
137        }
138
139        return noContent().build();
140    }
141
142    /**
143     * Create a new version checkpoint and tag it with the given label.  If
144     * that label already describes another version it will silently be
145     * reassigned to describe this version.
146     *
147     * @param slug the value of slug
148     * @return response
149     * @throws RepositoryException if repository exception occurred
150     */
151    @POST
152    public Response addVersion(@HeaderParam("Slug") final String slug) throws RepositoryException {
153        if (!isBlank(slug)) {
154            LOGGER.info("Request to add version '{}' for '{}'", slug, externalPath);
155            final String path = toPath(translator(), externalPath);
156            versionService.createVersion(session, path, slug);
157            return created(URI.create(nodeToResource(translator()).convert(
158                            resource().getBaseVersion().getFrozenNode()).getURI())).build();
159        }
160        return status(BAD_REQUEST).entity("Specify label for version").build();
161    }
162
163
164    /**
165     * Get the list of versions for the object
166     *
167     * @return List of versions for the object as RDF
168     */
169    @GET
170    @HtmlTemplate(value = "fcr:versions")
171    @Produces({TURTLE + ";qs=10", JSON_LD + ";qs=8", N3, N3_ALT2, RDF_XML, NTRIPLES, APPLICATION_XML, TEXT_PLAIN,
172            TURTLE_X, TEXT_HTML, APPLICATION_XHTML_XML, "*/*"})
173    public RdfStream getVersionList() {
174        if (!resource().isVersioned()) {
175            throw new RepositoryVersionRuntimeException("This operation requires that the node be versionable");
176        }
177
178        return resource().getTriples(translator(), VersionsRdfContext.class)
179                .session(session)
180                .topic(translator().reverse().convert(resource()).asNode());
181    }
182
183    protected FedoraResource resource() {
184        if (resource == null) {
185            resource = getResourceFromPath(externalPath);
186        }
187
188        return resource;
189    }
190
191    @Override
192    protected Session session() {
193        return session;
194    }
195}