LOGGER级别优化 #86

This commit is contained in:
jinyu
2016-08-10 01:05:16 +08:00
parent ee836c3193
commit 2951d86408
8 changed files with 178 additions and 181 deletions
@@ -5,8 +5,6 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
import com.foxinmy.weixin4j.logging.InternalLogger;
import com.foxinmy.weixin4j.logging.InternalLoggerFactory;
/**
* API基础
@@ -20,9 +18,6 @@ import com.foxinmy.weixin4j.logging.InternalLoggerFactory;
*/
public abstract class BaseApi {
protected final InternalLogger logger = InternalLoggerFactory
.getInstance(getClass());
protected final WeixinRequestExecutor weixinExecutor;
public BaseApi() {
@@ -5,20 +5,17 @@ import java.util.Set;
import com.foxinmy.weixin4j.http.entity.FormUrlEntity;
import com.foxinmy.weixin4j.http.entity.HttpEntity;
import com.foxinmy.weixin4j.logging.InternalLogger;
import com.foxinmy.weixin4j.logging.InternalLoggerFactory;
public abstract class AbstractHttpClient implements HttpClient {
protected final InternalLogger logger = InternalLoggerFactory.getInstance(getClass());
@Override
public HttpResponse get(String url) throws HttpClientException {
return execute(HttpMethod.GET, url);
}
@Override
public HttpResponse get(String url, URLParameter... parameters) throws HttpClientException {
public HttpResponse get(String url, URLParameter... parameters)
throws HttpClientException {
return execute(HttpMethod.GET, url, parameters);
}
@@ -28,7 +25,8 @@ public abstract class AbstractHttpClient implements HttpClient {
}
@Override
public HttpHeaders head(String url, URLParameter... parameters) throws HttpClientException {
public HttpHeaders head(String url, URLParameter... parameters)
throws HttpClientException {
return execute(HttpMethod.HEAD, url, parameters).getHeaders();
}
@@ -38,7 +36,8 @@ public abstract class AbstractHttpClient implements HttpClient {
}
@Override
public HttpResponse post(String url, URLParameter... parameters) throws HttpClientException {
public HttpResponse post(String url, URLParameter... parameters)
throws HttpClientException {
HttpEntity entity = null;
if (parameters != null && parameters.length > 0) {
entity = new FormUrlEntity(Arrays.asList(parameters));
@@ -47,7 +46,8 @@ public abstract class AbstractHttpClient implements HttpClient {
}
@Override
public HttpResponse post(String url, HttpEntity entity) throws HttpClientException {
public HttpResponse post(String url, HttpEntity entity)
throws HttpClientException {
HttpRequest request = new HttpRequest(HttpMethod.POST, url);
request.setEntity(entity);
return execute(request);
@@ -59,7 +59,8 @@ public abstract class AbstractHttpClient implements HttpClient {
}
@Override
public void put(String url, URLParameter... parameters) throws HttpClientException {
public void put(String url, URLParameter... parameters)
throws HttpClientException {
execute(HttpMethod.PUT, url, parameters);
}
@@ -69,7 +70,8 @@ public abstract class AbstractHttpClient implements HttpClient {
}
@Override
public void delete(String url, URLParameter... parameters) throws HttpClientException {
public void delete(String url, URLParameter... parameters)
throws HttpClientException {
execute(HttpMethod.DELETE, url, parameters);
}
@@ -79,17 +81,20 @@ public abstract class AbstractHttpClient implements HttpClient {
}
@Override
public Set<HttpMethod> options(String url, URLParameter... parameters) throws HttpClientException {
HttpHeaders headers = execute(HttpMethod.OPTIONS, url, parameters).getHeaders();
public Set<HttpMethod> options(String url, URLParameter... parameters)
throws HttpClientException {
HttpHeaders headers = execute(HttpMethod.OPTIONS, url, parameters)
.getHeaders();
return headers.getAllow();
}
protected HttpResponse execute(HttpMethod method, String url) throws HttpClientException {
protected HttpResponse execute(HttpMethod method, String url)
throws HttpClientException {
return execute(new HttpRequest(method, url));
}
protected HttpResponse execute(HttpMethod method, String url, URLParameter... parameters)
throws HttpClientException {
protected HttpResponse execute(HttpMethod method, String url,
URLParameter... parameters) throws HttpClientException {
StringBuilder buf = new StringBuilder(url);
if (parameters != null && parameters.length > 0) {
if (url.indexOf("?") < 0) {
@@ -103,20 +108,25 @@ public abstract class AbstractHttpClient implements HttpClient {
}
protected boolean hasError(HttpStatus status) {
return (status.series() == HttpStatus.Series.CLIENT_ERROR || status.series() == HttpStatus.Series.SERVER_ERROR);
return (status.series() == HttpStatus.Series.CLIENT_ERROR || status
.series() == HttpStatus.Series.SERVER_ERROR);
}
protected void handleResponse(HttpResponse response) throws HttpClientException {
protected void handleResponse(HttpResponse response)
throws HttpClientException {
HttpStatus status = response.getStatus();
HttpHeaders headers = response.getHeaders();
MimeType resultType = MimeType.valueOf(headers.getFirst(HttpHeaders.CONTENT_TYPE));
MimeType resultType = MimeType.valueOf(headers
.getFirst(HttpHeaders.CONTENT_TYPE));
if (!MimeType.APPLICATION_JSON.includes(resultType) && hasError(status)) {
switch (status.series()) {
case CLIENT_ERROR:
case SERVER_ERROR:
throw new HttpClientException(String.format("%d %s", status.getStatusCode(), status.getStatusText()));
throw new HttpClientException(String.format("%d %s",
status.getStatusCode(), status.getStatusText()));
default:
throw new HttpClientException("Unknown status code [" + status + "]");
throw new HttpClientException("Unknown status code [" + status
+ "]");
}
}
}
@@ -105,8 +105,6 @@ public class SimpleHttpClient extends AbstractHttpClient implements HttpClient {
if (!headers.containsKey(HttpHeaders.USER_AGENT)) {
headers.set(HttpHeaders.USER_AGENT, "jdk/httpclient");
}
logger.debug("request >> " + request.getMethod() + " "
+ request.getURI().toString());
for (Entry<String, List<String>> header : headers.entrySet()) {
if (HttpHeaders.COOKIE.equalsIgnoreCase(header.getKey())) {
connection.setRequestProperty(header.getKey(),
@@ -117,8 +115,6 @@ public class SimpleHttpClient extends AbstractHttpClient implements HttpClient {
headerValue != null ? headerValue : "");
}
}
logger.debug("headers >> " + header.getKey() + ":"
+ StringUtil.join(header.getValue(), ';'));
}
// set inputstream
HttpEntity httpEntity = request.getEntity();
@@ -132,8 +128,6 @@ public class SimpleHttpClient extends AbstractHttpClient implements HttpClient {
connection.setRequestProperty(HttpHeaders.CONTENT_TYPE,
httpEntity.getContentType().toString());
}
logger.debug("entity >> " + httpEntity.getContentType() + "("
+ httpEntity.getContentLength() + "byte)");
}
// connect
connection.connect();
@@ -149,13 +143,6 @@ public class SimpleHttpClient extends AbstractHttpClient implements HttpClient {
.getErrorStream() : connection.getInputStream();
byte[] content = IOUtil.toByteArray(input);
response = new SimpleHttpResponse(connection, content);
logger.debug("response << " + response.getProtocol()
+ response.getStatus().toString());
for (Entry<String, List<String>> header : response.getHeaders()
.entrySet()) {
logger.debug("headers << " + header.getKey() + ":"
+ StringUtil.join(header.getValue(), ';'));
}
input.close();
handleResponse(response);
} catch (IOException e) {
@@ -25,6 +25,7 @@ import com.foxinmy.weixin4j.http.entity.HttpEntity;
import com.foxinmy.weixin4j.http.entity.StringEntity;
import com.foxinmy.weixin4j.http.factory.HttpClientFactory;
import com.foxinmy.weixin4j.http.message.XmlMessageConverter;
import com.foxinmy.weixin4j.logging.InternalLogLevel;
import com.foxinmy.weixin4j.logging.InternalLogger;
import com.foxinmy.weixin4j.logging.InternalLoggerFactory;
import com.foxinmy.weixin4j.util.Consts;
@@ -123,11 +124,13 @@ public class WeixinRequestExecutor {
* @return 微信响应
* @throws WeixinException
*/
protected WeixinResponse doRequest(HttpRequest request)
public WeixinResponse doRequest(HttpRequest request)
throws WeixinException {
try {
logger.info("weixin request >> " + request.getMethod() + " "
+ request.getURI().toString());
if (logger.isEnabled(InternalLogLevel.DEBUG)) {
logger.debug("weixin request >> " + request.getMethod() + " "
+ request.getURI().toString());
}
HttpResponse httpResponse = httpClient.execute(request);
WeixinResponse response = new WeixinResponse(httpResponse);
handleResponse(response);
@@ -165,12 +168,14 @@ public class WeixinRequestExecutor {
protected void handleResponse(WeixinResponse response)
throws WeixinException {
boolean hasStreamMimeType = hasStreamMimeType(response);
logger.info("weixin response << "
+ response.getProtocol()
+ response.getStatus()
+ ":"
+ (hasStreamMimeType ? response.getHeaders().getContentType()
: response.getAsString()));
if (logger.isEnabled(InternalLogLevel.DEBUG)) {
logger.debug("weixin response << "
+ response.getProtocol()
+ response.getStatus()
+ ":"
+ (hasStreamMimeType ? response.getHeaders()
.getContentType() : response.getAsString()));
}
if (hasStreamMimeType) {
return;
}