001 //$HeadURL: svn+ssh://developername@svn.wald.intevation.org/deegree/base/trunk/src/org/deegree/portal/portlet/enterprise/AddWMSListener.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.net.MalformedURLException;
039 import java.net.URL;
040 import java.util.Enumeration;
041 import java.util.List;
042
043 import javax.servlet.http.HttpServletRequest;
044 import javax.servlet.http.HttpSession;
045
046 import org.deegree.enterprise.control.AbstractListener;
047 import org.deegree.enterprise.control.FormEvent;
048 import org.deegree.enterprise.control.RPCStruct;
049 import org.deegree.enterprise.control.RPCWebEvent;
050 import org.deegree.framework.log.ILogger;
051 import org.deegree.framework.log.LoggerFactory;
052 import org.deegree.framework.util.StringTools;
053 import org.deegree.framework.xml.XMLFragment;
054 import org.deegree.ogcbase.BaseURL;
055 import org.deegree.ogcbase.ImageURL;
056 import org.deegree.ogcwebservices.getcapabilities.InvalidCapabilitiesException;
057 import org.deegree.ogcwebservices.getcapabilities.MetadataURL;
058 import org.deegree.ogcwebservices.wms.capabilities.LegendURL;
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.portal.PortalException;
063 import org.deegree.portal.context.ContextException;
064 import org.deegree.portal.context.Format;
065 import org.deegree.portal.context.FormatList;
066 import org.deegree.portal.context.Layer;
067 import org.deegree.portal.context.LayerExtension;
068 import org.deegree.portal.context.Node;
069 import org.deegree.portal.context.Server;
070 import org.deegree.portal.context.Style;
071 import org.deegree.portal.context.StyleList;
072 import org.deegree.portal.context.ViewContext;
073 import org.deegree.portal.context.XMLFactory;
074 import org.deegree.portal.standard.wms.control.GetWMSLayerListener;
075
076 /**
077 *
078 *
079 *
080 * @version $Revision: 8284 $
081 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
082 * @author last edited by: $Author: apoth $
083 *
084 * @version 1.0. $Revision: 8284 $, $Date: 2007-10-11 11:02:19 +0200 (Do, 11 Okt 2007) $
085 *
086 * @since 2.0
087 */
088 public class AddWMSListener extends AbstractListener {
089
090 private static ILogger LOG = LoggerFactory.getLogger( GetWMSLayerListener.class );
091
092 /**
093 * @see org.deegree.enterprise.control.WebListener#actionPerformed(org.deegree.enterprise.control.FormEvent)
094 */
095 @SuppressWarnings("unchecked")
096 @Override
097 public synchronized void actionPerformed( FormEvent event ) {
098
099 RPCWebEvent rpc = (RPCWebEvent) event;
100 try {
101 validate( rpc );
102 } catch ( Exception e ) {
103 gotoErrorPage( "Not a valid RPC for AddWMSListener\n" + e.getMessage() );
104 }
105
106 HttpSession session = ( (HttpServletRequest) this.getRequest() ).getSession();
107 Enumeration<String> en = (Enumeration<String>)session.getAttributeNames();
108 try {
109 while ( en.hasMoreElements() ) {
110 String key = en.nextElement();
111 Object o = session.getAttribute( key );
112 synchronized ( session ) {
113 if ( o != null && o instanceof ViewContext ) {
114 appendWMS( rpc, (ViewContext) o );
115 }
116 }
117 }
118 } catch ( Exception e ) {
119 LOG.logError( e.getMessage(), e );
120 e.printStackTrace();
121 }
122 }
123
124 /**
125 * appends the selected layers of a WMS to the passed <code>ViewContext</code>
126 *
127 * @param context
128 * @throws ContextException
129 * @throws MalformedURLException
130 * @throws PortalException
131 * @throws InvalidCapabilitiesException
132 */
133 private void appendWMS( RPCWebEvent rpc, ViewContext context )
134 throws MalformedURLException, ContextException, PortalException,
135 InvalidCapabilitiesException {
136
137 RPCStruct struct = (RPCStruct) rpc.getRPCMethodCall().getParameters()[0].getValue();
138 URL url = new URL( (String) struct.getMember( "WMSURL" ).getValue() );
139 String name = (String) struct.getMember( "WMSNAME" ).getValue();
140 String version = (String) struct.getMember( "WMSVERSION" ).getValue();
141 String layers = (String) struct.getMember( "LAYERS" ).getValue();
142 String formatName = (String) struct.getMember( "FORMAT" ).getValue();
143 boolean useAuthentification = false;
144 if ( struct.getMember( "useAuthentification" ) != null ) {
145 String tmp = (String) struct.getMember( "useAuthentification" ).getValue();
146 useAuthentification = "true".equalsIgnoreCase( tmp );
147 }
148
149 List<String> list = StringTools.toList( layers, ";", true );
150
151 WMSCapabilitiesDocument capa = null;
152 try {
153 StringBuffer sb = new StringBuffer( 500 );
154 if ( "1.0.0".equals( version ) ) {
155 sb.append( url.toExternalForm() ).append( "?request=capabilities&service=WMS" );
156 } else {
157 sb.append( url.toExternalForm() ).append( "?request=GetCapabilities&service=WMS" );
158 }
159 if ( useAuthentification ) {
160 HttpSession session = ( (HttpServletRequest) getRequest() ).getSession();
161 String user = ( (org.apache.jetspeed.om.security.BaseJetspeedUser) session.getAttribute( "turbine.user" ) ).getUserName();
162 String password = ( (org.apache.jetspeed.om.security.BaseJetspeedUser) session.getAttribute( "turbine.user" ) ).getPassword();
163 if ( !"anon".equals( user ) ) {
164 sb.append( "&user=" ).append( user );
165 sb.append( "&password=" ).append( password );
166 }
167 }
168 LOG.logDebug( "GetCapabilites for added WMS", sb.toString() );
169 capa = WMSCapabilitiesDocumentFactory.getWMSCapabilitiesDocument( new URL( sb.toString() ) );
170 } catch ( Exception e ) {
171 LOG.logError( e.getMessage(), e );
172 String msg = null;
173 if ( "1.0.0".equals( version ) ) {
174 msg = StringTools.concat( 500, "could not load WMS capabilities from: ",
175 new URL( url.toExternalForm() + "?request=capabilities&service=WMS" ),
176 "; reason: ", e.getMessage() );
177 } else {
178 msg = StringTools.concat( 500, "could not load WMS capabilities from: ",
179 new URL( url.toExternalForm() + "?request=GetCapabilities&service=WMS" ),
180 "; reason: ", e.getMessage() );
181 }
182 throw new PortalException( msg );
183 }
184 WMSCapabilities capabilities = (WMSCapabilities) capa.parseCapabilities();
185 String rootTitle = capabilities.getLayer().getTitle();
186
187 // ----------------------------------------------------------------------------
188 // stuff required by layerlist tree view
189 Node root = context.getGeneral().getExtension().getLayerTreeRoot();
190 // check if Node width this title already exists
191 Node[] nodes = root.getNodes();
192 int newNodeId = -1;
193 for ( int j = 0; j < nodes.length; j++ ) {
194 if ( nodes[j].getTitle().equals( rootTitle ) ) {
195 newNodeId = nodes[j].getId();
196 break;
197 }
198 }
199 if ( newNodeId == -1 ) {
200 newNodeId = root.getMaxNodeId() + 1;
201 Node newNode = new Node( newNodeId, root, rootTitle, true, false );
202 Node[] newNodes = new Node[nodes.length + 1];
203 newNodes[0] = newNode;
204 for ( int j = 0; j < nodes.length; j++ ) {
205 newNodes[j + 1] = nodes[j];
206 }
207
208 root.setNodes( newNodes );
209 }
210 // ----------------------------------------------------------------------------
211 for ( int i = 0; i < list.size(); i++ ) {
212 String[] lay = StringTools.toArray( list.get( i ), "|", false );
213 Server server = new Server( name, version, "OGC:WMS", url, capabilities );
214 String srs = context.getGeneral().getBoundingBox()[0].getCoordinateSystem().getPrefixedName();
215 Format format = new Format( formatName, true );
216 FormatList fl = new FormatList( new Format[] { format } );
217 // read available styles from WMS capabilities and add them
218 // to the WMC layer
219 org.deegree.ogcwebservices.wms.capabilities.Layer wmslay = capabilities.getLayer( lay[0] );
220 org.deegree.ogcwebservices.wms.capabilities.Style[] wmsstyles = wmslay.getStyles();
221 Style[] styles = null;
222 if ( wmsstyles == null || wmsstyles.length == 0 ) {
223 // a wms capabilities layer may offeres one or more styles for
224 // a layer but it don't have to. But WMC must have at least one
225 // style for each layer; So we set a default style in the case
226 // a wms does not declares one
227 styles = new Style[1];
228 styles[0] = new Style( "", "default", "", null, true );
229 } else {
230 styles = new Style[wmsstyles.length];
231 for ( int j = 0; j < styles.length; j++ ) {
232 boolean isDefault = wmsstyles[j].getName().toLowerCase().indexOf( "default" ) > -1
233 || wmsstyles[j].getName().trim().length() == 0;
234 ImageURL legendURL = null;
235 LegendURL[] lUrl = wmsstyles[j].getLegendURL();
236 if ( lUrl != null && lUrl.length > 0 ) {
237 legendURL = new ImageURL( lUrl[0].getWidth(), lUrl[0].getHeight(), lUrl[0].getFormat(),
238 lUrl[0].getOnlineResource() );
239 }
240 styles[j] = new Style( wmsstyles[j].getName(), wmsstyles[j].getTitle(), wmsstyles[j].getAbstract(),
241 legendURL, isDefault );
242 }
243 }
244
245 StyleList styleList = new StyleList( styles );
246 BaseURL mdUrl = null;
247 MetadataURL[] mdUrls = wmslay.getMetadataURL();
248 if ( mdUrls != null && mdUrls.length == 1 && mdUrls[0] != null ) {
249 mdUrl = mdUrls[0];
250 }
251
252 int authentication = LayerExtension.NONE;
253 if ( useAuthentification ) {
254 authentication = LayerExtension.USERPASSWORD;
255 }
256 LayerExtension lex = new LayerExtension( null, false, wmslay.getScaleHint().getMin(),
257 wmslay.getScaleHint().getMax(), false, authentication, newNodeId,
258 false, null );
259 Layer newLay = new Layer( server, lay[0], lay[1], null, new String[] { srs }, null, mdUrl, fl, styleList,
260 wmslay.isQueryable(), false, lex );
261 if ( context.getLayerList().getLayer( newLay.getName(), server.getOnlineResource().toExternalForm() ) == null ) {
262 context.getLayerList().addLayerToTop( newLay );
263 }
264
265 }
266 try {
267 XMLFragment xml = XMLFactory.export( context );
268 System.out.println( xml.getAsPrettyString() );
269 } catch ( Exception e ) {
270 // TODO Auto-generated catch block
271 e.printStackTrace();
272 }
273
274 }
275
276 /**
277 * validates the incomming RPC
278 *
279 * @param rpc
280 */
281 private void validate( RPCWebEvent rpc ) {
282 // TODO Auto-generated method stub
283
284 }
285
286 }