ingo@143: /*
ingo@143:  * Copyright (c) 2011 by Intevation GmbH
ingo@143:  *
ingo@143:  * This program is free software under the LGPL (>=v2.1)
ingo@143:  * Read the file LGPL.txt coming with the software for details
ingo@143:  * or visit http://www.gnu.org/licenses/ if it does not exist.
ingo@143:  */
ingo@143: package de.intevation.artifactdatabase.rest;
ingo@143: 
sascha@207: import de.intevation.artifacts.common.utils.XMLUtils;
ingo@143: 
ingo@143: import de.intevation.artifacts.ArtifactDatabase;
ingo@143: import de.intevation.artifacts.ArtifactDatabaseException;
ingo@143: import de.intevation.artifacts.ArtifactNamespaceContext;
ingo@143: import de.intevation.artifacts.CallMeta;
ingo@143: 
ingo@143: import java.io.IOException;
ingo@143: 
ingo@143: import org.apache.log4j.Logger;
ingo@143: 
ingo@143: import org.restlet.Request;
ingo@143: import org.restlet.Response;
ingo@143: 
ingo@143: import org.restlet.data.MediaType;
ingo@143: import org.restlet.data.Status;
ingo@143: 
ingo@143: import org.restlet.ext.xml.DomRepresentation;
ingo@143: 
ingo@143: import org.restlet.representation.EmptyRepresentation;
ingo@143: import org.restlet.representation.Representation;
ingo@143: 
ingo@143: import org.restlet.resource.ResourceException;
ingo@143: 
ingo@143: import org.w3c.dom.Document;
ingo@143: 
ingo@143: 
ingo@143: /**
ingo@143:  * Base Resource to serve the out()-outputs of collections and artifacts.
ingo@143:  *
ingo@143:  * @author <a href="mailto:ingo.weinzierl@intevation">Ingo Weinzierl</a>
ingo@143:  */
ingo@143: public abstract class BaseOutResource
ingo@143: extends               BaseResource
ingo@143: {
ingo@143:     /** The logger used in this class.*/
ingo@143:     private static Logger logger = Logger.getLogger(BaseOutResource.class);
ingo@143: 
ingo@143:     /** XPath to figure out the MIME type of the requested result.*/
ingo@143:     public static final String XPATH_MIME_TYPE =
ingo@143:         "/art:action/art:out/art:mime-type/@value";
ingo@143: 
ingo@143:     /** Default result MIME type: octet stream.*/
ingo@143:     public static final MediaType DEFAULT_MIME_TYPE =
ingo@143:         MediaType.APPLICATION_OCTET_STREAM;
ingo@143: 
ingo@143: 
ingo@143:     @Override
ingo@143:     protected Representation innerPost(Representation requestRepr)
ingo@143:     throws    ResourceException
ingo@143:     {
ingo@143:         Document inputDocument = null;
ingo@143: 
ingo@143:         try {
ingo@143:             DomRepresentation input = new DomRepresentation(requestRepr);
ingo@143:             input.setNamespaceAware(true);
ingo@143:             inputDocument = input.getDocument();
ingo@143:         }
ingo@143:         catch (IOException ioe) {
ingo@143:             logger.error(ioe.getMessage());
ingo@143:             Response response = getResponse();
ingo@143:             response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe);
ingo@143:             return new EmptyRepresentation();
ingo@143:         }
ingo@143: 
ingo@143:         ArtifactDatabase db = getArtifactDatabase();
ingo@143: 
ingo@143:         Request request = getRequest();
ingo@143: 
ingo@143:         String identifier = getIdentifier();
ingo@269:         String outType    = getType();
ingo@143: 
ingo@143:         if (logger.isDebugEnabled()) {
ingo@143:             logger.debug("looking for artifact id '" + identifier + "'");
ingo@143:         }
ingo@143: 
ingo@143:         String mimeTypeString = XMLUtils.xpathString(
ingo@143:             inputDocument,
ingo@143:             XPATH_MIME_TYPE,
ingo@143:             ArtifactNamespaceContext.INSTANCE);
ingo@143: 
ingo@143:         MediaType mimeType = DEFAULT_MIME_TYPE;
ingo@143: 
ingo@143:         if (mimeTypeString != null && mimeTypeString.length() != 0) {
ingo@143:             try {
ingo@143:                 mimeType = MediaType.valueOf(mimeTypeString);
ingo@143:             }
ingo@143:             catch (Exception e) {
ingo@143:                 logger.error(e.getLocalizedMessage());
ingo@143:             }
ingo@143:         }
ingo@143: 
ingo@143:         try {
ingo@143:             return new OutRepresentation(
ingo@143:                 mimeType,
ingo@269:                 doOut(identifier, outType, inputDocument, db, getCallMeta()));
ingo@143:         }
ingo@143:         catch (ArtifactDatabaseException adbe) {
ingo@143:             Response response = getResponse();
ingo@143:             response.setStatus(
ingo@143:                 Status.CLIENT_ERROR_NOT_FOUND, adbe.getMessage());
ingo@143:             return new EmptyRepresentation();
ingo@143:         }
ingo@143:     }
ingo@143: 
ingo@143:     /**
ingo@143:      * Returns the identifier of the artifact or collection.
ingo@143:      *
ingo@143:      * @return the identifier.
ingo@143:      */
ingo@143:     protected abstract String getIdentifier();
ingo@143: 
ingo@269: 
ingo@269:     /**
ingo@269:      * Returns the concrete output type of the artifact or collection.
ingo@269:      *
ingo@269:      * @return the output type.
ingo@269:      */
ingo@269:     protected abstract String getType();
ingo@269: 
ingo@143:     /**
ingo@143:      * This method is called to process the operation on artifacts or
ingo@143:      * collections.
ingo@143:      *
ingo@143:      * @param identifier The identifier of the artifact or collection.
ingo@269:      * @param type The output type.
ingo@143:      * @param input The input document of the request.
ingo@143:      * @param db The artifact database.
ingo@143:      * @param meta The CallMeta object.
ingo@143:      *
ingo@143:      * @return the result of the operation.
ingo@143:      */
ingo@143:     protected abstract ArtifactDatabase.DeferredOutput doOut(
ingo@143:         String           identifier,
ingo@269:         String           type,
ingo@143:         Document         input,
ingo@143:         ArtifactDatabase db,
ingo@143:         CallMeta         meta)
ingo@143:     throws ArtifactDatabaseException;
ingo@143: }
ingo@143: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :