官网没有给出CREATE、SEARCH、WRITE等XML-RPC接口的Java 调用示例,在此补充一下。
There is no examples on the official site for the XML-RPC operation interfaces for Java, so I posted my code here.
/**
* Created by kylin on 14-9-22.
*/
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfig;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CmpClient {
private static final String LOGIN_PATH = "/xmlrpc/common";
private static final String EXECUTION_PATH = "/xmlrpc/object";
private static final String CREATE = "create";
private static final String UNLINK = "unlink";
private static final Object WRITE = "write";
private static final String SEARCH = "search";
private static final Object READ = "read";
private String protocol = "http";
private String host = "127.0.0.1";
private int port = 8069;
private String instance = "20140803";
private String username = "admin";
private String password = "admin";
private int uid = -1;
public CmpClient(String host, int port, String instance, String login, String password) {
this.host = host;
this.port = port;
this.instance = instance;
this.username = login;
this.password = password;
}
public CmpClient(){
}
XmlRpcClient client = new XmlRpcClient();
private XmlRpcClientConfig getLoginConfig() throws MalformedURLException {
XmlRpcClientConfigImpl xmlRpcConfigLogin = new XmlRpcClientConfigImpl();
xmlRpcConfigLogin.setEnabledForExtensions(true);
URL loginURL = new URL(protocol, host, port, LOGIN_PATH);
xmlRpcConfigLogin.setServerURL(loginURL);
return xmlRpcConfigLogin;
}
private XmlRpcClientConfig getExecutionConfig() throws MalformedURLException {
XmlRpcClientConfigImpl xmlRpcConfigLogin = new XmlRpcClientConfigImpl();
xmlRpcConfigLogin.setEnabledForExtensions(true);
URL loginURL = new URL(protocol, host, port, EXECUTION_PATH);
xmlRpcConfigLogin.setServerURL(loginURL);
return xmlRpcConfigLogin;
}
public int connect() {
try {
XmlRpcClientConfig loginConfig = getLoginConfig();
client.setConfig(loginConfig);
Object[] params = new Object[]{instance, username, password};
Object id = client.execute("login", params);
if (id instanceof Integer) {
uid = (Integer) id;
return uid;
}
return -1;
} catch (XmlRpcException e) {
e.printStackTrace();
return -2;
} catch (Exception e) {
e.printStackTrace();
return -3;
}
}
private Object[] getSearchFilters(Map<String, Object> filterMap){
List<Object[]> filterList = new ArrayList<Object[]>();
for(Map.Entry<String, Object> entry : filterMap.entrySet()){
String key = entry.getKey();
Object value = entry.getValue();
Object[] filter = new Object[]{key, "=", value};
filterList.add(filter);
}
return filterList.toArray();
}
public Integer create(String object, Map<String, Object> valueMap) throws MalformedURLException, XmlRpcException {
XmlRpcClientConfig executionConfig = getExecutionConfig();
client.setConfig(executionConfig);
Object[] params = new Object[]{instance, uid, password, object, CREATE, valueMap};
Object result = client.execute("execute", params);
if(result instanceof Integer){
int id = (Integer)result;
return id;
}
return -1;
}
public Boolean unlink(String object, Integer[] ids) throws MalformedURLException, XmlRpcException {
XmlRpcClientConfig executionConfig = getExecutionConfig();
client.setConfig(executionConfig);
Object[] params = new Object[]{instance, uid, password, object, UNLINK, ids};
Object result = client.execute("execute", params);
if(result instanceof Boolean){
return (Boolean)result;
}
return null;
}
public Boolean write(String object, Integer[] ids, Map<String, Object> valueMap) throws MalformedURLException, XmlRpcException {
XmlRpcClientConfig executionConfig = getExecutionConfig();
client.setConfig(executionConfig);
Object[] params = new Object[]{instance, uid, password, object, WRITE, ids, valueMap, null};
Object result = client.execute("execute", params);
if(result instanceof Boolean){
return (Boolean)result;
}
return null;
}
public Integer[] search(String object, Map<String, Object> filterMap) throws MalformedURLException, XmlRpcException {
XmlRpcClientConfig executionConfig = getExecutionConfig();
client.setConfig(executionConfig);
Object[] filters = getSearchFilters(filterMap);
Object[] params = new Object[]{instance, uid, password, object, SEARCH, filters, 0, 0, null};
Object result = client.execute("execute", params);
if(result instanceof Object[]){
Object[] resultA = (Object[]) result;
Integer[] ids = new Integer[resultA.length];
for(int i = 0; i < resultA.length; i++ ){
Object o = resultA[i];
int id = (Integer)o;
ids[i] = id;
}
return ids;
}
return null;
}
public List<Map<String, Object>> read(String object, Integer[] ids, String[] fields) throws MalformedURLException, XmlRpcException {
XmlRpcClientConfig executionConfig = getExecutionConfig();
client.setConfig(executionConfig);
Object[] params = new Object[]{instance, uid, password, object, READ, ids, fields, null};
Object result = client.execute("execute", params);
List<Map<String, Object>> records = new ArrayList<Map<String, Object>>();
if(result instanceof Object[]) {
for (Object row : (Object[]) result) {
Map<String, Object> map = (HashMap<String, Object>) row;
records.add(map);
}
return records;
}
return null;
}
private static String getArrayString(Object[] array){
StringBuilder sb = new StringBuilder();
sb.append("[");
for(Object element : array){
if(sb.length() > 1){
sb.append(",\t");
}
sb.append(element);
}
sb.append("]");
return sb.toString();
}
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, MalformedURLException, XmlRpcException {
String host = "127.0.0.1";
int port = 8069;
String instance = "20140803";
String username = "admin";
String password = "admin";
String object = "test";
CmpClient client = new CmpClient(host, port, instance, username, password);
// Test connect()
int uid = client.connect();
if(uid < 0){
System.err.println("Bad username or password !");
return;
}else {
System.out.println("UID:\t" + uid);
}
// Test create()
Map createValues = new HashMap<String, Object>();
createValues.put("name", "test");
Integer id = client.create(object, createValues);
if(id < 0){
System.err.println("Creation Failed");
}else {
System.out.println("Created ID:\t" + id);
}
// Test unlink()
Integer[] unlinkIds = new Integer[]{1};
boolean unlinked = client.unlink(object, unlinkIds);
System.out.println("Unlinked:\t" + unlinked);
// Test write()
Integer[] writeIds = new Integer[]{1};
Map writeValues = new HashMap<String, Object>();
writeValues.put("name", "test1");
boolean writen = client.write(object, writeIds, writeValues);
System.out.println("Writen:\t" + writen);
// Test search()
Map filterMap = new HashMap<String, Object>();
filterMap.put("name", "test1");
Integer[] searchedIds = client.search(object, filterMap);
System.out.println("Searched IDS:\t" + getArrayString(searchedIds));
// Test read()
Integer[] readIds = new Integer[]{1};
String[] readFields = new String[]{"name", "description"};
List<Map<String, Object>> records = client.read(object, readIds, readFields);
System.out.println("Read:");
for(Map<String, Object> map : records){
System.out.println(map);
}
}
}
需要使用的jar包:xmlrpc-client-3.1.3.jar, xmlrpc-common-3.1.3.jar, ws-commons-util-1.0.2.jar
The jar files required:xmlrpc-client-3.1.3.jar, xmlrpc-common-3.1.3.jar, ws-commons-util-1.0.2.jar