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