ingo@29: package de.intevation.flys.client.server;
ingo@29: 
christian@3540: import de.intevation.artifacts.common.ArtifactNamespaceContext;
christian@3540: import de.intevation.artifacts.common.utils.XMLUtils;
christian@3540: import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
christian@3540: import de.intevation.artifacts.httpclient.http.HttpClient;
christian@3540: import de.intevation.artifacts.httpclient.http.HttpClientImpl;
christian@3540: import de.intevation.flys.client.client.services.RiverService;
christian@3540: import de.intevation.flys.client.server.auth.User;
christian@3540: import de.intevation.flys.client.shared.exceptions.ServerException;
christian@3540: import de.intevation.flys.client.shared.model.DefaultRiver;
christian@3540: import de.intevation.flys.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 <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
ingo@29:  */
ingo@29: public class RiverServiceImpl
ingo@29: extends      RemoteServiceServlet
ingo@29: implements   RiverService
ingo@29: {
felix@2940:     /** Private logger. */
ingo@1367:     private static final Logger logger =
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<River> theRivers = new ArrayList<River>(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");
ingo@29: 
sascha@3496:                 if (name.length() > 0
sascha@3496:                 && (user == null || user.canUseFeature("river:" + name))) {
bjoern@3492:                     theRivers.add(new DefaultRiver(name));
bjoern@3492:                 }
ingo@29:             }
ingo@29: 
sascha@3496:             return theRivers.toArray(new River[theRivers.size()]);
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 :