com.wudaosoft.weixinsdk.httpclient.XmlResponseHandler Maven / Gradle / Ivy
/* Copyright(c)2010-2014 WUDAOSOFT.COM
* Email:[email protected]
* QQ:275100589
*/
package com.wudaosoft.weixinsdk.httpclient;
import java.io.IOException;
import java.nio.charset.Charset;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.entity.ContentType;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
/**
*
* @author Changsoul.Wu
* @date 2014-3-29 下午9:07:23
*/
public class XmlResponseHandler implements ResponseHandler {
@Override
public Document handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {
int status = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
if (status < 200 || status >= 300) {
throw new ClientProtocolException("Unexpected response status: " + status);
}
if (entity == null) {
throw new ClientProtocolException("Response contains no content");
}
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
dbfac.setIgnoringElementContentWhitespace(true);
dbfac.setCoalescing(true);
dbfac.setIgnoringComments(true);
try {
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
ContentType contentType = ContentType.getOrDefault(entity);
// if (!contentType.equals(ContentType.APPLICATION_XML)) {
// throw new ClientProtocolException("Unexpected content type:" +
// contentType);
// }
Charset charset = contentType.getCharset();
if (charset == null) {
charset = Consts.UTF_8;
}
return docBuilder.parse(entity.getContent(), charset.name());
} catch (ParserConfigurationException ex) {
throw new IllegalStateException(ex);
} catch (SAXException ex) {
throw new ClientProtocolException("Malformed XML document", ex);
}
}
}