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.fcrepo.http.commons.AbstractResource;
021import org.fcrepo.http.commons.api.rdf.HttpIdentifierConverter;
022import org.fcrepo.kernel.api.exception.RepositoryException;
023import org.fcrepo.kernel.api.identifiers.FedoraId;
024import org.slf4j.Logger;
025import org.springframework.context.annotation.Scope;
026
027import javax.ws.rs.GET;
028import javax.ws.rs.POST;
029import javax.ws.rs.PUT;
030import javax.ws.rs.Path;
031import javax.ws.rs.PathParam;
032import javax.ws.rs.core.Response;
033import java.net.URI;
034
035import static org.slf4j.LoggerFactory.getLogger;
036
037/**
038 * This class acts as the REST Resource endpoint against which integration tests are executed.
039 * This is used instead of the real F4 REST API for two reasons:
040 * - These integration tests are intended to test the AuthZ functionality, not the F4 REST API
041 * - A circular dependency between fcrepo-auth-common <--> fcrepo-http-api is bad
042 *
043 * @author awoods
044 * @since 2014-06-26
045 */
046@Scope("prototype")
047@Path("/{path: .*}")
048public class RootTestResource extends AbstractResource {
049
050    private static final Logger LOGGER = getLogger(RootTestResource.class);
051
052    @GET
053    public Response get(@PathParam("path") final String externalPath) {
054        final FedoraId id = identifierConverter().pathToInternalId(externalPath);
055        LOGGER.trace("GET: {}", id.getFullIdPath());
056        return Response.ok().build();
057    }
058
059    @PUT
060    public Response put(@PathParam("path") final String externalPath) throws Exception {
061        final FedoraId id = identifierConverter().pathToInternalId(externalPath);
062        LOGGER.trace("PUT: {}", id.getFullIdPath());
063        return doRequest(id);
064    }
065
066    @POST
067    public Response post(@PathParam("path") final String externalPath) throws Exception {
068        final FedoraId id = identifierConverter().pathToInternalId(externalPath);
069        LOGGER.trace("POST: {}", id.getFullIdPath());
070        return doRequest(id);
071    }
072
073    private Response doRequest(final FedoraId id) throws RepositoryException {
074        final URI location = URI.create(identifierConverter().toExternalId(id.getFullId()));
075        return Response.created(location).build();
076    }
077
078    private HttpIdentifierConverter identifierConverter() {
079        return new HttpIdentifierConverter(uriInfo.getBaseUriBuilder().clone().path(RootTestResource.class));
080    }
081
082}