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