145
|
1 package myVncProxy;
|
|
2
|
|
3 import java.net.MalformedURLException;
|
|
4 import java.net.URL;
|
|
5 import java.util.ArrayList;
|
|
6 import java.util.HashMap;
|
|
7 import java.util.List;
|
|
8 import java.util.Map;
|
|
9
|
|
10 import org.apache.xmlrpc.XmlRpcException;
|
|
11 import org.apache.xmlrpc.client.XmlRpcClient;
|
|
12 import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
|
|
13
|
|
14 public class XmlRpc {
|
|
15 static final String META_WEBLOG_NEW_POST = "metaWeblog.newPost";
|
|
16 static final String META_WEBLOG_EDIT_POST = "metaWeblog.editPost";
|
|
17 static final String META_WEBLOG_GET_POST = "metaWeblog.getPost";
|
|
18
|
|
19 XmlRpcClient client;
|
|
20 XmlRpcClientConfigImpl config;
|
|
21
|
|
22 String loginId, pw, postUrl;
|
|
23 String blogId;
|
|
24 String title = "";
|
|
25 String description = "";
|
|
26 ArrayList<String> categories = new ArrayList<String>();
|
|
27
|
|
28 XmlRpc(String blogId, String loginId, String pw, String url) throws MalformedURLException {
|
|
29 this.blogId = blogId;
|
|
30 this.loginId = loginId;
|
|
31 this.pw = pw;
|
|
32 postUrl = checkUrl(url);
|
|
33 client = createClient(postUrl);
|
|
34 }
|
|
35 String checkUrl(String url) {
|
|
36 String str;
|
|
37 if(url.endsWith("xmlrpc.php")){
|
|
38 return url;
|
|
39 } else if(url.endsWith("/")) {
|
|
40 str = url + "xmlrpc.php";
|
|
41 } else {
|
|
42 str = url + "/xmlrpc.php";
|
|
43 }
|
|
44 return str;
|
|
45 }
|
|
46 XmlRpcClient createClient(String url) throws MalformedURLException {
|
|
47 config = new XmlRpcClientConfigImpl();
|
|
48 config.setServerURL(new URL(url.trim()));
|
|
49
|
|
50 XmlRpcClient c = new XmlRpcClient();
|
|
51 c.setConfig(config);
|
|
52
|
|
53 return c;
|
|
54 }
|
|
55
|
|
56 String newPost() {
|
|
57 String result = post(META_WEBLOG_NEW_POST);
|
|
58 blogId = result;
|
|
59 return result;
|
|
60 }
|
|
61 String editPost() {
|
|
62 return post(META_WEBLOG_EDIT_POST);
|
|
63 }
|
|
64
|
|
65 String post(String API) {
|
|
66
|
|
67 String result = "";
|
|
68
|
|
69 Map<String, Object> contentParam = buildContent();
|
|
70 List<Object> params = buildParam(contentParam);
|
|
71
|
|
72 Object o = null;
|
|
73 try {
|
|
74 o = client.execute(API, params);
|
|
75 result = o.toString();
|
|
76 } catch (XmlRpcException e) {
|
|
77 e.printStackTrace();
|
|
78 }
|
|
79 return result;
|
|
80 }
|
|
81
|
|
82
|
|
83 protected void setTitle(String str) {
|
|
84 this.title = str;
|
|
85 }
|
|
86 protected void setDescription(String str) {
|
|
87 this.description = str;
|
|
88 }
|
|
89 protected void addDescription(String str) {
|
|
90 this.description += str;
|
|
91 }
|
|
92 protected void setCategories(String[] str) {
|
|
93 for(int i = 0; i < str.length; i ++)
|
|
94 categories.add(str[i]);
|
|
95 }
|
|
96 protected void addCategories(String str) {
|
|
97 categories.add(str);
|
|
98 }
|
|
99
|
|
100 protected Map<String, Object> buildContent() {
|
|
101 Map<String, Object> content = new HashMap<String, Object>();
|
|
102
|
|
103 description = description.replaceAll("\n", "<br/>");
|
|
104 content.put("title", title);
|
|
105 content.put("categories", categories.toArray());
|
|
106 content.put("description", description);
|
|
107 content.put("dateCreated", "");
|
|
108 content.put("wp_slug", "");
|
|
109
|
|
110 return content;
|
|
111 }
|
|
112
|
|
113 protected List<Object> buildParam(Map<String, Object> contentParam) {
|
|
114 List<Object> params = new ArrayList<Object>();
|
|
115
|
|
116 params.add(""); // appkey
|
|
117 params.add(blogId);
|
|
118 params.add(loginId);
|
|
119 params.add(pw);
|
|
120 params.add(contentParam);// content
|
|
121 params.add("1");// publish
|
|
122
|
|
123 return params;
|
|
124 }
|
|
125
|
|
126 String getUrl() {
|
|
127 List<Object> params = new ArrayList<Object>();
|
|
128
|
|
129 params.add(blogId);
|
|
130 params.add(loginId);
|
|
131 params.add(pw);
|
|
132
|
|
133 Object result = null;
|
|
134 String str = "false";
|
|
135 try {
|
|
136 result = weblog(params, META_WEBLOG_GET_POST);
|
|
137 Map<String, Object> map = (HashMap) result;
|
|
138 str = (String) map.get("link");
|
|
139 } catch (XmlRpcException e) {
|
|
140 e.printStackTrace();
|
|
141 }
|
|
142 return str;
|
|
143 }
|
|
144
|
|
145 Object weblog(List<Object> params, String API) throws XmlRpcException {
|
|
146 Object o = null;
|
|
147 o = client.execute(API, params);
|
|
148 return o;
|
|
149 }
|
|
150
|
|
151 public static void main(String[] args){
|
|
152 if (args.length < 2) {
|
|
153 System.out.println("usage: program username password ");
|
|
154 System.exit(0);
|
|
155 }
|
|
156 String username = args[0];
|
|
157 String password = args[1];
|
|
158
|
|
159 String blogId = "77";
|
|
160 // String url = "http://single.cr.ie.u-ryukyu.ac.jp/wordpress/xmlrpc.php";
|
|
161 // String url = "http://single.cr.ie.u-ryukyu.ac.jp/wordpress/";
|
|
162 String url = "http://single.cr.ie.u-ryukyu.ac.jp/wordpress";
|
|
163
|
|
164 XmlRpc rpc;
|
|
165 try {
|
|
166 rpc = new XmlRpc(blogId, username, password, url );
|
|
167 rpc.setTitle("xml-rpcを用いての投稿 @ java");
|
|
168 rpc.setDescription("java からの投稿\nテスト\n");
|
|
169 rpc.addDescription("addDescription()\n");
|
|
170 rpc.addCategories("XML-RPC");
|
|
171
|
|
172 String res = rpc.newPost();
|
|
173 // String res = rpc.editPost();
|
|
174 String res2 = rpc.getUrl();
|
|
175 System.out.println(res);
|
|
176 System.out.println(res2);
|
|
177 } catch (MalformedURLException e) {
|
|
178 e.printStackTrace();
|
|
179 }
|
|
180
|
|
181 }
|
|
182
|
|
183 }
|