Skip to content
This repository has been archived by the owner on Aug 20, 2021. It is now read-only.

Commit

Permalink
HttpRequestExecuter.execute(HttpRequest, ConnectionConfig) 需要显示的abort
Browse files Browse the repository at this point in the history
httpUriRequest fix #80

HttpRequestExecuter.execute(HttpRequest, ConnectionConfig) 方法删除 ,直接调用
HttpRequestExecuter.execute(HttpUriRequest, ConnectionConfig) fix #81

HttpClientUtil 相关方法可以合并 fix #32
  • Loading branch information
venusdrogon committed Feb 25, 2020
1 parent 2669ff8 commit 0816aaa
Show file tree
Hide file tree
Showing 7 changed files with 358 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,17 @@
*/
package com.feilong.net.httpclient4;

import static com.feilong.core.bean.ConvertUtil.toMap;
import static com.feilong.core.date.DateUtil.now;
import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;

import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.Validate;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.feilong.json.jsonlib.JavaToJsonConfig;
import com.feilong.json.jsonlib.JsonUtil;
import com.feilong.json.jsonlib.processor.StringOverLengthJsonValueProcessor;

import com.feilong.net.HttpMethodType;
import com.feilong.net.entity.ConnectionConfig;
import com.feilong.net.entity.HttpRequest;
import com.feilong.net.httpclient4.builder.HttpRequestExecuter;
import com.feilong.net.httpclient4.builder.HttpResponseBuilder;
import com.feilong.net.httpclient4.builder.HttpResponseUtil;

import net.sf.json.processors.JsonValueProcessor;
import com.feilong.net.httpclient4.callback.HttpResponseResultCallback;
import com.feilong.net.httpclient4.callback.ResponseBodyAsStringResultCallback;
import com.feilong.net.httpclient4.callback.StatusCodeResultCallback;

/**
* 基于 HttpClient4 的工具类.
Expand All @@ -53,11 +40,6 @@
*/
public final class HttpClientUtil{

/** The Constant log. */
private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientUtil.class);

//---------------------------------------------------------------

