sascha@262: package de.intevation.flys.client.server; sascha@262: sascha@262: import java.util.List; sascha@276: import java.util.ArrayList; sascha@276: sascha@276: import org.w3c.dom.Document; sascha@276: import org.w3c.dom.Element; sascha@276: import org.w3c.dom.Node; sascha@276: import org.w3c.dom.NodeList; sascha@262: sascha@262: import com.google.gwt.user.server.rpc.RemoteServiceServlet; sascha@262: sascha@276: import de.intevation.artifacts.common.ArtifactNamespaceContext; sascha@276: sascha@276: import de.intevation.artifacts.common.utils.XMLUtils; sascha@276: sascha@262: import de.intevation.flys.client.shared.exceptions.ServerException; sascha@262: sascha@262: import de.intevation.flys.client.client.services.MetaDataService; sascha@262: sascha@276: import de.intevation.artifacts.httpclient.exceptions.ConnectionException; sascha@276: sascha@276: import de.intevation.artifacts.httpclient.http.HttpClient; sascha@276: import de.intevation.artifacts.httpclient.http.HttpClientImpl; sascha@276: sascha@278: import de.intevation.flys.client.shared.model.DataCageTree; sascha@278: import de.intevation.flys.client.shared.model.DataCageNode; sascha@278: sascha@262: public class MetaDataServiceImpl sascha@262: extends RemoteServiceServlet sascha@262: implements MetaDataService sascha@262: { sascha@276: public static final String ERROR_NO_META_DATA_FOUND = sascha@276: "error_no_meta_data_found"; sascha@276: sascha@278: public DataCageTree getMetaData(String url, String locale, String river) sascha@262: throws ServerException sascha@262: { sascha@276: System.out.println("MetaDataService.getDistanceInfo"); sascha@276: sascha@276: Document doc = XMLUtils.newDocument(); sascha@276: sascha@276: XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator( sascha@276: doc, sascha@276: ArtifactNamespaceContext.NAMESPACE_URI, sascha@276: ArtifactNamespaceContext.NAMESPACE_PREFIX); sascha@276: sascha@276: Element riverEl = ec.create("river"); sascha@276: sascha@276: riverEl.setTextContent(river); sascha@276: sascha@276: doc.appendChild(riverEl); sascha@276: sascha@276: HttpClient client = new HttpClientImpl(url, locale); sascha@276: sascha@276: try { sascha@278: return convert(client.callService(url, "metadata", doc)); sascha@276: } sascha@276: catch (ConnectionException ce) { sascha@276: System.err.println(ce.getLocalizedMessage()); sascha@276: } sascha@276: sascha@276: throw new ServerException(ERROR_NO_META_DATA_FOUND); sascha@262: } sascha@278: sascha@278: protected static DataCageNode convert(Node node) { sascha@278: String name = node.getLocalName(); sascha@278: DataCageNode dvn = new DataCageNode(name); sascha@278: NodeList children = node.getChildNodes(); sascha@278: for (int i = 0, N = children.getLength(); i < N; ++i) { sascha@278: dvn.addChild(convert(children.item(i))); sascha@278: } sascha@278: return dvn; sascha@278: } sascha@278: sascha@278: protected static DataCageTree convert(Document document) { sascha@278: ArrayList roots = new ArrayList(); sascha@278: NodeList nodes = document.getChildNodes(); sascha@278: for (int i = 0, N = nodes.getLength(); i < N; ++i) { sascha@278: Node node = nodes.item(i); sascha@278: if (node.getNodeType() == Node.ELEMENT_NODE) { sascha@278: roots.add(convert(nodes.item(i))); sascha@278: } sascha@278: } sascha@278: return new DataCageTree(roots.get(0)); sascha@278: } sascha@262: } sascha@262: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :