/*
* 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.app.admin;
import org.atomojo.app.App;
import org.atomojo.app.client.XMLRepresentationParser;
import org.atomojo.app.db.DB;
import org.atomojo.app.db.SyncTarget;
import org.infoset.xml.Document;
import org.infoset.xml.util.DocumentDestination;
import org.restlet.Context;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.ServerResource;
/**
*
* @author alex
*/
public class SyncTargetResource extends ServerResource implements AdminXML
{
/** Creates a new instance of SyncResource */
public SyncTargetResource(Context context, Request request,Response response) {
setNegotiated(false);
}
public Representation get()
{
final DB db = (DB)getRequest().getAttributes().get(App.DB_ATTR);
final String name = getRequest().getAttributes().get("name").toString();
SyncTarget target = new SyncTarget(db,name);
if (target.exists()) {
return target.getRepresentation();
} else {
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
return null;
}
}
public Representation put(Representation entity)
{
final DB db = (DB)getRequest().getAttributes().get(App.DB_ATTR);
final String name = getRequest().getAttributes().get("name").toString();
if (!XMLRepresentationParser.isXML(entity.getMediaType())) {
getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
return new StringRepresentation("Non-XML media type for entity body: "+entity.getMediaType().getName());
}
XMLRepresentationParser parser = new XMLRepresentationParser();
Document doc = null;
try {
DocumentDestination dest = new DocumentDestination();
parser.parse(entity,AdminApplication.createAdminDocumentDestination(dest,AdminXML.NM_TARGET));
doc = dest.getDocument();
SyncTarget target = new SyncTarget(db,doc.getDocumentElement());
String lname = target.getName();
if (!lname.equals(name)) {
getResponse().setStatus(Status.CLIENT_ERROR_EXPECTATION_FAILED);
return new StringRepresentation("Cannot change the name of the target.");
}
if (target.exists()) {
if (target.update()) {
getResponse().setStatus(Status.SUCCESS_NO_CONTENT);
return null;
} else {
getContext().getLogger().severe("Cannot store XML for remote app.");
getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
return new StringRepresentation("Internal error, see logs.");
}
} else {
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
return null;
}
} catch (Exception ex) {
getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
return new StringRepresentation("XML parse error: "+ex.getMessage());
}
}
public Representation delete() {
final DB db = (DB)getRequest().getAttributes().get(App.DB_ATTR);
final String name = getRequest().getAttributes().get("name").toString();
SyncTarget target = new SyncTarget(db,name);
if (target.exists()) {
if (target.delete()) {
getResponse().setStatus(Status.SUCCESS_NO_CONTENT);
} else {
getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
}
} else {
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
}
return null;
}
}