Mercurial > dive4elements > river
changeset 1600:67468c90ca68
Added service to request discharge infos at a specific gauge.
flys-client/trunk@3938 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Raimund Renkert <raimund.renkert@intevation.de> |
---|---|
date | Mon, 06 Feb 2012 16:01:22 +0000 |
parents | 4ba6f1fb3a03 |
children | 42543705a857 |
files | flys-client/ChangeLog flys-client/src/main/java/de/intevation/flys/client/client/services/DischargeInfoService.java flys-client/src/main/java/de/intevation/flys/client/client/services/DischargeInfoServiceAsync.java flys-client/src/main/java/de/intevation/flys/client/server/DischargeInfoServiceImpl.java flys-client/src/main/java/de/intevation/flys/client/server/DischargeInfoXML.java flys-client/src/main/java/de/intevation/flys/client/shared/model/DischargeInfoObject.java flys-client/src/main/java/de/intevation/flys/client/shared/model/DischargeInfoObjectImpl.java flys-client/src/main/webapp/WEB-INF/web.xml |
diffstat | 8 files changed, 368 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/flys-client/ChangeLog Mon Feb 06 15:14:25 2012 +0000 +++ b/flys-client/ChangeLog Mon Feb 06 16:01:22 2012 +0000 @@ -1,3 +1,25 @@ +2012-02-06 Raimund Renkert <raimund.renkert@intevation.de> + + Added service to request discharge infos at a specific gauge. + + * src/main/java/de/intevation/flys/client/client/services/DischargeInfoService.java, + src/main/java/de/intevation/flys/client/client/services/DischargeInfoServiceAsync.java: + New. Interfaces for new service. + + * src/main/java/de/intevation/flys/client/server/DischargeInfoServiceImpl.java: + New. Service implementation. + + * src/main/java/de/intevation/flys/client/server/DischargeInfoXML.java: + New. Service to request the discharge infos and provide the data via servlet + to a data source object. + + * src/main/java/de/intevation/flys/client/shared/model/DischargeInfoObject.java, + src/main/java/de/intevation/flys/client/shared/model/DischargeInfoObjectImpl.java: + New. Interface and implementation for objects containing discharge infos. + + * src/main/webapp/WEB-INF/web.xml: + Added entries for discharge info servlets and servlet mappings. + 2012-02-06 Sascha L. Teichmann <sascha.teichmann@intevation.de> * src/main/java/de/intevation/flys/client/client/FLYSConstants_en.properties,
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/client/services/DischargeInfoService.java Mon Feb 06 16:01:22 2012 +0000 @@ -0,0 +1,26 @@ +package de.intevation.flys.client.client.services; + +import com.google.gwt.user.client.rpc.RemoteService; +import com.google.gwt.user.client.rpc.RemoteServiceRelativePath; + +import de.intevation.flys.client.shared.exceptions.ServerException; +import de.intevation.flys.client.shared.model.DischargeInfoObject; + +/** + * This service is used to fetch a list of DischargeInfoObjects from artifact + * server for a specific gauge. + * + * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> + */ +@RemoteServiceRelativePath("dischargeinfo") +public interface DischargeInfoService extends RemoteService { + + /** + * This method returns a list of DischargeInfoObjects for a specific gauge. + */ + DischargeInfoObject[] getDischargeInfo( + String locale, + long gauge) + throws ServerException; +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/client/services/DischargeInfoServiceAsync.java Mon Feb 06 16:01:22 2012 +0000 @@ -0,0 +1,18 @@ +package de.intevation.flys.client.client.services; + +import com.google.gwt.user.client.rpc.AsyncCallback; + +import de.intevation.flys.client.shared.model.DischargeInfoObject; + + +/** + * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> + */ +public interface DischargeInfoServiceAsync { + + void getDischargeInfo( + String locale, + long gauge, + AsyncCallback<DischargeInfoObject[]> cb); +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/server/DischargeInfoServiceImpl.java Mon Feb 06 16:01:22 2012 +0000 @@ -0,0 +1,138 @@ +package de.intevation.flys.client.server; + +import java.util.ArrayList; +import java.util.List; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +import org.apache.log4j.Logger; + +import com.google.gwt.user.server.rpc.RemoteServiceServlet; + +import de.intevation.artifacts.common.ArtifactNamespaceContext; +import de.intevation.artifacts.common.utils.XMLUtils; + +import de.intevation.artifacts.httpclient.exceptions.ConnectionException; +import de.intevation.artifacts.httpclient.http.HttpClient; +import de.intevation.artifacts.httpclient.http.HttpClientImpl; + +import de.intevation.flys.client.shared.exceptions.ServerException; +import de.intevation.flys.client.client.services.DischargeInfoService; +import de.intevation.flys.client.shared.model.DischargeInfoObject; +import de.intevation.flys.client.shared.model.DischargeInfoObjectImpl; + + +/** + * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> + */ +public class DischargeInfoServiceImpl +extends RemoteServiceServlet +implements DischargeInfoService +{ + private static final Logger logger = + Logger.getLogger(DischargeInfoServiceImpl.class); + + public static final String ERROR_NO_DISCHARGEINFO_FOUND = + "error_no_dischargeinfo_found"; + + public static final String XPATH_DISTANCES = "art:discharges/art:discharge"; + + + public DischargeInfoObject[] getDischargeInfo( + String locale, + long gauge) + throws ServerException + { + logger.info("DichargeInfoServiceImpl.getDischargeInfo"); + + String url = getServletContext().getInitParameter("server-url"); + + Document doc = XMLUtils.newDocument(); + + XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator( + doc, + ArtifactNamespaceContext.NAMESPACE_URI, + ArtifactNamespaceContext.NAMESPACE_PREFIX); + + Element gaugeEl = ec.create("gauge"); + gaugeEl.setTextContent(String.valueOf(gauge)); + + doc.appendChild(gaugeEl); + + HttpClient client = new HttpClientImpl(url, locale); + + try { + Document result = client.callService(url, "dischargeinfo", doc); + + logger.debug("Extract discharge info objects now."); + DischargeInfoObject[] objects = extractDischargeInfoObjects(result); + + if (objects != null && objects.length > 0) { + return objects; + } + } + catch (ConnectionException ce) { + logger.error(ce, ce); + } + + throw new ServerException(ERROR_NO_DISCHARGEINFO_FOUND); + } + + protected DischargeInfoObject[] extractDischargeInfoObjects( + Document result + ) + throws ServerException { + NodeList list = result.getElementsByTagName("discharge"); + + if (list == null || list.getLength() == 0) { + logger.warn("No discharge info found."); + throw new ServerException(ERROR_NO_DISCHARGEINFO_FOUND); + } + + int num = list.getLength(); + logger.debug("Response contains " + num + " objects."); + + List<DischargeInfoObject> objects = + new ArrayList<DischargeInfoObject>(num); + + for (int i = 0; i < num; i++) { + DischargeInfoObject obj = buildDischargeInfoObject( + (Element)list.item(i)); + + if (obj != null) { + objects.add(obj); + } + } + + logger.debug("Retrieved " + objects.size() + " discharges."); + + return (DischargeInfoObject[]) + objects.toArray(new DischargeInfoObject[num]); + + } + + protected DischargeInfoObject buildDischargeInfoObject(Element node) { + + String desc = node.getAttribute("description").trim(); + String start = node.getAttribute("start").trim(); + String end = node.getAttribute("end").trim(); + + if (start.length() > 0 && end.length() > 0) { + try { + Integer startYear = new Integer(start); + Integer endYear = new Integer(end); + return new DischargeInfoObjectImpl(desc, startYear, endYear); + } + catch (NumberFormatException nfe) { + logger.warn(nfe.getLocalizedMessage()); + } + } + + logger.warn("Invalid distance info object found."); + + return null; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/server/DischargeInfoXML.java Mon Feb 06 16:01:22 2012 +0000 @@ -0,0 +1,82 @@ +package de.intevation.flys.client.server; + +import java.io.InputStream; +import java.io.IOException; +import java.io.OutputStream; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import org.apache.log4j.Logger; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import de.intevation.artifacts.common.ArtifactNamespaceContext; +import de.intevation.artifacts.common.utils.XMLUtils; + +import de.intevation.artifacts.httpclient.exceptions.ConnectionException; +import de.intevation.artifacts.httpclient.http.HttpClient; +import de.intevation.artifacts.httpclient.http.HttpClientImpl; +import de.intevation.artifacts.httpclient.http.response.StreamResponseHandler; + + +/** + * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> + */ +public class DischargeInfoXML +extends HttpServlet +{ + private static final Logger logger = Logger.getLogger(DischargeInfoXML.class); + + + public static final String ERROR_NO_DISTANCEINFO_FOUND = + "error_no_dischargeinfo_found"; + + + public void doGet(HttpServletRequest req, HttpServletResponse resp) { + logger.info("DischargeInfoXML.doGet"); + + String url = getServletContext().getInitParameter("server-url"); + + String gauge = req.getParameter("gauge"); + + Document doc = XMLUtils.newDocument(); + + XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator( + doc, + ArtifactNamespaceContext.NAMESPACE_URI, + ArtifactNamespaceContext.NAMESPACE_PREFIX); + + Element gaugeEl = ec.create("gauge"); + gaugeEl.setTextContent(gauge); + + doc.appendChild(gaugeEl); + + HttpClient client = new HttpClientImpl(url); + + try { + InputStream in = (InputStream) client.callService( + url, "dischargeinfo", doc, new StreamResponseHandler()); + + OutputStream out = resp.getOutputStream(); + + byte[] b = new byte[4096]; + int i; + while ((i = in.read(b)) >= 0) { + out.write(b, 0, i); + } + + out.flush(); + out.close(); + } + catch (ConnectionException ce) { + logger.error(ce, ce); + } + catch (IOException ioe) { + logger.error(ioe, ioe); + } + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/DischargeInfoObject.java Mon Feb 06 16:01:22 2012 +0000 @@ -0,0 +1,17 @@ +package de.intevation.flys.client.shared.model; + +import java.io.Serializable; + + +/** + * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> + */ +public interface DischargeInfoObject extends Serializable { + + String getDescription(); + + Integer getStartYear(); + + Integer getEndYear(); +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/DischargeInfoObjectImpl.java Mon Feb 06 16:01:22 2012 +0000 @@ -0,0 +1,44 @@ +package de.intevation.flys.client.shared.model; + + +/** + * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> + */ +public class DischargeInfoObjectImpl implements DischargeInfoObject { + + protected String description; + + protected Integer startYear; + + protected Integer endYear; + + + public DischargeInfoObjectImpl() { + } + + + public DischargeInfoObjectImpl( + String description, + Integer startYear, + Integer endYear + ) { + this.description = description; + this.startYear = startYear; + this.endYear = endYear; + } + + public String getDescription() { + return description; + } + + + public Integer getStartYear() { + return startYear; + } + + + public Integer getEndYear() { + return endYear; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-client/src/main/webapp/WEB-INF/web.xml Mon Feb 06 15:14:25 2012 +0000 +++ b/flys-client/src/main/webapp/WEB-INF/web.xml Mon Feb 06 16:01:22 2012 +0000 @@ -7,7 +7,7 @@ <context-param> <param-name>server-url</param-name> - <param-value>http://localhost:8181</param-value> + <param-value>http://localhost:8182</param-value> </context-param> <!-- Servlets --> @@ -164,6 +164,26 @@ </servlet-mapping> <servlet> + <servlet-name>dischargeinfo</servlet-name> + <servlet-class>de.intevation.flys.client.server.DischargeInfoServiceImpl</servlet-class> + </servlet> + + <servlet-mapping> + <servlet-name>dischargeinfo</servlet-name> + <url-pattern>/flys/dischargeinfo</url-pattern> + </servlet-mapping> + + <servlet> + <servlet-name>DischargeInfoXML</servlet-name> + <servlet-class>de.intevation.flys.client.server.DischargeInfoXML</servlet-class> + </servlet> + + <servlet-mapping> + <servlet-name>DischargeInfoXML</servlet-name> + <url-pattern>/flys/dischargeinfoxml</url-pattern> + </servlet-mapping> + + <servlet> <servlet-name>meta-data</servlet-name> <servlet-class>de.intevation.flys.client.server.MetaDataServiceImpl</servlet-class> </servlet>