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