001/* 002 * Licensed to DuraSpace under one or more contributor license agreements. 003 * See the NOTICE file distributed with this work for additional information 004 * regarding copyright ownership. 005 * 006 * DuraSpace licenses this file to you under the Apache License, 007 * Version 2.0 (the "License"); you may not use this file except in 008 * compliance with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.fcrepo.http.api.repository; 019 020import static java.util.stream.Collectors.joining; 021import static javax.ws.rs.core.Response.noContent; 022import static javax.ws.rs.core.Response.serverError; 023import static org.slf4j.LoggerFactory.getLogger; 024 025import java.io.File; 026import java.io.IOException; 027import java.io.InputStream; 028import java.util.Collection; 029 030import javax.inject.Inject; 031import javax.ws.rs.POST; 032import javax.ws.rs.Path; 033import javax.ws.rs.WebApplicationException; 034import javax.ws.rs.core.Response; 035 036import org.apache.commons.io.IOUtils; 037import org.fcrepo.http.commons.AbstractResource; 038import org.fcrepo.http.commons.session.HttpSession; 039import org.fcrepo.kernel.api.services.RepositoryService; 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") 050@Deprecated 051public class FedoraRepositoryRestore extends AbstractResource { 052 053 private static final Logger LOGGER = getLogger(FedoraRepositoryRestore.class); 054 055 @Inject 056 protected HttpSession session; 057 058 /** 059 * The fcrepo repository service 060 */ 061 @Inject 062 protected RepositoryService repositoryService; 063 064 /** 065 * This method runs a repository restore. 066 * 067 * @param bodyStream the body stream 068 * @return response 069 * @throws IOException if IO exception occurred 070 */ 071 @POST 072 public Response runRestore(final InputStream bodyStream) throws IOException { 073 074 if (null == bodyStream) { 075 throw new WebApplicationException(serverError().entity( 076 "Request body must not be null").build()); 077 } 078 079 final String body = IOUtils.toString(bodyStream); 080 final File backupDirectory = new File(body.trim()); 081 if (!backupDirectory.exists()) { 082 throw new WebApplicationException(serverError().entity( 083 "Backup directory does not exist: " 084 + backupDirectory.getAbsolutePath()).build()); 085 } 086 087 final Collection<Throwable> problems = repositoryService.restoreRepository(session.getFedoraSession(), 088 backupDirectory); 089 if (!problems.isEmpty()) { 090 LOGGER.error("Problems restoring up the repository:"); 091 092 // Report the problems (we'll just print them out) ... 093 final String problemsOutput = problems.stream().map(Throwable::getMessage).peek(LOGGER::error) 094 .collect(joining("\n")); 095 096 throw new WebApplicationException(serverError() 097 .entity(problemsOutput).build()); 098 099 } 100 return noContent() 101 .header("Warning", "This endpoint will be moving to an extension module in a future release of Fedora") 102 .build(); 103 104 } 105}