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.jcr.ItemExistsException;
029import javax.jcr.RepositoryException;
030import javax.ws.rs.DefaultValue;
031import javax.ws.rs.POST;
032import javax.ws.rs.Path;
033import javax.ws.rs.PathParam;
034import javax.ws.rs.QueryParam;
035import javax.ws.rs.core.Response;
036
037import org.fcrepo.http.commons.domain.ContentLocation;
038import org.fcrepo.kernel.api.exception.InvalidChecksumException;
039import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
040import org.fcrepo.serialization.InvalidSerializationFormatException;
041import org.fcrepo.serialization.SerializerUtil;
042import org.slf4j.Logger;
043import org.springframework.beans.factory.annotation.Autowired;
044import org.springframework.context.annotation.Scope;
045
046/**
047 * Import serialized objects at a given endpoint
048 *
049 * @author ajs6f
050 * @author cbeer
051 */
052@Scope("prototype")
053@Path("/{path: .*}/fcr:import")
054@Deprecated
055public class FedoraImport extends FedoraBaseResource {
056
057    @Autowired
058    protected SerializerUtil serializers;
059
060    private static final Logger LOGGER = getLogger(FedoraImport.class);
061
062    /**
063     * Deserialize a serialized object at the current path POST
064     * /fcr:import?format=jcr/xml (with a JCR/XML payload)
065     *
066     * @param externalPath the external path
067     * @param format the format
068     * @param requestBodyStream the request body stream
069     * @return 201 with Location header to the path of the imported resource
070     * @throws IOException if IO exception occurred
071     * @throws InvalidChecksumException if invalid checksum exception occurred
072     * @throws URISyntaxException if uri syntax exception
073     */
074    @POST
075    public Response importObject(@PathParam("path") final String externalPath,
076        @QueryParam("format") @DefaultValue("jcr/xml") final String format,
077        @ContentLocation final InputStream requestBodyStream)
078        throws IOException, InvalidChecksumException, URISyntaxException {
079
080        final String path = toPath(translator(), externalPath);
081        LOGGER.info("Deserializing to {}, '{}'", format, path);
082
083        try {
084            serializers.getSerializer(format)
085                    .deserialize(session, path, requestBodyStream);
086            session.save();
087            return created(new URI(path))
088                    .header("Warning", "This endpoint is deprecated and will be removed in the 4.6.0 release.")
089                    .build();
090        } catch ( ItemExistsException ex ) {
091            return status(CONFLICT).entity("Item already exists").build();
092        } catch (final RepositoryException e) {
093            throw new RepositoryRuntimeException(e);
094        } catch (InvalidSerializationFormatException e) {
095            throw new RepositoryRuntimeException(e);
096        }
097    }
098
099}