001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.http.api.repository;
019
020import static java.util.stream.Collectors.joining;
021import static javax.ws.rs.core.Response.noContent;
022import static javax.ws.rs.core.Response.serverError;
023import static org.slf4j.LoggerFactory.getLogger;
024
025import java.io.File;
026import java.io.IOException;
027import java.io.InputStream;
028import java.util.Collection;
029
030import javax.inject.Inject;
031import javax.jcr.Session;
032import javax.ws.rs.POST;
033import javax.ws.rs.Path;
034import javax.ws.rs.WebApplicationException;
035import javax.ws.rs.core.Response;
036
037import org.apache.commons.io.IOUtils;
038import org.fcrepo.http.commons.AbstractResource;
039import org.fcrepo.kernel.api.services.RepositoryService;
040import org.slf4j.Logger;
041import org.springframework.context.annotation.Scope;
042
043/**
044 * Restore a backup of the repository
045 *
046 * @author cbeer
047 */
048@Scope("prototype")
049@Path("/fcr:restore")
050public class FedoraRepositoryRestore extends AbstractResource {
051
052    private static final Logger LOGGER = getLogger(FedoraRepositoryRestore.class);
053
054    @Inject
055    protected Session session;
056
057    /**
058     * The fcrepo repository service
059     */
060    @Inject
061    protected RepositoryService repositoryService;
062
063    /**
064     * This method runs a repository restore.
065     *
066     * @param bodyStream the body stream
067     * @return response
068     * @throws IOException if IO exception occurred
069     */
070    @POST
071    public Response runRestore(final InputStream bodyStream) throws IOException {
072
073        if (null == bodyStream) {
074            throw new WebApplicationException(serverError().entity(
075                    "Request body must not be null").build());
076        }
077
078        final String body = IOUtils.toString(bodyStream);
079        final File backupDirectory = new File(body.trim());
080        if (!backupDirectory.exists()) {
081            throw new WebApplicationException(serverError().entity(
082                    "Backup directory does not exist: "
083                            + backupDirectory.getAbsolutePath()).build());
084        }
085
086        final Collection<Throwable> problems = repositoryService.restoreRepository(session, backupDirectory);
087        if (!problems.isEmpty()) {
088            LOGGER.error("Problems restoring up the repository:");
089
090            // Report the problems (we'll just print them out) ...
091            final String problemsOutput = problems.stream().map(Throwable::getMessage).peek(LOGGER::error)
092                    .collect(joining("\n"));
093
094            throw new WebApplicationException(serverError()
095                    .entity(problemsOutput).build());
096
097        }
098        return noContent().build();
099
100    }
101}