    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/security/owsproxy/StringReplacementTrigger.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.security.owsproxy;
    037
    038    import java.util.ArrayList;
    039    import java.util.List;
    040
    041    import org.deegree.framework.log.ILogger;
    042    import org.deegree.framework.log.LoggerFactory;
    043    import org.deegree.framework.trigger.Trigger;
    044    import org.deegree.framework.util.StringTools;
    045    import org.deegree.ogcwebservices.OGCWebServiceRequest;
    046    import org.deegree.ogcwebservices.csw.capabilities.CatalogueGetCapabilities;
    047    import org.deegree.ogcwebservices.sos.capabilities.SOSGetCapabilities;
    048    import org.deegree.ogcwebservices.wcs.getcapabilities.WCSGetCapabilities;
    049    import org.deegree.ogcwebservices.wfs.operation.WFSGetCapabilities;
    050    import org.deegree.ogcwebservices.wmps.operation.WMPSGetCapabilities;
    051    import org.deegree.ogcwebservices.wms.operation.WMSGetCapabilities;
    052    import org.deegree.ogcwebservices.wps.capabilities.WPSGetCapabilities;
    053    import org.deegree.ogcwebservices.wpvs.operation.WPVSGetCapabilities;
    054
    055    /**
    056     * Trigger for replacing string within requests and responses to OGC Web Service packed behind a
    057     * owsProxy. At the moment replacements are just supported for responses to GetCapabilities
    058     * requests.
    059     *
    060     *
    061     * @version $Revision: 18195 $
    062     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
    063     * @author last edited by: $Author: mschneider $
    064     *
    065     * @version 1.0. $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
    066     *
    067     * @since 2.0
    068     */
    069    public class StringReplacementTrigger implements Trigger {
    070
    071        private static final ILogger LOG = LoggerFactory.getLogger( StringReplacementTrigger.class );
    072
    073        private String name;
    074
    075        private String consideredRequests;
    076
    077        private List<String> replacementsKeys;
    078
    079        private List<String> replacementsVals;
    080
    081        /**
    082         *
    083         * @param replacements
    084         *            list of replacement rules "{key1|value1}{key2|value2} ... {keyX|valueX}"
    085         * @param consideredRequests
    086         *            comma seperated list of request names to be considerded; e.g.
    087         *            "WMS:GetFeatureInfo,WMS:GetCapabilities,WFS:GetFeature"
    088         */
    089        public StringReplacementTrigger( String replacements, String consideredRequests ) {
    090
    091            this.consideredRequests = consideredRequests;
    092            String[] tmp = StringTools.extractStrings( replacements, "{", "}" );
    093            this.replacementsKeys = new ArrayList<String>( tmp.length );
    094            this.replacementsVals = new ArrayList<String>( tmp.length );
    095            for ( int i = 0; i < tmp.length; i++ ) {
    096                int pos = tmp[i].lastIndexOf( '|' );
    097                this.replacementsKeys.add( tmp[i].substring( 0, pos ) );
    098                this.replacementsVals.add( tmp[i].substring( pos + 1, tmp[i].length() ) );
    099            }
    100        }
    101
    102        /**
    103         *
    104         * @param caller
    105         *            instance of calling class
    106         * @param values
    107         *            values passed from the calling method
    108         */
    109        public Object[] doTrigger( Object caller, Object... values ) {
    110
    111            if ( values.length == 2 ) {
    112                values = handleRequestValidation( values );
    113            } else {
    114                values = handleResponseValidation( values );
    115            }
    116            return values;
    117        }
    118
    119        /**
    120         *
    121         * @param values
    122         * @return an array of objects
    123         */
    124        private Object[] handleResponseValidation( Object[] values ) {
    125
    126            // TODO
    127            // consider personal user rights
    128
    129            OGCWebServiceRequest request = (OGCWebServiceRequest) values[0];
    130            byte[] data = (byte[]) values[1];
    131
    132            if ( ( request instanceof WMSGetCapabilities && consideredRequests.indexOf( "WMS:GetCapabilities" ) > -1 )
    133                 || ( request instanceof WFSGetCapabilities && consideredRequests.indexOf( "WFS:GetCapabilities" ) > -1 )
    134                 || ( request instanceof CatalogueGetCapabilities && consideredRequests.indexOf( "CSW:GetCapabilities" ) > -1 )
    135                 || ( request instanceof WCSGetCapabilities && consideredRequests.indexOf( "WCS:GetCapabilities" ) > -1 )
    136                 || ( request instanceof WMPSGetCapabilities && consideredRequests.indexOf( "WMPS:GetCapabilities" ) > -1 )
    137                 || ( request instanceof WPVSGetCapabilities && consideredRequests.indexOf( "WPVS:GetCapabilities" ) > -1 )
    138                 || ( request instanceof SOSGetCapabilities && consideredRequests.indexOf( "SOS:GetCapabilities" ) > -1 )
    139                 || ( request instanceof WPSGetCapabilities && consideredRequests.indexOf( "WPS:GetCapabilities" ) > -1 ) ) {
    140                String tmp = new String( data );
    141                for ( int i = 0; i < replacementsKeys.size(); i++ ) {
    142                    LOG.logDebug( "response replacement: ", replacementsKeys.get( i ) + " - " + replacementsVals.get( i ) );
    143                    tmp = tmp.replaceAll( replacementsKeys.get( i ), replacementsVals.get( i ) );
    144                }
    145                values[1] = tmp.getBytes();
    146            } else {
    147                // TODO
    148                // support replacements within responses to other requests
    149                LOG.logInfo( "No string replacement implemented for request: ", request.getClass().getName() );
    150            }
    151
    152            return values;
    153        }
    154
    155        /**
    156         *
    157         * @param values
    158         * @return the values.
    159         */
    160        private Object[] handleRequestValidation( Object[] values ) {
    161            // OGCWebServiceRequest request = (OGCWebServiceRequest)values[0];
    162            // User user = (User)values[1];
    163
    164            // TODO
    165            // consider personal user rights
    166
    167            // TODO
    168            // support replacements within requests
    169
    170            return values;
    171        }
    172
    173        /**
    174         * returns the name of the trigger
    175         *
    176         * @return name of the trigger
    177         */
    178        public String getName() {
    179            return name;
    180        }
    181
    182        /**
    183         * sets the name of the trigger
    184         *
    185         * @param name
    186         *            name of the trigger
    187         */
    188        public void setName( String name ) {
    189            this.name = name;
    190        }
    191
    192    }