001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/security/owsrequestvalidator/wms/AbstractWMSRequestValidator.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.owsrequestvalidator.wms;
037    
038    import java.util.List;
039    
040    import org.deegree.ogcwebservices.InvalidParameterValueException;
041    import org.deegree.security.owsproxy.Condition;
042    import org.deegree.security.owsproxy.OperationParameter;
043    import org.deegree.security.owsrequestvalidator.Messages;
044    import org.deegree.security.owsrequestvalidator.Policy;
045    import org.deegree.security.owsrequestvalidator.RequestValidator;
046    
047    /**
048     *
049     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a>
050     * @author last edited by: $Author: mschneider $
051     *
052     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
053     */
054    abstract class AbstractWMSRequestValidator extends RequestValidator {
055    
056        // known condition parameter
057        private static final String FORMAT = "format";
058    
059        private static final String MAXWIDTH = "maxWidth";
060    
061        private static final String MAXHEIGHT = "maxHeight";
062    
063        private static final String INVALIDFORMAT = Messages.getString( "AbstractWMSRequestValidator.INVALIDFORMAT" );
064    
065        private static final String INVALIDWIDTH1 = Messages.getString( "AbstractWMSRequestValidator.INVALIDWIDTH1" );
066    
067        private static final String INVALIDWIDTH2 = Messages.getString( "AbstractWMSRequestValidator.INVALIDWIDTH2" );
068    
069        private static final String INVALIDHEIGHT1 = Messages.getString( "AbstractWMSRequestValidator.INVALIDHEIGHT1" );
070    
071        private static final String INVALIDHEIGHT2 = Messages.getString( "AbstractWMSRequestValidator.INVALIDHEIGHT2" );
072    
073        /**
074         * @param policy
075         */
076        public AbstractWMSRequestValidator( Policy policy ) {
077            super( policy );
078        }
079    
080        /**
081         * checks if the passed format is valid against the formats defined in the policy. If
082         * <tt>user</ff> != <tt>null</tt> the valid
083         * formats will be read from the user/rights repository
084         * @param condition condition containing the definition of the valid format
085         * @param format
086         * @throws InvalidParameterValueException
087         */
088        protected void validateFormat( Condition condition, String format )
089                                throws InvalidParameterValueException {
090    
091            OperationParameter op = condition.getOperationParameter( FORMAT );
092    
093            // version is valid because no restrictions are made
094            if ( op.isAny() ) {
095                return;
096            }
097    
098            List<String> list = op.getValues();
099    
100            if ( !list.contains( format ) ) {
101                if ( !op.isUserCoupled() ) {
102                    throw new InvalidParameterValueException( INVALIDFORMAT + format );
103                }
104                userCoupled = true;
105            }
106    
107        }
108    
109        /**
110         * checks if the passed width is > 0 and if it's valid against the maxWidth defined in the
111         * policy. If <tt>user</ff> != <tt>null</tt> the valid
112         * width will be read from the user/rights repository
113         * @param condition condition containing the definition of the valid width
114         * @param width
115         * @throws InvalidParameterValueException
116         */
117        protected void validateMaxWidth( Condition condition, int width )
118                                throws InvalidParameterValueException {
119    
120            if ( width < 1 ) {
121                throw new InvalidParameterValueException( INVALIDWIDTH1 + width );
122            }
123    
124            OperationParameter op = condition.getOperationParameter( MAXWIDTH );
125    
126            // version is valid because no restrictions are made
127            if ( op.isAny() ) {
128                return;
129            }
130    
131            if ( width > op.getFirstAsInt() ) {
132                if ( !op.isUserCoupled() ) {
133                    throw new InvalidParameterValueException( INVALIDWIDTH2 + width );
134                }
135                userCoupled = true;
136            }
137    
138        }
139    
140        /**
141         * checks if the passed height is > 0 and if it's valid against the maxHeight defined in the
142         * policy. If <tt>user</ff> != <tt>null</tt> the valid
143         * height will be read from the user/rights repository
144         * @param condition condition containing the definition of the valid height
145         * @param height
146         * @throws InvalidParameterValueException
147         */
148        protected void validateMaxHeight( Condition condition, int height )
149                                throws InvalidParameterValueException {
150    
151            if ( height < 1 ) {
152                throw new InvalidParameterValueException( INVALIDHEIGHT1 + height );
153            }
154    
155            OperationParameter op = condition.getOperationParameter( MAXHEIGHT );
156    
157            // version is valid because no restrictions are made
158            if ( op.isAny() ) {
159                return;
160            }
161    
162            if ( height > op.getFirstAsInt() ) {
163                if ( !op.isUserCoupled() ) {
164                    throw new InvalidParameterValueException( INVALIDHEIGHT2 + height );
165                }
166                userCoupled = true;
167            }
168        }
169    }