PageRenderTime 32ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/server/XMLRPCServer.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 145 lines | 86 code | 19 blank | 40 comment | 5 complexity | dc3cce09a009abf13acc0c4113b35148 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.server;
  18. import java.util.ArrayList;
  19. import mpv5.db.common.Context;
  20. import mpv5.globals.LocalSettings;
  21. import mpv5.logging.Log;
  22. import mpv5.usermanagement.MPSecurityManager;
  23. import org.apache.xmlrpc.XmlRpcException;
  24. import org.apache.xmlrpc.XmlRpcRequest;
  25. import org.apache.xmlrpc.common.XmlRpcHttpRequestConfig;
  26. import org.apache.xmlrpc.metadata.XmlRpcSystemImpl;
  27. import org.apache.xmlrpc.server.AbstractReflectiveHandlerMapping;
  28. import org.apache.xmlrpc.server.PropertyHandlerMapping;
  29. import org.apache.xmlrpc.server.XmlRpcErrorLogger;
  30. import org.apache.xmlrpc.server.XmlRpcHandlerMapping;
  31. import org.apache.xmlrpc.server.XmlRpcServer;
  32. import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
  33. import org.apache.xmlrpc.server.XmlRpcStreamServer;
  34. import org.apache.xmlrpc.webserver.WebServer;
  35. //Client-Side:
  36. //XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
  37. //config.setServerUrl("http://127.0.0.1:8080/xmlrpc");
  38. //config.setBasicUserName("adst-test");
  39. //config.setBasicPassword("adst-test#@!");
  40. //XmlRpcClient client = new XmlRpcClient();
  41. //client.setConfig(config);
  42. /**
  43. * This class implements an XML-RPC server which is bound to all importable {@link Context}s
  44. */
  45. public class XMLRPCServer {
  46. private static int port = 8484;
  47. private WebServer webServer;
  48. /**
  49. * @return the port
  50. */
  51. public static int getPort() {
  52. return port;
  53. }
  54. /**
  55. * @return the webServer
  56. */
  57. public WebServer getWebServer() {
  58. return webServer;
  59. }
  60. /**
  61. * Starts the server
  62. * @throws Exception
  63. */
  64. public XMLRPCServer() throws Exception {
  65. if (LocalSettings.hasProperty(LocalSettings.SERVER_PORT)) {
  66. port = Integer.valueOf(LocalSettings.getProperty(LocalSettings.SERVER_PORT));
  67. }
  68. webServer = new WebServer(getPort());
  69. XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();
  70. ((XmlRpcStreamServer) xmlRpcServer).setErrorLogger(new XmlRpcErrorLogger() {
  71. @Override
  72. public void log(java.lang.String pMessage) {
  73. Log.Debug(this, pMessage);
  74. }
  75. @Override
  76. public void log(java.lang.String pMessage, java.lang.Throwable pThrowable) {
  77. Log.Debug(this, pMessage);
  78. Log.Debug(pThrowable);
  79. }
  80. });
  81. PropertyHandlerMapping phm = new XPropertyHandlerMapping();
  82. // ArrayList<Context> cx = Context.getImportableContexts();
  83. // for (int i = 0; i < cx.size(); i++) {
  84. // Context context = cx.get(i);
  85. // }
  86. phm.addHandler("remote", XMLRPCHandler.class);
  87. xmlRpcServer.setHandlerMapping(phm);
  88. XmlRpcServerConfigImpl serverConfig =
  89. (XmlRpcServerConfigImpl) xmlRpcServer.getConfig();
  90. serverConfig.setEnabledForExtensions(true);
  91. serverConfig.setContentLengthOptional(false);
  92. PropertyHandlerMapping mapping = (PropertyHandlerMapping)((XmlRpcStreamServer) xmlRpcServer).getHandlerMapping();
  93. XmlRpcSystemImpl.addSystemHandler(mapping);
  94. webServer.start();
  95. Log.Debug(this, "XML RPC Server started! Listening port: " + port);
  96. if (Log.getLoglevel() == Log.LOGLEVEL_DEBUG) {
  97. Log.Debug(this, "XML RPC Server handler: ");
  98. String[] lm = phm.getListMethods();
  99. for (int i = 0; i < lm.length; i++) {
  100. String string = lm[i];
  101. Log.Debug(this, string);
  102. }
  103. }
  104. }
  105. }
  106. class XPropertyHandlerMapping extends PropertyHandlerMapping {
  107. private boolean isAuthenticated(String pUserName, String pPassword) {
  108. Log.Debug(this, "XML RPC Server isAuthenticated?: " + pUserName);
  109. return MPSecurityManager.checkAuth(pUserName, pPassword) != null;
  110. }
  111. protected XmlRpcHandlerMapping newXmlRpcHandlerMapping() throws XmlRpcException {
  112. PropertyHandlerMapping mapping = (PropertyHandlerMapping) this;
  113. AbstractReflectiveHandlerMapping.AuthenticationHandler handler =
  114. new AbstractReflectiveHandlerMapping.AuthenticationHandler() {
  115. @Override
  116. public boolean isAuthorized(XmlRpcRequest pRequest) {
  117. XmlRpcHttpRequestConfig config =
  118. (XmlRpcHttpRequestConfig) pRequest.getConfig();
  119. return isAuthenticated(config.getBasicUserName(),
  120. config.getBasicPassword());
  121. }
  122. };
  123. mapping.setAuthenticationHandler(handler);
  124. return mapping;
  125. }
  126. }