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 com.google.common.io.Files.createTempDir;
019import static java.util.stream.Collectors.joining;
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;
033
034import org.apache.commons.io.IOUtils;
035import org.fcrepo.http.commons.AbstractResource;
036import org.fcrepo.kernel.api.services.RepositoryService;
037import org.slf4j.Logger;
038import org.springframework.context.annotation.Scope;
039
040/**
041 * Repository-wide backup endpoint
042 *
043 * @author cbeer
044 */
045@Scope("prototype")
046@Path("/fcr:backup")
047public class FedoraRepositoryBackup extends AbstractResource {
048
049    private static final Logger LOGGER = getLogger(FedoraRepositoryBackup.class);
050
051    @Inject
052    protected Session session;
053
054    /**
055     * The fcrepo repository service
056     */
057    @Inject
058    protected RepositoryService repositoryService;
059
060    /**
061     * This method runs a repository backup.
062     *
063     * @param bodyStream the input body stream
064     * @return path to the backup
065     * @throws IOException if IO exception occurred
066     */
067    @POST
068    public String runBackup(final InputStream bodyStream) throws IOException {
069
070        File backupDirectory;
071        if (null != bodyStream) {
072            final String body = IOUtils.toString(bodyStream).trim();
073
074            backupDirectory = new File(body.trim());
075            if (body.isEmpty()) {
076                // Backup to a temp directory
077                backupDirectory = createTempDir();
078
079            } else if (!backupDirectory.exists() || !backupDirectory.canWrite()) {
080                throw new WebApplicationException(
081                        serverError().entity(
082                                "Backup directory does not exist or is not writable: " +
083                                        backupDirectory.getAbsolutePath())
084                                .build());
085            }
086
087        } else {
088            // Backup to a temp directory
089            backupDirectory = createTempDir();
090        }
091
092        LOGGER.debug("Backing up to: {}", backupDirectory.getAbsolutePath());
093        final Collection<Throwable> problems = repositoryService.backupRepository(session, backupDirectory);
094
095        if (!problems.isEmpty()) {
096            LOGGER.error("Problems backing up the repository:");
097
098            // Report the problems (we'll just print them out) ...
099            final String output = problems.stream().map(Throwable::getMessage).peek(LOGGER::error)
100                    .collect(joining("\n"));
101
102            throw new WebApplicationException(serverError().entity(output).build());
103
104        }
105        return backupDirectory.getCanonicalPath();
106    }
107}