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

Commit

Permalink
添加 HttpRequestExecuter 专属异常处理类 fix #26
Browse files Browse the repository at this point in the history
新增 http 出异常了,输出 http 相关参数 fix #61
  • Loading branch information
venusdrogon committed Jun 18, 2019
1 parent 3303f1e commit 12a0c05
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,15 @@
*/
package com.feilong.net.httpclient4.builder;

import static com.feilong.core.Validator.isNullOrEmpty;
import static com.feilong.tools.slf4j.Slf4jUtil.format;
import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;

import java.io.IOException;
import java.net.SocketTimeoutException;

import javax.net.ssl.SSLException;

import org.apache.commons.lang3.Validate;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;

import com.feilong.json.jsonlib.JsonUtil;
import com.feilong.net.UncheckedHttpException;
import com.feilong.net.entity.ConnectionConfig;
import com.feilong.net.entity.HttpRequest;
Expand Down Expand Up @@ -66,47 +60,13 @@ public static HttpResponse execute(HttpRequest httpRequest,ConnectionConfig conn
//---------------------------------------------------------------
try{
return HttpRequestExecuter.execute(httpUriRequest, connectionConfig);
}catch (SocketTimeoutException e){ //链接超时
StringBuilder sb = new StringBuilder();
// sb.append("pls check:").append(lineSeparator());
// sb.append("1.wangluo");
// sb.append("2.can not ");
throw new UncheckedHttpException(buildMessage(sb.toString(), httpRequest, useConnectionConfig), e);
}catch (SSLException e){//https://www.cnblogs.com/sunny08/p/8038440.html
StringBuilder sb = new StringBuilder();
throw new UncheckedHttpException(buildMessage(sb.toString(), httpRequest, useConnectionConfig), e);
}catch (Exception e){
throw new UncheckedHttpException(buildMessage("", httpRequest, useConnectionConfig), e);
throw new UncheckedHttpException(HttpRequestExecuterExceptionMessageBuilder.build(httpRequest, useConnectionConfig, e), e);
}
}

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

/**
* Builds the message.
*
* @param handlerMessage
* the handler message
* @param httpRequest
* the http request
* @param useConnectionConfig
* the use connection config
* @return the string
* @since 1.11.4
*/
private static String buildMessage(String handlerMessage,HttpRequest httpRequest,ConnectionConfig useConnectionConfig){
if (isNullOrEmpty(handlerMessage)){
String pattern = "httpRequest:[{}],useConnectionConfig:[{}]";
return format(pattern, JsonUtil.format(httpRequest), JsonUtil.format(useConnectionConfig));
}

//---------------------------------------------------------------
String pattern = "[{}],httpRequest:[{}],useConnectionConfig:[{}]";
return format(pattern, handlerMessage, JsonUtil.format(httpRequest), JsonUtil.format(useConnectionConfig));
}

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

/**
* Execute.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* 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.builder;

import static com.feilong.core.Validator.isNullOrEmpty;
import static com.feilong.tools.slf4j.Slf4jUtil.format;
import static org.apache.commons.lang3.StringUtils.EMPTY;

import java.net.SocketTimeoutException;
import java.util.Map;

import javax.net.ssl.SSLException;

import com.feilong.core.lang.ClassUtil;
import com.feilong.core.lang.SystemUtil;
import com.feilong.core.util.MapUtil;
import com.feilong.json.jsonlib.JsonUtil;
import com.feilong.net.entity.ConnectionConfig;
import com.feilong.net.entity.HttpRequest;

/**
* http 请求执行异常 message 构造器.
*
* @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
* @since 1.13.3
*/
public class HttpRequestExecuterExceptionMessageBuilder{

/**
* Builds the message.
*
* @param httpRequest
* the http request
* @param useConnectionConfig
* the use connection config
*
* @return the string
* @since 1.11.4
* @since 1.13.3 rename from buildMessage
*/
static String build(HttpRequest httpRequest,ConnectionConfig useConnectionConfig,Exception e){
String handlerMessage = buildHandlerMessage(e);

String result = commonMessage(httpRequest, useConnectionConfig);
if (isNullOrEmpty(handlerMessage)){
return result;
}
//---------------------------------------------------------------
//带自定义头 handlerMessage
return format("[{}],{}", handlerMessage, result);
}

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

/**
* @param e
* @return
* @since 1.13.3
*/
//XXX 留坑
private static String buildHandlerMessage(Exception e){
if (ClassUtil.isInstance(e, SocketTimeoutException.class)){ //链接超时
//StringBuilder sb = new StringBuilder();
// sb.append("pls check:").append(lineSeparator());
// sb.append("1.wangluo");
// sb.append("2.can not ");
return EMPTY;
}

//https://www.cnblogs.com/sunny08/p/8038440.html
if (ClassUtil.isInstance(e, SSLException.class)){
return EMPTY;
}
return EMPTY;
}

/**
* Common message.
*
* @param httpRequest
* the http request
* @param useConnectionConfig
* the use connection config
* @return the string
*/
private static String commonMessage(HttpRequest httpRequest,ConnectionConfig useConnectionConfig){
Map<String, String> httpPropertiesMap = buildHttpPropertiesMap();

String pattern = "httpRequest:[{}],useConnectionConfig:[{}]";
String commonResult = format(pattern, JsonUtil.format(httpRequest), JsonUtil.format(useConnectionConfig));
if (isNullOrEmpty(httpPropertiesMap)){
return commonResult;
}
//带 httpPropertiesMap的
return format("{},http system properties:[{}]", commonResult, JsonUtil.format(httpPropertiesMap));
}

/**
* 构造和 http 相关的属性.
*
* @return the string
*/
private static Map<String, String> buildHttpPropertiesMap(){
Map<String, String> propertiesMap = SystemUtil.getPropertiesMap();
// "http.nonProxyHosts": "local|*.local|169.254/16|*.169.254/16",
// "java.protocol.handler.pkgs": "com.sun.net.ssl.internal.www.protocol",
// "javax.net.ssl.trustStore": "/Users/feilong/workspace/feilong/feilong-net/feilong-net-httpclient4/src/test/java/baozun/ssi/utilities/https/ami***uat.keystore",
// "javax.net.ssl.trustStorePassword": "ami***t",
// "socksNonProxyHosts": "local|*.local|169.254/16|*.169.254/16",
return MapUtil.getSubMap(
propertiesMap,
"http.nonProxyHosts",
"java.protocol.handler.pkgs",
"javax.net.ssl.trustStore",
"javax.net.ssl.trustStorePassword",
"socksNonProxyHosts");
}
}

0 comments on commit 12a0c05

Please sign in to comment.