sascha@73: package de.intevation.artifactdatabase.rest; sascha@73: sascha@73: import org.restlet.representation.Representation; sascha@73: import org.restlet.representation.EmptyRepresentation; sascha@73: sascha@73: import org.restlet.ext.xml.DomRepresentation; sascha@73: sascha@73: import de.intevation.artifacts.ArtifactDatabase; sascha@73: import de.intevation.artifacts.ArtifactDatabaseException; sascha@73: sascha@73: import org.restlet.data.Request; sascha@73: import org.restlet.data.Response; sascha@73: import org.restlet.data.Status; sascha@73: import org.restlet.data.MediaType; sascha@73: sascha@73: import org.apache.log4j.Logger; sascha@73: sascha@73: import org.w3c.dom.Document; sascha@73: sascha@73: import java.io.IOException; sascha@73: sascha@73: /** sascha@88: * Resource to process incoming XML documents with a given service. sascha@88: * sascha@77: * @author Sascha L. Teichmann sascha@73: */ sascha@73: public class ServiceResource sascha@73: extends BaseResource sascha@73: { sascha@73: private static Logger logger = Logger.getLogger(ServiceResource.class); sascha@73: sascha@88: /** sascha@88: * server URL where to reach the resource. sascha@88: */ sascha@73: public static final String PATH = "/service/{service}"; sascha@73: sascha@88: /** sascha@88: * Error message if no corresponing service is provided by sascha@88: * the artifact database. sascha@88: */ sascha@73: public static final String NO_SUCH_ACTION_MESSAGE = "no such service"; sascha@73: sascha@73: protected Representation innerPost(Representation requestRepr) { sascha@73: sascha@73: Document inputDocument = null; sascha@73: try { sascha@73: DomRepresentation input = new DomRepresentation(requestRepr); sascha@73: input.setNamespaceAware(true); sascha@73: inputDocument = input.getDocument(); sascha@73: } sascha@73: catch (IOException ioe) { sascha@73: logger.error(ioe.getMessage()); sascha@73: Response response = getResponse(); sascha@73: response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe); sascha@73: return new EmptyRepresentation(); sascha@73: } sascha@73: sascha@73: Request request = getRequest(); sascha@73: sascha@73: String service = (String)request.getAttributes().get("service"); sascha@73: sascha@73: ArtifactDatabase db = (ArtifactDatabase)getContext() sascha@73: .getAttributes().get("database"); sascha@73: sascha@73: try { sascha@73: return new DomRepresentation( sascha@73: MediaType.APPLICATION_XML, sascha@73: db.process(service, inputDocument, getCallMeta())); sascha@73: } sascha@73: catch (ArtifactDatabaseException adbe) { sascha@73: logger.warn(adbe.getLocalizedMessage(), adbe); sascha@73: Response response = getResponse(); sascha@73: response.setStatus( sascha@73: Status.CLIENT_ERROR_BAD_REQUEST, adbe.getMessage()); sascha@73: return new EmptyRepresentation(); sascha@73: } sascha@73: } sascha@73: } sascha@73: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :