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