teichmann@5861: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
teichmann@5861: * Software engineering by Intevation GmbH
teichmann@5861: *
teichmann@5993: * This file is Free Software under the GNU AGPL (>=v3)
teichmann@5861: * and comes with ABSOLUTELY NO WARRANTY! Check out the
teichmann@5993: * documentation coming with Dive4Elements River for details.
teichmann@5861: */
teichmann@5861:
teichmann@5835: package org.dive4elements.river.client.server;
ingo@29:
teichmann@5835: import org.dive4elements.artifacts.common.ArtifactNamespaceContext;
teichmann@5835: import org.dive4elements.artifacts.common.utils.XMLUtils;
teichmann@5835: import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException;
teichmann@5835: import org.dive4elements.artifacts.httpclient.http.HttpClient;
teichmann@5835: import org.dive4elements.artifacts.httpclient.http.HttpClientImpl;
teichmann@5835: import org.dive4elements.river.client.client.services.RiverService;
teichmann@5835: import org.dive4elements.river.client.server.auth.User;
teichmann@5835: import org.dive4elements.river.client.shared.exceptions.ServerException;
teichmann@5835: import org.dive4elements.river.client.shared.model.DefaultRiver;
teichmann@5835: import org.dive4elements.river.client.shared.model.River;
christian@3540:
ingo@29: import java.util.ArrayList;
ingo@29: import java.util.List;
ingo@29:
ingo@29: import javax.xml.xpath.XPathConstants;
ingo@29:
ingo@1367: import org.apache.log4j.Logger;
christian@3540: import org.w3c.dom.Document;
christian@3540: import org.w3c.dom.Element;
christian@3540: import org.w3c.dom.NodeList;
bjoern@3492:
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: {
teichmann@8203: /** Private log. */
teichmann@8203: private static final Logger log =
ingo@1367: Logger.getLogger(RiverServiceImpl.class);
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:
felix@2940: /** Get river list. */
sascha@3496: @Override
raimund@1425: public River[] list(String locale)
ingo@217: throws ServerException
ingo@217: {
raimund@1425: String url = getServletContext().getInitParameter("server-url");
raimund@1425:
felix@2940: 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);
bjoern@3492: User user = this.getUser();
ingo@29:
ingo@29: for (int i = 0; i < count; i++) {
sascha@3496: Element tmp = (Element)rivers.item(i);
ingo@29:
sascha@3496: String name = tmp.getAttributeNS(
sascha@3496: ArtifactNamespaceContext.NAMESPACE_URI, "name");
rrenkert@7761: String mUuid = tmp.getAttributeNS(
rrenkert@7761: ArtifactNamespaceContext.NAMESPACE_URI, "modeluuid");
ingo@29:
sascha@3496: if (name.length() > 0
sascha@3496: && (user == null || user.canUseFeature("river:" + name))) {
rrenkert@7761: theRivers.add(new DefaultRiver(name, mUuid));
bjoern@3492: }
ingo@29: }
ingo@29:
sascha@3496: return theRivers.toArray(new River[theRivers.size()]);
ingo@29: }
ingo@29: catch (ConnectionException ce) {
teichmann@8203: log.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 :