001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/portal/standard/sos/control/AbstractSOSListener.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstr. 19
030: 53115 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042:
043: ---------------------------------------------------------------------------*/
044: package org.deegree.portal.standard.sos.control;
045:
046: import java.io.InputStreamReader;
047: import java.net.URL;
048: import java.util.HashMap;
049:
050: import org.apache.commons.httpclient.HttpClient;
051: import org.apache.commons.httpclient.methods.PostMethod;
052: import org.apache.commons.httpclient.methods.StringRequestEntity;
053: import org.deegree.enterprise.WebUtils;
054: import org.deegree.enterprise.control.AbstractListener;
055: import org.deegree.enterprise.control.FormEvent;
056: import org.deegree.enterprise.control.RPCMethodCall;
057: import org.deegree.enterprise.control.RPCWebEvent;
058: import org.deegree.framework.xml.XMLTools;
059: import org.deegree.portal.standard.sos.SOSClientException;
060: import org.deegree.portal.standard.sos.configuration.SOSClientConfiguration;
061: import org.w3c.dom.Document;
062:
063: /**
064: * ...
065: *
066: * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
067: *
068: */
069: public abstract class AbstractSOSListener extends AbstractListener {
070:
071: /**
072: * Validates a request
073: * @param mc the remote procedure call bean
074: * @throws SOSClientException
075: */
076: protected abstract void validateRequest(RPCMethodCall mc)
077: throws SOSClientException;
078:
079: /**
080: * Create a request
081: * @param mc the remote procedure call bean
082: * @return a String containing the request.
083: * @throws SOSClientException
084: */
085: protected abstract String createRequest(RPCMethodCall mc)
086: throws SOSClientException;
087:
088: /**
089: * Create a Data from the call and the given map.
090: * @param mc the remote procedure call bean
091: * @param map containing the document nodes.
092: * @return the data bean.
093: * @throws SOSClientException
094: */
095: protected abstract Object createData(RPCMethodCall mc,
096: HashMap<String, Document> map) throws SOSClientException;
097:
098: /**
099: * sets the parameter as ServletRequest attribute to enable access to the result for the next
100: * page
101: *
102: * @param data
103: * param/result
104: */
105: protected abstract void setNextPageData(Object data);
106:
107: @Override
108: public void actionPerformed(FormEvent e) {
109:
110: RPCWebEvent rpcEvent = (RPCWebEvent) e;
111: RPCMethodCall mc = rpcEvent.getRPCMethodCall();
112:
113: try {
114: validateRequest(mc);
115: } catch (Exception ex) {
116: gotoErrorPage("Invalid Sensor Observation Service DescribePlatform request: "
117: + ex.toString());
118: return;
119: }
120:
121: String request = null;
122: try {
123: request = createRequest(mc);
124: } catch (Exception ex) {
125: gotoErrorPage("Couldn't create Sensor Observation Service DescribePlatform request: "
126: + ex.toString());
127: return;
128: }
129:
130: HashMap<String, Document> map = null;
131: try {
132: map = performRequest(request);
133: } catch (Exception ex) {
134: gotoErrorPage("Couldn't perform Sensor Observation Service DescribePlatform request: "
135: + ex.toString());
136: return;
137: }
138:
139: Object data = null;
140: try {
141: data = createData(mc, map);
142: } catch (Exception ex) {
143: gotoErrorPage("Couldn't format Sensor Observation Service result: "
144: + ex.toString());
145: return;
146: }
147:
148: // setParamForNextPage( key, data );
149: setNextPageData(data);
150: }
151:
152: // FIXME make return type a "Map"
153: protected HashMap<String, Document> performRequest(String request)
154: throws SOSClientException {
155:
156: // FIXME or I'm not sure whether we should return a hash map here. Doc will do too
157: HashMap<String, Document> map = new HashMap<String, Document>();
158:
159: SOSClientConfiguration conf = SOSClientConfiguration
160: .getInstance();
161: String[] sosResources = conf.getSOSNames();
162: // perform a request for each registered sensor observation service
163: for (int i = 0; i < sosResources.length; i++) {
164: URL url = conf.getSOSAddress(sosResources[i]);
165:
166: try {
167:
168: HttpClient httpclient = new HttpClient();
169: httpclient = WebUtils.enableProxyUsage(httpclient, url);
170: httpclient.getHttpConnectionManager().getParams()
171: .setSoTimeout(30000);
172:
173: PostMethod httppost = new PostMethod(url.toString());
174: StringRequestEntity sre = new StringRequestEntity(
175: request);
176: httppost.setRequestEntity(sre);
177: httpclient.executeMethod(httppost);
178:
179: java.io.InputStream is = httppost
180: .getResponseBodyAsStream();
181: InputStreamReader inputStreamRdr = new InputStreamReader(
182: is);
183: Document doc = XMLTools.parse(inputStreamRdr);
184:
185: map.put(sosResources[i], doc);
186:
187: } catch (Exception e) {
188: throw new SOSClientException(
189: "Couldn't connect to Observation Service at "
190: + url.toString(), e);
191: }
192: }
193:
194: return map;
195: }
196:
197: }
|