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: sascha@72: package de.intevation.artifactdatabase.rest; sascha@72: sascha@72: import de.intevation.artifactdatabase.XMLUtils.ElementCreator; sascha@72: sascha@72: import de.intevation.artifactdatabase.XMLUtils; sascha@72: sascha@72: import de.intevation.artifacts.ArtifactDatabase; sascha@72: import de.intevation.artifacts.ArtifactNamespaceContext; sascha@72: sascha@72: import org.apache.log4j.Logger; sascha@72: sascha@72: import org.restlet.data.MediaType; sascha@72: sascha@72: import org.restlet.ext.xml.DomRepresentation; sascha@72: sascha@72: import org.restlet.representation.Representation; sascha@72: sascha@72: import org.restlet.resource.ResourceException; sascha@72: sascha@72: import org.w3c.dom.Document; sascha@72: import org.w3c.dom.Element; sascha@72: sascha@72: /** sascha@88: * Resource to list the available service offered by the artifact database. sascha@88: * sascha@77: * @author Sascha L. Teichmann sascha@72: */ sascha@72: public class ServicesResource sascha@72: extends BaseResource sascha@72: { sascha@72: private static Logger logger = Logger.getLogger(ServicesResource.class); sascha@72: sascha@88: /** sascha@88: * server URL where to reach the resource. sascha@88: */ sascha@72: public static final String PATH = "/services"; sascha@72: sascha@89: @Override sascha@72: protected Representation innerGet() sascha@72: throws ResourceException sascha@72: { sascha@72: Document document = XMLUtils.newDocument(); sascha@72: sascha@72: ElementCreator ec = new ElementCreator( sascha@72: document, sascha@72: ArtifactNamespaceContext.NAMESPACE_URI, sascha@72: ArtifactNamespaceContext.NAMESPACE_PREFIX); sascha@72: sascha@72: ArtifactDatabase db = (ArtifactDatabase)getContext() sascha@72: .getAttributes().get("database"); sascha@72: sascha@72: Element root = ec.create("result"); sascha@72: document.appendChild(root); sascha@72: sascha@72: Element type = ec.create("type"); sascha@72: ec.addAttr(type, "name", "service-list"); sascha@72: root.appendChild(type); sascha@72: sascha@72: Element factories = ec.create("services"); sascha@72: root.appendChild(factories); sascha@72: sascha@72: String [][] factoryNames = db.serviceNamesAndDescriptions(); sascha@72: sascha@72: for (int i = 0; i < factoryNames.length; ++i) { sascha@72: String [] nd = factoryNames[i]; sascha@72: Element factoryElement = ec.create("service"); sascha@72: ec.addAttr(factoryElement, "name", nd[0]); sascha@72: ec.addAttr(factoryElement, "description", nd[1]); sascha@72: factories.appendChild(factoryElement); sascha@72: } sascha@72: sascha@72: document.normalizeDocument(); sascha@72: sascha@72: return new DomRepresentation( sascha@72: MediaType.APPLICATION_XML, document); sascha@72: } sascha@72: } sascha@72: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :