# HG changeset patch # User e085711 # Date 1315025228 -32400 # Node ID fc77596d306449f1f089c5f676fe9ae5fd24267c # Parent 001f0b770f963fa6c6678ba2195a2f6ed4e3445b add XMLRPCTest.java diff -r 001f0b770f96 -r fc77596d3064 .classpath --- a/.classpath Tue Aug 30 19:59:02 2011 +0900 +++ b/.classpath Sat Sep 03 13:47:08 2011 +0900 @@ -3,5 +3,11 @@ + + + + + + diff -r 001f0b770f96 -r fc77596d3064 src/myVncProxy/MyRfbProto.java --- a/src/myVncProxy/MyRfbProto.java Tue Aug 30 19:59:02 2011 +0900 +++ b/src/myVncProxy/MyRfbProto.java Sat Sep 03 13:47:08 2011 +0900 @@ -26,7 +26,6 @@ import myVncProxy.MulticastQueue.Client; import java.util.concurrent.ExecutorService; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.zip.DataFormatException; import java.util.zip.Deflater; @@ -230,7 +229,7 @@ if (serverMajor < 3) { throw new IOException( - "RFB server does not support protocol version 3"); + "RFB server does not support protocol version 3"); } if (serverMinor == 998) { diff -r 001f0b770f96 -r fc77596d3064 src/test/XMLRPCTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/XMLRPCTest.java Sat Sep 03 13:47:08 2011 +0900 @@ -0,0 +1,177 @@ +package test; + +import java.io.IOException; +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 XMLRPCTest { + protected static final String META_WEBLOG_NEW_POST = "metaWeblog.newPost"; + protected static final String META_WEBLOG_EDIT_POST = "metaWeblog.editPost"; + + public String getBlogid(String username,String password, String url) throws Exception { + + XmlRpcClient client = createClient(url); + + List params = new ArrayList(); + params.add("77"); + params.add(username); + params.add(password); + +// Object result = client.execute("blogger.getUsersBlogs", params); + Object result = client.execute("metaWeblog.getPost", params); + + if (result instanceof Boolean) { + throw new Exception("can't get UsersBlogs"); + } + +// Object obj = ((Object[])result)[0]; +// Map map = (HashMap) obj; +// String blogid = (String) map.get("blogid"); +// return blogid; + + Map map = (HashMap) result; + String s = String.valueOf(map.get("description")); + return s; + } + + public int post(String title, String description, String blogId, + String loginId, String pw, String url) throws IOException { + int ret = -1; + + // 記事を作る + // 必要に応じてカスタムフィールドやslugの引数も追加。 + Map contentParam = buildContent(title, description); + // パラメータとして記事を渡す。 + List params = buildParam(blogId, loginId, pw, contentParam); + + // クライアント作る + XmlRpcClient client = createClient(url); + Object o = null; + try { + // 送信 + o = client.execute(META_WEBLOG_NEW_POST, params); + ret = Integer.parseInt(o.toString()); + } catch (XmlRpcException e) { + e.printStackTrace(); + } + return ret; // 成功なら1 + } + + public int edit(String title, String description, String blogId, + String loginId, String pw, String url) throws IOException { + int ret = -1; + + Map contentParam = buildContent(title, description); + List params = buildParam(blogId, loginId, pw, contentParam); + + XmlRpcClient client = createClient(url); + Object o = null; + try { + o = client.execute(META_WEBLOG_EDIT_POST, params); + } catch (XmlRpcException e) { + e.printStackTrace(); + } + return ret; // 成功ならtrue + } + + + /** + * XmlRpcClientをインスタンス化する + * + * @param postUrl + * @return + * @throws MalformedURLException + */ + protected XmlRpcClient createClient(String postUrl) + throws MalformedURLException { + XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); + + config.setServerURL(new URL(postUrl.trim())); + + XmlRpcClient client = new XmlRpcClient(); + + client.setConfig(config); + + return client; + } + + /** + * コンテンツ用のパラメーターを組み立てる + * + * @param wordsalad + * @return + */ + protected Map buildContent(String title, String description) { + Map content = new HashMap(); + // XMLRPC用に改行コードを書き換える + description = description.replaceAll("\n", "
"); + + content.put("title", title); + + String[] categories = new String[1]; + categories[0] = "XML-RPC"; + content.put("categories", categories); + + content.put("description", description); + + content.put("dateCreated", ""); + + content.put("wp_slug", ""); + + + // カスタムフィールドの設定 + /* + * Map[] customFields = new Map[1]; + * + * Map jimusho = new HashMap(); + * + * jimusho.put("key", "company"); + * + * jimusho.put("value", "じむしょ"); + * + * customFields[0] = jimusho; + * + * content.put("custom_fields", customFields); + */ + return content; + } + + protected List buildParam(String blogId, String loginId, String pw, + Map contentParam) { + List params = new ArrayList(); + + 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 Exception { + String title = "xml-rpcを用いての投稿テスト @ editPost"; + String description = "metaWeblog.editPost
xml-rpc @ java"; + String blogId = "77"; + String loginId = "aotokage52"; + String pw = "jasonkidd"; + String url = "http://single.cr.ie.u-ryukyu.ac.jp/wordpress/xmlrpc.php"; + + 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); + + } +}