001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/ogcwebservices/wass/common/XMLFactory.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2008 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     53115 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     ---------------------------------------------------------------------------*/
044    
045    package org.deegree.ogcwebservices.wass.common;
046    
047    import java.io.IOException;
048    import java.net.URI;
049    import java.util.ArrayList;
050    
051    import org.deegree.framework.log.ILogger;
052    import org.deegree.framework.log.LoggerFactory;
053    import org.deegree.framework.xml.XMLTools;
054    import org.deegree.i18n.Messages;
055    import org.deegree.ogcbase.CommonNamespaces;
056    import org.deegree.ogcwebservices.getcapabilities.DCPType;
057    import org.deegree.ogcwebservices.getcapabilities.Operation;
058    import org.deegree.ogcwebservices.getcapabilities.ServiceIdentification;
059    import org.deegree.ogcwebservices.getcapabilities.ServiceProvider;
060    import org.deegree.ogcwebservices.wass.was.capabilities.WASCapabilities;
061    import org.deegree.ogcwebservices.wass.was.capabilities.WASCapabilitiesDocument;
062    import org.deegree.ogcwebservices.wass.wss.capabilities.WSSCapabilities;
063    import org.deegree.ogcwebservices.wass.wss.capabilities.WSSCapabilitiesDocument;
064    import org.deegree.owscommon.OWSDomainType;
065    import org.w3c.dom.Element;
066    import org.xml.sax.SAXException;
067    
068    /**
069     * This is the XMLFactory for both the WAS and the WSS. Please note that it only works with the 1.0
070     * version of the OWS base classes, mostly recognizable by the appendix _1_0.
071     * 
072     * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a>
073     * @author last edited by: $Author: apoth $
074     * 
075     * @version $Revision: 9345 $, $Date: 2007-12-27 17:22:25 +0100 (Do, 27 Dez 2007) $
076     */
077    
078    public class XMLFactory extends org.deegree.owscommon.XMLFactory {
079    
080        private static final URI WAS = CommonNamespaces.GDINRW_WAS;
081    
082        private static final String PWAS = CommonNamespaces.GDINRWWAS_PREFIX + ":";
083    
084        private static final URI WSS = CommonNamespaces.GDINRW_WSS;
085    
086        private static final String PWSS = CommonNamespaces.GDINRWWSS_PREFIX + ":";
087    
088        private static final URI OWS = CommonNamespaces.OWSNS;
089    
090        private static final String POWS = CommonNamespaces.OWS_PREFIX + ":";
091    
092        private static final URI AUTHN = CommonNamespaces.GDINRW_AUTH;
093    
094        private static final String PAUTHN = CommonNamespaces.GDINRW_AUTH_PREFIX + ":";
095    
096        private static final ILogger LOG = LoggerFactory.getLogger( XMLFactory.class );
097    
098        /**
099         * 
100         * Exports the given WAS capabilities as XML document.
101         * 
102         * @param capabilities
103         *            the data to export
104         * @return the XMLFragment
105         */
106        public static WASCapabilitiesDocument export( WASCapabilities capabilities ) {
107            
108            WASCapabilitiesDocument doc = new WASCapabilitiesDocument();
109    
110            try {
111                doc.createEmptyDocument();
112                Element root = doc.getRootElement();
113    
114                appendBaseCapabilities( capabilities, root );
115                appendExtendedCapabilities( capabilities, root, WAS, PWAS );
116    
117            } catch ( SAXException e ) {
118                LOG.logError( Messages.getMessage( "WASS_ERROR_XML_TEMPLATE_NOT_PARSED",
119                                               new Object[] { "WAS",
120                                                             WASCapabilitiesDocument.XML_TEMPLATE } ),
121                              e );
122            } catch ( IOException e ) {
123                LOG.logError( Messages.getMessage( "WASS_ERROR_XML_TEMPLATE_NOT_READ",
124                                               new Object[] { "WAS",
125                                                             WASCapabilitiesDocument.XML_TEMPLATE } ),
126                              e );
127            }
128            
129            return doc;
130        }
131    
132        /**
133         * 
134         * Exports the given WSS capabilities as XML document. Also appends the WSS specific element
135         * SecuredServiceType.
136         * 
137         * @param capabilities
138         *            the data to export
139         * @return the XMLFragment
140         */
141        public static WSSCapabilitiesDocument export( WSSCapabilities capabilities ) {
142            
143            WSSCapabilitiesDocument doc = new WSSCapabilitiesDocument();
144    
145            try {
146                doc.createEmptyDocument();
147                Element root = doc.getRootElement();
148    
149                appendBaseCapabilities( capabilities, root );
150                Element cap = appendExtendedCapabilities( capabilities, root, WSS, PWSS );
151    
152                XMLTools.appendElement( cap, WSS, PWSS + "SecuredServiceType",
153                                        capabilities.getSecuredServiceType() );
154            } catch ( SAXException e ) {
155                LOG.logError( Messages.getMessage( "WASS_ERROR_XML_TEMPLATE_NOT_PARSED",
156                                               new Object[] { "WSS",
157                                                             WSSCapabilitiesDocument.XML_TEMPLATE } ),
158                              e );
159            } catch ( IOException e ) {
160                LOG.logError( Messages.getMessage( "WASS_ERROR_XML_TEMPLATE_NOT_READ",
161                                               new Object[] { "WSS",
162                                                             WSSCapabilitiesDocument.XML_TEMPLATE } ),
163                              e );
164            }
165            
166            return doc;
167        }
168    
169        /**
170         * 
171         * Appends the WAS/WSS specific capabilities elements, but only those WAS and WSS have in
172         * common.
173         * 
174         * @param capabilities
175         *            the data to append
176         * @param root
177         *            the WAS/WSS_Capabilities element
178         * @param namespace
179         *            the namespace URI, WAS or WSS
180         * @param prefix
181         * @return the appended Capability element
182         */
183        private static Element appendExtendedCapabilities( OWSCapabilitiesBaseType_1_0 capabilities,
184                                                          Element root, URI namespace, String prefix ) {
185            
186            Element cap = XMLTools.appendElement( root, namespace, prefix + "Capability" );
187            Element sams = XMLTools.appendElement( cap, namespace,
188                                                   prefix + "SupportedAuthenticationMethodList" );
189    
190            ArrayList<SupportedAuthenticationMethod> methods = capabilities.getAuthenticationMethods();
191            for ( SupportedAuthenticationMethod method : methods )
192                appendSupportedAuthenticationMethod( sams, method );
193    
194            
195            return cap;
196        }
197    
198        /**
199         * 
200         * Appends a SupportedAuthenticationMethod element to the given element.
201         * 
202         * @param sams
203         *            the SupportedAuthenticationMethodList element
204         * @param method
205         *            the data to append
206         */
207        private static void appendSupportedAuthenticationMethod( Element sams,
208                                                                SupportedAuthenticationMethod method ) {
209            
210            Element sam = XMLTools.appendElement( sams, AUTHN, PAUTHN + "SupportedAuthenticationMethod" );
211            Element authMethod = XMLTools.appendElement( sam, AUTHN, PAUTHN + "AuthenticationMethod" );
212            authMethod.setAttribute( "id", method.getMethod().toString() );
213    
214            String unknownMD = method.getMetadata();
215            if ( unknownMD != null )
216                XMLTools.appendElement( sam, AUTHN, PAUTHN + "UnknownMethodMetadata", unknownMD );
217    
218            WASAuthenticationMethodMD metadata = method.getWasMetadata();
219            if ( metadata != null )
220                appendMetadata( sam, metadata );
221            
222        }
223    
224        /**
225         * 
226         * Appends the OWS base capabilities data to the given element.
227         * 
228         * @param capabilities
229         *            the data to append
230         * @param root
231         *            the element to append to, WAS_- or WSS_Capabilities
232         */
233        private static void appendBaseCapabilities( OWSCapabilitiesBaseType_1_0 capabilities,
234                                                   Element root ) {
235            
236            root.setAttribute( "version", capabilities.getVersion() );
237            root.setAttribute( "updateSequence", capabilities.getUpdateSequence() );
238    
239            // may have to be changed/overwritten (?)
240            ServiceIdentification serviceIdentification = capabilities.getServiceIdentification();
241            if ( serviceIdentification != null )
242                appendServiceIdentification( root, serviceIdentification );
243    
244            ServiceProvider serviceProvider = capabilities.getServiceProvider();
245            if ( serviceProvider != null )
246                appendServiceProvider( root, serviceProvider );
247    
248            OperationsMetadata_1_0 metadata = capabilities.getOperationsMetadata();
249            if ( metadata != null )
250                appendOperationsMetadata_1_0( root, metadata );
251            
252        }
253    
254        /**
255         * 
256         * Appends an OperationsMetadata element to the given element.
257         * 
258         * @param elem
259         *            the element to append to
260         * @param operationsMetadata
261         *            the data to append
262         */
263        private static void appendOperationsMetadata_1_0( Element elem,
264                                                         OperationsMetadata_1_0 operationsMetadata ) {
265            
266            Element root = XMLTools.appendElement( elem, OWS, POWS + "OperationsMetadata" );
267    
268            Operation[] operations = operationsMetadata.getAllOperations();
269            for ( Operation operation : operations )
270                appendOperation_1_0( root, (Operation_1_0) operation );
271    
272            OWSDomainType[] parameters = operationsMetadata.getParameter();
273            if ( parameters != null )
274                for ( OWSDomainType parameter : parameters )
275                    appendParameter( root, parameter, POWS + "Parameter" );
276    
277            OWSDomainType[] constraints = operationsMetadata.getConstraints();
278            if ( constraints != null )
279                for ( OWSDomainType constraint : constraints )
280                    appendParameter( root, constraint, POWS + "Constraint" );
281    
282            String extCap = operationsMetadata.getExtendedCapabilities();
283            if ( extCap != null )
284                XMLTools.appendElement( root, OWS, POWS + "ExtendedCapabilities", extCap );
285            
286        }
287    
288        /**
289         * 
290         * Appends an Operation element to the argument element.
291         * 
292         * @param root
293         *            the element to append to
294         * @param operation
295         *            the data to append
296         */
297        private static void appendOperation_1_0( Element root, Operation_1_0 operation ) {
298            
299            Element op = XMLTools.appendElement( root, OWS, POWS + "Operation" );
300    
301            op.setAttribute( "Name", operation.getName() );
302    
303            DCPType[] dcps = operation.getDCPs();
304            for ( DCPType dcp : dcps )
305                appendDCP( op, dcp );
306    
307            OWSDomainType[] parameters = operation.getParameters();
308            for ( OWSDomainType parameter : parameters )
309                appendParameter( op, parameter, "Parameter" );
310    
311            OWSDomainType[] constraints = operation.getConstraints();
312            for ( OWSDomainType constraint : constraints )
313                appendParameter( op, constraint, "Constraint" );
314    
315            Object[] metadatas = operation.getMetadata();
316            for ( Object metadata : metadatas )
317                appendMetadata( op, metadata );
318            
319        }
320    
321    }