bjoern@3515: package de.intevation.flys.client.server;
bjoern@3515: 
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.ModuleService;
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.DefaultModule;
christian@3540: import de.intevation.flys.client.shared.model.Module;
christian@3540: 
bjoern@3515: import java.util.ArrayList;
bjoern@3515: import java.util.List;
bjoern@3515: 
bjoern@3529: import javax.xml.xpath.XPathConstants;
bjoern@3529: 
bjoern@3515: import org.apache.log4j.Logger;
bjoern@3529: import org.w3c.dom.Document;
bjoern@3529: import org.w3c.dom.Element;
bjoern@3529: import org.w3c.dom.NodeList;
bjoern@3529: 
bjoern@3515: public class ModuleServiceImpl
bjoern@3515: extends      RemoteServiceServlet
bjoern@3515: implements   ModuleService
bjoern@3515: {
bjoern@3515:     private static final Logger logger =
bjoern@3515:         Logger.getLogger(ModuleServiceImpl.class);
bjoern@3515: 
bjoern@3529:     public static final String XPATH_MODULES = "/art:modules/art:module";
bjoern@3515: 
bjoern@3529:     public static final String ERROR_NO_MODULES_FOUND =
bjoern@3529:         "error_no_module_found";
bjoern@3515: 
bjoern@3515:     @Override
bjoern@3529:     public Module[] list(String locale) throws ServerException {
bjoern@3515:         User user = this.getUser();
bjoern@3515: 
bjoern@3529:         logger.info("ModuleService.list");
bjoern@3515: 
bjoern@3529:         String url = getServletContext().getInitParameter("server-url");
bjoern@3529: 
bjoern@3529:         // create dummy xml
bjoern@3529:         Document doc = XMLUtils.newDocument();
bjoern@3529: 
bjoern@3529:         XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
bjoern@3529:             doc,
bjoern@3529:             ArtifactNamespaceContext.NAMESPACE_URI,
bjoern@3529:             ArtifactNamespaceContext.NAMESPACE_PREFIX);
bjoern@3529: 
bjoern@3529:         Element dummy = ec.create("modules");
bjoern@3529:         doc.appendChild(dummy);
bjoern@3529: 
bjoern@3529:         HttpClient client = new HttpClientImpl(url, locale);
bjoern@3529:         try {
bjoern@3529:             Document result = client.callService(url, "modules", doc);
bjoern@3529: 
bjoern@3529:             NodeList list = (NodeList) XMLUtils.xpath(
bjoern@3529:                 result,
bjoern@3529:                 XPATH_MODULES,
bjoern@3529:                 XPathConstants.NODESET,
bjoern@3529:                 ArtifactNamespaceContext.INSTANCE);
bjoern@3529: 
bjoern@3529:             if (list == null) {
bjoern@3529:                 logger.warn("No modules found.");
bjoern@3529: 
bjoern@3529:                 throw new ServerException(ERROR_NO_MODULES_FOUND);
bjoern@3515:             }
bjoern@3529: 
bjoern@3529:             int num = list.getLength();
bjoern@3529: 
bjoern@3529:             List<Module> modules = new ArrayList<Module>(list.getLength());
bjoern@3529:             for(int i =0; i < num; i++) {
bjoern@3529:                 Element em = (Element)list.item(i);
bjoern@3529:                 String name = em.getAttributeNS(
bjoern@3529:                         ArtifactNamespaceContext.NAMESPACE_URI, "name");
bjoern@3529:                 String localname = em.getAttributeNS(
bjoern@3529:                         ArtifactNamespaceContext.NAMESPACE_URI, "localname");
bjoern@3529:                 String strselected = em.getAttributeNS(
bjoern@3529:                         ArtifactNamespaceContext.NAMESPACE_URI, "selected");
bjoern@3529:                 boolean selected = strselected == null ? false :
bjoern@3529:                         strselected.equalsIgnoreCase("true");
bjoern@3529:                 logger.debug("Found module " + name + " " + localname);
bjoern@3529:                 if (user == null || user.canUseFeature("module:" + name)) {
bjoern@3529:                     modules.add(new DefaultModule(name, localname, selected));
bjoern@3529:                 }
bjoern@3529:             }
bjoern@3529:             return modules.toArray(new Module[modules.size()]);
bjoern@3515:         }
bjoern@3529:         catch (ConnectionException ce) {
bjoern@3529:             logger.error(ce, ce);
bjoern@3529:         }
bjoern@3529: 
bjoern@3529:         throw new ServerException(ERROR_NO_MODULES_FOUND);
bjoern@3515:     }
bjoern@3515: }
bjoern@3515: 
bjoern@3515: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 tw=80 :