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 javax.ws.rs.core.Response.serverError;
020import static org.slf4j.LoggerFactory.getLogger;
021
022import java.io.File;
023import java.io.IOException;
024import java.io.InputStream;
025
026import javax.inject.Inject;
027import javax.jcr.Session;
028import javax.ws.rs.POST;
029import javax.ws.rs.Path;
030import javax.ws.rs.WebApplicationException;
031
032import org.apache.commons.io.IOUtils;
033import org.fcrepo.http.commons.AbstractResource;
034import org.fcrepo.kernel.services.RepositoryService;
035import org.modeshape.jcr.api.Problem;
036import org.modeshape.jcr.api.Problems;
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 Problems problems = repositoryService.backupRepository(session, backupDirectory);
094
095        if ( problems.hasProblems() ) {
096            LOGGER.error("Problems backing up the repository:");
097
098            final StringBuilder problemsOutput = new StringBuilder();
099
100            // Report the problems (we'll just print them out) ...
101            for ( final Problem problem : problems ) {
102                LOGGER.error("{}", problem.getMessage());
103                problemsOutput.append(problem.getMessage());
104                problemsOutput.append("\n");
105            }
106
107            throw new WebApplicationException(serverError().entity(problemsOutput.toString()).build());
108
109        }
110        return backupDirectory.getCanonicalPath();
111    }
112}