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@29: package de.intevation.artifactdatabase.rest; sascha@29: sascha@93: import de.intevation.artifactdatabase.XMLUtils; sascha@93: sascha@93: import de.intevation.artifacts.ArtifactDatabase; sascha@93: import de.intevation.artifacts.ArtifactDatabaseException; sascha@93: import de.intevation.artifacts.ArtifactNamespaceContext; sascha@93: sascha@93: import java.io.IOException; sascha@93: sascha@29: import org.apache.log4j.Logger; sascha@29: ingo@101: import org.restlet.Request; ingo@101: import org.restlet.Response; ingo@101: sascha@93: import org.restlet.data.MediaType; sascha@29: import org.restlet.data.Status; sascha@29: sascha@29: import org.restlet.ext.xml.DomRepresentation; sascha@29: sascha@93: import org.restlet.representation.EmptyRepresentation; sascha@93: import org.restlet.representation.Representation; sascha@29: sascha@93: import org.restlet.resource.ResourceException; sascha@29: sascha@29: import org.w3c.dom.Document; sascha@29: sascha@29: /** sascha@88: * Resource to serve the out()-outputs of artifacts. sascha@77: * @author Sascha L. Teichmann sascha@29: */ sascha@29: public class ArtifactOutResource sascha@40: extends BaseResource sascha@29: { sascha@88: /** sascha@88: * server URL where to find the resource. sascha@88: */ sascha@29: public static final String PATH = "/artifact/{uuid}/{type}"; sascha@29: sascha@88: /** sascha@88: * XPath to figure out the MIME type of the requested result. sascha@88: */ sascha@94: public static final String XPATH_MIME_TYPE = sascha@94: "/art:action/art:out/art:mime-type/@value"; sascha@29: sascha@88: /** sascha@88: * Default result MIME type: octet stream sascha@88: */ sascha@29: public static final MediaType DEFAULT_MIME_TYPE = sascha@29: MediaType.APPLICATION_OCTET_STREAM; sascha@29: sascha@29: private static Logger logger = Logger.getLogger(ArtifactOutResource.class); sascha@29: sascha@88: @Override sascha@40: protected Representation innerPost(Representation requestRepr) sascha@40: throws ResourceException sascha@40: { sascha@29: Document inputDocument = null; sascha@29: try { sascha@29: DomRepresentation input = new DomRepresentation(requestRepr); ingo@65: input.setNamespaceAware(true); sascha@29: inputDocument = input.getDocument(); sascha@29: } sascha@29: catch (IOException ioe) { sascha@29: logger.error(ioe.getMessage()); sascha@29: Response response = getResponse(); sascha@29: response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe); sascha@29: return new EmptyRepresentation(); sascha@29: } sascha@29: sascha@29: ArtifactDatabase db = (ArtifactDatabase)getContext() sascha@29: .getAttributes().get("database"); sascha@29: sascha@40: Request request = getRequest(); sascha@40: sascha@29: String identifier = (String)request.getAttributes().get("uuid"); sascha@29: sascha@29: if (logger.isDebugEnabled()) { sascha@29: logger.debug("looking for artifact id '" + identifier + "'"); sascha@29: } sascha@29: sascha@29: String mimeTypeString = XMLUtils.xpathString( sascha@29: inputDocument, sascha@29: XPATH_MIME_TYPE, sascha@29: ArtifactNamespaceContext.INSTANCE); sascha@29: sascha@29: MediaType mimeType = DEFAULT_MIME_TYPE; sascha@29: sascha@29: if (mimeTypeString != null && mimeTypeString.length() != 0) { sascha@29: try { sascha@29: mimeType = MediaType.valueOf(mimeTypeString); sascha@29: } sascha@29: catch (Exception e) { sascha@29: logger.error(e.getLocalizedMessage()); sascha@29: } sascha@29: } sascha@29: sascha@32: try { sascha@32: return new OutRepresentation( sascha@32: mimeType, sascha@48: db.out(identifier, inputDocument, getCallMeta())); sascha@32: } sascha@32: catch (ArtifactDatabaseException adbe) { sascha@32: Response response = getResponse(); sascha@32: response.setStatus( sascha@32: Status.CLIENT_ERROR_NOT_FOUND, adbe.getMessage()); sascha@32: return new EmptyRepresentation(); sascha@32: } sascha@29: } sascha@29: } sascha@88: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :