Change-Id: I680c87287ddd8b1d80f960c963fee3adcb46a425
This commit is contained in:
rui.zhang 2018-12-28 12:23:33 +08:00
parent 5b8e9ef5d8
commit f2f31cd60a
5 changed files with 56 additions and 7 deletions

View File

@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.xmlrpc.XmlRpcException; import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient; import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfig;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.chobit.wp.exception.WPClientException; import org.chobit.wp.exception.WPClientException;
import org.chobit.wp.model.request.PostFilter; import org.chobit.wp.model.request.PostFilter;
@ -32,9 +33,15 @@ class WPClient {
private XmlRpcClient client; private XmlRpcClient client;
WPClient(String xmlRpcUrl, boolean trustAll) throws MalformedURLException { WPClient(String xmlRpcUrl, boolean trustAll, int connectTimeout, int readTimeout) throws MalformedURLException {
XmlRpcClientConfigImpl c = new XmlRpcClientConfigImpl(); XmlRpcClientConfigImpl c = new XmlRpcClientConfigImpl();
c.setServerURL(new URL(xmlRpcUrl)); c.setServerURL(new URL(xmlRpcUrl));
if (connectTimeout > 0) {
c.setConnectionTimeout(config.getConnectTimeout());
}
if (readTimeout > 0) {
c.setReplyTimeout(config.getReadTimeout());
}
if (trustAll) { if (trustAll) {
acceptAndCertificate(); acceptAndCertificate();
} }
@ -44,7 +51,8 @@ class WPClient {
WPClient(WPConfig cfg) throws MalformedURLException { WPClient(WPConfig cfg) throws MalformedURLException {
this(cfg.getXmlRpcUrl(), cfg.isTrustAll()); this(cfg.getXmlRpcUrl(), cfg.isTrustAll(), cfg.getConnectTimeout(), cfg.getReadTimeout());
XmlRpcClientConfig c = this.client.getClientConfig();
this.config = cfg; this.config = cfg;
} }

View File

@ -15,6 +15,10 @@ public final class WPConfig {
private boolean trustAll; private boolean trustAll;
private int connectTimeout;
private int readTimeout;
WPConfig() { WPConfig() {
} }
@ -57,4 +61,20 @@ public final class WPConfig {
public void setTrustAll(boolean trustAll) { public void setTrustAll(boolean trustAll) {
this.trustAll = trustAll; this.trustAll = trustAll;
} }
public int getConnectTimeout() {
return connectTimeout;
}
public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}
public int getReadTimeout() {
return readTimeout;
}
public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
}
} }

View File

@ -37,9 +37,30 @@ public final class WPConfigBuilder {
} }
public WPConfigBuilder connectTimeout(int connectTimeout) {
if (connectTimeout < 0) {
throw new IllegalArgumentException("timeout cannot be negative");
}
this.config.setConnectTimeout(connectTimeout);
return this;
}
public WPConfigBuilder readTimeout(int readTimeout) {
if (readTimeout < 0) {
throw new IllegalArgumentException("timeout cannot be negative");
}
this.config.setReadTimeout(readTimeout);
return this;
}
public WPConfig build() { public WPConfig build() {
try { try {
WPClient client = new WPClient(config.getXmlRpcUrl(), config.isTrustAll()); WPClient client = new WPClient(config.getXmlRpcUrl(),
config.isTrustAll(),
config.getConnectTimeout(),
config.getReadTimeout());
List<UserBlog> list = client.getUsersBlogs(config.getUsername(), config.getPassword()); List<UserBlog> list = client.getUsersBlogs(config.getUsername(), config.getPassword());
if (null == list || list.isEmpty()) { if (null == list || list.isEmpty()) {
throw new WPClientException("Error in wp config, please check"); throw new WPClientException("Error in wp config, please check");

View File

@ -215,7 +215,7 @@ public class PostRequest extends Request {
} }
@Transient @Transient
public void setCategories(String... categories) { public void addCategories(String... categories) {
if (null == termsNames) { if (null == termsNames) {
setTermsNames(new HashMap<>(2)); setTermsNames(new HashMap<>(2));
} }
@ -225,7 +225,7 @@ public class PostRequest extends Request {
} }
@Transient @Transient
public void setTags(String... tags) { public void addTags(String... tags) {
if (null == termsNames) { if (null == termsNames) {
setTermsNames(new HashMap<>(2)); setTermsNames(new HashMap<>(2));
} }

View File

@ -73,8 +73,8 @@ public class WordPressTest {
PostRequest post = new PostRequest(); PostRequest post = new PostRequest();
post.setPostTitle("测试PostName"); post.setPostTitle("测试PostName");
post.setPostContent("这是一段测试文章内容"); post.setPostContent("这是一段测试文章内容");
post.setCategories("测试"); post.addCategories("测试");
post.setTags("a", "b", "c"); post.addTags("a", "b", "c");
post.setPostName("test-post-name"); post.setPostName("test-post-name");
int postId = wp.newPost(post); int postId = wp.newPost(post);
System.out.println(postId); System.out.println(postId);