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