    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/portal/standard/security/control/UpdateServiceListener.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.acquireAccess;
    043    import static org.deegree.portal.standard.security.control.SecurityHelper.checkForAdminRole;
    044
    045    import java.io.IOException;
    046    import java.net.URL;
    047    import java.util.LinkedList;
    048    import java.util.List;
    049
    050    import javax.servlet.ServletRequest;
    051
    052    import org.deegree.enterprise.control.AbstractListener;
    053    import org.deegree.enterprise.control.FormEvent;
    054    import org.deegree.enterprise.control.RPCParameter;
    055    import org.deegree.enterprise.control.RPCWebEvent;
    056    import org.deegree.framework.log.ILogger;
    057    import org.deegree.framework.util.StringPair;
    058    import org.deegree.framework.xml.XMLParsingException;
    059    import org.deegree.ogcwebservices.getcapabilities.InvalidCapabilitiesException;
    060    import org.deegree.ogcwebservices.getcapabilities.OGCCapabilities;
    061    import org.deegree.ogcwebservices.wfs.capabilities.WFSCapabilities;
    062    import org.deegree.ogcwebservices.wfs.capabilities.WFSCapabilitiesDocument_1_1_0;
    063    import org.deegree.ogcwebservices.wfs.capabilities.WFSFeatureType;
    064    import org.deegree.ogcwebservices.wms.capabilities.Layer;
    065    import org.deegree.ogcwebservices.wms.capabilities.WMSCapabilities;
    066    import org.deegree.security.GeneralSecurityException;
    067    import org.deegree.security.drm.SecurityAccess;
    068    import org.deegree.security.drm.model.Service;
    069    import org.xml.sax.SAXException;
    070
    071    /**
    072     * <code>UpdateServiceListener</code>
    073     *
    074     * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a>
    075     * @author last edited by: $Author: mschneider $
    076     *
    077     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
    078     */
    079    public class UpdateServiceListener extends AbstractListener {
    080
    081        private static final ILogger LOG = getLogger( UpdateServiceListener.class );
    082
    083        private static void addAllLayers( String prefix, Layer layer, List<StringPair> objects )
    084                                throws GeneralSecurityException {
    085            if ( layer.getName() != null ) {
    086                objects.add( new StringPair( layer.getName(), layer.getTitle() ) );
    087            }
    088            for ( Layer l : layer.getLayer() ) {
    089                addAllLayers( prefix, l, objects );
    090            }
    091        }
    092
    093        @Override
    094        public void actionPerformed( FormEvent event ) {
    095            RPCParameter[] params = ( (RPCWebEvent) event ).getRPCMethodCall().getParameters();
    096
    097            ServletRequest request = getRequest();
    098
    099            SecurityAccess access = null;
    100
    101            try {
    102                // perform access check
    103                access = acquireAccess( this );
    104                checkForAdminRole( access );
    105
    106                boolean isWMS = params[1].getValue().toString().equalsIgnoreCase( "wms" );
    107                boolean isWFS = params[1].getValue().toString().equalsIgnoreCase( "wfs" );
    108                if ( !isWMS && !isWFS ) {
    109                    // error message? client is faulty
    110                    LOG.logError( "Unknown/unsupported service type to register: " + params[1].getValue() );
    111                }
    112
    113                String address = params[0].getValue().toString();
    114                String getCapa = address + ( ( !address.endsWith( "&" ) && !address.endsWith( "?" ) ) ? "?" : "" )
    115                                 + "request=GetCapabilities&";
    116
    117                Service newService = null;
    118
    119                if ( isWMS ) {
    120                    OGCCapabilities capa = getWMSCapabilitiesDocument( new URL( getCapa + "service=WMS&version=1.1.1" ) ).parseCapabilities();
    121                    if ( capa instanceof WMSCapabilities ) {
    122                        WMSCapabilities cap = (WMSCapabilities) capa;
    123                        LinkedList<StringPair> objects = new LinkedList<StringPair>();
    124
    125                        addAllLayers( "[" + address + "]:", cap.getLayer(), objects );
    126
    127                        newService = new Service( -1, address, cap.getServiceIdentification().getTitle(), objects, "WMS" );
    128                    }
    129                }
    130
    131                if ( isWFS ) {
    132                    WFSCapabilitiesDocument_1_1_0 doc = new WFSCapabilitiesDocument_1_1_0();
    133                    doc.load( new URL( getCapa + "service=WFS&version=1.1.0" ) );
    134                    WFSCapabilities capa = (WFSCapabilities) doc.parseCapabilities();
    135                    LinkedList<StringPair> objects = new LinkedList<StringPair>();
    136                    for ( WFSFeatureType ft : capa.getFeatureTypeList().getFeatureTypes() ) {
    137                        objects.add( new StringPair( ft.getName().getFormattedString(), ft.getTitle() ) );
    138                    }
    139
    140                    newService = new Service( -1, address, capa.getServiceIdentification().getTitle(), objects, "WFS" );
    141                }
    142
    143                Service oldService = access.getServiceByAddress( address );
    144                request.setAttribute( "OLDSERVICE", oldService );
    145                request.setAttribute( "NEWSERVICE", newService );
    146
    147            } catch ( GeneralSecurityException e ) {
    148                getRequest().setAttribute( "SOURCE", this.getClass().getName() );
    149                getRequest().setAttribute( "MESSAGE", get( "IGEO_STD_SEC_FAIL_INIT_SERVICES_EDITOR", e.getMessage() ) );
    150                setNextPage( "error.jsp" );
    151                LOG.logError( e.getMessage(), e );
    152            } catch ( SAXException e ) {
    153                getRequest().setAttribute( "SOURCE", this.getClass().getName() );
    154                getRequest().setAttribute( "MESSAGE", get( "IGEO_STD_SEC_ERROR_UPDATE_SERVICE", e.getMessage() ) );
    155                setNextPage( "error.jsp" );
    156            } catch ( IOException e ) {
    157                getRequest().setAttribute( "SOURCE", this.getClass().getName() );
    158                getRequest().setAttribute( "MESSAGE", get( "IGEO_STD_SEC_ERROR_UPDATE_SERVICE", e.getMessage() ) );
    159                setNextPage( "error.jsp" );
    160            } catch ( XMLParsingException e ) {
    161                getRequest().setAttribute( "SOURCE", this.getClass().getName() );
    162                getRequest().setAttribute( "MESSAGE", get( "IGEO_STD_SEC_ERROR_UPDATE_SERVICE", e.getMessage() ) );
    163                setNextPage( "error.jsp" );
    164            } catch ( InvalidCapabilitiesException e ) {
    165                getRequest().setAttribute( "SOURCE", this.getClass().getName() );
    166                getRequest().setAttribute( "MESSAGE", get( "IGEO_STD_SEC_ERROR_UPDATE_SERVICE", e.getMessage() ) );
    167                setNextPage( "error.jsp" );
    168            } catch ( Exception e ) {
    169                LOG.logError( get( "IGEO_STD_SEC_ERROR_UNKNOWN", stackTraceToString( e ) ) );
    170                getRequest().setAttribute( "SOURCE", this.getClass().getName() );
    171                getRequest().setAttribute( "MESSAGE", get( "IGEO_STD_SEC_ERROR_UPDATE_SERVICE", e.getMessage() ) );
    172                setNextPage( "error.jsp" );
    173            }
    174        }
    175
    176    }