001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/security/owsrequestvalidator/wfs/DescribeFeatureTypeRequestValidator.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.wfs;
037    
038    import static org.deegree.portal.standard.security.control.ClientHelper.TYPE_FEATURETYPE;
039    import static org.deegree.security.drm.model.RightType.DESCRIBEFEATURETYPE;
040    
041    import java.util.List;
042    
043    import org.deegree.datatypes.QualifiedName;
044    import org.deegree.datatypes.Types;
045    import org.deegree.framework.log.ILogger;
046    import org.deegree.framework.log.LoggerFactory;
047    import org.deegree.i18n.Messages;
048    import org.deegree.model.feature.Feature;
049    import org.deegree.model.feature.FeatureFactory;
050    import org.deegree.model.feature.FeatureProperty;
051    import org.deegree.model.feature.schema.FeatureType;
052    import org.deegree.model.feature.schema.PropertyType;
053    import org.deegree.ogcwebservices.InvalidParameterValueException;
054    import org.deegree.ogcwebservices.OGCWebServiceRequest;
055    import org.deegree.ogcwebservices.wfs.operation.DescribeFeatureType;
056    import org.deegree.security.UnauthorizedException;
057    import org.deegree.security.drm.model.User;
058    import org.deegree.security.owsproxy.Condition;
059    import org.deegree.security.owsproxy.OperationParameter;
060    import org.deegree.security.owsproxy.Request;
061    import org.deegree.security.owsrequestvalidator.Policy;
062    
063    /**
064     *
065     *
066     *
067     * @version $Revision: 18195 $
068     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
069     * @author last edited by: $Author: mschneider $
070     *
071     * @version 1.0. $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
072     *
073     * @since 2.0
074     */
075    class DescribeFeatureTypeRequestValidator extends AbstractWFSRequestValidator {
076    
077        private static final ILogger LOG = LoggerFactory.getLogger( DescribeFeatureTypeRequestValidator.class );
078    
079        // known condition parameter
080        private static final String FORMAT = "format";
081    
082        private static FeatureType gfFT = null;
083    
084        static {
085            if ( gfFT == null ) {
086                gfFT = DescribeFeatureTypeRequestValidator.createFeatureType();
087            }
088        }
089    
090        /**
091         * @param policy
092         */
093        public DescribeFeatureTypeRequestValidator( Policy policy ) {
094            super( policy );
095        }
096    
097        @Override
098        public void validateRequest( OGCWebServiceRequest request, User user )
099                                throws InvalidParameterValueException, UnauthorizedException {
100            userCoupled = false;
101            Request req = policy.getRequest( "WFS", "DescribeFeatureType" );
102    
103            if ( req == null ) {
104                String msg = "Did you define a DescribeFeatureType section in your WFS policy file?";
105                LOG.logWarning( msg );
106                throw new InvalidParameterValueException( msg );
107            }
108    
109            // request is valid because no restrictions are made
110            if ( req.isAny() || req.getPreConditions().isAny() ) {
111                return;
112            }
113            Condition condition = req.getPreConditions();
114    
115            DescribeFeatureType wfsreq = (DescribeFeatureType) request;
116    
117            validateVersion( condition, wfsreq.getVersion() );
118    
119            QualifiedName[] typeNames = wfsreq.getTypeNames();
120            String[] ft = new String[typeNames.length];
121            for ( int i = 0; i < ft.length; i++ ) {
122                StringBuffer sb = new StringBuffer( 200 );
123                sb.append( '{' ).append( typeNames[i].getNamespace().toASCIIString() );
124                sb.append( "}:" ).append( typeNames[i].getLocalName() );
125                ft[i] = sb.toString();
126            }
127            validateFeatureTypes( condition, ft );
128            validateFormat( condition, wfsreq.getOutputFormat() );
129    
130            if ( userCoupled ) {
131                validateAgainstRightsDB( wfsreq, user );
132            }
133        }
134    
135        /**
136         * valides if the format you in a GetFeature request is valid against the policy assigned to Validator. If the
137         * passed user is not <tt>null</tt> and the format parameter is user coupled the format will be validated against a
138         * users and rights management system.
139         *
140         * @param condition
141         * @param format
142         * @throws InvalidParameterValueException
143         */
144        private void validateFormat( Condition condition, String format )
145                                throws InvalidParameterValueException {
146            OperationParameter op = condition.getOperationParameter( FORMAT );
147    
148            if ( op == null ) {
149                String msg = "You forgot to add a format parameter to your DescribeFeatureType section in your policy.";
150                LOG.logWarning( msg );
151                throw new InvalidParameterValueException( msg );
152            }
153    
154            // version is valid because no restrictions are made
155            if ( op.isAny() )
156                return;
157    
158            List<String> validLayers = op.getValues();
159            if ( op.isUserCoupled() ) {
160                userCoupled = true;
161            } else {
162                if ( !validLayers.contains( format ) ) {
163                    String s = Messages.getMessage( "OWSPROXY_DESCRIBEFEATURETYPE_FORMAT", format );
164                    throw new InvalidParameterValueException( s );
165                }
166            }
167    
168        }
169    
170        /**
171         * validates the passed WMS GetMap request against a User- and Rights-Management DB.
172         *
173         * @param wfsreq
174         * @param user
175         * @throws InvalidParameterValueException
176         */
177        private void validateAgainstRightsDB( DescribeFeatureType wfsreq, User user )
178                                throws InvalidParameterValueException, UnauthorizedException {
179    
180            if ( user == null ) {
181                throw new UnauthorizedException( "no access to anonymous user" );
182            }
183    
184            // create feature that describes the map request
185            FeatureProperty[] fps = new FeatureProperty[2];
186            fps[0] = FeatureFactory.createFeatureProperty( new QualifiedName( "version" ), wfsreq.getVersion() );
187            fps[1] = FeatureFactory.createFeatureProperty( new QualifiedName( "outputformat" ), wfsreq.getOutputFormat() );
188    
189            Feature feature = FeatureFactory.createFeature( "id", gfFT, fps );
190            QualifiedName[] typeNames = wfsreq.getTypeNames();
191            for ( int i = 0; i < typeNames.length; i++ ) {
192                StringBuffer sb = new StringBuffer( 200 );
193                sb.append( '{' ).append( typeNames[i].getNamespace().toASCIIString() );
194                sb.append( "}:" ).append( typeNames[i].getLocalName() );
195                LOG.logDebug( "validating feature type (DESCRIBEFEATURETYPE) against rights database: ", sb );
196                if ( securityConfig.getProxiedUrl() == null ) {
197                    handleUserCoupledRules( user, feature, sb.toString(), TYPE_FEATURETYPE, DESCRIBEFEATURETYPE );
198                } else {
199                    handleUserCoupledRules( user, feature, "[" + securityConfig.getProxiedUrl() + "]:" + sb,
200                                            TYPE_FEATURETYPE, DESCRIBEFEATURETYPE );
201                }
202            }
203    
204        }
205    
206        /**
207         * creates a feature type that matches the parameters of a GetLagendGraphic request
208         *
209         * @return created <tt>FeatureType</tt>
210         */
211        private static FeatureType createFeatureType() {
212            PropertyType[] ftps = new PropertyType[2];
213            ftps[0] = FeatureFactory.createSimplePropertyType( new QualifiedName( "version" ), Types.VARCHAR, false );
214            ftps[1] = FeatureFactory.createSimplePropertyType( new QualifiedName( "outputformat" ), Types.VARCHAR, false );
215    
216            return FeatureFactory.createFeatureType( "DescribeFeatureType", false, ftps );
217        }
218    
219    }