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.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.fcrepo.kernel.modeshape.FedoraSessionImpl.getJcrSession;
041import static org.slf4j.LoggerFactory.getLogger;
042
043/**
044 * This class acts as the REST Resource endpoint against which integration tests are executed.
045 * This is used instead of the real F4 REST API for two reasons:
046 * - These integration tests are intended to test the AuthZ functionality, not the F4 REST API
047 * - A circular dependency between fcrepo-auth-common <--> fcrepo-http-api is bad
048 *
049 * @author awoods
050 * @since 2014-06-26
051 */
052@Scope("prototype")
053@Path("/{path: .*}")
054public class RootTestResource extends AbstractResource {
055
056    @Inject
057    protected HttpSession session;
058
059    private static final Logger LOGGER = getLogger(RootTestResource.class);
060
061    @PUT
062    public Response put(@PathParam("path") final String externalPath) throws Exception {
063        final String path = toPath(translator(), externalPath);
064        LOGGER.trace("PUT: {}", path);
065        return doRequest(path);
066    }
067
068    @POST
069    public Response post(@PathParam("path") final String externalPath) throws Exception {
070        final String path = toPath(translator(), externalPath);
071        LOGGER.trace("POST: {}", path);
072        return doRequest(path);
073    }
074
075    private Response doRequest(final String path) throws RepositoryException {
076        final JcrTools jcrTools = new JcrTools();
077        final Node node = jcrTools.findOrCreateNode(getJcrSession(session.getFedoraSession()), path);
078        final URI location = uriInfo.getBaseUriBuilder().path(node.getPath()).build();
079        return Response.created(location).build();
080    }
081
082    protected IdentifierConverter<Resource,FedoraResource> translator() {
083        return new HttpResourceConverter(session,
084                    uriInfo.getBaseUriBuilder().clone().path(RootTestResource.class));
085    }
086
087}