PageRenderTime 30ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/src/mpv5/webshopinterface/WSConnectionClient.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 136 lines | 60 code | 29 blank | 47 comment | 1 complexity | f3b7e239cfdc2d384344389a990ec147 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  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.webshopinterface;
  18. //~--- non-JDK imports --------------------------------------------------------
  19. import mpv5.globals.Constants;
  20. import mpv5.logging.Log;
  21. import mpv5.ui.dialogs.Popup;
  22. import mpv5.utils.xml.XMLRpcClient;
  23. import org.apache.xmlrpc.XmlRpcException;
  24. //~--- JDK imports ------------------------------------------------------------
  25. import java.net.URL;
  26. import java.util.Arrays;
  27. import java.util.List;
  28. import java.util.Vector;
  29. /**
  30. * This class acts as connector to webshops using the WebShopAPI specified at
  31. * http://code.google.com/p/mp-rechnungs-und-kundenverwaltung/wiki/WebShopInterfaceSpecs
  32. */
  33. public class WSConnectionClient {
  34. private XMLRpcClient client;
  35. /**
  36. * Create a new connection to the specified url.<br/>
  37. * e.g. new URL("http://127.0.0.1:8080/xmlrpc")
  38. * @param host
  39. * @param requCompression
  40. * @throws NoCompatibleHostFoundException
  41. */
  42. public WSConnectionClient(final URL host, boolean requCompression, String user, String pw)
  43. throws NoCompatibleHostFoundException {
  44. if (!connect(host, requCompression, user, pw)) {
  45. throw new NoCompatibleHostFoundException(host);
  46. }
  47. }
  48. /**
  49. * Contains all known xml-rpc commands
  50. */
  51. public static enum COMMANDS {
  52. GETVERSION("getYWSIVersion"), GET_NEW_CONTACTS("getNewContacts"), GET_CONTACT("getContact"),
  53. GET_NEW_ADDRESSES("getNewAddresses"), GET_ADDRESSES("getAddresses"), GET_NEW_ORDERS("getNewOrders"),
  54. GET_ORDER_ROWS("getOrderRows"),
  55. // //////////////////////////////////////////////////////////////////////
  56. GET_NEW_SYSTEM_MESSAGES("getNewSystemMessages"), GET_CHANGED_CONTACTS("getUpdatedContacts"),
  57. GET_CHANGED_ADRESSES("getUpdatedAdresses"), GET_CHANGED_ORDERS("getUpdatedOrders"),
  58. GET_CHANGED_ORDER_ROWS("getUpdatedOrderRows"),
  59. // //////////////////////////////////////////////////////////////////////
  60. ADD_NEW_PRODUCT("addNewProduct"), ADD_NEW_CONTACT("addNewContact"), SET_ORDER_STATUS("setOrderStatus"),
  61. SET_DISABLED_CONTACT("setDisabledContact"), SET_DISABLED_PRODUCT("setDisabledProduct");
  62. String command;
  63. private COMMANDS(String command) {
  64. this.command = command;
  65. }
  66. /**
  67. * Overriden to return the actual command
  68. * @return The command specified in this enum
  69. */
  70. @Override
  71. public String toString() {
  72. return command;
  73. }
  74. }
  75. /**
  76. * @return the client
  77. */
  78. public XMLRpcClient getClient() {
  79. return client;
  80. }
  81. /**
  82. * Test the shop & runs system.listMethods
  83. * @return The shop impl version
  84. * @throws XmlRpcException If the test fails
  85. */
  86. @SuppressWarnings("unchecked")
  87. public String test() throws XmlRpcException {
  88. Object o = client.execute("system.listMethods", new Object[0]);
  89. Object[] methodList = (Object[]) o;
  90. Arrays.sort(methodList);
  91. Popup.notice(Arrays.asList(methodList), "Supported methods:\n");
  92. Object[] params = new Object[] { Constants.RELEASE_VERSION };
  93. Integer v = (Integer) getClient().execute(COMMANDS.GETVERSION.toString(), params);
  94. return "Server XML RPC Yabs Version : " + v;
  95. }
  96. private boolean connect(URL host, boolean requCompression, String user, String pw) {
  97. client = new XMLRpcClient(host, requCompression, user, pw);
  98. try {
  99. Log.Debug(this, test());
  100. return true;
  101. } catch (XmlRpcException ex) {
  102. Log.Debug(this, ex.getMessage());
  103. return false;
  104. }
  105. }
  106. }
  107. //~ Formatted by Jindent --- http://www.jindent.com