001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/portal/standard/security/control/ReallyUpdateServiceListener.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.security.control; 037 038 import static org.deegree.framework.log.LoggerFactory.getLogger; 039 import static org.deegree.framework.util.StringTools.stackTraceToString; 040 import static org.deegree.i18n.Messages.get; 041 import static org.deegree.ogcwebservices.wms.capabilities.WMSCapabilitiesDocumentFactory.getWMSCapabilitiesDocument; 042 import static org.deegree.portal.standard.security.control.ClientHelper.TYPE_FEATURETYPE; 043 import static org.deegree.portal.standard.security.control.ClientHelper.TYPE_LAYER; 044 import static org.deegree.portal.standard.security.control.SecurityHelper.acquireTransaction; 045 import static org.deegree.portal.standard.security.control.SecurityHelper.checkForAdminRole; 046 import static org.deegree.security.drm.SecurityAccessManager.getInstance; 047 048 import java.io.IOException; 049 import java.net.URL; 050 import java.util.HashMap; 051 import java.util.LinkedList; 052 import java.util.List; 053 import java.util.Map; 054 055 import javax.servlet.ServletRequest; 056 057 import org.deegree.enterprise.control.AbstractListener; 058 import org.deegree.enterprise.control.FormEvent; 059 import org.deegree.enterprise.control.RPCParameter; 060 import org.deegree.enterprise.control.RPCWebEvent; 061 import org.deegree.framework.log.ILogger; 062 import org.deegree.framework.util.StringPair; 063 import org.deegree.framework.xml.XMLParsingException; 064 import org.deegree.i18n.Messages; 065 import org.deegree.ogcwebservices.getcapabilities.InvalidCapabilitiesException; 066 import org.deegree.ogcwebservices.getcapabilities.OGCCapabilities; 067 import org.deegree.ogcwebservices.wfs.capabilities.WFSCapabilities; 068 import org.deegree.ogcwebservices.wfs.capabilities.WFSCapabilitiesDocument_1_1_0; 069 import org.deegree.ogcwebservices.wfs.capabilities.WFSFeatureType; 070 import org.deegree.ogcwebservices.wms.capabilities.Layer; 071 import org.deegree.ogcwebservices.wms.capabilities.WMSCapabilities; 072 import org.deegree.security.GeneralSecurityException; 073 import org.deegree.security.drm.SecurityAccessManager; 074 import org.deegree.security.drm.SecurityTransaction; 075 import org.deegree.security.drm.model.SecuredObject; 076 import org.deegree.security.drm.model.Service; 077 import org.xml.sax.SAXException; 078 079 /** 080 * <code>ReallyUpdateServiceListener</code> 081 * 082 * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a> 083 * @author last edited by: $Author: mschneider $ 084 * 085 * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $ 086 */ 087 public class ReallyUpdateServiceListener extends AbstractListener { 088 089 private static final ILogger LOG = getLogger( ReallyUpdateServiceListener.class ); 090 091 private static void addAllLayers( String prefix, Layer layer, List<StringPair> objects ) 092 throws GeneralSecurityException { 093 if ( layer.getName() != null ) { 094 objects.add( new StringPair( layer.getName(), layer.getTitle() ) ); 095 } 096 for ( Layer l : layer.getLayer() ) { 097 addAllLayers( prefix, l, objects ); 098 } 099 } 100 101 private static void updateObjects( SecurityTransaction transaction, Service oldService, Service newService, 102 boolean isWMS ) 103 throws GeneralSecurityException { 104 Map<String, String> oldObjects = new HashMap<String, String>(); 105 for ( StringPair pair : oldService.getObjects() ) { 106 oldObjects.put( pair.first, pair.second ); 107 } 108 Map<String, String> newObjects = new HashMap<String, String>(); 109 for ( StringPair pair : newService.getObjects() ) { 110 newObjects.put( pair.first, pair.second ); 111 } 112 113 String prefix = "[" + newService.getAddress() + "]:"; 114 for ( StringPair pair : newService.getObjects() ) { 115 if ( !oldObjects.containsKey( pair.first ) ) { 116 transaction.registerSecuredObject( isWMS ? TYPE_LAYER : TYPE_FEATURETYPE, prefix + pair.first, 117 pair.second ); 118 } 119 } 120 121 for ( StringPair pair : oldService.getObjects() ) { 122 if ( !newObjects.containsKey( pair.first ) ) { 123 SecuredObject obj = transaction.getSecuredObjectByName( prefix + pair.first, isWMS ? TYPE_LAYER 124 : TYPE_FEATURETYPE ); 125 transaction.deregisterSecuredObject( obj ); 126 } // title of secured object is updated in SQLRegistry through below updateService call 127 } 128 129 transaction.updateService( oldService, newService ); 130 } 131 132 @Override 133 public void actionPerformed( FormEvent event ) { 134 RPCParameter[] params = ( (RPCWebEvent) event ).getRPCMethodCall().getParameters(); 135 136 ServletRequest request = getRequest(); 137 138 SecurityTransaction transaction = null; 139 SecurityAccessManager manager = null; 140 141 try { 142 // perform access check 143 manager = getInstance(); 144 transaction = acquireTransaction( this ); 145 checkForAdminRole( transaction ); 146 147 boolean isWMS = params[1].getValue().toString().equalsIgnoreCase( "wms" ); 148 boolean isWFS = params[1].getValue().toString().equalsIgnoreCase( "wfs" ); 149 if ( !isWMS && !isWFS ) { 150 // error message? client is faulty 151 LOG.logError( "Unknown/unsupported service type to register: " + params[1].getValue() ); 152 } 153 154 String address = params[0].getValue().toString(); 155 String getCapa = address + ( ( !address.endsWith( "&" ) && !address.endsWith( "?" ) ) ? "?" : "" ) 156 + "request=GetCapabilities&"; 157 158 Service oldService = transaction.getServiceByAddress( address ); 159 Service newService = null; 160 161 if ( isWMS ) { 162 OGCCapabilities capa = getWMSCapabilitiesDocument( new URL( getCapa + "service=WMS&version=1.1.1" ) ).parseCapabilities(); 163 if ( capa instanceof WMSCapabilities ) { 164 WMSCapabilities cap = (WMSCapabilities) capa; 165 LinkedList<StringPair> objects = new LinkedList<StringPair>(); 166 167 addAllLayers( "[" + address + "]:", cap.getLayer(), objects ); 168 169 newService = new Service( -1, address, cap.getServiceIdentification().getTitle(), objects, "WMS" ); 170 } 171 } 172 173 if ( isWFS ) { 174 WFSCapabilitiesDocument_1_1_0 doc = new WFSCapabilitiesDocument_1_1_0(); 175 doc.load( new URL( getCapa + "service=WFS&version=1.1.0" ) ); 176 WFSCapabilities capa = (WFSCapabilities) doc.parseCapabilities(); 177 LinkedList<StringPair> objects = new LinkedList<StringPair>(); 178 for ( WFSFeatureType ft : capa.getFeatureTypeList().getFeatureTypes() ) { 179 objects.add( new StringPair( ft.getName().getFormattedString(), ft.getTitle() ) ); 180 } 181 182 newService = new Service( -1, address, capa.getServiceIdentification().getTitle(), objects, "WFS" ); 183 } 184 185 updateObjects( transaction, oldService, newService, isWMS ); 186 187 request.setAttribute( "MESSAGE", Messages.getMessage( "IGEO_STD_SEC_SUCCESS_INITSERVICESEDITOR" ) ); 188 189 manager.commitTransaction( transaction ); 190 } catch ( GeneralSecurityException e ) { 191 getRequest().setAttribute( "SOURCE", this.getClass().getName() ); 192 getRequest().setAttribute( "MESSAGE", get( "IGEO_STD_SEC_FAIL_INIT_SERVICES_EDITOR", e.getMessage() ) ); 193 setNextPage( "error.jsp" ); 194 LOG.logError( e.getMessage(), e ); 195 try { 196 if ( manager != null ) { 197 manager.abortTransaction( transaction ); 198 } 199 } catch ( GeneralSecurityException e1 ) { 200 LOG.logError( "Unknown error", e1 ); 201 } 202 } catch ( SAXException e ) { 203 getRequest().setAttribute( "SOURCE", this.getClass().getName() ); 204 getRequest().setAttribute( "MESSAGE", get( "IGEO_STD_SEC_ERROR_EDIT_SERVICE", e.getMessage() ) ); 205 setNextPage( "error.jsp" ); 206 try { 207 if ( manager != null ) { 208 manager.abortTransaction( transaction ); 209 } 210 } catch ( GeneralSecurityException e1 ) { 211 LOG.logError( "Unknown error", e1 ); 212 } 213 } catch ( IOException e ) { 214 getRequest().setAttribute( "SOURCE", this.getClass().getName() ); 215 getRequest().setAttribute( "MESSAGE", get( "IGEO_STD_SEC_ERROR_EDIT_SERVICE", e.getMessage() ) ); 216 setNextPage( "error.jsp" ); 217 try { 218 if ( manager != null ) { 219 manager.abortTransaction( transaction ); 220 } 221 } catch ( GeneralSecurityException e1 ) { 222 LOG.logError( "Unknown error", e1 ); 223 } 224 } catch ( XMLParsingException e ) { 225 getRequest().setAttribute( "SOURCE", this.getClass().getName() ); 226 getRequest().setAttribute( "MESSAGE", get( "IGEO_STD_SEC_ERROR_EDIT_SERVICE", e.getMessage() ) ); 227 setNextPage( "error.jsp" ); 228 try { 229 if ( manager != null ) { 230 manager.abortTransaction( transaction ); 231 } 232 } catch ( GeneralSecurityException e1 ) { 233 LOG.logError( "Unknown error", e1 ); 234 } 235 } catch ( InvalidCapabilitiesException e ) { 236 getRequest().setAttribute( "SOURCE", this.getClass().getName() ); 237 getRequest().setAttribute( "MESSAGE", get( "IGEO_STD_SEC_ERROR_EDIT_SERVICE", e.getMessage() ) ); 238 setNextPage( "error.jsp" ); 239 try { 240 if ( manager != null ) { 241 manager.abortTransaction( transaction ); 242 } 243 } catch ( GeneralSecurityException e1 ) { 244 LOG.logError( "Unknown error", e1 ); 245 } 246 } catch ( Exception e ) { 247 LOG.logError( get( "IGEO_STD_SEC_ERROR_UNKNOWN", stackTraceToString( e ) ) ); 248 try { 249 if ( manager != null ) { 250 manager.abortTransaction( transaction ); 251 } 252 } catch ( GeneralSecurityException e1 ) { 253 LOG.logError( "Unknown error", e1 ); 254 getRequest().setAttribute( "SOURCE", this.getClass().getName() ); 255 getRequest().setAttribute( "MESSAGE", get( "IGEO_STD_SEC_ERROR_EDIT_SERVICE", e1.getMessage() ) ); 256 setNextPage( "error.jsp" ); 257 return; 258 259 } 260 getRequest().setAttribute( "SOURCE", this.getClass().getName() ); 261 getRequest().setAttribute( "MESSAGE", get( "IGEO_STD_SEC_ERROR_EDIT_SERVICE", e.getMessage() ) ); 262 setNextPage( "error.jsp" ); 263 } 264 } 265 266 }