ingo@100: /* ingo@100: * Copyright (c) 2010 by Intevation GmbH ingo@100: * ingo@100: * This program is free software under the LGPL (>=v2.1) ingo@100: * Read the file LGPL.txt coming with the software for details ingo@100: * or visit http://www.gnu.org/licenses/ if it does not exist. ingo@100: */ ingo@100: sascha@73: package de.intevation.artifactdatabase.rest; sascha@73: sascha@73: import de.intevation.artifacts.ArtifactDatabase; sascha@73: import de.intevation.artifacts.ArtifactDatabaseException; sascha@73: sascha@93: import java.io.IOException; sascha@93: sascha@93: import org.apache.log4j.Logger; sascha@93: ingo@101: import org.restlet.Request; ingo@101: import org.restlet.Response; sascha@93: import org.restlet.data.MediaType; sascha@73: import org.restlet.data.Status; sascha@73: sascha@93: import org.restlet.ext.xml.DomRepresentation; sascha@93: sascha@93: import org.restlet.representation.EmptyRepresentation; sascha@93: import org.restlet.representation.Representation; sascha@73: sascha@73: import org.w3c.dom.Document; sascha@73: sascha@380: import de.intevation.artifacts.Service; sascha@380: 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@89: @Override 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@380: return guessRepresentation( 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@380: sascha@380: protected static Representation guessRepresentation(Service.Output output) { sascha@380: sascha@380: MediaType mediaType = new MediaType(output.getMIMEType()); sascha@380: Object data = output.getData(); sascha@380: sascha@380: if (data instanceof Document) { sascha@380: return new DomRepresentation(mediaType, (Document)data); sascha@380: } sascha@380: sascha@380: if (data instanceof byte []) { sascha@380: return new ByteArrayRepresentation(mediaType, (byte [])data); sascha@380: } sascha@380: sascha@380: return new EmptyRepresentation(); sascha@380: } sascha@73: } sascha@73: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :