001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/security/owsrequestvalidator/wms/GetLegendGraphicResponseValidator.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 org.deegree.framework.util.MimeTypeMapper;
039    import org.deegree.ogcwebservices.InvalidParameterValueException;
040    import org.deegree.security.drm.model.User;
041    import org.deegree.security.owsproxy.Request;
042    import org.deegree.security.owsrequestvalidator.Policy;
043    import org.deegree.security.owsrequestvalidator.ResponseValidator;
044    
045    /**
046     *
047     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a>
048     * @author last edited by: $Author: mschneider $
049     *
050     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
051     */
052    class GetLegendGraphicResponseValidator extends ResponseValidator {
053    
054        /**
055         * @param policy
056         */
057        public GetLegendGraphicResponseValidator( Policy policy ) {
058            super( policy );
059        }
060    
061        /**
062         * validates the passed object as a response to a OWS request. The validity of the response may
063         * is assigned to specific user rights. If the passed user is <>null this will be evaluated.
064         * <br>
065         * the reponse may contain three valid kinds of objects:
066         * <ul>
067         * <li>a serialized image
068         * <li>a xml encoded exception
069         * <li>a svg-encoded vector image
070         * </ul>
071         * Each of these types can be identified by the mime-type of the response that is also passed to
072         * the method. <br>
073         * If something basic went wrong it is possible that not further specified kind of object is
074         * passed as response. In this case the method will throw an
075         * <tt>InvalidParameterValueException</tt> to avoid sending bad responses to the client.
076         *
077         * @param service
078         *            service which produced the response (WMS, WFS ...)
079         * @param response
080         * @param mime
081         *            mime-type of the response
082         * @param user
083         * @return the validated response or the response as given.
084         * @throws InvalidParameterValueException
085         */
086        @Override
087        public byte[] validateResponse( String service, byte[] response, String mime, User user )
088                                throws InvalidParameterValueException {
089    
090            Request req = policy.getRequest( service, "GetLegendGraphic" );
091            // request is valid because no restrictions are made
092            if ( req.isAny() || req.getPostConditions().isAny() ) {
093                return response;
094            }
095    
096            //Condition condition = req.getPostConditions();
097    
098            if ( MimeTypeMapper.isKnownImageType( mime ) ) {
099                response = validateImage( response, mime, user );
100            } else if ( MimeTypeMapper.isKnownOGCType( mime ) ) {
101                // if the mime-type isn't an image type but a known
102                // OGC mime-type it must be an XML document.
103                // probably it is an exception but it also could be
104                // a GML document
105                response = validateXML( response, mime, user );
106            } else if ( mime.equals( "text/xml" ) ) {
107                // if the mime-type isn't an image type but 'text/xml'
108                // it could be an exception
109                response = validateXML( response, mime, user );
110            } else {
111                throw new InvalidParameterValueException( UNKNOWNMIMETYPE + mime );
112            }
113    
114            return response;
115        }
116    
117        /**
118         * validates the passed object/image to be valid against the policy
119         *
120         * @param image
121         * @param mime
122         * @param user
123         * @return the image parameter.
124         */
125        private byte[] validateImage( byte[] image, String mime, User user ) {
126            // TODO
127            // define useful post-validation for images
128            // at the moment everything is valid
129            return image;
130        }
131    
132        /**
133         * validates the passed object/xml to be valid against the policy
134         *
135         * @param xml
136         * @param mime
137         * @param user
138         * @return the xml parameter
139         */
140        private byte[] validateXML( byte[] xml, String mime, User user ) {
141            // TODO
142            // define useful post-validation for xml-documents
143            // at the moment everything is valid
144            return xml;
145        }
146    }