ingo@100: /* ingo@100: * Copyright (c) 2010 by Intevation GmbH ingo@100: * ingo@100: * This program is free software under the LGPL (>=v2.1) ingo@100: * Read the file LGPL.txt coming with the software for details ingo@100: * or visit http://www.gnu.org/licenses/ if it does not exist. ingo@100: */ ingo@100: teichmann@475: package org.dive4elements.artifactdatabase.rest; sascha@20: teichmann@475: import org.dive4elements.artifacts.common.utils.XMLUtils; teichmann@475: import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator; sascha@93: teichmann@475: import org.dive4elements.artifacts.ArtifactDatabase; teichmann@475: import org.dive4elements.artifacts.ArtifactNamespaceContext; sascha@20: tom@570: import org.apache.logging.log4j.Logger; tom@570: import org.apache.logging.log4j.LogManager; sascha@93: sascha@20: import org.restlet.data.MediaType; sascha@20: sascha@20: import org.restlet.ext.xml.DomRepresentation; sascha@20: sascha@22: import org.restlet.representation.Representation; sascha@22: sascha@93: import org.restlet.resource.ResourceException; sascha@20: sascha@20: import org.w3c.dom.Document; sascha@20: import org.w3c.dom.Element; sascha@20: sascha@21: /** sascha@88: * Resource to list the available factories. sascha@77: * @author Sascha L. Teichmann sascha@21: */ sascha@20: public class FactoriesResource sascha@40: extends BaseResource sascha@20: { tom@570: private static Logger logger = LogManager.getLogger(FactoriesResource.class); sascha@47: sascha@88: /** sascha@88: * server URL where to reach the resource. sascha@88: */ sascha@20: public static final String PATH = "/factories"; sascha@20: sascha@89: @Override sascha@40: protected Representation innerGet() sascha@40: throws ResourceException sascha@40: { sascha@20: Document document = XMLUtils.newDocument(); sascha@20: sascha@20: ElementCreator ec = new ElementCreator( sascha@20: document, sascha@24: ArtifactNamespaceContext.NAMESPACE_URI, sascha@24: ArtifactNamespaceContext.NAMESPACE_PREFIX); sascha@20: sascha@20: ArtifactDatabase db = (ArtifactDatabase)getContext() sascha@20: .getAttributes().get("database"); sascha@20: sascha@20: Element root = ec.create("result"); sascha@20: document.appendChild(root); sascha@20: sascha@20: Element type = ec.create("type"); sascha@20: ec.addAttr(type, "name", "factory-list"); sascha@20: root.appendChild(type); sascha@20: sascha@20: Element factories = ec.create("factories"); sascha@20: root.appendChild(factories); sascha@20: sascha@32: String [][] factoryNames = db.artifactFactoryNamesAndDescriptions(); sascha@20: sascha@20: for (int i = 0; i < factoryNames.length; ++i) { sascha@32: String [] nd = factoryNames[i]; sascha@20: Element factoryElement = ec.create("factory"); sascha@32: ec.addAttr(factoryElement, "name", nd[0]); sascha@32: ec.addAttr(factoryElement, "description", nd[1]); sascha@20: factories.appendChild(factoryElement); sascha@20: } sascha@20: sascha@41: document.normalizeDocument(); sascha@41: sascha@20: return new DomRepresentation( sascha@20: MediaType.APPLICATION_XML, document); sascha@20: } sascha@20: } sascha@91: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :