• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package de.timroes.axmlrpc;
2 
3 import de.timroes.axmlrpc.serializer.Serializer;
4 import de.timroes.axmlrpc.serializer.SerializerHandler;
5 import de.timroes.axmlrpc.xmlcreator.SimpleXMLCreator;
6 import de.timroes.axmlrpc.xmlcreator.XmlElement;
7 
8 /**
9  * A Call object represents a call of a remote methode.
10  * It contains the name of the method to be called and the parameters to use
11  * in this remote procedure call. To send it over the network the method getXML
12  * returns an xml representation according to the XML-RPC specification as a String.
13  *
14  * @author Tim Roes
15  */
16 public class Call {
17 
18 	private String method;
19 	private Object[] params;
20 	private final SerializerHandler serializerHandler;
21 
22 	/**
23 	 * Create a new method call with the given name and no parameters.
24 	 * @param serializerHandler You can inject an arbitrary one if you want to use your own transport protocol.
25 	 *     See the README (section "Using an arbitrary transport") for more info on this feature.
26 	 * @param method The method to be called.
27 	 */
Call(SerializerHandler serializerHandler, String method)28 	public Call(SerializerHandler serializerHandler, String method) {
29 		this(serializerHandler, method, null);
30 	}
31 
32 	/**
33 	 * Create a new method call with the given name and parameters.
34 	 * @param serializerHandler You can inject an arbitrary one if you want to use your own transport protocol.
35 	 *     See the README (section "Using an arbitrary transport") for more info on this feature.
36 	 * @param method The method to be called.
37 	 * @param params An array of parameters for the method.
38 	 */
Call(SerializerHandler serializerHandler, String method, Object[] params)39 	public Call(SerializerHandler serializerHandler, String method, Object[] params) {
40 		this.method = method;
41 		this.params = params;
42 		this.serializerHandler = serializerHandler;
43 	}
44 
45 	/**
46 	 * Return an xml representation of the method call as specified in
47 	 * http://www.xmlrpc.com/spec. If flags have been set in the XMLRPCClient
48 	 * the returning xml does not comply strict to the standard.
49 	 *
50 	 * @param debugMode This prints data on System.out to make it easy to debug
51 	 *
52 	 * @return The string of the xml representing this call.
53 	 * @throws XMLRPCException Will be thrown whenever the xml representation cannot
54 	 * 		be build without errors.
55 	 * @see XMLRPCClient
56 	 */
getXML(boolean debugMode)57 	public String getXML(boolean debugMode) throws XMLRPCException {
58 
59 		SimpleXMLCreator creator = new SimpleXMLCreator();
60 
61 		XmlElement methodCall = new XmlElement(XMLRPCClient.METHOD_CALL);
62 		creator.setRootElement(methodCall);
63 
64 		XmlElement methodName = new XmlElement(XMLRPCClient.METHOD_NAME);
65 		methodName.setContent(method);
66 		methodCall.addChildren(methodName);
67 
68 		if(params != null && params.length > 0) {
69 			XmlElement callParams = new XmlElement(XMLRPCClient.PARAMS);
70 			methodCall.addChildren(callParams);
71 
72 			for(Object o : this.params) {
73 				callParams.addChildren(getXMLParam(o));
74 			}
75 		}
76 
77 		String result = creator.toString();
78 
79 		if ( debugMode){
80 			System.out.println(result);
81 		}
82 
83 		return result;
84 	}
85 
86 	/**
87 	 * Generates the param xml tag for a specific parameter object.
88 	 *
89 	 * @param o The parameter object.
90 	 * @return The object serialized into an xml tag.
91 	 * @throws XMLRPCException Will be thrown if the serialization failed.
92 	 */
getXMLParam(Object o)93 	private XmlElement getXMLParam(Object o) throws XMLRPCException {
94 		XmlElement param = new XmlElement(XMLRPCClient.PARAM);
95 		XmlElement value = new XmlElement(XMLRPCClient.VALUE);
96 		param.addChildren(value);
97 		value.addChildren(serializerHandler.serialize(o));
98 		return param;
99 	}
100 
101 }
102