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