This commit is contained in:
parent
adf50b186a
commit
688467887e
@ -10,7 +10,6 @@ import org.apache.http.config.SocketConfig;
|
|||||||
import org.apache.http.conn.HttpClientConnectionManager;
|
import org.apache.http.conn.HttpClientConnectionManager;
|
||||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||||
import org.apache.http.conn.ssl.X509HostnameVerifier;
|
import org.apache.http.conn.ssl.X509HostnameVerifier;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
import org.apache.http.impl.client.HttpClients;
|
import org.apache.http.impl.client.HttpClients;
|
||||||
import org.apache.http.protocol.HttpProcessor;
|
import org.apache.http.protocol.HttpProcessor;
|
||||||
@ -31,7 +30,6 @@ import com.foxinmy.weixin4j.util.Consts;
|
|||||||
*/
|
*/
|
||||||
public class HttpComponent4_2Factory extends HttpClientFactory {
|
public class HttpComponent4_2Factory extends HttpClientFactory {
|
||||||
|
|
||||||
private volatile CloseableHttpClient httpClient;
|
|
||||||
private final HttpClientBuilder clientBuilder;
|
private final HttpClientBuilder clientBuilder;
|
||||||
|
|
||||||
public HttpComponent4_2Factory() {
|
public HttpComponent4_2Factory() {
|
||||||
|
|||||||
@ -44,12 +44,7 @@ import com.foxinmy.weixin4j.util.StringUtil;
|
|||||||
public final class XmlStream {
|
public final class XmlStream {
|
||||||
private final static String ROOT_ELEMENT_XML = "xml";
|
private final static String ROOT_ELEMENT_XML = "xml";
|
||||||
private final static String XML_VERSION = "1.0";
|
private final static String XML_VERSION = "1.0";
|
||||||
private final static Map<Class<?>, Unmarshaller> messageUnmarshaller;
|
private final static ConcurrentHashMap<Class<?>, JAXBContext> jaxbContexts = new ConcurrentHashMap<Class<?>, JAXBContext>();;
|
||||||
private final static Map<Class<?>, Marshaller> messageMarshaller;
|
|
||||||
static {
|
|
||||||
messageUnmarshaller = new ConcurrentHashMap<Class<?>, Unmarshaller>();
|
|
||||||
messageMarshaller = new ConcurrentHashMap<Class<?>, Marshaller>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Xml2Bean
|
* Xml2Bean
|
||||||
@ -62,28 +57,28 @@ public final class XmlStream {
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static <T> T fromXML(InputStream content, Class<T> clazz) {
|
public static <T> T fromXML(InputStream content, Class<T> clazz) {
|
||||||
Unmarshaller unmarshaller = messageUnmarshaller.get(clazz);
|
JAXBContext jaxbContext = getJaxbContext(clazz);
|
||||||
if (unmarshaller == null) {
|
|
||||||
try {
|
|
||||||
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
|
|
||||||
unmarshaller = jaxbContext.createUnmarshaller();
|
|
||||||
messageUnmarshaller.put(clazz, unmarshaller);
|
|
||||||
} catch (JAXBException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
|
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
|
||||||
Source source = new StreamSource(content);
|
Source source = new StreamSource(content);
|
||||||
XmlRootElement rootElement = clazz.getAnnotation(XmlRootElement.class);
|
XmlRootElement rootElement = clazz
|
||||||
|
.getAnnotation(XmlRootElement.class);
|
||||||
if (rootElement == null
|
if (rootElement == null
|
||||||
|| rootElement.name().equals(XmlRootElement.class.getMethod("name").getDefaultValue().toString())) {
|
|| rootElement.name().equals(
|
||||||
JAXBElement<T> jaxbElement = unmarshaller.unmarshal(source, clazz);
|
XmlRootElement.class.getMethod("name")
|
||||||
|
.getDefaultValue().toString())) {
|
||||||
|
JAXBElement<T> jaxbElement = unmarshaller.unmarshal(source,
|
||||||
|
clazz);
|
||||||
return jaxbElement.getValue();
|
return jaxbElement.getValue();
|
||||||
} else {
|
} else {
|
||||||
return (T) unmarshaller.unmarshal(source);
|
return (T) unmarshaller.unmarshal(source);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (JAXBException ex) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException("Could not unmarshaller class [" + clazz
|
||||||
|
+ "]: " + ex.getMessage(), ex);
|
||||||
|
} catch (NoSuchMethodException ex) {
|
||||||
|
throw new RuntimeException("Could not unmarshaller class [" + clazz
|
||||||
|
+ "]: " + ex.getMessage(), ex);
|
||||||
} finally {
|
} finally {
|
||||||
if (content != null) {
|
if (content != null) {
|
||||||
try {
|
try {
|
||||||
@ -105,7 +100,8 @@ public final class XmlStream {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static <T> T fromXML(String content, Class<T> clazz) {
|
public static <T> T fromXML(String content, Class<T> clazz) {
|
||||||
return fromXML(new ByteArrayInputStream(content.getBytes(Consts.UTF_8)), clazz);
|
return fromXML(
|
||||||
|
new ByteArrayInputStream(content.getBytes(Consts.UTF_8)), clazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -118,7 +114,8 @@ public final class XmlStream {
|
|||||||
public static String map2xml(Map<String, String> map) {
|
public static String map2xml(Map<String, String> map) {
|
||||||
StringWriter sw = new StringWriter();
|
StringWriter sw = new StringWriter();
|
||||||
try {
|
try {
|
||||||
XMLStreamWriter xw = XMLOutputFactory.newInstance().createXMLStreamWriter(sw);
|
XMLStreamWriter xw = XMLOutputFactory.newInstance()
|
||||||
|
.createXMLStreamWriter(sw);
|
||||||
xw.writeStartDocument(Consts.UTF_8.name(), XML_VERSION);
|
xw.writeStartDocument(Consts.UTF_8.name(), XML_VERSION);
|
||||||
xw.writeStartElement(ROOT_ELEMENT_XML);
|
xw.writeStartElement(ROOT_ELEMENT_XML);
|
||||||
for (Entry<String, String> entry : map.entrySet()) {
|
for (Entry<String, String> entry : map.entrySet()) {
|
||||||
@ -154,7 +151,8 @@ public final class XmlStream {
|
|||||||
public static String map2xml(JSONObject json) {
|
public static String map2xml(JSONObject json) {
|
||||||
StringWriter sw = new StringWriter();
|
StringWriter sw = new StringWriter();
|
||||||
try {
|
try {
|
||||||
XMLStreamWriter xw = XMLOutputFactory.newInstance().createXMLStreamWriter(sw);
|
XMLStreamWriter xw = XMLOutputFactory.newInstance()
|
||||||
|
.createXMLStreamWriter(sw);
|
||||||
xw.writeStartDocument(Consts.UTF_8.name(), XML_VERSION);
|
xw.writeStartDocument(Consts.UTF_8.name(), XML_VERSION);
|
||||||
xw.writeStartElement(ROOT_ELEMENT_XML);
|
xw.writeStartElement(ROOT_ELEMENT_XML);
|
||||||
for (Entry<String, Object> entry : json.entrySet()) {
|
for (Entry<String, Object> entry : json.entrySet()) {
|
||||||
@ -191,7 +189,8 @@ public final class XmlStream {
|
|||||||
Map<String, String> map = new HashMap<String, String>();
|
Map<String, String> map = new HashMap<String, String>();
|
||||||
StringReader sr = new StringReader(content);
|
StringReader sr = new StringReader(content);
|
||||||
try {
|
try {
|
||||||
XMLStreamReader xr = XMLInputFactory.newInstance().createXMLStreamReader(sr);
|
XMLStreamReader xr = XMLInputFactory.newInstance()
|
||||||
|
.createXMLStreamReader(sr);
|
||||||
while (true) {
|
while (true) {
|
||||||
int event = xr.next();
|
int event = xr.next();
|
||||||
if (event == XMLStreamConstants.END_DOCUMENT) {
|
if (event == XMLStreamConstants.END_DOCUMENT) {
|
||||||
@ -244,27 +243,28 @@ public final class XmlStream {
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static <T> void toXML(T t, OutputStream os) {
|
public static <T> void toXML(T t, OutputStream os) {
|
||||||
Class<T> clazz = (Class<T>) t.getClass();
|
Class<T> clazz = (Class<T>) t.getClass();
|
||||||
Marshaller marshaller = messageMarshaller.get(clazz);
|
JAXBContext jaxbContext = getJaxbContext(clazz);
|
||||||
if (marshaller == null) {
|
|
||||||
try {
|
|
||||||
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
|
|
||||||
marshaller = jaxbContext.createMarshaller();
|
|
||||||
marshaller.setProperty(Marshaller.JAXB_ENCODING, Consts.UTF_8.name());
|
|
||||||
messageMarshaller.put(clazz, marshaller);
|
|
||||||
} catch (JAXBException e) {
|
|
||||||
throw new IllegalArgumentException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
XmlRootElement rootElement = clazz.getAnnotation(XmlRootElement.class);
|
Marshaller marshaller = jaxbContext.createMarshaller();
|
||||||
|
marshaller.setProperty(Marshaller.JAXB_ENCODING,
|
||||||
|
Consts.UTF_8.name());
|
||||||
|
XmlRootElement rootElement = clazz
|
||||||
|
.getAnnotation(XmlRootElement.class);
|
||||||
if (rootElement == null
|
if (rootElement == null
|
||||||
|| rootElement.name().equals(XmlRootElement.class.getMethod("name").getDefaultValue().toString())) {
|
|| rootElement.name().equals(
|
||||||
marshaller.marshal(new JAXBElement<T>(new QName(ROOT_ELEMENT_XML), clazz, t), os);
|
XmlRootElement.class.getMethod("name")
|
||||||
|
.getDefaultValue().toString())) {
|
||||||
|
marshaller.marshal(new JAXBElement<T>(new QName(
|
||||||
|
ROOT_ELEMENT_XML), clazz, t), os);
|
||||||
} else {
|
} else {
|
||||||
marshaller.marshal(t, os);
|
marshaller.marshal(t, os);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (JAXBException ex) {
|
||||||
throw new IllegalArgumentException(e);
|
throw new RuntimeException("Could not marshal class [" + clazz
|
||||||
|
+ "]: " + ex.getMessage(), ex);
|
||||||
|
} catch (NoSuchMethodException ex) {
|
||||||
|
throw new RuntimeException("Could not marshaller class [" + clazz
|
||||||
|
+ "]: " + ex.getMessage(), ex);
|
||||||
} finally {
|
} finally {
|
||||||
if (os != null) {
|
if (os != null) {
|
||||||
try {
|
try {
|
||||||
@ -275,4 +275,19 @@ public final class XmlStream {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static JAXBContext getJaxbContext(Class<?> clazz) {
|
||||||
|
JAXBContext jaxbContext = jaxbContexts.get(clazz);
|
||||||
|
if (jaxbContext == null) {
|
||||||
|
try {
|
||||||
|
jaxbContext = JAXBContext.newInstance(clazz);
|
||||||
|
jaxbContexts.putIfAbsent(clazz, jaxbContext);
|
||||||
|
} catch (JAXBException ex) {
|
||||||
|
throw new RuntimeException(
|
||||||
|
"Could not instantiate JAXBContext for class [" + clazz
|
||||||
|
+ "]: " + ex.getMessage(), ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return jaxbContext;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user