001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.auth.integration;
007
008import org.fcrepo.http.commons.AbstractResource;
009import org.fcrepo.http.commons.api.rdf.HttpIdentifierConverter;
010import org.fcrepo.kernel.api.exception.RepositoryException;
011import org.fcrepo.kernel.api.identifiers.FedoraId;
012import org.slf4j.Logger;
013import org.springframework.context.annotation.Scope;
014
015import javax.ws.rs.GET;
016import javax.ws.rs.POST;
017import javax.ws.rs.PUT;
018import javax.ws.rs.Path;
019import javax.ws.rs.PathParam;
020import javax.ws.rs.core.Response;
021import java.net.URI;
022
023import static org.slf4j.LoggerFactory.getLogger;
024
025/**
026 * This class acts as the REST Resource endpoint against which integration tests are executed.
027 * This is used instead of the real F4 REST API for two reasons:
028 * - These integration tests are intended to test the AuthZ functionality, not the F4 REST API
029 * - A circular dependency between fcrepo-auth-common <--> fcrepo-http-api is bad
030 *
031 * @author awoods
032 * @since 2014-06-26
033 */
034@Scope("prototype")
035@Path("/{path: .*}")
036public class RootTestResource extends AbstractResource {
037
038    private static final Logger LOGGER = getLogger(RootTestResource.class);
039
040    @GET
041    public Response get(@PathParam("path") final String externalPath) {
042        final FedoraId id = identifierConverter().pathToInternalId(externalPath);
043        LOGGER.trace("GET: {}", id.getFullIdPath());
044        return Response.ok().build();
045    }
046
047    @PUT
048    public Response put(@PathParam("path") final String externalPath) throws Exception {
049        final FedoraId id = identifierConverter().pathToInternalId(externalPath);
050        LOGGER.trace("PUT: {}", id.getFullIdPath());
051        return doRequest(id);
052    }
053
054    @POST
055    public Response post(@PathParam("path") final String externalPath) throws Exception {
056        final FedoraId id = identifierConverter().pathToInternalId(externalPath);
057        LOGGER.trace("POST: {}", id.getFullIdPath());
058        return doRequest(id);
059    }
060
061    private Response doRequest(final FedoraId id) throws RepositoryException {
062        final URI location = URI.create(identifierConverter().toExternalId(id.getFullId()));
063        return Response.created(location).build();
064    }
065
066    private HttpIdentifierConverter identifierConverter() {
067        return new HttpIdentifierConverter(uriInfo.getBaseUriBuilder().clone().path(RootTestResource.class));
068    }
069
070}