001/** 002 * Copyright 2014 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 071 * @param format 072 * @param requestBodyStream 073 * @return 201 with Location header to the path of the imported resource 074 * @throws IOException 075 * @throws RepositoryException 076 * @throws InvalidChecksumException 077 * @throws URISyntaxException 078 */ 079 @POST 080 public Response importObject(@PathParam("path") final String externalPath, 081 @QueryParam("format") @DefaultValue("jcr/xml") final String format, 082 @ContentLocation final InputStream requestBodyStream) 083 throws IOException, InvalidChecksumException, URISyntaxException { 084 085 final String path = toPath(translator(), externalPath); 086 LOGGER.info("Deserializing to {}, '{}'", format, path); 087 088 try { 089 serializers.getSerializer(format) 090 .deserialize(session, path, requestBodyStream); 091 session.save(); 092 return created(new URI(path)).build(); 093 } catch ( ItemExistsException ex ) { 094 return status(CONFLICT).entity("Item already exists").build(); 095 } catch (final RepositoryException e) { 096 throw new RepositoryRuntimeException(e); 097 } catch (InvalidSerializationFormatException e) { 098 throw new RepositoryRuntimeException(e); 099 } 100 } 101 102 @Override 103 protected Session session() { 104 return session; 105 } 106}