/src/mpv5/utils/xml/XMLRpcClient.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/ · Java · 124 lines · 46 code · 25 blank · 53 comment · 4 complexity · 0e9ea730870eebcb3ad19deff9e47a37 MD5 · raw file

  1. /*
  2. * This file is part of YaBS.
  3. *
  4. * YaBS is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * YaBS is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with YaBS. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package mpv5.utils.xml;
  18. //~--- non-JDK imports --------------------------------------------------------
  19. import mpv5.logging.Log;
  20. import org.apache.xmlrpc.XmlRpcException;
  21. import org.apache.xmlrpc.client.XmlRpcClient;
  22. import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
  23. //~--- JDK imports ------------------------------------------------------------
  24. import java.net.URL;
  25. import java.util.HashMap;
  26. /**
  27. * Our XML RPC Client implementation
  28. */
  29. public class XMLRpcClient extends XmlRpcClient {
  30. // private final XmlRpcCommonsTransportFactory transport;
  31. /**
  32. * Generate a new client for the given host
  33. * @param host
  34. * @param requCompression
  35. */
  36. public XMLRpcClient(URL host, boolean requCompression, String basicAuthUsername, String basicAuthPaswword) {
  37. super();
  38. XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
  39. config.setServerURL(host);
  40. config.setGzipRequesting(requCompression);
  41. config.setEnabledForExtensions(requCompression);
  42. if ((basicAuthUsername != null) && (basicAuthPaswword != null)) {
  43. Log.Debug(this, "Using basic authentication, with username: " + basicAuthUsername);
  44. config.setBasicUserName(basicAuthUsername);
  45. config.setBasicPassword(basicAuthPaswword);
  46. }
  47. // transport = new XmlRpcCommonsTransportFactory(this);
  48. // setTransportFactory(transport);
  49. setConfig(config);
  50. }
  51. /**
  52. * Invoke a remote get command
  53. * @param <T>
  54. * @param commandName The name of the remote procedure
  55. * @param params The parameters to the remote procedure
  56. * @param expectedReturnType The type you expect to get back
  57. * @return The response
  58. * @throws XmlRpcException If any error occurs
  59. */
  60. @SuppressWarnings("unchecked")
  61. public <T extends Object> T invokeGetCommand(String commandName, Object[] params, T expectedReturnType)
  62. throws XmlRpcException {
  63. Object data = execute(commandName, params);
  64. Log.Debug(this,
  65. "RPC call to '" + ((XmlRpcClientConfigImpl) getClientConfig()).getServerURL() + "#" + commandName
  66. + "' returned a: " + data.getClass().getSimpleName() + " [" + data + "]");
  67. return (T) data;
  68. }
  69. /**
  70. * Invoke a remote get command
  71. * @param commandName The name of the remote procedure
  72. * @param params The parameters to the remote procedure
  73. * @return The response
  74. * @throws XmlRpcException If any error occurs
  75. */
  76. @SuppressWarnings("unchecked")
  77. public HashMap<String, Object> invokeGetCommand(String commandName, Object[] params) throws XmlRpcException {
  78. HashMap<String, Object> data = (HashMap<String, Object>) execute(commandName, params);
  79. Log.Debug(this,
  80. "RPC call to '" + ((XmlRpcClientConfigImpl) getClientConfig()).getServerURL() + "#" + commandName
  81. + "' returned " + data.size() + " values.");
  82. return data;
  83. }
  84. /**
  85. * Invoke a remote set command
  86. * @param commandName
  87. * @param params
  88. * @return
  89. * @throws XmlRpcException
  90. */
  91. public Object invokeSetCommand(String commandName, Object[] params) throws XmlRpcException {
  92. Object result = execute(commandName, params);
  93. Log.Debug(this,
  94. "RPC call to '" + ((XmlRpcClientConfigImpl) getClientConfig()).getServerURL() + "#" + commandName
  95. + "' returned a: " + result.getClass().getSimpleName() + " [" + result + "]");
  96. return result;
  97. }
  98. }
  99. //~ Formatted by Jindent --- http://www.jindent.com