/*
* SyncResource.java
*
* Created on April 12, 2007, 1:39 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package org.atomojo.auth.service.app;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import org.atomojo.auth.service.db.AuthDB;
import org.atomojo.auth.service.db.RestoreDestination;
import org.infoset.xml.DocumentLoader;
import org.infoset.xml.XMLException;
import org.infoset.xml.sax.SAXDocumentLoader;
import org.restlet.Request;
import org.restlet.data.Form;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.ServerResource;
/**
*
* @author alex
*/
public class RestoreResource extends ServerResource
{
long expiration = 3600*1000;
AuthDB db;
/** Creates a new instance of SyncResource */
public RestoreResource() {
setNegotiated(false);
}
protected void doInit() {
db = (AuthDB)getRequest().getAttributes().get(AuthApplication.DB_ATTR);
}
public Representation get()
{
try {
Form form = getRequest().getResourceRef().getQueryAsForm();
String location = form.getValues("location");
if (location==null) {
getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
return null;
}
File inFile = new File(location);
if (!inFile.canRead()) {
getResponse().setStatus(Status.CLIENT_ERROR_EXPECTATION_FAILED);
return new StringRepresentation("Cannot read input file "+inFile.getAbsolutePath());
}
getContext().getLogger().info("Restoring database from "+inFile.getAbsolutePath());
DocumentLoader loader = new SAXDocumentLoader();
loader.generate(inFile.toURI(),new RestoreDestination(getContext().getLogger(),db));
getContext().getLogger().info("Restore finished.");
getResponse().setStatus(Status.SUCCESS_NO_CONTENT);
return null;
} catch (IOException ex) {
getContext().getLogger().log(Level.SEVERE,"Cannot get write data to output.",ex);
getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
return new StringRepresentation("I/O error, see logs.");
} catch (XMLException ex) {
getContext().getLogger().log(Level.SEVERE,"XML error while writing data to output.",ex);
getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
return new StringRepresentation("XML error, see logs.");
}
}
}