ingo@137: /* ingo@137: * Copyright (c) 2011 by Intevation GmbH ingo@137: * ingo@137: * This program is free software under the LGPL (>=v2.1) ingo@137: * Read the file LGPL.txt coming with the software for details ingo@137: * or visit http://www.gnu.org/licenses/ if it does not exist. ingo@137: */ ingo@137: package de.intevation.artifactdatabase.rest; ingo@137: ingo@137: import de.intevation.artifacts.ArtifactDatabase; ingo@137: import de.intevation.artifacts.ArtifactDatabaseException; ingo@137: ingo@137: import de.intevation.artifacts.common.ArtifactNamespaceContext; ingo@137: import de.intevation.artifacts.common.utils.XMLUtils; ingo@137: ingo@137: import java.io.IOException; ingo@137: ingo@137: import org.apache.log4j.Logger; ingo@137: ingo@137: import org.restlet.data.MediaType; ingo@137: import org.restlet.data.Status; ingo@137: import org.restlet.ext.xml.DomRepresentation; ingo@137: import org.restlet.representation.EmptyRepresentation; ingo@137: import org.restlet.representation.Representation; ingo@137: import org.restlet.Request; ingo@137: import org.restlet.Response; ingo@137: ingo@137: import org.w3c.dom.Document; ingo@137: ingo@137: /** ingo@137: * A resource that handles actions to a specific user. ingo@137: * ingo@137: * @author Ingo Weinzierl ingo@137: */ ingo@137: public class UserResource ingo@137: extends BaseResource ingo@137: { felix@395: /** The logger that is used in this class. */ ingo@137: private static Logger logger = Logger.getLogger(UserResource.class); ingo@137: felix@395: /** server URL where to reach the resource. */ ingo@137: public static final String PATH = "/user/{uuid}"; ingo@137: ingo@137: /** ingo@137: * XPath to figure out the type of action (feed, advance) via the ingo@137: * incoming POST request. ingo@137: */ ingo@137: public static final String XPATH_ACTION = "/art:action/art:type/@name"; ingo@137: felix@395: /** Error message if no action was given. */ ingo@137: public static final String NO_ACTION_MSG = "no action given"; ingo@137: felix@395: /** Error message if a unknown action was given. */ ingo@137: public static final String NO_SUCH_ACTION_MSG = "no such action"; ingo@137: felix@395: /** Action name for deleting users. */ ingo@137: public static final String ACTION_DELETE = "delete"; ingo@137: ingo@137: ingo@137: @Override ingo@137: protected Representation innerPost(Representation requestRepr) { ingo@137: Document inputDocument = null; ingo@137: ingo@137: try { ingo@137: DomRepresentation input = new DomRepresentation(requestRepr); ingo@137: input.setNamespaceAware(true); ingo@137: inputDocument = input.getDocument(); ingo@137: } ingo@137: catch (IOException ioe) { ingo@137: logger.error(ioe.getMessage()); ingo@137: Response response = getResponse(); ingo@137: response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe); ingo@137: return new EmptyRepresentation(); ingo@137: } ingo@137: ingo@137: String action = XMLUtils.xpathString( ingo@137: inputDocument, ingo@137: XPATH_ACTION, ingo@137: ArtifactNamespaceContext.INSTANCE); ingo@137: ingo@137: if (action == null || action.length() == 0) { ingo@137: Response response = getResponse(); ingo@137: response.setStatus( ingo@137: Status.CLIENT_ERROR_BAD_REQUEST, NO_ACTION_MSG); ingo@137: return new EmptyRepresentation(); ingo@137: } ingo@137: ingo@137: Request request = getRequest(); ingo@137: ingo@137: String identifier = (String)request.getAttributes().get("uuid"); ingo@137: ingo@137: ArtifactDatabase db = getArtifactDatabase(); ingo@137: ingo@137: return dispatch(identifier, action, inputDocument, db); ingo@137: } ingo@137: ingo@137: /** ingo@137: * Method to figure out which POST action (feed or advance) was ingo@137: * triggered and perform this operation on the artifact specified ingo@137: * by 'identifier' and found in the artifact database 'db' ingo@137: * @param identifier The identifier of the artifact. ingo@137: * @param action The action to be performed. ingo@137: * @param source The input document to further parameterize the ingo@137: * operation. ingo@137: * @param db The artifact database where to find the artifact. ingo@137: * @return The representation produced by the performed action. ingo@137: */ ingo@137: protected Representation dispatch( ingo@137: String identifier, ingo@137: String action, ingo@137: Document source, ingo@137: ArtifactDatabase db) ingo@137: { ingo@137: Document out = null; ingo@137: ingo@137: logger.info("Action: " + action + " | User: " + identifier); ingo@137: ingo@137: try { ingo@137: if (action.equals(ACTION_DELETE)) { ingo@137: out = db.deleteUser(identifier, getCallMeta()); ingo@137: } ingo@137: else { ingo@137: throw new ArtifactDatabaseException(NO_SUCH_ACTION_MSG); ingo@137: } ingo@137: } ingo@137: catch (ArtifactDatabaseException adbe) { ingo@137: logger.warn(adbe.getLocalizedMessage(), adbe); ingo@137: Response response = getResponse(); ingo@137: response.setStatus( ingo@137: Status.CLIENT_ERROR_BAD_REQUEST, adbe.getMessage()); ingo@137: return new EmptyRepresentation(); ingo@137: } ingo@137: ingo@137: return new DomRepresentation(MediaType.APPLICATION_XML, out); ingo@137: } ingo@137: } ingo@137: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :