sascha@29: package de.intevation.artifactdatabase.rest; sascha@29: sascha@29: import org.apache.log4j.Logger; sascha@29: sascha@40: import org.restlet.resource.ResourceException; sascha@29: sascha@29: import org.restlet.data.Request; sascha@29: import org.restlet.data.Response; sascha@29: import org.restlet.data.Status; sascha@29: import org.restlet.data.MediaType; sascha@29: sascha@29: import org.restlet.representation.Representation; sascha@29: import org.restlet.representation.EmptyRepresentation; sascha@29: sascha@29: import org.restlet.ext.xml.DomRepresentation; sascha@29: sascha@29: import de.intevation.artifactdatabase.XMLUtils; sascha@29: sascha@29: import de.intevation.artifacts.ArtifactNamespaceContext; sascha@32: import de.intevation.artifacts.ArtifactDatabaseException; sascha@29: import de.intevation.artifacts.ArtifactDatabase; sascha@29: sascha@29: import org.w3c.dom.Document; sascha@29: sascha@29: import java.io.IOException; sascha@29: sascha@29: /** sascha@77: * @author Sascha L. Teichmann sascha@29: */ sascha@29: public class ArtifactOutResource sascha@40: extends BaseResource sascha@29: { sascha@29: public static final String PATH = "/artifact/{uuid}/{type}"; sascha@29: ingo@65: public static final String XPATH_MIME_TYPE = "/art:action/art:out/art:mime-type/@value"; sascha@29: 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@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@29: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: