001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/enterprise/control/RPCFactory.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: package org.deegree.enterprise.control;
045:
046: import java.io.Reader;
047: import java.util.Date;
048:
049: import org.deegree.framework.log.ILogger;
050: import org.deegree.framework.log.LoggerFactory;
051: import org.deegree.framework.util.TimeTools;
052: import org.deegree.framework.xml.ElementList;
053: import org.deegree.framework.xml.NamespaceContext;
054: import org.deegree.framework.xml.XMLTools;
055: import org.deegree.ogcbase.CommonNamespaces;
056: import org.w3c.dom.Document;
057: import org.w3c.dom.Element;
058:
059: /**
060: * Factory class for creating RPC methodCall and methodResponse objects from their XML
061: * representation
062: *
063: * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
064: * @version $Revision: 10261 $ $Date: 2008-02-25 06:14:40 -0800 (Mon, 25 Feb 2008) $
065: */
066: public class RPCFactory {
067:
068: private static final ILogger LOG = LoggerFactory
069: .getLogger(RPCFactory.class);
070:
071: private static NamespaceContext nsContext = CommonNamespaces
072: .getNamespaceContext();
073:
074: /**
075: * creates an instance of <tt>RPCMethodCall</tt> from an XML document that can be accessed
076: * through the passed <tt>Reader</tt>
077: *
078: * @param reader
079: * reader to access an XML document
080: * @return an RPCMethodCall
081: * @throws RPCException
082: */
083: public static RPCMethodCall createRPCMethodCall(Reader reader)
084: throws RPCException {
085:
086: Document doc = null;
087: try {
088: doc = XMLTools.parse(reader);
089: } catch (Exception e) {
090: LOG.logError(e.getMessage(), e);
091: throw new RPCException(e.toString());
092: }
093:
094: return createRPCMethodCall(doc);
095: }
096:
097: /**
098: * creates an instance of <tt>RPCMethodCall</tt> from the XML document passed
099: *
100: * @param doc
101: * XML document containing a RPC method call
102: * @return an RPCMethodCall
103: * @throws RPCException
104: */
105: public static RPCMethodCall createRPCMethodCall(Document doc)
106: throws RPCException {
107:
108: RPCMethodCall mc = null;
109: try {
110: Element root = doc.getDocumentElement();
111: // get methode name - mandatory
112: String methodName = XMLTools.getRequiredStringValue(
113: "methodName", null, root);
114:
115: Element params = XMLTools.getChildElement("params", null,
116: root);
117:
118: RPCParameter[] parameters = null;
119: if (params != null) {
120: ElementList el = XMLTools.getChildElements(params);
121: if (el != null) {
122: parameters = new RPCParameter[el.getLength()];
123: for (int i = 0; i < el.getLength(); i++) {
124: parameters[i] = createRPCParam(el.item(i));
125: }
126: }
127: } else {
128: parameters = new RPCParameter[0];
129: }
130:
131: mc = new RPCMethodCall(methodName, parameters);
132: } catch (Exception e) {
133: LOG.logError(e.getMessage(), e);
134: throw new RPCException(e.toString());
135: }
136:
137: return mc;
138: }
139:
140: /**
141: * creates an instance of <tt>RPCMethodResponse</tt> from an XML document that can be accessed
142: * through the passed <tt>Reader</tt>
143: *
144: * @param reader
145: * reader to access an XML document
146: * @return created <tt>RPCMethodResponse</tt>
147: * @throws RPCException
148: */
149: public static RPCMethodResponse createRPCMethodResponse(
150: Reader reader) throws RPCException {
151:
152: Document doc = null;
153: try {
154: doc = XMLTools.parse(reader);
155: } catch (Exception e) {
156: LOG.logError(e.getMessage(), e);
157: throw new RPCException(e.toString());
158: }
159:
160: return createRPCMethodResponse(doc);
161: }
162:
163: /**
164: * creates an instance of <tt>RPCMethodResponse</tt> from the XML document passed
165: *
166: * @param doc
167: * XML document containing a RPC method call
168: * @return created <tt>RPCMethodResponse</tt>
169: * @throws RPCException
170: */
171: public static RPCMethodResponse createRPCMethodResponse(Document doc)
172: throws RPCException {
173:
174: RPCMethodResponse mc = null;
175: try {
176: Element root = doc.getDocumentElement();
177:
178: Element params = XMLTools.getChildElement("params", null,
179: root);
180:
181: if (params != null) {
182: ElementList el = XMLTools.getChildElements(params);
183: RPCParameter[] parameters = null;
184: if (el != null) {
185: parameters = new RPCParameter[el.getLength()];
186: for (int i = 0; i < el.getLength(); i++) {
187: parameters[i] = createRPCParam(el.item(i));
188: }
189: }
190: mc = new RPCMethodResponse(parameters);
191: } else {
192: // a fault is contained instead of the expected result
193: Element fault = XMLTools.getChildElement("fault", null,
194: root);
195: RPCFault rpcFault = createRPCFault(fault);
196: mc = new RPCMethodResponse(rpcFault);
197: }
198:
199: } catch (Exception e) {
200: LOG.logError(e.getMessage(), e);
201: throw new RPCException(e.toString());
202: }
203:
204: return mc;
205: }
206:
207: /**
208: * @param par
209: * @return
210: * @throws RPCException
211: */
212: public static RPCMethodResponse createRPCMethodResponse(
213: RPCParameter[] par) {
214: RPCMethodResponse mc = null;
215: if (par != null) {
216: RPCParameter[] params = par;
217: mc = new RPCMethodResponse(params);
218: } else {
219: LOG
220: .logError("Fehler createRPCMEthodResponse in RPSFactory");
221: }
222: return mc;
223: }
224:
225: /**
226: * creates a <tt>RPCParameter</tt> from its XML representation
227: *
228: * @param param
229: * element containing a RPC param
230: * @return created <tt>RPCParameter</tt>
231: */
232: private static RPCParameter createRPCParam(Element param)
233: throws RPCException {
234:
235: RPCParameter parameter = null;
236: try {
237: Element value = XMLTools.getChildElement("value", null,
238: param);
239: Element child = XMLTools.getFirstChildElement(value);
240: Object o = null;
241: Class cl = null;
242: if (child.getNodeName().equals("struct")) {
243: o = createRPCStruct(child);
244: cl = RPCStruct.class;
245: } else if (child.getNodeName().equals("string")) {
246: o = XMLTools.getRequiredStringValue("string", null,
247: value);
248: cl = String.class;
249: } else if (child.getNodeName().equals("int")) {
250: double d = XMLTools.getRequiredNodeAsDouble(value,
251: "./int", nsContext);
252: o = new Integer((int) d);
253: cl = Integer.class;
254: } else if (child.getNodeName().equals("i4")) {
255: double d = XMLTools.getRequiredNodeAsDouble(value,
256: "./i4", nsContext);
257: o = new Integer((int) d);
258: cl = Integer.class;
259: } else if (child.getNodeName().equals("double")) {
260: double d = XMLTools.getRequiredNodeAsDouble(value,
261: "./double", nsContext);
262: o = new Double(d);
263: cl = Double.class;
264: } else if (child.getNodeName().equals("boolean")) {
265: o = Boolean.valueOf(child.getFirstChild()
266: .getNodeValue().equals("1"));
267: cl = Boolean.class;
268: } else if (child.getNodeName().equals("dateTime.iso8601")) {
269: String s = XMLTools.getRequiredStringValue(
270: "dateTime.iso8601", null, value);
271: o = TimeTools.createCalendar(s).getTime();
272: cl = Date.class;
273: } else if (child.getNodeName().equals("base64")) {
274: } else if (child.getNodeName().equals("array")) {
275: o = createArray(child);
276: cl = RPCParameter[].class;
277: }
278: parameter = new RPCParameter(cl, o);
279: } catch (Exception e) {
280: LOG.logError(e.getMessage(), e);
281: throw new RPCException(e.toString());
282: }
283:
284: return parameter;
285: }
286:
287: /**
288: * creates a RPC struture object from the passed <tt>Element</tt>
289: *
290: * @param struct
291: * element containing a RPC struct
292: * @return created <tt>RPCStruct</tt>
293: */
294: private static RPCStruct createRPCStruct(Element struct)
295: throws RPCException {
296:
297: RPCStruct structure = null;
298: try {
299: ElementList el = XMLTools.getChildElements(struct);
300: RPCMember[] members = null;
301: if (el != null) {
302: members = new RPCMember[el.getLength()];
303: for (int i = 0; i < el.getLength(); i++) {
304: members[i] = createRPCMember(el.item(i));
305: }
306: }
307: structure = new RPCStruct(members);
308: } catch (Exception e) {
309: LOG.logError(e.getMessage(), e);
310: throw new RPCException(e.toString());
311: }
312:
313: return structure;
314: }
315:
316: /**
317: * creates a RPC struture member object from the passed <tt>Element</tt>
318: *
319: * @param member
320: * element containing a RPC member
321: * @return created <tt>RPCMember</tt>
322: */
323: private static RPCMember createRPCMember(Element member)
324: throws RPCException {
325:
326: RPCMember mem = null;
327: try {
328: String name = XMLTools.getRequiredStringValue("name", null,
329: member);
330: Element value = XMLTools.getChildElement("value", null,
331: member);
332: Element child = XMLTools.getFirstChildElement(value);
333: Object o = null;
334: Class cl = null;
335: if (child.getNodeName().equals("struct")) {
336: o = createRPCStruct(child);
337: cl = RPCStruct.class;
338: } else if (child.getNodeName().equals("string")) {
339: o = XMLTools.getRequiredStringValue("string", null,
340: value);
341: cl = String.class;
342: } else if (child.getNodeName().equals("int")) {
343: double d = XMLTools.getRequiredNodeAsDouble(value,
344: "./int", nsContext);
345: o = new Integer((int) d);
346: cl = Integer.class;
347: } else if (child.getNodeName().equals("i4")) {
348: double d = XMLTools.getRequiredNodeAsDouble(value,
349: "./i4", nsContext);
350: o = new Integer((int) d);
351: cl = Integer.class;
352: } else if (child.getNodeName().equals("double")) {
353: double d = XMLTools.getRequiredNodeAsDouble(value,
354: "./double", nsContext);
355: o = new Double(d);
356: cl = Double.class;
357: } else if (child.getNodeName().equals("boolean")) {
358: o = Boolean.valueOf(child.getFirstChild()
359: .getNodeValue().equals("1"));
360: cl = Boolean.class;
361: } else if (child.getNodeName().equals("dateTime.iso8601")) {
362: String s = XMLTools.getRequiredStringValue(
363: "dateTime.iso8601", null, value);
364: o = TimeTools.createCalendar(s).getTime();
365: cl = Date.class;
366: } else if (child.getNodeName().equals("base64")) {
367: } else if (child.getNodeName().equals("array")) {
368: o = createArray(child);
369: cl = RPCParameter[].class;
370: }
371: mem = new RPCMember(cl, o, name);
372: } catch (Exception e) {
373: LOG.logError(e.getMessage(), e);
374: throw new RPCException(e.toString());
375: }
376:
377: return mem;
378: }
379:
380: /**
381: * creates an <tt>ArrayList</tt> object from the passed <tt>Element</tt>
382: *
383: * @param array
384: * @return created <tt>ArrayList</tt>
385: */
386: private static RPCParameter[] createArray(Element array)
387: throws RPCException {
388:
389: RPCParameter[] param = null;
390: try {
391: Element data = XMLTools
392: .getChildElement("data", null, array);
393: ElementList el = XMLTools.getChildElements(data);
394: if (el != null) {
395: param = new RPCParameter[el.getLength()];
396: for (int i = 0; i < el.getLength(); i++) {
397: Element child = XMLTools.getFirstChildElement(el
398: .item(i));
399: Object o = null;
400: Class cl = null;
401: if (child.getNodeName().equals("struct")) {
402: o = createRPCStruct(child);
403: cl = RPCStruct.class;
404: } else if (child.getNodeName().equals("string")) {
405: o = XMLTools.getRequiredStringValue("string",
406: null, el.item(i));
407: cl = String.class;
408: } else if (child.getNodeName().equals("int")) {
409: double d = XMLTools.getRequiredNodeAsDouble(el
410: .item(i), "./int", nsContext);
411: o = new Integer((int) d);
412: cl = Integer.class;
413: } else if (child.getNodeName().equals("i4")) {
414: double d = XMLTools.getRequiredNodeAsDouble(el
415: .item(i), "./i4", nsContext);
416: o = new Integer((int) d);
417: cl = Integer.class;
418: } else if (child.getNodeName().equals("double")) {
419: double d = XMLTools.getRequiredNodeAsDouble(el
420: .item(i), "./double", nsContext);
421: o = new Double(d);
422: cl = Double.class;
423: } else if (child.getNodeName().equals("boolean")) {
424: o = Boolean.valueOf(child.getFirstChild()
425: .getNodeValue().equals("1"));
426: cl = Boolean.class;
427: } else if (child.getNodeName().equals(
428: "dateTime.iso8601")) {
429: String s = XMLTools.getRequiredStringValue(
430: "dateTime.iso8601", null, el.item(i));
431: o = TimeTools.createCalendar(s).getTime();
432: cl = Date.class;
433: } else if (child.getNodeName().equals("base64")) {
434: } else if (child.getNodeName().equals("array")) {
435: o = createArray(child);
436: cl = RPCParameter[].class;
437: }
438: param[i] = new RPCParameter(cl, o);
439: }
440: }
441: } catch (Exception e) {
442: LOG.logError(e.getMessage(), e);
443: throw new RPCException(e.toString());
444: }
445:
446: return param;
447: }
448:
449: /**
450: * creates an <tt>RPCFault</tt> object from the passed <tt>Element</tt>
451: *
452: * @param fault
453: * fault element
454: * @return created <tt>RPCFault</tt>
455: */
456: private static RPCFault createRPCFault(Element fault)
457: throws RPCException {
458:
459: RPCFault rpcFault = null;
460: try {
461: Element value = XMLTools.getChildElement("value", null,
462: fault);
463: Element child = XMLTools.getFirstChildElement(value);
464: RPCStruct struct = createRPCStruct(child);
465: String s1 = null;
466: String s2 = null;
467: Object o = struct.getMember("faultCode").getValue();
468: if (o != null) {
469: s1 = o.toString();
470: }
471: o = struct.getMember("faultString").getValue();
472: if (o != null) {
473: s2 = o.toString();
474: }
475: rpcFault = new RPCFault(s1, s2);
476: } catch (Exception e) {
477: LOG.logError(e.getMessage(), e);
478: throw new RPCException(e.toString());
479: }
480:
481: return rpcFault;
482: }
483:
484: }
|