ingo@29: package de.intevation.flys.client.server; ingo@29: ingo@29: import java.util.ArrayList; ingo@29: import java.util.List; ingo@29: ingo@29: import javax.xml.xpath.XPathConstants; ingo@29: ingo@29: import org.w3c.dom.Document; ingo@29: import org.w3c.dom.NodeList; ingo@29: import org.w3c.dom.Node; ingo@29: ingo@1367: import org.apache.log4j.Logger; ingo@1367: ingo@29: import com.google.gwt.user.server.rpc.RemoteServiceServlet; ingo@29: ingo@29: import de.intevation.artifacts.common.ArtifactNamespaceContext; ingo@29: import de.intevation.artifacts.common.utils.XMLUtils; ingo@29: ingo@29: import de.intevation.artifacts.httpclient.exceptions.ConnectionException; ingo@29: import de.intevation.artifacts.httpclient.http.HttpClient; ingo@29: import de.intevation.artifacts.httpclient.http.HttpClientImpl; ingo@29: ingo@217: import de.intevation.flys.client.shared.exceptions.ServerException; ingo@29: import de.intevation.flys.client.shared.model.DefaultRiver; ingo@29: import de.intevation.flys.client.shared.model.River; ingo@29: import de.intevation.flys.client.client.services.RiverService; ingo@29: ingo@29: ingo@29: /** ingo@29: * This interface provides a method to list the supported rivers of the artifact ingo@29: * server. ingo@29: * ingo@29: * @author Ingo Weinzierl ingo@29: */ ingo@29: public class RiverServiceImpl ingo@29: extends RemoteServiceServlet ingo@29: implements RiverService ingo@29: { ingo@1367: private static final Logger logger = ingo@1367: Logger.getLogger(RiverServiceImpl.class); ingo@1367: ingo@1367: ingo@29: /** The XPath string that points to the rivers in the resulting document.*/ ingo@29: public static final String XPATH_RIVERS = "/art:rivers/art:river"; ingo@29: ingo@217: /** The error message key that is thrown if an error occured while reading ingo@217: * the supported rivers from server.*/ ingo@217: public static final String ERROR_NO_RIVERS_FOUND = "error_no_rivers_found"; ingo@29: ingo@217: raimund@1425: public River[] list(String locale) ingo@217: throws ServerException ingo@217: { raimund@1425: String url = getServletContext().getInitParameter("server-url"); raimund@1425: ingo@29: Document doc = XMLUtils.newDocument(); ingo@29: ingo@29: XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator( ingo@29: doc, ingo@29: ArtifactNamespaceContext.NAMESPACE_URI, ingo@29: ArtifactNamespaceContext.NAMESPACE_PREFIX); ingo@29: ingo@29: doc.appendChild(ec.create("action")); ingo@29: raimund@1425: HttpClient client = new HttpClientImpl(url, locale); ingo@29: ingo@29: try { raimund@1425: Document res = client.callService(url, "rivers", doc); ingo@29: ingo@29: NodeList rivers = (NodeList) XMLUtils.xpath( ingo@29: res, ingo@29: XPATH_RIVERS, ingo@29: XPathConstants.NODESET, ingo@29: ArtifactNamespaceContext.INSTANCE); ingo@29: ingo@217: if (rivers == null || rivers.getLength() == 0) { ingo@217: throw new ServerException(ERROR_NO_RIVERS_FOUND); ingo@217: } ingo@217: ingo@29: int count = rivers.getLength(); ingo@29: ingo@29: List theRivers = new ArrayList(count); ingo@29: ingo@29: for (int i = 0; i < count; i++) { ingo@29: Node tmp = rivers.item(i); ingo@29: ingo@29: String name = XMLUtils.xpathString( ingo@29: tmp, "@art:name", ArtifactNamespaceContext.INSTANCE); ingo@29: ingo@29: theRivers.add(new DefaultRiver(name)); ingo@29: } ingo@29: ingo@29: return (River[]) theRivers.toArray(new River[count]); ingo@29: } ingo@29: catch (ConnectionException ce) { ingo@1367: logger.error(ce, ce); ingo@29: } ingo@29: ingo@217: throw new ServerException(ERROR_NO_RIVERS_FOUND); ingo@29: } ingo@29: } ingo@29: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :