001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/ogcwebservices/csw/discovery/DescribeRecord.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    
037    package org.deegree.ogcwebservices.csw.discovery;
038    
039    import java.net.URI;
040    import java.net.URISyntaxException;
041    import java.util.HashMap;
042    import java.util.Map;
043    
044    import org.deegree.framework.log.ILogger;
045    import org.deegree.framework.log.LoggerFactory;
046    import org.deegree.framework.xml.NamespaceContext;
047    import org.deegree.framework.xml.XMLParsingException;
048    import org.deegree.framework.xml.XMLTools;
049    import org.deegree.ogcbase.CommonNamespaces;
050    import org.deegree.ogcwebservices.InvalidParameterValueException;
051    import org.deegree.ogcwebservices.MissingParameterValueException;
052    import org.deegree.ogcwebservices.OGCWebServiceException;
053    import org.deegree.ogcwebservices.csw.AbstractCSWRequest;
054    import org.deegree.ogcwebservices.csw.CSWPropertiesAccess;
055    import org.w3c.dom.Element;
056    
057    /**
058     * The mandatory DescribeRecord operation allows a client to discover elements of the information
059     * model supported by the target catalogue service. The operation allows some or all of the
060     * information model to be described.
061     *
062     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a>
063     * @author <a href="mailto:tfr@users.sourceforge.net">Torsten Friebe </a>
064     * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider </a>
065     *
066     * @author last edited by: $Author: mschneider $
067     *
068     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
069     */
070    public class DescribeRecord extends AbstractCSWRequest {
071    
072        private static final long serialVersionUID = 6554937884331546780L;
073    
074        private static final ILogger LOG = LoggerFactory.getLogger( DescribeRecord.class );
075    
076        private static NamespaceContext nsContext = CommonNamespaces.getNamespaceContext();
077    
078        private Map<String, URI> namespaceMappings;
079    
080        private String[] typeNames;
081    
082        private String outputFormat;
083    
084        private URI schemaLanguage;
085    
086        /**
087         * creates a GetRecords request from the XML fragment passed. The passed element must be valid
088         * against the OGC CSW 2.0 GetRecords schema.
089         *
090         * @param id
091         *            unique ID of the request
092         * @param root
093         *            root element of the GetRecors request
094         * @return the new instance
095         * @throws MissingParameterValueException
096         * @throws InvalidParameterValueException
097         * @throws OGCWebServiceException
098         */
099        public static DescribeRecord create( String id, Element root )
100                                throws MissingParameterValueException, InvalidParameterValueException,
101                                OGCWebServiceException {
102    
103            String version = null;
104            try {
105                // first try to read verdsion attribute which is optional for CSW 2.0.0 and 2.0.1
106                version = XMLTools.getNodeAsString( root, "./@version", nsContext, null );
107            } catch ( XMLParsingException e ) {
108                // a default version will be used
109            }
110            if ( version == null ) {
111                // if no version attribute has been set try mapping namespace URI to a version;
112                // this is not well defined for 2.0.0 and 2.0.1 which uses the same namespace.
113                // in this case 2.0.0 will be returned!
114                version = CSWPropertiesAccess.getString( root.getNamespaceURI() );
115            }
116    
117            // read class for version depenging parsing of DescribeRecord request from properties
118            String className = CSWPropertiesAccess.getString( "DescribeRecord" + version );
119            Class<?> clzz = null;
120            try {
121                clzz = Class.forName( className );
122            } catch ( ClassNotFoundException e ) {
123                LOG.logError( e.getMessage(), e );
124                throw new InvalidParameterValueException( e.getMessage(), e );
125            }
126            DescribeRecordDocument document = null;
127            try {
128                document = (DescribeRecordDocument) clzz.newInstance();
129            } catch ( InstantiationException e ) {
130                LOG.logError( e.getMessage(), e );
131                throw new InvalidParameterValueException( e.getMessage(), e );
132            } catch ( IllegalAccessException e ) {
133                LOG.logError( e.getMessage(), e );
134                throw new InvalidParameterValueException( e.getMessage(), e );
135            }
136    
137            document.setRootElement( root );
138            return document.parse( id );
139    
140        }
141    
142        /**
143         * Creates a new <code>DecribeRecord</code> instance from the values stored in the submitted
144         * Map. Keys (parameter names) in the Map must be uppercase.
145         *
146         * @TODO evaluate vendorSpecificParameter
147         *
148         * @param kvp
149         *            Map containing the parameters
150         * @return the new instance
151         * @exception InvalidParameterValueException
152         * @throws MissingParameterValueException
153         */
154        public static DescribeRecord create( Map<String, String> kvp )
155                                throws InvalidParameterValueException, MissingParameterValueException {
156    
157            String id;
158            String version;
159            Map<String, String> vendorSpecificParameter = new HashMap<String, String>();
160            Map<String, URI> namespaceMappings;
161            String[] typeNames = new String[0];
162            String outputFormat;
163            URI schemaLanguage;
164    
165            // 'ID'-attribute (optional)
166            id = getParam( "ID", kvp, "" );
167    
168            // 'VERSION'-attribute (mandatory)
169            version = getRequiredParam( "VERSION", kvp );
170    
171            // 'NAMESPACE'-attribute (optional)
172            namespaceMappings = getNSMappings( getParam( "NAMESPACE", kvp, null ) );
173    
174            // 'TYPENAME'-attribute (optional)
175            String typeNamesString = getParam( "TYPENAME", kvp, null );
176            if ( typeNamesString != null ) {
177                typeNames = typeNamesString.split( "," );
178            }
179    
180            // 'OUTPUTFORMAT'-attribute (optional)
181            if ( "2.0.2".equals( version ) ) {
182                outputFormat = getParam( "OUTPUTFORMAT", kvp, "application/xml" );
183            } else {
184                outputFormat = getParam( "OUTPUTFORMAT", kvp, "text/xml" );
185            }
186    
187            // 'SCHEMALANGUAGE'-attribute (optional)
188            String schemaLanguageString = getParam( "SCHEMALANGUAGE", kvp, "XMLSCHEMA" );
189            try {
190                schemaLanguage = new URI( schemaLanguageString );
191            } catch ( URISyntaxException e ) {
192                String msg = "Value '" + schemaLanguageString
193                             + "' for parameter 'SCHEMALANGUAGE' is invalid. Must denote a valid URI.";
194                throw new InvalidParameterValueException( msg );
195            }
196    
197            return new DescribeRecord( id, version, vendorSpecificParameter, namespaceMappings, typeNames, outputFormat,
198                                       schemaLanguage );
199        }
200    
201        /**
202         * Creates a new <code>DescribeRecord</code> instance.
203         *
204         * @param id
205         * @param version
206         * @param vendorSpecificParameter
207         */
208        DescribeRecord( String id, String version, Map<String, String> vendorSpecificParameter ) {
209            super( version, id, vendorSpecificParameter );
210        }
211    
212        /**
213         * Creates a new <code>DescribeRecord</code> instance.
214         *
215         * @param id
216         * @param version
217         * @param vendorSpecificParameter
218         * @param namespaceMappings
219         * @param typeNames
220         * @param outputFormat
221         * @param schemaLanguage
222         */
223        DescribeRecord( String id, String version, Map<String, String> vendorSpecificParameter,
224                        Map<String, URI> namespaceMappings, String[] typeNames, String outputFormat, URI schemaLanguage ) {
225            this( id, version, vendorSpecificParameter );
226            this.namespaceMappings = namespaceMappings;
227            this.typeNames = typeNames;
228            this.outputFormat = outputFormat;
229            this.schemaLanguage = schemaLanguage;
230        }
231    
232        /**
233         * Used to specify namespace(s) and their prefix(es). Format is [prefix:]uri. If prefix is not
234         * specified, then this is the default namespace.
235         * <p>
236         * Zero or one (Optional). Include value for each namespace used by a TypeName. If not included,
237         * all qualified names are in the default namespace
238         *
239         * @return the mappings
240         */
241        public Map<String, URI> getNamespaces() {
242            return this.namespaceMappings;
243        }
244    
245        /**
246         * One or more qualified type names to be described.
247         * <p>
248         * Zero or one (Optional). Default action is to describe all types known to server.
249         *
250         * @return the type names
251         *
252         */
253        public String[] getTypeNames() {
254            return this.typeNames;
255        }
256    
257        /**
258         * A MIME type indicating the format that the output document should have.
259         * <p>
260         * Zero or one (Optional). Default value is text/xml
261         *
262         * @return the format
263         *
264         */
265        public String getOutputFormat() {
266            return this.outputFormat;
267        }
268    
269        /**
270         * Default value is 'XMLSCHEMA'.
271         *
272         * @return the language
273         *
274         */
275        public URI getSchemaLanguage() {
276            return this.schemaLanguage;
277        }
278    }