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