/** Don't let anyone instantiate this class. */
private HttpClientUtil(){
//AssertionError不是必须的. 但它可以避免不小心在类的内部调用构造器. 保证该类在任何情况下都不会被实例化.
Expand Down Expand Up @@ -186,24 +168,7 @@ public static int getResponseStatusCode(String urlString,ConnectionConfig connec
*/
public static int getResponseStatusCode(HttpRequest httpRequest,ConnectionConfig connectionConfig){
Validate.notNull(httpRequest, "httpRequest can't be null!");

ConnectionConfig useConnectionConfig = defaultIfNull(connectionConfig, ConnectionConfig.INSTANCE);

HttpResponse httpResponse = HttpRequestExecuter.execute(httpRequest, useConnectionConfig);
StatusLine statusLine = httpResponse.getStatusLine();

int statusCode = statusLine.getStatusCode();

//---------------------------------------------------------------

if (LOGGER.isTraceEnabled()){
LOGGER.trace(
"httpRequest:[{}],connectionConfig:[{}],statusCode:[{}]",
JsonUtil.format(httpRequest),
JsonUtil.format(useConnectionConfig),
statusCode);
}
return statusCode;
return HttpRequestExecuter.execute(httpRequest, connectionConfig, StatusCodeResultCallback.INSTANCE);
}

//---------------------------getHttpResponse------------------------------------
Expand Down Expand Up @@ -470,26 +435,7 @@ public static com.feilong.net.entity.HttpResponse getHttpResponse(String urlStri
*/
public static com.feilong.net.entity.HttpResponse getHttpResponse(HttpRequest httpRequest,ConnectionConfig connectionConfig){
Validate.notNull(httpRequest, "httpRequest can't be null!");

ConnectionConfig useConnectionConfig = defaultIfNull(connectionConfig, ConnectionConfig.INSTANCE);

//---------------------------------------------------------------
Date beginDate = now();
HttpResponse httpResponse = HttpRequestExecuter.execute(httpRequest, useConnectionConfig);
com.feilong.net.entity.HttpResponse resultResponse = HttpResponseBuilder.build(beginDate, httpResponse);

//---------------------------------------------------------------

if (LOGGER.isInfoEnabled()){
String pattern = "request:[{}],useConnectionConfig:[{}],response:[{}]";
String response = JsonUtil.format(
resultResponse,
new JavaToJsonConfig(toMap("resultString", (JsonValueProcessor) new StringOverLengthJsonValueProcessor())));

LOGGER.info(pattern, JsonUtil.format(httpRequest), JsonUtil.format(useConnectionConfig), response);
}

return resultResponse;
return HttpRequestExecuter.execute(httpRequest, connectionConfig, HttpResponseResultCallback.INSTANCE);
}

//----------------------getResponseBodyAsString-----------------------------------------
Expand Down Expand Up @@ -901,7 +847,7 @@ public static String getResponseBodyAsString(HttpRequest httpRequest){
* @param requestParamMap
* the request param map
* @param httpMethod
* the method
* <span style="color:red">不区分大小写</span>, 比如get,Get,GET都可以,但是需要对应 {@link HttpMethodType}的支持的枚举值
* @return 如果 <code>uri</code> 是null,抛出 {@link NullPointerException}<br>
* 如果 <code>uri</code> 是blank,抛出 {@link IllegalArgumentException}<br>
* @since 1.11.0
Expand Down Expand Up @@ -979,21 +925,7 @@ public static String getResponseBodyAsString(String uri,Map<String, String> requ
*/
public static String getResponseBodyAsString(HttpRequest httpRequest,ConnectionConfig connectionConfig){
Validate.notNull(httpRequest, "httpRequest can't be null!");

ConnectionConfig useConnectionConfig = defaultIfNull(connectionConfig, ConnectionConfig.INSTANCE);

HttpResponse httpResponse = HttpRequestExecuter.execute(httpRequest, connectionConfig);
String resultString = HttpResponseUtil.getResultString(httpResponse);

//---------------------------------------------------------------
if (LOGGER.isInfoEnabled()){
LOGGER.info(
"request:[{}],useConnectionConfig:[{}],resultString:[{}]",
JsonUtil.format(httpRequest),
JsonUtil.format(useConnectionConfig),
StringOverLengthJsonValueProcessor.format(resultString, 1000));
}
return resultString;
return HttpRequestExecuter.execute(httpRequest, connectionConfig, ResponseBodyAsStringResultCallback.INSTANCE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
*/
package com.feilong.net.httpclient4.builder;

import static com.feilong.core.date.DateUtil.now;
import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;

import java.io.IOException;
import java.util.Date;

import org.apache.commons.lang3.Validate;
import org.apache.http.HttpResponse;
Expand All @@ -27,6 +28,7 @@
import com.feilong.net.UncheckedHttpException;
import com.feilong.net.entity.ConnectionConfig;
import com.feilong.net.entity.HttpRequest;
import com.feilong.net.httpclient4.callback.ResultCallback;

/**
* 专门发送请求 <code>httpUriRequest</code> .
Expand All @@ -44,45 +46,70 @@ private HttpRequestExecuter(){
}

//---------------------------------------------------------------

/**
* Execute.
*
* @param <T>
* the generic type
* @param httpRequest
* the http request
* @param connectionConfig
* the connection config
* @return the http response
* @param resultCallback
* the call
* @return 如果 <code>resultCallback</code> 是null,抛出 {@link NullPointerException}<br>
* @since 2.0.3
*/
public static HttpResponse execute(HttpRequest httpRequest,ConnectionConfig connectionConfig){
public static <T> T execute(HttpRequest httpRequest,ConnectionConfig connectionConfig,ResultCallback<T> resultCallback){
Validate.notNull(resultCallback, "resultCallback can't be null!");

//---------------------------------------------------------------

Date beginDate = now();
ConnectionConfig useConnectionConfig = defaultIfNull(connectionConfig, ConnectionConfig.INSTANCE);
HttpUriRequest httpUriRequest = HttpUriRequestBuilder.build(httpRequest, useConnectionConfig);
HttpResponse httpResponse = execute(httpRequest, httpUriRequest, useConnectionConfig);

//---------------------------------------------------------------
try{
return execute(httpUriRequest, useConnectionConfig);
}catch (Exception e){
throw new UncheckedHttpException(HttpRequestExecuterExceptionMessageBuilder.build(httpRequest, useConnectionConfig, e), e);
}
//回手掏
T t = resultCallback.on(httpRequest, httpUriRequest, httpResponse, useConnectionConfig, beginDate);
httpUriRequest.abort();
return t;
}

//---------------------------------------------------------------

/**
* Execute.
*
* @param httpRequest
* the http request
* @param httpUriRequest
* the http uri request
* @param connectionConfig
* @param useConnectionConfig
* the connection config
* @return 如果 <code>httpUriRequest</code> 是null,抛出 {@link NullPointerException}<br>
* @throws IOException
* Signals that an I/O exception has occurred.
* @since 1.11.0 change method Access Modifiers
* @since 2.0.3 change method Access Modifiers to private
*
*/
public static HttpResponse execute(HttpUriRequest httpUriRequest,ConnectionConfig connectionConfig) throws IOException{
private static HttpResponse execute(HttpRequest httpRequest,HttpUriRequest httpUriRequest,ConnectionConfig useConnectionConfig){
Validate.notNull(httpUriRequest, "httpUriRequest can't be null!");

HttpClient httpClient = HttpClientBuilder.build(connectionConfig);
return httpClient.execute(httpUriRequest);
//---------------------------------------------------------------
try{
HttpClient httpClient = HttpClientBuilder.build(useConnectionConfig);
return httpClient.execute(httpUriRequest);
}catch (Exception e){
httpUriRequest.abort();
String message = HttpRequestExecuterExceptionMessageBuilder.build(httpRequest, useConnectionConfig, e);
throw new UncheckedHttpException(message, e);
}finally{
//不能直接abort,否则内容查不出来
//参见 https://github.com/venusdrogon/feilong-net/issues/80
//httpUriRequest.abort()
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (C) 2008 feilong
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.feilong.net.httpclient4.callback;

import static com.feilong.core.bean.ConvertUtil.toMap;

import java.util.Date;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.feilong.json.jsonlib.JavaToJsonConfig;
import com.feilong.json.jsonlib.JsonUtil;
import com.feilong.json.jsonlib.processor.StringOverLengthJsonValueProcessor;
import com.feilong.net.entity.ConnectionConfig;
import com.feilong.net.entity.HttpRequest;
import com.feilong.net.httpclient4.builder.HttpResponseBuilder;

import net.sf.json.processors.JsonValueProcessor;

/**
* 用来解析 全数据 {@link com.feilong.net.entity.HttpResponse} 的 {@link ResultCallback}.
*
* @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
* @since 2.0.3
*/
public class HttpResponseResultCallback implements ResultCallback<com.feilong.net.entity.HttpResponse>{

/** The Constant LOGGER. */
private static final Logger LOGGER = LoggerFactory.getLogger(HttpResponseResultCallback.class);

/** Static instance. */
// the static instance works for all types
public static final HttpResponseResultCallback INSTANCE = new HttpResponseResultCallback();

//---------------------------------------------------------------

/**
* On.
*
* @param httpRequest
* the http request
* @param httpUriRequest
* the http uri request
* @param httpResponse
* the http response
* @param useConnectionConfig
* the use connection config
* @param beginDate
* the begin date
* @return the com.feilong.net.entity. http response
*/
@Override
public com.feilong.net.entity.HttpResponse on(
HttpRequest httpRequest,
HttpUriRequest httpUriRequest,
HttpResponse httpResponse,
ConnectionConfig useConnectionConfig,
Date beginDate){

com.feilong.net.entity.HttpResponse resultResponse = HttpResponseBuilder.build(beginDate, httpResponse);

//---------------------------------------------------------------
if (LOGGER.isInfoEnabled()){
String pattern = "request:[{}],useConnectionConfig:[{}],response:[{}]";
String response = JsonUtil.format(
resultResponse,
new JavaToJsonConfig(toMap("resultString", (JsonValueProcessor) new StringOverLengthJsonValueProcessor())));

LOGGER.info(pattern, JsonUtil.format(httpRequest), JsonUtil.format(useConnectionConfig), response);
}

return resultResponse;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (C) 2008 feilong
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.feilong.net.httpclient4.callback;

import java.util.Date;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.feilong.json.jsonlib.JsonUtil;
import com.feilong.json.jsonlib.processor.StringOverLengthJsonValueProcessor;
import com.feilong.net.entity.ConnectionConfig;
import com.feilong.net.entity.HttpRequest;
import com.feilong.net.httpclient4.builder.HttpResponseUtil;

/**
* 通常用来解析接口返回的字符串 的 {@link ResultCallback}.
*
* @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
* @since 2.0.3
*/
public class ResponseBodyAsStringResultCallback implements ResultCallback<String>{

private static final Logger LOGGER = LoggerFactory.getLogger(ResponseBodyAsStringResultCallback.class);

/** Static instance. */
// the static instance works for all types
public static final ResponseBodyAsStringResultCallback INSTANCE = new ResponseBodyAsStringResultCallback();

//---------------------------------------------------------------

@Override
public String on(
HttpRequest httpRequest,
HttpUriRequest httpUriRequest,
HttpResponse httpResponse,
ConnectionConfig useConnectionConfig,
Date beginDate){
String resultString = HttpResponseUtil.getResultString(httpResponse);
//---------------------------------------------------------------
if (LOGGER.isInfoEnabled()){
LOGGER.info(
"request:[{}],useConnectionConfig:[{}],resultString:[{}]",
JsonUtil.format(httpRequest),
JsonUtil.format(useConnectionConfig),
StringOverLengthJsonValueProcessor.format(resultString, 1000));
}
return resultString;
}

}
Loading

0 comments on commit 0816aaa

Please sign in to comment.