001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.http.api;
017
018import static javax.ws.rs.core.Response.created;
019import static javax.ws.rs.core.Response.Status.CONFLICT;
020import static javax.ws.rs.core.Response.status;
021import static org.slf4j.LoggerFactory.getLogger;
022
023import java.io.IOException;
024import java.io.InputStream;
025import java.net.URI;
026import java.net.URISyntaxException;
027
028import javax.inject.Inject;
029import javax.jcr.ItemExistsException;
030import javax.jcr.RepositoryException;
031import javax.jcr.Session;
032import javax.ws.rs.DefaultValue;
033import javax.ws.rs.POST;
034import javax.ws.rs.Path;
035import javax.ws.rs.PathParam;
036import javax.ws.rs.QueryParam;
037import javax.ws.rs.core.Response;
038
039import org.fcrepo.http.commons.domain.ContentLocation;
040import org.fcrepo.kernel.exception.InvalidChecksumException;
041import org.fcrepo.kernel.exception.RepositoryRuntimeException;
042import org.fcrepo.serialization.InvalidSerializationFormatException;
043import org.fcrepo.serialization.SerializerUtil;
044import org.slf4j.Logger;
045import org.springframework.beans.factory.annotation.Autowired;
046import org.springframework.context.annotation.Scope;
047
048/**
049 * Import serialized objects at a given endpoint
050 *
051 * @author ajs6f
052 * @author cbeer
053 */
054@Scope("prototype")
055@Path("/{path: .*}/fcr:import")
056public class FedoraImport extends FedoraBaseResource {
057
058    @Inject
059    protected Session session;
060
061    @Autowired
062    protected SerializerUtil serializers;
063
064    private static final Logger LOGGER = getLogger(FedoraImport.class);
065
066    /**
067     * Deserialize a serialized object at the current path POST
068     * /fcr:import?format=jcr/xml (with a JCR/XML payload)
069     *
070     * @param externalPath the external path
071     * @param format the format
072     * @param requestBodyStream the request body stream
073     * @return 201 with Location header to the path of the imported resource
074     * @throws IOException if IO exception occurred
075     * @throws InvalidChecksumException if invalid checksum exception occurred
076     * @throws URISyntaxException if uri syntax exception
077     */
078    @POST
079    public Response importObject(@PathParam("path") final String externalPath,
080        @QueryParam("format") @DefaultValue("jcr/xml") final String format,
081        @ContentLocation final InputStream requestBodyStream)
082        throws IOException, InvalidChecksumException, URISyntaxException {
083
084        final String path = toPath(translator(), externalPath);
085        LOGGER.info("Deserializing to {}, '{}'", format, path);
086
087        try {
088            serializers.getSerializer(format)
089                    .deserialize(session, path, requestBodyStream);
090            session.save();
091            return created(new URI(path)).build();
092        } catch ( ItemExistsException ex ) {
093            return status(CONFLICT).entity("Item already exists").build();
094        } catch (final RepositoryException e) {
095            throw new RepositoryRuntimeException(e);
096        } catch (InvalidSerializationFormatException e) {
097            throw new RepositoryRuntimeException(e);
098        }
099    }
100
101    @Override
102    protected Session session() {
103        return session;
104    }
105}