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