clear code with findbugs plugin and change the version to 1.1

This commit is contained in:
jy.hu
2014-12-18 11:32:14 +08:00
parent 54963b283c
commit 5b8666d2f0
49 changed files with 419 additions and 231 deletions
@@ -8,7 +8,7 @@ import org.dom4j.DocumentException;
import com.foxinmy.weixin4j.model.BaseMsg;
import com.foxinmy.weixin4j.response.ResponseMessage;
import com.foxinmy.weixin4j.util.MessageUtil;
import com.foxinmy.weixin4j.xml.XStream;
import com.foxinmy.weixin4j.xml.XmlStream;
/**
* 继承的类需实现execute(M inMessage)
@@ -28,7 +28,7 @@ public abstract class AbstractAction<M extends BaseMsg> implements WeixinAction
public ResponseMessage execute(String msg) throws DocumentException {
BaseMsg message = MessageUtil.xml2msg(msg);
if (message == null) {
return execute(XStream.get(msg, getGenericType()));
return execute(XmlStream.get(msg, getGenericType()));
}
return execute((M) message);
}
@@ -7,7 +7,7 @@ import java.util.regex.Pattern;
import com.foxinmy.weixin4j.http.HttpRequest;
import com.foxinmy.weixin4j.xml.Map2ObjectConverter;
import com.foxinmy.weixin4j.xml.XStream;
import com.foxinmy.weixin4j.xml.XmlStream;
import com.thoughtworks.xstream.core.ClassLoaderReference;
import com.thoughtworks.xstream.mapper.DefaultMapper;
@@ -20,14 +20,13 @@ import com.thoughtworks.xstream.mapper.DefaultMapper;
* @see <a href="http://mp.weixin.qq.com/wiki/index.php">微信公众平台API文档</a>
* @see <a href="http://qydev.weixin.qq.com/wiki/index.php">微信企业号API文档</a>
*/
public class BaseApi {
public abstract class BaseApi {
protected final HttpRequest request = new HttpRequest();
protected final static XStream mapXstream = XStream.get();
protected static ResourceBundle weixinBundle;
protected final static XmlStream mapXstream = XmlStream.get();
static {
mapXstream.alias("xml", Map.class);
mapXstream.registerConverter(new Map2ObjectConverter(new DefaultMapper(
new ClassLoaderReference(XStream.class.getClassLoader()))));
new ClassLoaderReference(XmlStream.class.getClassLoader()))));
}
protected String map2xml(Map<?, ?> map) {
@@ -38,9 +37,9 @@ public class BaseApi {
protected Map<String, String> xml2map(String xml) {
return mapXstream.fromXML(xml, Map.class);
}
protected abstract ResourceBundle getWeixinBundle();
protected String getRequestUri(String key) {
String url = weixinBundle.getString(key);
String url = getWeixinBundle().getString(key);
Pattern p = Pattern.compile("(\\{[^\\}]*\\})");
Matcher m = p.matcher(url);
StringBuffer sb = new StringBuffer();
@@ -1,5 +1,6 @@
package com.foxinmy.weixin4j.http;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.lang3.StringUtils;
@@ -11,7 +12,7 @@ import org.dom4j.io.SAXReader;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.foxinmy.weixin4j.xml.XStream;
import com.foxinmy.weixin4j.xml.XmlStream;
public class Response {
@@ -57,13 +58,13 @@ public class Response {
if (isXmlResult) {
@SuppressWarnings("unchecked")
Class<T> clazz = (Class<T>) typeReference.getType();
return XStream.get(text, clazz);
return XmlStream.get(text, clazz);
}
return null;
}
public XmlResult getAsXmlResult() {
return XStream.get(text, XmlResult.class);
return XmlStream.get(text, XmlResult.class);
}
/**
@@ -79,36 +80,48 @@ public class Response {
result.setCode(code);
SAXReader reader = new SAXReader();
Document doc = null;
InputStream is = null;
try {
doc = reader.read(Response.class.getResourceAsStream("error.xml"));
is = Response.class.getResourceAsStream("error.xml");
doc = reader.read(is);
Node node = doc.getRootElement().selectSingleNode(
String.format("error/code[text()=%d]", code));
if (node != null) {
node = node.getParent();
String desc = null;
Node _node = node.selectSingleNode("desc");
if (_node != null) {
desc = _node.getStringValue();
}
String text = null;
_node = node.selectSingleNode("text");
if (_node != null) {
text = _node.getStringValue();
}
if (StringUtils.isBlank(desc) && StringUtils.isNotBlank(text)) {
desc = text;
}
if (StringUtils.isBlank(text) && StringUtils.isNotBlank(desc)) {
text = desc;
}
result.setDesc(desc);
result.setText(text);
} else {
result.setDesc("unknown error");
result.setText("未知错误");
}
} catch (DocumentException e) {
e.printStackTrace();
}
Node node = doc.getRootElement().selectSingleNode(
String.format("error/code[text()=%d]", code));
if (node != null) {
node = node.getParent();
String desc = null;
Node _node = node.selectSingleNode("desc");
if (_node != null) {
desc = _node.getStringValue();
}
String text = null;
_node = node.selectSingleNode("text");
if (_node != null) {
text = _node.getStringValue();
}
if (StringUtils.isBlank(desc) && StringUtils.isNotBlank(text)) {
desc = text;
}
if (StringUtils.isBlank(text) && StringUtils.isNotBlank(desc)) {
text = desc;
}
result.setDesc(desc);
result.setText(text);
} else {
result.setDesc("unknown error");
result.setText("未知错误");
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
;
}
}
}
return result;
}
@@ -134,11 +147,15 @@ public class Response {
}
public byte[] getBody() {
return body;
return (byte[]) body.clone();
}
/**
* May expose internal representation by incorporating reference to mutable
* object
*/
public void setBody(byte[] body) {
this.body = body;
this.body = (byte[]) body.clone();
}
public InputStream getStream() {
@@ -87,6 +87,21 @@ public class BaseMsg implements Serializable {
return agentId;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((agentId == null) ? 0 : agentId.hashCode());
result = prime * result + (int) (createTime ^ (createTime >>> 32));
result = prime * result
+ ((fromUserName == null) ? 0 : fromUserName.hashCode());
result = prime * result + (int) (msgId ^ (msgId >>> 32));
result = prime * result + ((msgType == null) ? 0 : msgType.hashCode());
result = prime * result
+ ((toUserName == null) ? 0 : toUserName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof BaseMsg) {
@@ -19,8 +19,9 @@ import com.thoughtworks.xstream.annotations.XStreamAlias;
*/
@XStreamAlias("app-token")
public class Token implements Serializable {
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = -7564855472419104084L;
@JSONField(name = "access_token")
private String accessToken;
@JSONField(name = "expires_in")
@@ -51,14 +52,6 @@ public class Token implements Serializable {
this.time = time;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Token) {
return accessToken.equals(((Token) obj).getAccessToken());
}
return false;
}
@Override
public String toString() {
return "Token [accessToken=" + accessToken + ", expiresIn=" + expiresIn
@@ -1,5 +1,7 @@
package com.foxinmy.weixin4j.msg.event.menu;
import java.io.Serializable;
import com.foxinmy.weixin4j.type.EventType;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@@ -30,7 +32,10 @@ public class MenuLocationEventMessage extends MenuEventMessage {
return locationInfo;
}
public static class LocationInfo {
public static class LocationInfo implements Serializable {
private static final long serialVersionUID = 4904181780216819965L;
@XStreamAlias("Location_X")
private double x; // 地理位置维度
@XStreamAlias("Location_Y")
@@ -1,5 +1,6 @@
package com.foxinmy.weixin4j.msg.event.menu;
import java.io.Serializable;
import java.util.List;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@@ -27,7 +28,10 @@ public class MenuPhotoEventMessage extends MenuEventMessage {
return pictureInfo;
}
public static class PictureInfo {
public static class PictureInfo implements Serializable {
private static final long serialVersionUID = -3361375879168233258L;
@XStreamAlias("Count")
private int count;
@XStreamAlias("PicList")
@@ -48,7 +52,10 @@ public class MenuPhotoEventMessage extends MenuEventMessage {
}
@XStreamAlias("item")
public static class PictureItem {
public static class PictureItem implements Serializable {
private static final long serialVersionUID = -7636697449096645590L;
@XStreamAlias("PicMd5Sum")
private String md5;
@@ -1,5 +1,7 @@
package com.foxinmy.weixin4j.msg.event.menu;
import java.io.Serializable;
import com.thoughtworks.xstream.annotations.XStreamAlias;
/**
@@ -25,7 +27,10 @@ public class MenuScanEventMessage extends MenuEventMessage {
return scanInfo;
}
public static class ScanInfo {
public static class ScanInfo implements Serializable {
private static final long serialVersionUID = 2237570238164900421L;
@XStreamAlias("ScanType")
private String type;
@XStreamAlias("ScanResult")
@@ -8,7 +8,7 @@ import com.foxinmy.weixin4j.msg.model.Base;
import com.foxinmy.weixin4j.msg.model.News;
import com.foxinmy.weixin4j.msg.model.Responseable;
import com.foxinmy.weixin4j.util.ClassUtil;
import com.foxinmy.weixin4j.xml.XStream;
import com.foxinmy.weixin4j.xml.XmlStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
/**
@@ -40,7 +40,7 @@ public class ResponseMessage extends BaseMsg {
private static final long serialVersionUID = 7761192742840031607L;
protected final static XStream xmlStream = XStream.get();
protected final static XmlStream xmlStream = XmlStream.get();
static {
Class<?>[] classes = ClassUtil.getClasses(Base.class.getPackage())
.toArray(new Class[0]);
@@ -58,7 +58,8 @@ public class ResponseMessage extends BaseMsg {
private final Base box;
public ResponseMessage(Base box) {
this(box, null);
super(box.getMediaType().name());
this.box = box;
}
public ResponseMessage(Base box, BaseMsg inMessage) {
@@ -15,7 +15,7 @@ import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.model.WeixinAccount;
import com.foxinmy.weixin4j.type.AccountType;
import com.foxinmy.weixin4j.util.ConfigUtil;
import com.foxinmy.weixin4j.xml.XStream;
import com.foxinmy.weixin4j.xml.XmlStream;
/**
* 基于文件保存的Token获取类
@@ -66,7 +66,7 @@ public class FileTokenHolder extends AbstractTokenHolder {
long now_time = ca.getTimeInMillis();
try {
if (token_file.exists()) {
token = XStream.get(new FileInputStream(token_file),
token = XmlStream.get(new FileInputStream(token_file),
Token.class);
long expire_time = token.getTime()
+ (token.getExpiresIn() * 1000) - 2;
@@ -78,7 +78,7 @@ public class FileTokenHolder extends AbstractTokenHolder {
token = response.getAsObject(new TypeReference<Token>() {
});
token.setTime(now_time);
XStream.to(token, new FileOutputStream(token_file));
XmlStream.to(token, new FileOutputStream(token_file));
} catch (IOException e) {
throw new WeixinException(e.getMessage());
}
@@ -23,11 +23,16 @@ public class ConfigUtil {
static {
weixinBundle = ResourceBundle.getBundle("weixin");
Set<String> keySet = weixinBundle.keySet();
File file = null;
for (String key : keySet) {
if (!key.endsWith("_path")) {
continue;
}
new File(getValue(key)).mkdirs();
file = new File(getValue(key));
if (!file.exists() && !file.mkdirs()) {
System.err.append(String.format("%s create fail.%n",
file.getAbsolutePath()));
}
}
}
@@ -92,6 +92,7 @@ public class FileUtil {
/**
* 获取文件类型
*
* @param file
* @return
*/
@@ -101,14 +102,16 @@ public class FileUtil {
try {
fis = new FileInputStream(file);
byte[] b = new byte[10];
fis.read(b, 0, b.length);
String fileCode = bytesToHexString(b).toLowerCase();
Iterator<String> keyIter = FILE_TYPE_MAP.keySet().iterator();
while (keyIter.hasNext()) {
String key = keyIter.next().toLowerCase();
if (key.startsWith(fileCode) || fileCode.startsWith(key)) {
fileType = FILE_TYPE_MAP.get(key);
break;
int t = fis.read(b, 0, b.length);
if (t > 0) {
String fileCode = bytesToHexString(b).toLowerCase();
Iterator<String> keyIter = FILE_TYPE_MAP.keySet().iterator();
while (keyIter.hasNext()) {
String key = keyIter.next().toLowerCase();
if (key.startsWith(fileCode) || fileCode.startsWith(key)) {
fileType = FILE_TYPE_MAP.get(key);
break;
}
}
}
} catch (IOException e) {
@@ -20,7 +20,7 @@ import com.foxinmy.weixin4j.model.BaseMsg;
import com.foxinmy.weixin4j.model.Consts;
import com.foxinmy.weixin4j.type.EventType;
import com.foxinmy.weixin4j.type.MessageType;
import com.foxinmy.weixin4j.xml.XStream;
import com.foxinmy.weixin4j.xml.XmlStream;
/**
* 消息工具类
@@ -205,7 +205,7 @@ public class MessageUtil {
messageClass = EventType.valueOf(type.toLowerCase())
.getEventClass();
}
return XStream.get(xmlMsg, messageClass);
return XmlStream.get(xmlMsg, messageClass);
}
/**
@@ -48,11 +48,11 @@ public class PKCS7Encoder {
// 获得补位所用的字符
byte target = (byte) (amountToPad & 0xFF);
char padChr = (char) target;
String tmp = new String();
StringBuilder tmp = new StringBuilder();
for (int index = 0; index < amountToPad; index++) {
tmp += padChr;
tmp.append(padChr);
}
return tmp.getBytes(Consts.UTF_8);
return tmp.toString().getBytes(Consts.UTF_8);
}
/**
@@ -10,9 +10,9 @@ import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.Xpp3Driver;
public class XStream extends com.thoughtworks.xstream.XStream {
public class XmlStream extends com.thoughtworks.xstream.XStream {
public XStream() {
public XmlStream() {
super(new Xpp3Driver() {
@@ -31,7 +31,7 @@ public class XStream extends com.thoughtworks.xstream.XStream {
});
}
public XStream(HierarchicalStreamDriver hierarchicalStreamDriver) {
public XmlStream(HierarchicalStreamDriver hierarchicalStreamDriver) {
super(hierarchicalStreamDriver);
}
@@ -45,29 +45,29 @@ public class XStream extends com.thoughtworks.xstream.XStream {
return (T) super.fromXML(inputStream);
}
public static XStream get() {
XStream xstream = new XStream();
public static XmlStream get() {
XmlStream xstream = new XmlStream();
xstream.ignoreUnknownElements();
xstream.autodetectAnnotations(true);
return xstream;
}
public static <T> T get(InputStream inputStream, Class<T> clazz) {
XStream xStream = get();
XmlStream xStream = get();
xStream.alias("xml", clazz);
xStream.processAnnotations(clazz);
return xStream.fromXML(inputStream, clazz);
}
public static <T> T get(String xml, Class<T> clazz) {
XStream xStream = get();
XmlStream xStream = get();
xStream.alias("xml", clazz);
xStream.processAnnotations(clazz);
return xStream.fromXML(xml, clazz);
}
public static String to(Object obj) {
XStream xStream = get();
XmlStream xStream = get();
Class<?> clazz = obj.getClass();
xStream.alias("xml", clazz);
xStream.processAnnotations(clazz);
@@ -75,7 +75,7 @@ public class XStream extends com.thoughtworks.xstream.XStream {
}
public static void to(Object obj, OutputStream out) {
XStream xStream = get();
XmlStream xStream = get();
Class<?> clazz = obj.getClass();
xStream.alias("xml", clazz);
xStream.processAnnotations(clazz);