changeset 143:1a0c60efc627

add XmlRpc
author e085711
date Sat, 03 Sep 2011 15:24:41 +0900
parents 3d765eb3aa62
children c1d7d4fbcfb9
files src/test/XMLRPCTest.java src/test/XmlRpc.java
diffstat 2 files changed, 141 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/test/XMLRPCTest.java	Sat Sep 03 13:49:14 2011 +0900
+++ b/src/test/XMLRPCTest.java	Sat Sep 03 15:24:41 2011 +0900
@@ -65,9 +65,9 @@
 		return ret; // 成功なら1
 	}
 
-	public int edit(String title, String description, String blogId,
+	public String edit(String title, String description, String blogId,
 			String loginId, String pw, String url) throws IOException {
-		int ret = -1;
+		String result = "";
 
 		Map<String, Object> contentParam = buildContent(title, description);
 		List<Object> params = buildParam(blogId, loginId, pw, contentParam);
@@ -76,10 +76,11 @@
 		Object o = null;
 		try {
 			o = client.execute(META_WEBLOG_EDIT_POST, params);
+			result = o.toString();
 		} catch (XmlRpcException e) {
 			e.printStackTrace();
 		}
-		return ret; // 成功ならtrue
+		return result; // 成功ならtrue
 	}
 
 	
@@ -159,7 +160,7 @@
 	}
 
 	public static void main(String[] args) throws Exception {
-		String title = "xml-rpcを用いての投稿テスト @ editPost";
+		String title = "xml-rpcを用いての投稿テスト @ newPost";
 		String description = "metaWeblog.editPost<br>xml-rpc @ java";
 		String blogId = "77";
 		String loginId = "aotokage52";
@@ -168,10 +169,10 @@
 
 		XMLRPCTest logic = new XMLRPCTest();
 
-//		logic.post(title, description, blogId, loginId, pw, url);
-//		logic.edit(title, description, blogId, loginId, pw, url);
-		String result = logic.getBlogid(loginId, pw,url);
-		System.out.println("result = "+ result);
+//		int res = logic.post(title, description, blogId, loginId, pw, url);
+		String res = logic.edit(title, description, blogId, loginId, pw, url);
+//		String result = logic.getBlogid(loginId, pw,url);
+		System.out.println("result = "+ res);
 		
 	}
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/XmlRpc.java	Sat Sep 03 15:24:41 2011 +0900
@@ -0,0 +1,132 @@
+package test;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.xmlrpc.XmlRpcException;
+import org.apache.xmlrpc.client.XmlRpcClient;
+import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
+
+public class XmlRpc {
+	static final String META_WEBLOG_NEW_POST = "metaWeblog.newPost";
+	static final String META_WEBLOG_EDIT_POST = "metaWeblog.editPost";
+
+	XmlRpcClient client;
+
+	String loginId, pw, postUrl;
+	String blogId;
+	String title = ""; 
+	String description = "";
+	ArrayList<String> categories = new ArrayList<String>();
+	
+	XmlRpc(String blogId, String loginId, String pw, String url) throws MalformedURLException {
+		this.blogId = blogId;
+		this.loginId = loginId;
+		this.pw = pw;
+		postUrl = url;
+		client = createClient(url);
+	}
+	
+	XmlRpcClient createClient(String url) throws MalformedURLException {
+		XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
+		config.setServerURL(new URL(url.trim()));
+
+		XmlRpcClient c = new XmlRpcClient();
+		c.setConfig(config);
+		
+		return c; 
+	}
+
+	String editPost() {
+		return post(META_WEBLOG_EDIT_POST);
+	}
+	
+	String post(String API) {
+
+		String result = "";
+
+		Map<String, Object> contentParam = buildContent();
+		List<Object> params = buildParam(contentParam);
+
+		Object o = null;
+		try {
+			o = client.execute(META_WEBLOG_EDIT_POST, params);
+			result = o.toString();
+		} catch (XmlRpcException e) {
+			e.printStackTrace();
+		}
+		return result; 
+	}
+	
+	
+	protected void setTitle(String str) {
+		this.title = str;
+	}
+	protected void setDescription(String str) {
+		this.description = str;
+	}
+	protected void addDescription(String str) {
+		this.description += str;
+	}
+	protected void setCategories(String[] str) {
+		for(int i = 0; i < str.length; i ++)
+			categories.add(str[i]);
+	}	
+	protected void addCategories(String str) {
+		categories.add(str);
+	}
+
+	protected Map<String, Object> buildContent() {
+		Map<String, Object> content = new HashMap<String, Object>();
+
+		description = description.replaceAll("\n", "<br/>");
+		content.put("title", title);
+		content.put("categories", categories.toArray());
+		content.put("description", description);
+		content.put("dateCreated", "");
+		content.put("wp_slug", "");
+		
+		return content;
+	}
+	protected List<Object> buildParam(Map<String, Object> contentParam) {
+		List<Object> params = new ArrayList<Object>();
+
+		params.add(""); // appkey
+		params.add(blogId);
+		params.add(loginId);
+		params.add(pw);
+		params.add(contentParam);// content
+		params.add("1");// publish
+
+		return params;
+	}
+		
+	public static void main(String[] args) throws MalformedURLException {
+		if (args.length < 2) {
+			System.out.println("usage: XmlRpc username password ");
+			System.exit(0);
+		}
+		String username = args[0];
+		String password = args[1];
+		
+		String blogId = "77";
+		String url = "http://single.cr.ie.u-ryukyu.ac.jp/wordpress/xmlrpc.php";
+		
+		XmlRpc rpc = new XmlRpc(blogId, username, password, url );
+		rpc.setTitle("xml-rpcを用いての投稿 @ java");
+		rpc.setDescription("java からの投稿\nテスト");
+		rpc.addCategories("XML-RPC");
+
+		String res = rpc.editPost();
+		System.out.println(res);
+
+		
+	}
+	
+	
+	
+}