001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/portal/owswatch/configs/OwsWatchConfigFactory.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.portal.owswatch.configs;
038
039 import java.io.File;
040 import java.io.FileInputStream;
041 import java.io.IOException;
042 import java.util.ArrayList;
043 import java.util.Arrays;
044 import java.util.HashMap;
045 import java.util.List;
046 import java.util.Map;
047
048 import javax.xml.parsers.DocumentBuilder;
049 import javax.xml.parsers.DocumentBuilderFactory;
050 import javax.xml.parsers.ParserConfigurationException;
051
052 import org.deegree.framework.util.StringTools;
053 import org.deegree.framework.xml.NamespaceContext;
054 import org.deegree.framework.xml.XMLFragment;
055 import org.deegree.framework.xml.XMLParsingException;
056 import org.deegree.framework.xml.XMLTools;
057 import org.deegree.portal.owswatch.CommonNamepspaces;
058 import org.w3c.dom.Document;
059 import org.w3c.dom.Element;
060 import org.xml.sax.SAXException;
061
062 /**
063 * This class takes the path to the config file and produce an instance of OwsWatchConfig Class
064 *
065 * @author <a href="mailto:elmasry@lat-lon.de">Moataz Elmasry</a>
066 * @author last edited by: $Author: jmays $
067 *
068 * @version $Revision: 20271 $, $Date: 2009-10-21 13:07:15 +0200 (Mi, 21. Okt 2009) $
069 */
070 public class OwsWatchConfigFactory {
071
072 private static NamespaceContext cnxt = CommonNamepspaces.getNameSpaceContext();
073
074 private static String prefix = null;
075
076 private static String dotPrefix = null;
077
078 private OwsWatchConfigFactory() {
079 }
080
081 /**
082 * @param filePath
083 * @param webinfPath
084 * @return OwsWatchConfig
085 * @throws SAXException
086 * @throws IOException
087 * @throws XMLParsingException
088 */
089 public static OwsWatchConfig createOwsWatchConfig( String filePath, String webinfPath )
090 throws SAXException, IOException, XMLParsingException {
091
092 File file = new File( filePath );
093 FileInputStream stream = new FileInputStream( file );
094 Document doc = instantiateParser().parse( stream, XMLFragment.DEFAULT_URL );
095 return createOwsWatchConfig( doc.getDocumentElement(), webinfPath );
096 }
097
098 /**
099 * @param root
100 * @param webinfPath
101 * @return OwsWatchConfig
102 * @throws XMLParsingException
103 * @throws IOException
104 */
105 public static OwsWatchConfig createOwsWatchConfig( Element root, String webinfPath )
106 throws XMLParsingException, IOException {
107
108 if ( cnxt == null ) {
109 cnxt = CommonNamepspaces.getNameSpaceContext();
110 }
111 prefix = root.lookupPrefix( CommonNamepspaces.DEEGREEWCNS.toASCIIString() );
112 if ( prefix == null ) {
113 throw new XMLParsingException( "The Configurations xml file does not contain the namespace: "
114 + CommonNamepspaces.DEEGREEWCNS );
115 } else {
116 dotPrefix = prefix + ":";
117 }
118 cnxt.addNamespace( prefix, CommonNamepspaces.DEEGREEWCNS );
119 GeneralConfig general = createGeneralConfig( XMLTools.getRequiredElement(
120 root,
121 StringTools.concat( 100, "./",
122 dotPrefix,
123 "GeneralConfiguration" ),
124 cnxt ) );
125 ServiceDescription serviceConfig = createServiceConfig(
126 XMLTools.getRequiredElement(
127 root,
128 StringTools.concat(
129 100,
130 "./",
131 dotPrefix,
132 "ServiceConfiguration" ),
133 cnxt ), webinfPath );
134 return new OwsWatchConfig( general, serviceConfig );
135 }
136
137 /**
138 * Parses the General Config section
139 *
140 * @param elem
141 * @return GeneralConfig
142 * @throws XMLParsingException
143 */
144 protected static GeneralConfig createGeneralConfig( Element elem )
145 throws XMLParsingException {
146
147 Map<String, User> users = createUsers( XMLTools.getRequiredElement( elem, StringTools.concat( 100, "./",
148 dotPrefix,
149 "Users" ), cnxt ) );
150 int globalRefreshRate = XMLTools.getNodeAsInt( elem, StringTools.concat( 100, "./", dotPrefix,
151 "GlobalRefreshRate" ), cnxt, 1 );
152 String mailFrom = XMLTools.getRequiredNodeAsString( elem, StringTools.concat( 100, "./", dotPrefix, "Mail/",
153 dotPrefix, "mailFrom" ), cnxt );
154 String mailServer = XMLTools.getRequiredNodeAsString( elem, StringTools.concat( 100, "./", dotPrefix, "Mail/",
155 dotPrefix, "mailServer" ), cnxt );
156 Element locElement = XMLTools.getRequiredElement( elem, StringTools.concat( 100, "./", dotPrefix, "Location" ),
157 cnxt );
158 String serviceAddress = XMLTools.getRequiredNodeAsString( locElement, StringTools.concat( 100, "./", dotPrefix,
159 "ServiceAddress" ),
160 cnxt );
161 String protFolderPath = XMLTools.getRequiredNodeAsString( locElement, StringTools.concat( 100, "./", dotPrefix,
162 "ProtocolLocation" ),
163 cnxt );
164 String serviceInstancesPath = XMLTools.getRequiredNodeAsString(
165 locElement,
166 StringTools.concat( 100, "./", dotPrefix,
167 "ServiceInstanceLocation" ),
168 cnxt );
169 return new GeneralConfig( globalRefreshRate, users, mailFrom, mailServer, protFolderPath, serviceInstancesPath,
170 serviceAddress );
171 }
172
173 /**
174 * Parses the ServiceDescription Section
175 *
176 * @param elem
177 * @param webinfPath
178 * @return ServiceDescription
179 * @throws XMLParsingException
180 * @throws IOException
181 */
182 protected static ServiceDescription createServiceConfig( Element elem, String webinfPath )
183 throws XMLParsingException, IOException {
184 Map<String, Service> services = createServices( XMLTools.getElement( elem, StringTools.concat( 100, "./",
185 dotPrefix,
186 "Services" ),
187 cnxt ), webinfPath );
188 List<Integer> testIntervals = createTestIntervals( XMLTools.getElement( elem,
189 StringTools.concat( 100, "./",
190 dotPrefix,
191 "TestInterval" ),
192 cnxt ) );
193
194 return new ServiceDescription( testIntervals, services );
195 }
196
197 /**
198 * Parses the Users section
199 *
200 * @param elem
201 * @return Map
202 * @throws XMLParsingException
203 */
204 protected static Map<String, User> createUsers( Element elem )
205 throws XMLParsingException {
206 Map<String, User> users = new HashMap<String, User>();
207 List<Element> list = XMLTools.getElements( elem, StringTools.concat( 100, "./", dotPrefix, "User" ), cnxt );
208 for ( Element child : list ) {
209 String username = XMLTools.getRequiredNodeAsString( child, StringTools.concat( 100, "./", dotPrefix,
210 "UserName" ), cnxt );
211 String password = XMLTools.getRequiredNodeAsString( child, StringTools.concat( 100, "./", dotPrefix,
212 "Password" ), cnxt );
213 String firstName = XMLTools.getRequiredNodeAsString( child, StringTools.concat( 100, "./", dotPrefix,
214 "FirstName" ), cnxt );
215 String lastName = XMLTools.getRequiredNodeAsString( child, StringTools.concat( 100, "./", dotPrefix,
216 "LastName" ), cnxt );
217 String email = XMLTools.getRequiredNodeAsString( child,
218 StringTools.concat( 100, "./", dotPrefix, "Email" ), cnxt );
219 String rolesValue = XMLTools.getRequiredNodeAsString( child, StringTools.concat( 100, "./", dotPrefix,
220 "Roles" ), cnxt );
221 List<String> roles = Arrays.asList( rolesValue.split( "," ) );
222 User user = new User( username, password, firstName, lastName, email, roles );
223 users.put( username, user );
224 }
225 return users;
226 }
227
228 /**
229 * Parses the TestIntervals section
230 *
231 * @param elem
232 * @return List
233 * @throws XMLParsingException
234 */
235 protected static List<Integer> createTestIntervals( Element elem )
236 throws XMLParsingException {
237
238 List<Element> list = XMLTools.getElements( elem, StringTools.concat( 100, "./", dotPrefix, "Value" ), cnxt );
239 List<Integer> testIntervals = new ArrayList<Integer>();
240 for ( Element child : list ) {
241 testIntervals.add( XMLTools.getRequiredNodeAsInt( child, ".", cnxt ) );
242 }
243 return testIntervals;
244 }
245
246 /**
247 * Parses the Services section
248 *
249 * @param elem
250 * @param webinfPath
251 * @return Map
252 * @throws XMLParsingException
253 * @throws IOException
254 */
255 protected static Map<String, Service> createServices( Element elem, String webinfPath )
256 throws XMLParsingException, IOException {
257
258 Map<String, Service> tmpServices = new HashMap<String, Service>();
259 List<Element> list = XMLTools.getElements( elem, StringTools.concat( 100, "./", dotPrefix, "Service" ), cnxt );
260 for ( Element child : list ) {
261 String serviceName = XMLTools.getAttrValue( child, null, "type", null );
262
263 String[] tokens = null;
264 if ( ( tokens = serviceName.split( ":" ) ).length == 2 ) {
265 serviceName = tokens[1];
266 }
267 ServiceVersion serviceVersion = createServiceVersion( child, webinfPath );
268 if ( !tmpServices.containsKey( serviceName ) ) {
269 tmpServices.put( serviceName, new Service( serviceName ) );
270 }
271 tmpServices.get( serviceName ).getServiceVersions().put( serviceVersion.getVersion(), serviceVersion );
272 }
273 return tmpServices;
274 }
275
276 /**
277 * Parses the (multiple)Service section(s) in Services
278 *
279 * @param elem
280 * @param webinfPath
281 * @return Service
282 * @throws XMLParsingException
283 * @throws IOException
284 */
285 protected static ServiceVersion createServiceVersion( Element elem, String webinfPath )
286 throws XMLParsingException, IOException {
287
288 String version = XMLTools.getAttrValue( elem, null, "version", null );
289 ServiceVersion serviceVersion = new ServiceVersion( version );
290
291 List<Element> list = XMLTools.getElements( elem, StringTools.concat( 100, "./", dotPrefix, "ServiceRequest" ),
292 cnxt );
293 Map<String, ServiceRequest> requests = new HashMap<String, ServiceRequest>();
294 for ( Element child : list ) {
295 ServiceRequest request = createServiceRequest( child, webinfPath );
296 requests.put( request.getName(), request );
297 }
298 serviceVersion.setRequests( requests );
299 return serviceVersion;
300 }
301
302 /**
303 * Parses the Request Section in Service
304 *
305 * @param elem
306 * @param webinfPath
307 * @return ServiceRequest
308 * @throws XMLParsingException
309 * @throws IOException
310 */
311 protected static ServiceRequest createServiceRequest( Element elem, String webinfPath )
312 throws XMLParsingException, IOException {
313 String requestName = XMLTools.getAttrValue( elem, null, "name", null );
314 String attr = XMLTools.getAttrValue( elem, null, "isGetable", null );
315 boolean canGet = ( attr != null && attr.equals( "1" ) ) ? true : false;
316
317 attr = XMLTools.getAttrValue( elem, null, "isPostable", null );
318 boolean canPost = ( attr != null && attr.equals( "1" ) ) ? true : false;
319
320 String getForm = XMLTools.getNodeAsString( elem, StringTools.concat( 100, "./", dotPrefix, "GetForm" ), cnxt,
321 null );
322 String postForm = XMLTools.getNodeAsString( elem, StringTools.concat( 100, "./", dotPrefix, "PostForm" ), cnxt,
323 null );
324 List<Element> list = XMLTools.getElements( elem, StringTools.concat( 100, "./", dotPrefix,
325 "RequestParameters/", dotPrefix, "Key" ),
326 cnxt );
327 List<String> htmlKeys = new ArrayList<String>();
328 for ( Element key : list ) {
329 htmlKeys.add( key.getTextContent() );
330 }
331 String getSnippetPath = null;
332 if ( getForm != null && getForm.length() > 0 ) {
333 StringBuilder builder = new StringBuilder( 150 );
334 builder.append( webinfPath );
335 if ( !webinfPath.endsWith( "/" ) ) {
336 builder.append( "/" );
337 }
338 builder.append( getForm );
339 getSnippetPath = builder.toString();
340 }
341
342 String postSnippetPath = null;
343 if ( postForm != null && postForm.length() > 0 ) {
344 StringBuilder builder = new StringBuilder( 150 );
345 builder.append( webinfPath );
346 if ( !webinfPath.endsWith( "/" ) ) {
347 builder.append( "/" );
348 }
349 builder.append( postForm );
350 postSnippetPath = builder.toString();
351 }
352
353 return new ServiceRequest( requestName, getSnippetPath, postSnippetPath, canPost, canGet, htmlKeys );
354 }
355
356 /**
357 * Creates a new instance of DocumentBuilder
358 *
359 * @return DocumentBuilder
360 * @throws IOException
361 */
362 private static DocumentBuilder instantiateParser()
363 throws IOException {
364
365 DocumentBuilder parser = null;
366
367 try {
368 DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
369 fac.setNamespaceAware( true );
370 fac.setValidating( false );
371 fac.setIgnoringElementContentWhitespace( false );
372 parser = fac.newDocumentBuilder();
373 return parser;
374 } catch ( ParserConfigurationException e ) {
375 throw new IOException( "Unable to initialize DocumentBuilder: " + e.getMessage() );
376 }
377 }
378
379 /**
380 * @return Prefix of the Configurations xml file Will only work after the first call to createOwsWatchConfig,
381 * otherwise it will return null;
382 */
383 public static String getPrefix() {
384 return prefix;
385 }
386 }