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.repository;
017
018import static java.util.stream.Collectors.joining;
019import static javax.ws.rs.core.Response.noContent;
020import static javax.ws.rs.core.Response.serverError;
021import static org.slf4j.LoggerFactory.getLogger;
022
023import java.io.File;
024import java.io.IOException;
025import java.io.InputStream;
026import java.util.Collection;
027
028import javax.inject.Inject;
029import javax.jcr.Session;
030import javax.ws.rs.POST;
031import javax.ws.rs.Path;
032import javax.ws.rs.WebApplicationException;
033import javax.ws.rs.core.Response;
034
035import org.apache.commons.io.IOUtils;
036import org.fcrepo.http.commons.AbstractResource;
037import org.fcrepo.kernel.api.services.RepositoryService;
038import org.slf4j.Logger;
039import org.springframework.context.annotation.Scope;
040
041/**
042 * Restore a backup of the repository
043 *
044 * @author cbeer
045 */
046@Scope("prototype")
047@Path("/fcr:restore")
048public class FedoraRepositoryRestore extends AbstractResource {
049
050    private static final Logger LOGGER = getLogger(FedoraRepositoryRestore.class);
051
052    @Inject
053    protected Session session;
054
055    /**
056     * The fcrepo repository service
057     */
058    @Inject
059    protected RepositoryService repositoryService;
060
061    /**
062     * This method runs a repository restore.
063     *
064     * @param bodyStream the body stream
065     * @return response
066     * @throws IOException if IO exception occurred
067     */
068    @POST
069    public Response runRestore(final InputStream bodyStream) throws IOException {
070
071        if (null == bodyStream) {
072            throw new WebApplicationException(serverError().entity(
073                    "Request body must not be null").build());
074        }
075
076        final String body = IOUtils.toString(bodyStream);
077        final File backupDirectory = new File(body.trim());
078        if (!backupDirectory.exists()) {
079            throw new WebApplicationException(serverError().entity(
080                    "Backup directory does not exist: "
081                            + backupDirectory.getAbsolutePath()).build());
082        }
083
084        final Collection<Throwable> problems = repositoryService.restoreRepository(session, backupDirectory);
085        if (!problems.isEmpty()) {
086            LOGGER.error("Problems restoring up the repository:");
087
088            // Report the problems (we'll just print them out) ...
089            final String problemsOutput = problems.stream().map(Throwable::getMessage).peek(LOGGER::error)
090                    .collect(joining("\n"));
091
092            throw new WebApplicationException(serverError()
093                    .entity(problemsOutput).build());
094
095        }
096        return noContent().build();
097
098    }
099}