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