001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.auth.integration;
019
020import org.apache.jena.rdf.model.Resource;
021import org.fcrepo.http.commons.AbstractResource;
022import org.fcrepo.http.commons.api.rdf.HttpResourceConverter;
023import org.fcrepo.http.commons.session.HttpSession;
024import org.fcrepo.kernel.api.models.FedoraResource;
025import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
026import org.modeshape.jcr.api.JcrTools;
027import org.slf4j.Logger;
028import org.springframework.context.annotation.Scope;
029
030import javax.inject.Inject;
031import javax.jcr.Node;
032import javax.jcr.RepositoryException;
033import javax.ws.rs.GET;
034import javax.ws.rs.POST;
035import javax.ws.rs.PUT;
036import javax.ws.rs.Path;
037import javax.ws.rs.PathParam;
038import javax.ws.rs.core.Response;
039import java.net.URI;
040
041import static org.fcrepo.kernel.modeshape.FedoraSessionImpl.getJcrSession;
042import static org.slf4j.LoggerFactory.getLogger;
043
044/**
045 * This class acts as the REST Resource endpoint against which integration tests are executed.
046 * This is used instead of the real F4 REST API for two reasons:
047 * - These integration tests are intended to test the AuthZ functionality, not the F4 REST API
048 * - A circular dependency between fcrepo-auth-common <--> fcrepo-http-api is bad
049 *
050 * @author awoods
051 * @since 2014-06-26
052 */
053@Scope("prototype")
054@Path("/{path: .*}")
055public class RootTestResource extends AbstractResource {
056
057    @Inject
058    private HttpSession session;
059
060    private static final Logger LOGGER = getLogger(RootTestResource.class);
061
062    @GET
063    public Response get(@PathParam("path") final String externalPath) {
064        final String path = toPath(translator(), externalPath);
065        LOGGER.trace("GET: {}", path);
066        return Response.ok().build();
067    }
068
069    @PUT
070    public Response put(@PathParam("path") final String externalPath) throws Exception {
071        final String path = toPath(translator(), externalPath);
072        LOGGER.trace("PUT: {}", path);
073        return doRequest(path);
074    }
075
076    @POST
077    public Response post(@PathParam("path") final String externalPath) throws Exception {
078        final String path = toPath(translator(), externalPath);
079        LOGGER.trace("POST: {}", path);
080        return doRequest(path);
081    }
082
083    private Response doRequest(final String path) throws RepositoryException {
084        final JcrTools jcrTools = new JcrTools();
085        final Node node = jcrTools.findOrCreateNode(getJcrSession(session.getFedoraSession()), path);
086        final URI location = uriInfo.getBaseUriBuilder().path(node.getPath()).build();
087        return Response.created(location).build();
088    }
089
090    private IdentifierConverter<Resource,FedoraResource> translator() {
091        return new HttpResourceConverter(session,
092                    uriInfo.getBaseUriBuilder().clone().path(RootTestResource.class));
093    }
094
095}