ingo@131: /* ingo@131: * Copyright (c) 2011 by Intevation GmbH ingo@131: * ingo@131: * This program is free software under the LGPL (>=v2.1) ingo@131: * Read the file LGPL.txt coming with the software for details ingo@131: * or visit http://www.gnu.org/licenses/ if it does not exist. ingo@131: */ ingo@131: package de.intevation.artifactdatabase.rest; ingo@131: ingo@131: import java.io.IOException; ingo@131: ingo@131: import org.apache.log4j.Logger; ingo@131: ingo@131: import org.restlet.Response; ingo@131: import org.restlet.data.MediaType; ingo@131: import org.restlet.data.Status; ingo@131: import org.restlet.ext.xml.DomRepresentation; ingo@131: import org.restlet.representation.EmptyRepresentation; ingo@131: import org.restlet.representation.Representation; ingo@131: import org.restlet.resource.ResourceException; ingo@131: ingo@131: import org.w3c.dom.Document; ingo@131: ingo@131: import de.intevation.artifacts.ArtifactDatabase; ingo@131: import de.intevation.artifacts.ArtifactDatabaseException; ingo@131: ingo@131: ingo@131: /** ingo@131: * Resource to create a new users within the artifact database. ingo@131: * ingo@131: * @author Ingo Weinzierl ingo@131: */ ingo@131: public class CreateUserResource ingo@131: extends BaseResource ingo@131: { ingo@131: /** The logger used in this class.*/ ingo@131: private static Logger logger = Logger.getLogger(CreateUserResource.class); ingo@131: ingo@131: /** The URL part for this resource.*/ ingo@131: public static final String PATH = "/create-user"; ingo@131: ingo@131: ingo@131: @Override ingo@131: protected Representation innerPost(Representation requestRepr) ingo@131: throws ResourceException ingo@131: { ingo@131: Document input = null; ingo@131: ingo@131: try { ingo@131: DomRepresentation in = new DomRepresentation(requestRepr); ingo@131: in.setNamespaceAware(true); ingo@131: input = in.getDocument(); ingo@131: } ingo@131: catch (IOException ioe) { ingo@131: logger.error(ioe.getLocalizedMessage(), ioe); ingo@131: ingo@131: Response response = getResponse(); ingo@131: response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe); ingo@131: return new EmptyRepresentation(); ingo@131: } ingo@131: ingo@131: logger.debug("Create user"); ingo@131: ingo@131: ArtifactDatabase db = getArtifactDatabase(); ingo@131: ingo@131: try { ingo@131: return new DomRepresentation( ingo@131: MediaType.APPLICATION_XML, ingo@131: db.createUser(input, getCallMeta())); ingo@131: } ingo@131: catch (ArtifactDatabaseException adbe) { ingo@131: Response response = getResponse(); ingo@131: response.setStatus( ingo@131: Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY, adbe.getMessage()); ingo@131: return new EmptyRepresentation(); ingo@131: } ingo@131: } ingo@131: } ingo@131: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :