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