All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alogic.remote.httpclient.HttpClient Maven / Gradle / Ivy

There is a newer version: 1.6.16
Show newest version
package com.alogic.remote.httpclient;

import com.alogic.rpc.CallException;
import com.anysoft.util.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpOptions;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpTrace;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.w3c.dom.Element;

import com.alogic.remote.AbstractClient;
import com.alogic.remote.Request;
import com.alogic.remote.httpclient.customizer.Default;

/**
 * 基于apache http client实现的Client
 * 
 * @author yyduan
 * @since 1.6.8.12
 * 
 * @version 1.6.8.14 
* - 优化http远程调用的超时机制
* * @version 1.6.10.3 [20171009 duanyy]
* - 增加PUT,GET,DELETE,HEAD,OPTIONS,TRACE,PATCH等http方法;
* * @version 1.6.10.6 [20171114 duanyy]
* - 增加Http调用请求级别的Filter和Client级别的Customizer
* * @version 1.6.13.6 [20200629 duanyy]
* - 增加rpc的debug信息的输出;
* * @version 1.6.13.15 [20200909 duanyy]
* - filter支持enable参数 * * @version 1.6.13.21 [20201021 duanyy]
* - 优化代码结构
* - 支持gzip压缩
*/ public class HttpClient extends AbstractClient{ /** * http client */ protected CloseableHttpClient httpClient = null; /** * request configuration */ protected RequestConfig requestConfig = null; protected HttpClientConfig clientConfig = null; protected HttpCientFilter filter = null; protected HttpClientCustomizer customizer = null; protected String $blockMode = "false"; protected String $blockCode = "core.e1607"; protected String $blockReason = "Request blocked"; /** * Http method * @author yyduan * */ public enum Method { GET(HttpGet.class), POST(HttpPost.class), DELETE(HttpDelete.class), HEAD(HttpHead.class), OPTIONS(HttpOptions.class), TRACE(HttpTrace.class), PATCH(HttpPatch.class), PUT(HttpPut.class); protected Class clazz; Method(Class clazz){ this.clazz = clazz; } public HttpRequestBase createRequest(){ try { return this.clazz.newInstance(); } catch (Exception ex){ return null; } } }; @Override protected void onConfigure(Element e,Properties p){ //装入filter配置 Element filterElem = XmlTools.getFirstElementByPath(e, "filter"); if (filterElem != null){ Factory factory = new Factory(); try { filter = factory.newInstance(filterElem, p, "module"); }catch (Exception ex){ LOG.error(String.format("Can not create filter with %s", XmlTools.node2String(filterElem))); } } Element customizerElem = XmlTools.getFirstElementByPath(e, "customizer"); if (customizerElem != null){ Factory factory = new Factory(); try { customizer = factory.newInstance(customizerElem, p, "module"); }catch (Exception ex){ LOG.error(String.format("Can not create customizer with %s", XmlTools.node2String(customizerElem))); } } configure(p); } @Override public void configure(Properties p) { super.configure(p); if (StringUtils.isEmpty(System.getProperty("https.protocols"))){ System.setProperty("https.protocols", PropertiesConstants.getString(p,"https.protocols","SSLv3,TLSv1,TLSv1.1,TLSv1.2")); } if (StringUtils.isEmpty(System.getProperty("https.cipherSuites"))){ System.setProperty("https.cipherSuites", PropertiesConstants.getString(p,"https.cipherSuites","")); } $blockMode = PropertiesConstants.getRaw(p,"rpc.block.mode",$blockMode); $blockCode = PropertiesConstants.getRaw(p,"rpc.block.code",$blockCode); $blockReason = PropertiesConstants.getRaw(p,"rpc.block.reason",$blockReason); if (customizer == null){ customizer = new Default(); customizer.configure(p); } clientConfig = new HttpClientConfig(); clientConfig.configure(p); clientConfig.setRequestConfig(customizer.customizeRequestConfig(RequestConfig.custom(), p).build()); httpClient = customizer.customizeHttpClient(HttpClients.custom(), p).build(); } @Override public Request build(String method) { Settings settings = Settings.get(); boolean blockMode = PropertiesConstants.transform(settings,$blockMode,false); if (blockMode){ throw new CallException( PropertiesConstants.transform(settings,$blockCode,"core.e1607"), PropertiesConstants.transform(settings,$blockReason,"Request blocked")); } return new HttpClientRequest(method,httpClient,this,this.clientConfig,false); } @Override public Request build(String method,boolean debug) { Settings settings = Settings.get(); boolean blockMode = PropertiesConstants.transform(settings,$blockMode,false); if (blockMode){ throw new CallException( PropertiesConstants.transform(settings,$blockCode,"core.e1607"), PropertiesConstants.transform(settings,$blockReason,"Request blocked")); } return new HttpClientRequest(method,httpClient,this,this.clientConfig,debug); } public HttpRequestBase getRequestByMethod(final String method){ Method m = Method.valueOf(method.toUpperCase()); return m.createRequest(); } public void onRequest(HttpClientRequest request){ if (this.clientConfig.isGzipEnable()){ request.setHeader("Accept-Encoding","gzip"); } if (filter != null && filter.isEnable()){ filter.onRequest(request); } } public void onResponse(HttpClientResponse response){ if (filter != null && filter.isEnable()){ filter.onResponse(response); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy