ingo@142: /* ingo@142: * Copyright (c) 2011 by Intevation GmbH ingo@142: * ingo@142: * This program is free software under the LGPL (>=v2.1) ingo@142: * Read the file LGPL.txt coming with the software for details ingo@142: * or visit http://www.gnu.org/licenses/ if it does not exist. ingo@142: */ ingo@142: package de.intevation.artifactdatabase.rest; ingo@142: ingo@142: import de.intevation.artifacts.ArtifactDatabase; ingo@142: import de.intevation.artifacts.ArtifactDatabaseException; ingo@142: import de.intevation.artifacts.CallMeta; ingo@142: ingo@142: import de.intevation.artifacts.common.ArtifactNamespaceContext; ingo@142: import de.intevation.artifacts.common.utils.XMLUtils; ingo@142: ingo@268: import de.intevation.artifactdatabase.ArtifactDatabaseImpl; ingo@268: ingo@142: import java.io.IOException; ingo@142: ingo@268: import javax.xml.xpath.XPathConstants; ingo@268: ingo@142: import org.apache.log4j.Logger; ingo@142: ingo@142: import org.restlet.data.MediaType; ingo@142: import org.restlet.data.Status; ingo@142: import org.restlet.ext.xml.DomRepresentation; ingo@142: import org.restlet.representation.EmptyRepresentation; ingo@142: import org.restlet.representation.Representation; ingo@142: import org.restlet.Request; ingo@142: import org.restlet.Response; ingo@142: ingo@142: import org.w3c.dom.Document; ingo@268: import org.w3c.dom.Node; ingo@142: ingo@142: ingo@142: /** ingo@142: * @author Ingo Weinzierl ingo@142: */ ingo@142: public class CollectionResource ingo@142: extends BaseResource ingo@142: { ingo@142: /** The logger that is used in this class.*/ ingo@142: private static Logger logger = Logger.getLogger(CollectionResource.class); ingo@142: ingo@142: /** server URL where to reach the resource.*/ ingo@142: public static final String PATH = "/collection/{uuid}"; ingo@142: ingo@142: /** ingo@142: * XPath to figure out the type of action (feed, advance) via the ingo@142: * incoming POST request. ingo@142: */ ingo@142: public static final String XPATH_ACTION = "/art:action/art:type/@name"; ingo@142: ingo@142: /** ingo@142: * XPath to figure out the identifier of the artifact described in the ingo@142: * action. ingo@142: */ ingo@142: public static final String XPATH_ARTIFACT = ingo@142: "/art:action/art:type/art:artifact/@uuid"; ingo@142: ingo@142: /** Error message if no action was given.*/ ingo@142: public static final String NO_ACTION_MSG = "no action given"; ingo@142: ingo@142: /** Error message if a unknown action was given.*/ ingo@142: public static final String NO_SUCH_ACTION_MSG = "no such action"; ingo@142: ingo@142: /** Action name for deleting a collection.*/ ingo@142: public static final String ACTION_DELETE = "delete"; ingo@142: ingo@196: /** Action name for describing the collection.*/ ingo@196: public static final String ACTION_DESCRIBE = "describe"; ingo@196: ingo@253: /** Action name for retrieving the attribute of a collection.*/ ingo@253: public static final String ACTION_GET_ATTRIBUTE = "getattribute"; ingo@253: ingo@142: /** Action name for retrieving the attributes of an artifact stored in the ingo@142: * collection.*/ ingo@253: public static final String ACTION_GET_ITEM_ATTRIBUTE = "getitemattribute"; ingo@253: ingo@253: /** Action name for setting the attribute of a collection.*/ ingo@253: public static final String ACTION_SET_ATTRIBUTE = "setattribute"; ingo@142: ingo@142: /** Action name for setting the attribute for an artifact stored in the ingo@142: * collection.*/ ingo@253: public static final String ACTION_SET_ITEM_ATTRIBUTE = "setitemattribute"; ingo@142: ingo@142: /** Action name for adding a new artifact to the collection.*/ ingo@142: public static final String ACTION_ADD_ARTIFACT = "addartifact"; ingo@142: ingo@142: /** Action name for removing an artifact from the collection.*/ ingo@142: public static final String ACTION_REMOVE_ARTIFACT = "removeartifact"; ingo@142: ingo@142: /** Action name for listing the artifacts of the collection.*/ ingo@142: public static final String ACTION_LIST_ARTIFACTS = "listartifacts"; ingo@142: ingo@273: /** Action name for setting the ttl of a collection.*/ ingo@273: public static final String ACTION_SET_TTL = "settimetolive"; ingo@273: ingo@275: /** Action name for setting the name of a collection.*/ ingo@275: public static final String ACTION_SET_NAME = "setname"; ingo@275: ingo@142: ingo@142: /** ingo@142: * Method to figure out which POST action was triggered and perform this ingo@142: * operation on the collection specified by 'identifier' and found in the ingo@142: * artifact database 'db'. ingo@142: * ingo@142: * @param identifier The identifier of the collection. ingo@142: * @param action The action to be performed. ingo@142: * @param source The input document to further parameterize the operation. ingo@142: * @param db The artifact database where to find the collection. ingo@142: * ingo@142: * @return The representation produced by the performed action. ingo@142: */ ingo@142: protected Representation dispatch( ingo@142: String identifier, ingo@142: String action, ingo@142: Document source, ingo@142: ArtifactDatabase db ingo@142: ) { ingo@142: Document out = null; ingo@142: ingo@142: try { ingo@142: CallMeta meta = getCallMeta(); ingo@142: ingo@142: if (action.equals(ACTION_DELETE)) { ingo@142: logger.info("Delete collection '" + identifier + "'"); ingo@142: out = db.deleteCollection(identifier, getCallMeta()); ingo@142: } ingo@196: else if (action.equals(ACTION_DESCRIBE)) { ingo@196: logger.info("Describe collection '" + identifier + "'"); ingo@196: ingo@196: out = db.describeCollection(identifier, meta); ingo@196: } ingo@142: else if (action.equals(ACTION_ADD_ARTIFACT)) { ingo@142: String art = getArtifactIdentifier(source); ingo@142: ingo@142: logger.info("Add artifact '" + art + "' to collection."); ingo@189: out = db.addCollectionArtifact(identifier, art, source, meta); ingo@142: } ingo@142: else if (action.equals(ACTION_REMOVE_ARTIFACT)) { ingo@142: String art = getArtifactIdentifier(source); ingo@142: ingo@142: logger.info("Remove artifact '" + art + "' from collection."); ingo@142: out = db.removeCollectionArtifact(identifier, art, meta); ingo@142: } ingo@142: else if (action.equals(ACTION_LIST_ARTIFACTS)) { ingo@142: logger.info("List artifacts of collection '" + identifier +"'"); ingo@142: out = db.listCollectionArtifacts(identifier, meta); ingo@142: } ingo@142: else if (action.equals(ACTION_SET_ATTRIBUTE)) { ingo@142: String art = getArtifactIdentifier(source); ingo@142: ingo@253: logger.info("Set attribute for collection '" + identifier + "'"); ingo@268: ingo@268: Document attr = getCollectionAttribute(source); ingo@268: ingo@268: out = db.setCollectionAttribute(identifier, meta, attr); ingo@253: } ingo@253: else if (action.equals(ACTION_SET_ITEM_ATTRIBUTE)) { ingo@253: String art = getArtifactIdentifier(source); ingo@253: ingo@142: logger.info("Set attribute for artifact '" + art + "'"); ingo@252: out = db.setCollectionItemAttribute(identifier, art, source, meta); ingo@142: } ingo@142: else if (action.equals(ACTION_GET_ATTRIBUTE)) { ingo@142: String art = getArtifactIdentifier(source); ingo@142: ingo@253: logger.info("Retrieve attribute of collection '" + identifier + "'"); ingo@253: out = db.getCollectionAttribute(identifier, meta); ingo@253: } ingo@253: else if (action.equals(ACTION_GET_ITEM_ATTRIBUTE)) { ingo@253: String art = getArtifactIdentifier(source); ingo@253: ingo@142: logger.info("Retrieve attribute of artifact '" + art + "'"); ingo@252: out = db.getCollectionItemAttribute(identifier, art, meta); ingo@142: } ingo@273: else if (action.equals(ACTION_SET_TTL)) { ingo@273: out = db.setCollectionTTL(identifier, source, meta); ingo@273: } ingo@275: else if (action.equals(ACTION_SET_NAME)) { ingo@275: out = db.setCollectionName(identifier, source, meta); ingo@275: } ingo@142: else { ingo@142: throw new ArtifactDatabaseException(NO_SUCH_ACTION_MSG); ingo@142: } ingo@142: } ingo@142: catch (ArtifactDatabaseException adbe) { ingo@142: logger.warn(adbe.getLocalizedMessage(), adbe); ingo@142: ingo@142: Response response = getResponse(); ingo@142: response.setStatus( ingo@142: Status.CLIENT_ERROR_BAD_REQUEST, adbe.getMessage()); ingo@142: return new EmptyRepresentation(); ingo@142: } ingo@142: ingo@142: return new DomRepresentation(MediaType.APPLICATION_XML, out); ingo@142: } ingo@142: ingo@142: ingo@142: @Override ingo@142: protected Representation innerPost(Representation requestRepr) { ingo@142: Document input = null; ingo@142: try { ingo@142: DomRepresentation in = new DomRepresentation(requestRepr); ingo@142: in.setNamespaceAware(true); ingo@142: input = in.getDocument(); ingo@142: } ingo@142: catch (IOException ioe) { ingo@142: logger.error(ioe.getLocalizedMessage(), ioe); ingo@142: ingo@142: Response response = getResponse(); ingo@142: response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe); ingo@142: return new EmptyRepresentation(); ingo@142: } ingo@142: ingo@142: String action = XMLUtils.xpathString( ingo@142: input, XPATH_ACTION, ArtifactNamespaceContext.INSTANCE); ingo@142: ingo@142: if (action == null || action.length() == 0) { ingo@142: Response response = getResponse(); ingo@142: response.setStatus( ingo@142: Status.CLIENT_ERROR_BAD_REQUEST, NO_ACTION_MSG); ingo@142: return new EmptyRepresentation(); ingo@142: } ingo@142: ingo@142: Request request = getRequest(); ingo@142: ingo@142: String identifier = (String) request.getAttributes().get("uuid"); ingo@142: ingo@142: ArtifactDatabase db = getArtifactDatabase(); ingo@142: ingo@142: return dispatch(identifier, action, input, db); ingo@142: } ingo@142: ingo@142: ingo@142: /** ingo@142: * Retrieves the identifier of the artifact used in the action. ingo@142: * ingo@142: * @param source The incoming document that describes the operation. ingo@142: * ingo@142: * @return the uuid of the artifact described in the document. ingo@142: */ ingo@142: protected String getArtifactIdentifier(Document source) { ingo@142: return XMLUtils.xpathString( ingo@142: source, XPATH_ARTIFACT, ArtifactNamespaceContext.INSTANCE); ingo@142: } ingo@268: ingo@268: ingo@268: /** ingo@268: * Returns the attribute for a collection of the incoming request document. ingo@268: * ingo@268: * @param request The request document. ingo@268: * ingo@268: * @return the contained attribute as document. ingo@268: */ ingo@268: protected Document getCollectionAttribute(Document request) { ingo@268: Node attr = (Node) XMLUtils.xpath( ingo@268: request, ingo@268: ArtifactDatabaseImpl.XPATH_COLLECTION_ATTRIBUTE, ingo@268: XPathConstants.NODE, ingo@268: ArtifactNamespaceContext.INSTANCE); ingo@268: ingo@268: Document newAttr = XMLUtils.newDocument(); ingo@268: ingo@268: if (attr == null) { ingo@268: logger.error("Collection attribute document not found!"); ingo@268: return newAttr; ingo@268: } ingo@268: ingo@268: newAttr.appendChild(newAttr.importNode(attr, true)); ingo@268: ingo@268: return newAttr; ingo@268: } ingo@142: }