PageRenderTime 43ms CodeModel.GetById 32ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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