sascha@21: package de.intevation.artifactdatabase.rest; sascha@21: sascha@21: import java.io.IOException; sascha@21: tim@75: import org.apache.log4j.Logger; tim@75: import org.restlet.data.MediaType; tim@75: import org.restlet.data.Response; tim@75: import org.restlet.data.Status; tim@75: import org.restlet.ext.xml.DomRepresentation; tim@75: import org.restlet.representation.EmptyRepresentation; tim@75: import org.restlet.representation.Representation; tim@75: import org.restlet.resource.ResourceException; tim@75: import org.w3c.dom.Document; tim@75: tim@75: import de.intevation.artifactdatabase.XMLUtils; tim@75: import de.intevation.artifacts.ArtifactDatabase; tim@75: import de.intevation.artifacts.ArtifactDatabaseException; sascha@26: import de.intevation.artifacts.ArtifactNamespaceContext; sascha@26: sascha@21: /** sascha@77: * @author Sascha L. Teichmann sascha@21: */ sascha@21: public class CreateResource sascha@40: extends BaseResource sascha@21: { sascha@21: private static Logger logger = Logger.getLogger(CreateResource.class); sascha@21: sascha@21: public static final String PATH = "/create"; sascha@21: ingo@65: public static final String XPATH_FACTORY = "/art:action/art:factory/@name"; sascha@26: sascha@26: public static final String NO_FACTORY_MESSAGE = "No factory given"; sascha@26: sascha@26: public static final String NO_ARTIFACT_CREATED = "No artifact created"; sascha@26: sascha@40: protected Representation innerPost(Representation requestRepr) sascha@40: throws ResourceException sascha@40: { sascha@21: Document inputDocument = null; sascha@21: try { sascha@21: DomRepresentation input = new DomRepresentation(requestRepr); ingo@65: input.setNamespaceAware(true); sascha@21: inputDocument = input.getDocument(); sascha@21: } sascha@21: catch (IOException ioe) { sascha@21: logger.error(ioe.getMessage()); sascha@21: Response response = getResponse(); sascha@21: response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe); sascha@21: return new EmptyRepresentation(); sascha@21: } sascha@26: sascha@26: String factory = XMLUtils.xpathString( sascha@26: inputDocument, sascha@26: XPATH_FACTORY, sascha@26: ArtifactNamespaceContext.INSTANCE); sascha@26: sascha@26: if (factory == null || factory.length() == 0) { sascha@26: Response response = getResponse(); sascha@32: response.setStatus( sascha@32: Status.CLIENT_ERROR_BAD_REQUEST, NO_FACTORY_MESSAGE); sascha@26: return new EmptyRepresentation(); sascha@26: } sascha@26: sascha@26: if (logger.isDebugEnabled()) { sascha@26: logger.debug("Create artifact with factory '" + factory + "'"); sascha@26: } sascha@26: sascha@26: ArtifactDatabase db = (ArtifactDatabase)getContext() sascha@26: .getAttributes().get("database"); sascha@26: sascha@32: try { sascha@32: return new DomRepresentation( sascha@32: MediaType.APPLICATION_XML, tim@75: db.createArtifactWithFactory(factory, tim@75: getCallMeta(), tim@75: inputDocument)); sascha@32: } sascha@32: catch (ArtifactDatabaseException adbe) { sascha@26: Response response = getResponse(); sascha@32: response.setStatus( sascha@32: Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY, adbe.getMessage()); sascha@26: return new EmptyRepresentation(); sascha@26: } sascha@21: } sascha@21: } sascha@21: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: