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