diff --git a/core.sdk/pom.xml b/core.sdk/pom.xml index e3071fe..7c2a30d 100644 --- a/core.sdk/pom.xml +++ b/core.sdk/pom.xml @@ -7,4 +7,26 @@ core.sdk jar + + + + 4.5.3 + + + + + + org.apache.httpcomponents + httpclient + ${httpclient.version} + + + + org.apache.httpcomponents + httpmime + ${httpclient.version} + + + + \ No newline at end of file diff --git a/core.sdk/src/main/java/com/lyms/util/HttpUtils.java b/core.sdk/src/main/java/com/lyms/util/HttpUtils.java index 27a087f..e90ceda 100644 --- a/core.sdk/src/main/java/com/lyms/util/HttpUtils.java +++ b/core.sdk/src/main/java/com/lyms/util/HttpUtils.java @@ -31,6 +31,8 @@ import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.lyms.util.remote.HttpRemote; + /** *

@@ -47,6 +49,12 @@ public class HttpUtils { /** + * http远程调用 + */ + public static HttpRemote HTTP = HttpRemote.build(); + + + /** * * 允许 JS 跨域设置 * @@ -404,4 +412,6 @@ public class HttpUtils { } } } + + } diff --git a/core.sdk/src/main/java/com/lyms/util/remote/AbsRemote.java b/core.sdk/src/main/java/com/lyms/util/remote/AbsRemote.java new file mode 100644 index 0000000..07d470c --- /dev/null +++ b/core.sdk/src/main/java/com/lyms/util/remote/AbsRemote.java @@ -0,0 +1,152 @@ +package com.lyms.util.remote; + +import java.io.IOException; +import java.util.Map; +import java.util.Map.Entry; + +import org.apache.commons.collections.MapUtils; +import org.apache.http.HttpEntity; +import org.apache.http.ParseException; +import org.apache.http.config.ConnectionConfig; +import org.apache.http.config.RegistryBuilder; +import org.apache.http.config.SocketConfig; +import org.apache.http.conn.socket.ConnectionSocketFactory; +import org.apache.http.conn.socket.PlainConnectionSocketFactory; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.apache.http.protocol.HTTP; +import org.apache.http.util.EntityUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.lyms.util.CharsetUtils; + +public abstract class AbsRemote { + + public Logger LOG = LoggerFactory.getLogger(getClass()); + + public static PoolingHttpClientConnectionManager connectionManager; + + /** + * 连接 + */ + public static CloseableHttpClient httpclient = null; + + /** + * 最大连接数 + */ + public final static int MAX_TOTAL_CONNECTIONS = 800; + /** + * 获取连接的最大等待时间 + */ + public final static int WAIT_TIMEOUT = 60000; + /** + * 每个路由最大连接数 + */ + public final static int MAX_ROUTE_CONNECTIONS = 250; + /** + * 连接超时时间 + */ + public final static int CONNECT_TIMEOUT = 5000; + /** + * 读取超时时间 + */ + public final static int READ_TIMEOUT = 5000; + /** + * 请求缓存 + */ + public final static int BUFFER_SIZE = 4096; + /** + * socket超时时间 + */ + public final static int SO_TIMEOUT = 3000; + + static { + SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setSoTimeout(CONNECT_TIMEOUT).setTcpNoDelay(false).setSoReuseAddress(true).setSoKeepAlive(true).build(); + + ConnectionConfig connectionConfig = ConnectionConfig.custom().setBufferSize(BUFFER_SIZE).build(); + + connectionManager = new PoolingHttpClientConnectionManager(RegistryBuilder. create() + .register("http", PlainConnectionSocketFactory.getSocketFactory()) + .register("https", SSLConnectionSocketFactory.getSocketFactory()) + .build()); + + connectionManager.setDefaultSocketConfig(socketConfig); + connectionManager.setDefaultConnectionConfig(connectionConfig); + connectionManager.setMaxTotal(MAX_TOTAL_CONNECTIONS); + connectionManager.setDefaultMaxPerRoute(MAX_ROUTE_CONNECTIONS); + httpclient = HttpClients.custom().addInterceptorFirst(new GZIPRequestInterceptor()).addInterceptorFirst(new BrowserRequestInterceptor()) + .addInterceptorFirst(new GZIPResponseInterceptor()).setConnectionManager(connectionManager).build(); + } + + + /** + * 返回UTF8编码字符串 + * @param entity + * @return + * @throws IOException + * @throws ParseException + */ + public String responseUTF8(HttpEntity entity) throws ParseException, IOException { + + if(entity == null) + return null; + + return EntityUtils.toString(entity, CharsetUtils.UTF_8); + } + + /** + * post 请求参数构建 + * @param params + * @return + */ + public HttpEntity postEntity(Map params){ + MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create(); + ContentType content = ContentType.create(HTTP.PLAIN_TEXT_TYPE, CharsetUtils.UTF_8); + for (Entry entry : params.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + reqEntity.addTextBody(key, value, content); + } + return reqEntity.build(); + } + + /** + * 拼接 get 请求参数 + *

忽略请求地址携带参数的情况,例如 http://www.baidu.com?k1=123 + * @param params + * @return + */ + public String handlerReqParams(String url,Map params) { + if(MapUtils.isEmpty(params))return url; + return url+handler(params); + } + + /** + * 参数拼接 + * @param params + * @return + */ + private String handler(Map params) { + StringBuilder sb = new StringBuilder(); + if(MapUtils.isNotEmpty(params)) + { + int tag = 0; + for(Entry entry : params.entrySet()) + { + String key = entry.getKey(),value = entry.getValue(); + if(tag==0)sb.append("?"); + if(tag>0)sb.append("&"); + sb.append(key).append("=").append(value); + tag ++; + } + } + return sb.toString(); + } + + +} diff --git a/core.sdk/src/main/java/com/lyms/util/remote/BrowserRequestInterceptor.java b/core.sdk/src/main/java/com/lyms/util/remote/BrowserRequestInterceptor.java new file mode 100644 index 0000000..2ff9d56 --- /dev/null +++ b/core.sdk/src/main/java/com/lyms/util/remote/BrowserRequestInterceptor.java @@ -0,0 +1,26 @@ +package com.lyms.util.remote; + +import java.io.IOException; + +import org.apache.http.HttpException; +import org.apache.http.HttpRequest; +import org.apache.http.HttpRequestInterceptor; +import org.apache.http.protocol.HttpContext; + +/** + * + * 封装header + * + */ +public class BrowserRequestInterceptor implements HttpRequestInterceptor { + @Override + public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { +// if (!request.containsHeader("Accept")) { +// request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); +// request.addHeader("Accept-Language", "zh-cn,en-us;q=0.8,zh;q=0.5,en;q=0.3"); +// request.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0"); + request.addHeader("User-Agent","Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"); +// } + } + +} diff --git a/core.sdk/src/main/java/com/lyms/util/remote/GZIPRequestInterceptor.java b/core.sdk/src/main/java/com/lyms/util/remote/GZIPRequestInterceptor.java new file mode 100644 index 0000000..49c4421 --- /dev/null +++ b/core.sdk/src/main/java/com/lyms/util/remote/GZIPRequestInterceptor.java @@ -0,0 +1,23 @@ +package com.lyms.util.remote; + + +import java.io.IOException; + +import org.apache.http.HttpException; +import org.apache.http.HttpRequest; +import org.apache.http.HttpRequestInterceptor; +import org.apache.http.protocol.HttpContext; + +/** + * header压缩 + */ +public class GZIPRequestInterceptor implements HttpRequestInterceptor { + + @Override + public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { + if (!request.containsHeader("Accept-Encoding")) { + request.addHeader("Accept-Encoding", "gzip"); + } + } + +} diff --git a/core.sdk/src/main/java/com/lyms/util/remote/GZIPResponseInterceptor.java b/core.sdk/src/main/java/com/lyms/util/remote/GZIPResponseInterceptor.java new file mode 100644 index 0000000..537de8c --- /dev/null +++ b/core.sdk/src/main/java/com/lyms/util/remote/GZIPResponseInterceptor.java @@ -0,0 +1,36 @@ +package com.lyms.util.remote; + +import java.io.IOException; + +import org.apache.http.Header; +import org.apache.http.HeaderElement; +import org.apache.http.HttpEntity; +import org.apache.http.HttpException; +import org.apache.http.HttpResponse; +import org.apache.http.HttpResponseInterceptor; +import org.apache.http.client.entity.GzipDecompressingEntity; +import org.apache.http.protocol.HttpContext; + +/** + * response解压 + */ +public class GZIPResponseInterceptor implements HttpResponseInterceptor { + + public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { + + HttpEntity entity = response.getEntity(); + if (entity != null) { + Header ceheader = entity.getContentEncoding(); + if (ceheader != null) { + HeaderElement[] codecs = ceheader.getElements(); + for (int i = 0; i < codecs.length; i++) { + if (codecs[i].getName().equalsIgnoreCase("gzip")) { + response.setEntity(new GzipDecompressingEntity(response.getEntity())); + return; + } + } + } + } + } + +} diff --git a/core.sdk/src/main/java/com/lyms/util/remote/HttpRemote.java b/core.sdk/src/main/java/com/lyms/util/remote/HttpRemote.java new file mode 100644 index 0000000..bf9a9a6 --- /dev/null +++ b/core.sdk/src/main/java/com/lyms/util/remote/HttpRemote.java @@ -0,0 +1,103 @@ +package com.lyms.util.remote; + +import java.io.IOException; +import java.util.Map; + +import org.apache.http.HttpEntity; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.protocol.HttpClientContext; + +/** + * http,https 远程调用 + * @author maliang + * + */ +public class HttpRemote extends AbsRemote{ + + private HttpRemote(){} + + public static HttpRemote build(){ + return new HttpRemote(); + } + + /** + * get 请求 + *

该方法参数只接受String类型参数 + * @param url + * @param params + * @return + */ + public String get(String url,Map params) { + HttpClientContext httpContext = HttpClientContext.create(); + //处理参数,模拟http get 请求连接方式 + url = super.handlerReqParams(url,params); + LOG.debug("remote : {}",url); + HttpGet get = new HttpGet(url); + HttpEntity entity = null; + CloseableHttpResponse response = null; + try { + response = httpclient.execute(get, httpContext); + entity = response.getEntity(); + httpContext.getCookieStore().clear(); + return responseUTF8(entity); + } catch (ClientProtocolException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (response != null) { + response.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + return null; + } + + /** + * post 请求 + *

目前参数只接受String类型参数 + * @param url + * @param params + * @return + */ + public String post(String url,Map params) { + HttpClientContext httpContext = HttpClientContext.create(); + HttpPost post = new HttpPost(url); + HttpEntity entity = null; + CloseableHttpResponse response = null; + try { + + entity = super.postEntity(params); + if(entity!=null) { + post.setEntity(entity); + } + + response = httpclient.execute(post, httpContext); + entity = response.getEntity(); + httpContext.getCookieStore().clear(); + return responseUTF8(entity); + } catch (ClientProtocolException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (response != null) { + response.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + return null; + } + + + +} diff --git a/core.sdk/src/main/java/com/lyms/util/remote/package-info.java b/core.sdk/src/main/java/com/lyms/util/remote/package-info.java new file mode 100644 index 0000000..e0b2671 --- /dev/null +++ b/core.sdk/src/main/java/com/lyms/util/remote/package-info.java @@ -0,0 +1,9 @@ +/** + * 远程调用包 + *

http + *

https + *

webservice TODO + * @author maliang + * + */ +package com.lyms.util.remote; \ No newline at end of file diff --git a/core.sdk/src/test/java/com/core/sdk/utils/HttpUtilTest.java b/core.sdk/src/test/java/com/core/sdk/utils/HttpUtilTest.java new file mode 100644 index 0000000..0d25764 --- /dev/null +++ b/core.sdk/src/test/java/com/core/sdk/utils/HttpUtilTest.java @@ -0,0 +1,42 @@ +package com.core.sdk.utils; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; + +import com.lyms.util.HttpUtils; + +public class HttpUtilTest { + + private Map params = new HashMap(); + + @Before + public void init () + { + params.put("info", "hello"); + } + + + @Test + public void httpRemote() + { + + Map params = new HashMap<>(); + params.put("k1", "fd"); + params.put("k5", "fd"); + params.put("k3", "fd"); + params.put("k2", "fd"); + + String content = HttpUtils.HTTP.get("https://www.baidu.com", params); + System.out.println(content); + } + + @Test + public void httpsRemote() { + + } + + +} diff --git a/core.sdk/src/test/java/com/core/sdk/utils/package-info.java b/core.sdk/src/test/java/com/core/sdk/utils/package-info.java new file mode 100644 index 0000000..329e8ad --- /dev/null +++ b/core.sdk/src/test/java/com/core/sdk/utils/package-info.java @@ -0,0 +1,5 @@ +/** + * utils 测试包 + * @author maliang + */ +package com.core.sdk.utils; \ No newline at end of file