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 * @return path to the backup 064 * @throws IOException 065 */ 066 @POST 067 public String runBackup(final InputStream bodyStream) throws IOException { 068 069 File backupDirectory; 070 if (null != bodyStream) { 071 final String body = IOUtils.toString(bodyStream).trim(); 072 073 backupDirectory = new File(body.trim()); 074 if (body.isEmpty()) { 075 // Backup to a temp directory 076 backupDirectory = createTempDir(); 077 078 } else if (!backupDirectory.exists() || !backupDirectory.canWrite()) { 079 throw new WebApplicationException( 080 serverError().entity( 081 "Backup directory does not exist or is not writable: " + 082 backupDirectory.getAbsolutePath()) 083 .build()); 084 } 085 086 } else { 087 // Backup to a temp directory 088 backupDirectory = createTempDir(); 089 } 090 091 LOGGER.debug("Backing up to: {}", backupDirectory.getAbsolutePath()); 092 final Problems problems = repositoryService.backupRepository(session, backupDirectory); 093 094 if ( problems.hasProblems() ) { 095 LOGGER.error("Problems backing up the repository:"); 096 097 final StringBuilder problemsOutput = new StringBuilder(); 098 099 // Report the problems (we'll just print them out) ... 100 for ( final Problem problem : problems ) { 101 LOGGER.error("{}", problem.getMessage()); 102 problemsOutput.append(problem.getMessage()); 103 problemsOutput.append("\n"); 104 } 105 106 throw new WebApplicationException(serverError().entity(problemsOutput.toString()).build()); 107 108 } 109 return backupDirectory.getCanonicalPath(); 110 } 111}