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 java.lang.Boolean.parseBoolean;
019import static javax.ws.rs.core.Response.ok;
020import static org.slf4j.LoggerFactory.getLogger;
021
022import java.io.IOException;
023import java.io.OutputStream;
024
025import javax.inject.Inject;
026import javax.jcr.RepositoryException;
027import javax.jcr.Session;
028import javax.ws.rs.BadRequestException;
029import javax.ws.rs.DefaultValue;
030import javax.ws.rs.GET;
031import javax.ws.rs.Path;
032import javax.ws.rs.PathParam;
033import javax.ws.rs.QueryParam;
034import javax.ws.rs.WebApplicationException;
035import javax.ws.rs.core.Response;
036import javax.ws.rs.core.StreamingOutput;
037
038import org.fcrepo.kernel.models.FedoraResource;
039import org.fcrepo.serialization.FedoraObjectSerializer;
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 * Serialization for nodes
048 *
049 * @author awoods
050 */
051@Scope("request")
052@Path("/{path: .*}/fcr:export")
053public class FedoraExport extends FedoraBaseResource {
054
055    @Autowired
056    protected SerializerUtil serializers;
057
058    @Inject
059    protected Session session;
060
061    private static final Logger LOGGER = getLogger(FedoraExport.class);
062
063    /**
064     * Export an object with the given format, e.g.: GET
065     * /path/to/object/fcr:export?format=jcr/xml : the node as JCR/XML
066     *
067     * @param externalPath the external path
068     * @param format the format string
069     * @param skipBinary the value whether skip binary
070     * @param recurse the value whether recurse
071     * @return object in the given format
072     */
073    @GET
074    public Response exportObject(
075        @PathParam("path") final String externalPath,
076        @QueryParam("format") @DefaultValue("jcr/xml") final String format,
077        @QueryParam("skipBinary") @DefaultValue("true") final String skipBinary,
078        @QueryParam("recurse") @DefaultValue("false") final String recurse) {
079
080        final FedoraResource resource = getResourceFromPath(externalPath);
081
082        LOGGER.debug("Requested object serialization for {} using serialization format {}", resource, format);
083
084        final FedoraObjectSerializer serializer =
085            serializers.getSerializer(format);
086
087        return ok().type(serializer.getMediaType()).entity(
088                new StreamingOutput() {
089
090                    @Override
091                    public void write(final OutputStream out)
092                        throws IOException {
093
094                        try {
095                            LOGGER.debug("Selecting from serializer map: {}", serializers);
096                            LOGGER.debug("Retrieved serializer for format: {}", format);
097                            serializer.serialize(resource,
098                                                 out,
099                                                 parseBoolean(skipBinary),
100                                                 parseBoolean(recurse));
101                            LOGGER.info("Serialized to {}, '{}'", format, externalPath);
102                        } catch (final RepositoryException e) {
103                            throw new WebApplicationException(e);
104                        } catch (InvalidSerializationFormatException e) {
105                            throw new BadRequestException(e.getMessage());
106                        }
107                    }
108                }).build();
109
110    }
111
112    @Override
113    protected Session session() {
114        return session;
115    }
116}