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 javax.ws.rs.core.Response.noContent; 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; 025import java.util.ArrayList; 026import java.util.List; 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.services.RepositoryService; 038import org.modeshape.jcr.api.Problem; 039import org.modeshape.jcr.api.Problems; 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 Problems problems = repositoryService.restoreRepository(session, backupDirectory); 087 if (problems.hasProblems()) { 088 LOGGER.error("Problems restoring up the repository:"); 089 090 final List<String> problemsOutput = new ArrayList<>(); 091 092 // Report the problems (we'll just print them out) ... 093 for (final Problem problem : problems) { 094 LOGGER.error("{}", problem.getMessage()); 095 problemsOutput.add(problem.getMessage()); 096 } 097 098 throw new WebApplicationException(serverError() 099 .entity(problemsOutput).build()); 100 101 } 102 return noContent().build(); 103 104 } 105}