001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/portal/standard/wms/control/GetWMSLayerListener.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.wms.control;
037
038 import java.io.IOException;
039 import java.net.MalformedURLException;
040 import java.net.URL;
041 import java.util.List;
042
043 import org.deegree.datatypes.QualifiedName;
044 import org.deegree.datatypes.values.TypedLiteral;
045 import org.deegree.enterprise.control.AbstractListener;
046 import org.deegree.enterprise.control.FormEvent;
047 import org.deegree.enterprise.control.RPCMember;
048 import org.deegree.enterprise.control.RPCMethodCall;
049 import org.deegree.enterprise.control.RPCParameter;
050 import org.deegree.enterprise.control.RPCStruct;
051 import org.deegree.enterprise.control.RPCUtils;
052 import org.deegree.enterprise.control.RPCWebEvent;
053 import org.deegree.framework.log.ILogger;
054 import org.deegree.framework.log.LoggerFactory;
055 import org.deegree.framework.xml.XMLParsingException;
056 import org.deegree.i18n.Messages;
057 import org.deegree.ogcwebservices.OWSUtils;
058 import org.deegree.ogcwebservices.getcapabilities.InvalidCapabilitiesException;
059 import org.deegree.ogcwebservices.wms.capabilities.WMSCapabilities;
060 import org.deegree.ogcwebservices.wms.capabilities.WMSCapabilitiesDocument;
061 import org.deegree.ogcwebservices.wms.capabilities.WMSCapabilitiesDocumentFactory;
062 import org.deegree.owscommon_new.DomainType;
063 import org.deegree.owscommon_new.Operation;
064 import org.deegree.owscommon_new.OperationsMetadata;
065 import org.deegree.portal.PortalException;
066 import org.deegree.portal.standard.wms.util.ClientHelper;
067 import org.xml.sax.SAXException;
068
069 /**
070 * Lister class for accessing the layers of WMS and return it to the client.
071 *
072 * @version $Revision: 24802 $
073 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
074 * @author last edited by: $Author: apoth $
075 *
076 * @version 1.0. $Revision: 24802 $, $Date: 2010-06-09 14:54:22 +0200 (Mi, 09. Jun 2010) $
077 *
078 * @since 2.0
079 */
080 public class GetWMSLayerListener extends AbstractListener {
081
082 private static ILogger LOG = LoggerFactory.getLogger( GetWMSLayerListener.class );
083
084 /**
085 * @see org.deegree.enterprise.control.WebListener#actionPerformed(org.deegree.enterprise.control.FormEvent)
086 */
087 @Override
088 public void actionPerformed( FormEvent event ) {
089
090 RPCWebEvent rpc = (RPCWebEvent) event;
091 try {
092 validate( rpc );
093 } catch ( Exception e ) {
094 gotoErrorPage( "Not a valid RPC for GetWMSLayer\n" + e.getMessage() );
095 }
096
097 WMSCapabilities capabilities = null;
098 URL url = null;
099 try {
100 url = getURL( rpc );
101 capabilities = getWMSCapabilities( url );
102 } catch ( MalformedURLException ue ) {
103 LOG.logError( ue.getMessage(), ue );
104 gotoErrorPage( Messages.getMessage( "IGEO_STD_INVALID_URL", url ) );
105 return;
106 } catch ( PortalException e ) {
107 LOG.logError( e.getMessage(), e );
108 gotoErrorPage( Messages.getMessage( "IGEO_STD_ADDWMS_INVALID_VERSION" ) );
109 return;
110 } catch ( IOException e ) {
111 LOG.logError( e.getMessage(), e );
112 gotoErrorPage( Messages.getMessage( "IGEO_STD_ADDWMS_NO_TARGET", url ) );
113 return;
114 } catch ( SAXException e ) {
115 LOG.logError( e.getMessage(), e );
116 gotoErrorPage( Messages.getMessage( "IGEO_STD_INVALID_XML", url ) );
117 return;
118 } catch ( InvalidCapabilitiesException e ) {
119 LOG.logError( e.getMessage(), e );
120 gotoErrorPage( Messages.getMessage( "IGEO_STD_ADDWMS_INVALID_CAPS", url ) );
121 return;
122 } catch ( XMLParsingException e ) {
123 LOG.logError( e.getMessage(), e );
124 gotoErrorPage( Messages.getMessage( "IGEO_STD_INVALID_XML", url ) );
125 return;
126 }
127
128 String s = ClientHelper.getLayersAsTree( capabilities.getLayer() );
129 getRequest().setAttribute( "WMSLAYER", s );
130 RPCMethodCall mc = rpc.getRPCMethodCall();
131 RPCParameter param = mc.getParameters()[0];
132 RPCStruct struct = (RPCStruct) param.getValue();
133 String address = RPCUtils.getRpcPropertyAsString( struct, "WMSURL" );
134 getRequest().setAttribute( "WMSURL", address );
135 getRequest().setAttribute( "WMSVERSION", capabilities.getVersion() );
136 s = capabilities.getServiceIdentification().getTitle();
137 s = s.replaceAll( "'", "" );
138 getRequest().setAttribute( "WMSNAME", s );
139
140 OperationsMetadata om = capabilities.getOperationMetadata();
141 Operation op = om.getOperation( new QualifiedName( "GetMap" ) );
142 if ( op == null )
143 om.getOperation( new QualifiedName( "map" ) );
144 DomainType parameter = (DomainType) op.getParameter( new QualifiedName( "Format" ) );
145
146 // the request needs a String[], not a String
147 List<TypedLiteral> values = parameter.getValues();
148 String[] valueArray = new String[values.size()];
149 for ( int i = 0; i < values.size(); ++i )
150 valueArray[i] = values.get( i ).getValue();
151 getRequest().setAttribute( "FORMATS", valueArray );
152 }
153
154 /**
155 * @param rpc
156 * @throws PortalException
157 */
158 private void validate( RPCWebEvent rpc )
159 throws PortalException {
160 RPCMethodCall mc = rpc.getRPCMethodCall();
161 RPCParameter param = mc.getParameters()[0];
162 RPCStruct struct = (RPCStruct) param.getValue();
163 RPCMember address = struct.getMember( "WMSURL" );
164 if ( address == null ) {
165 throw new PortalException( "missing parameter WMSURL in RPC for GetWMSLayer" );
166 }
167 RPCMember version = struct.getMember( "VERSION" );
168 if ( version != null
169 && ( ( (String) version.getValue() ).compareTo( "1.3.0" ) > 0 || ( (String) version.getValue() ).compareTo( "1.1.0" ) < 0 ) ) {
170 throw new PortalException( "VERSION must be >= 1.0.0 and <= 1.3.0 but it is " + version.getValue() );
171 }
172 }
173
174 /**
175 *
176 * @param rpc
177 * @return wms url
178 * @throws MalformedURLException
179 */
180 private URL getURL( RPCWebEvent rpc )
181 throws MalformedURLException {
182 RPCMethodCall mc = rpc.getRPCMethodCall();
183 RPCParameter param = mc.getParameters()[0];
184 RPCStruct struct = (RPCStruct) param.getValue();
185 String address = RPCUtils.getRpcPropertyAsString( struct, "WMSURL" );
186
187 // according to OGC WMS 1.3 Testsuite a URL to a service operation
188 // via HTTPGet must end with '?' or '&'
189 String href = OWSUtils.validateHTTPGetBaseURL( address );
190
191 StringBuffer sb = new StringBuffer( href );
192 String version = RPCUtils.getRpcPropertyAsString( struct, "VERSION" );
193 if ( "1.0.0".equals( version ) ) {
194 sb.append( "service=WMS&request=capabilities" );
195 } else {
196 sb.append( "service=WMS&request=GetCapabilities" );
197 }
198 if ( version != null ) {
199 if ( "1.0.0".equals( version ) ) {
200 sb.append( "&WMTVER=" ).append( version );
201 } else {
202 sb.append( "&version=" ).append( version );
203 }
204 }
205
206 String sessionid = RPCUtils.getRpcPropertyAsString( struct, "SESSIONID" );
207 if ( sessionid != null ) {
208 sb.append( "&sessionID=" ).append( sessionid );
209 }
210
211 String useAuthentification = RPCUtils.getRpcPropertyAsString( struct, "useAuthentification" );
212 getRequest().setAttribute( "USEAUTHENTICATION", "true".equals( useAuthentification ) );
213 if ( "true".equals( useAuthentification ) ) {
214 String user = RPCUtils.getRpcPropertyAsString( struct, "USER" );
215 if ( user != null ) {
216 String password = RPCUtils.getRpcPropertyAsString( struct, "USER" );
217 sb.append( "&user=" ).append( user );
218 if ( password != null ) {
219 sb.append( "&password=" ).append( password );
220 }
221 }
222 }
223 LOG.logDebug( "get capabilities URL: ", sb );
224 return new URL( sb.toString() );
225 }
226
227 /**
228 *
229 * @param url
230 * @return the capabilities object
231 * @throws XMLParsingException
232 * @throws PortalException
233 * @throws IOException
234 * @throws SAXException
235 * @throws InvalidCapabilitiesException
236 * @throws XMLParsingException
237 */
238 private WMSCapabilities getWMSCapabilities( URL url )
239 throws PortalException, IOException, SAXException, InvalidCapabilitiesException,
240 XMLParsingException {
241
242 WMSCapabilitiesDocument doc = WMSCapabilitiesDocumentFactory.getWMSCapabilitiesDocument( url );
243 WMSCapabilities capabilities = (WMSCapabilities) doc.parseCapabilities();
244
245 if ( capabilities.getVersion().compareTo( "1.0.0" ) < 0 || capabilities.getVersion().compareTo( "1.1.1" ) > 0 ) {
246 throw new PortalException( "VERSION must be >= 1.0.0 && <= 1.1.1" );
247 }
248
249 return capabilities;
250 }
251 }