001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/security/owsproxy/StringReplacementTrigger.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2006 by:
006     EXSE, Department of Geography, University of Bonn
007     http://www.giub.uni-bonn.de/deegree/
008     lat/lon GmbH
009     http://www.lat-lon.de
010    
011     This library is free software; you can redistribute it and/or
012     modify it under the terms of the GNU Lesser General Public
013     License as published by the Free Software Foundation; either
014     version 2.1 of the License, or (at your option) any later version.
015    
016     This library is distributed in the hope that it will be useful,
017     but WITHOUT ANY WARRANTY; without even the implied warranty of
018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019     Lesser General Public License for more details.
020    
021     You should have received a copy of the GNU Lesser General Public
022     License along with this library; if not, write to the Free Software
023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024    
025     Contact:
026    
027     Andreas Poth
028     lat/lon GmbH
029     Aennchenstr. 19
030     53177 Bonn
031     Germany
032     E-Mail: poth@lat-lon.de
033    
034     Prof. Dr. Klaus Greve
035     Department of Geography
036     University of Bonn
037     Meckenheimer Allee 166
038     53115 Bonn
039     Germany
040     E-Mail: greve@giub.uni-bonn.de
041    
042     ---------------------------------------------------------------------------*/
043    package org.deegree.security.owsproxy;
044    
045    import java.util.ArrayList;
046    import java.util.List;
047    
048    import org.deegree.framework.log.ILogger;
049    import org.deegree.framework.log.LoggerFactory;
050    import org.deegree.framework.trigger.Trigger;
051    import org.deegree.framework.util.StringTools;
052    import org.deegree.ogcwebservices.OGCWebServiceRequest;
053    import org.deegree.ogcwebservices.csw.capabilities.CatalogueGetCapabilities;
054    import org.deegree.ogcwebservices.sos.capabilities.SOSGetCapabilities;
055    import org.deegree.ogcwebservices.wcs.getcapabilities.WCSGetCapabilities;
056    import org.deegree.ogcwebservices.wfs.operation.WFSGetCapabilities;
057    import org.deegree.ogcwebservices.wmps.operation.WMPSGetCapabilities;
058    import org.deegree.ogcwebservices.wms.operation.WMSGetCapabilities;
059    import org.deegree.ogcwebservices.wps.capabilities.WPSGetCapabilities;
060    import org.deegree.ogcwebservices.wpvs.operation.WPVSGetCapabilities;
061    
062    /**
063     * Trigger for replacing string within requests and responses to 
064     * OGC Web Service packed behind a owsProxy. At the moment replacements 
065     * are just supported for responses to GetCapabilities requests.
066     * 
067     *
068     * @version $Revision: 6259 $
069     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
070     * @author last edited by: $Author: bezema $
071     *
072     * @version 1.0. $Revision: 6259 $, $Date: 2007-03-20 10:15:15 +0100 (Di, 20 Mär 2007) $
073     *
074     * @since 2.0
075     */
076    public class StringReplacementTrigger implements Trigger {
077        
078        private static final ILogger LOG = LoggerFactory.getLogger( StringReplacementTrigger.class );
079        
080        private String name;
081        private String consideredRequests;
082        private List<String> replacementsKeys;
083        private List<String> replacementsVals;
084    
085        /**
086         * 
087         * @param replacements list of replacement rules "{key1|value1}{key2|value2} ... {keyX|valueX}"
088         * @param consideredRequests comma seperated list of request names to be 
089         *                           considerded; e.g. "WMS:GetFeatureInfo,WMS:GetCapabilities,WFS:GetFeature"
090         */
091        public StringReplacementTrigger(String replacements, String consideredRequests) {
092            
093            this.consideredRequests = consideredRequests;
094            String[] tmp = StringTools.extractStrings( replacements, "{", "}" );
095            this.replacementsKeys = new ArrayList<String>( tmp.length );
096            this.replacementsVals = new ArrayList<String>( tmp.length );
097            for ( int i = 0; i < tmp.length; i++ ) {
098                int pos = tmp[i].lastIndexOf( '|' );
099                this.replacementsKeys.add( tmp[i].substring( 0, pos ) ); 
100                this.replacementsVals.add( tmp[i].substring( pos+1, tmp[i].length()  ) );
101            }
102        }
103        
104        /**
105         * 
106         * @param caller instance of calling class
107         * @param values 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
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 && 
133                   consideredRequests.indexOf( "WMS:GetCapabilities" ) > -1 ) ||
134                 ( request instanceof WFSGetCapabilities && 
135                   consideredRequests.indexOf( "WFS:GetCapabilities" ) > -1 ) ||
136                 ( request instanceof CatalogueGetCapabilities && 
137                   consideredRequests.indexOf( "CSW:GetCapabilities" ) > -1 ) ||
138                 ( request instanceof WCSGetCapabilities && 
139                   consideredRequests.indexOf( "WCS:GetCapabilities" ) > -1 ) || 
140                 ( request instanceof WMPSGetCapabilities && 
141                   consideredRequests.indexOf( "WMPS:GetCapabilities" ) > -1 ) ||
142                 ( request instanceof WPVSGetCapabilities && 
143                   consideredRequests.indexOf( "WPVS:GetCapabilities" ) > -1 ) ||
144                 ( request instanceof SOSGetCapabilities && 
145                   consideredRequests.indexOf( "SOS:GetCapabilities" ) > -1 ) ||
146                 ( request instanceof WPSGetCapabilities && 
147                   consideredRequests.indexOf( "WPS:GetCapabilities" ) > -1 ) ) {
148                String tmp = new String( data );
149                for ( int i = 0; i < replacementsKeys.size(); i++ ) {
150                    LOG.logDebug( "response replacement: ", replacementsKeys.get( i ) +  " - " + 
151                                  replacementsVals.get( i ) );
152                    tmp = tmp.replaceAll( replacementsKeys.get( i ), replacementsVals.get( i ) );
153                }
154                values[1] = tmp.getBytes();
155            } else {
156                // TODO
157                // support replacements within responses to other requests
158                LOG.logInfo( "No string replacement implemented for request: ",
159                             request.getClass().getName() );
160            }        
161            
162            return values;
163        }
164    
165        /**
166         * 
167         * @param caller
168         * @param values
169         * @return
170         */
171        private Object[] handleRequestValidation( Object[] values ) {
172            //OGCWebServiceRequest request = (OGCWebServiceRequest)values[0];
173            //User user = (User)values[1];
174            
175            // TODO
176            // consider personal user rights
177            
178            // TODO
179            // support replacements within requests
180            
181            return values;
182        }
183    
184        /**
185         * returns the name of the trigger
186         * @return name of the trigger
187         */
188        public String getName() {
189            return name;
190        }
191    
192        /**
193         * sets the name of the trigger
194         * @param name name of the trigger
195         */
196        public void setName( String name ) {
197            this.name = name;
198        }
199    
200    }
201    
202    /* ********************************************************************
203    Changes to this class. What the people have been up to:
204    $Log$
205    Revision 1.1  2006/12/31 10:33:55  poth
206    trigger for OWSProxyFilter added
207    
208    
209    ********************************************************************** */