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