All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.kaizen4j.common.httpclient.AbstractHttpClientProxy Maven / Gradle / Ivy
package org.kaizen4j.common.httpclient;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicNameValuePair;
import org.kaizen4j.common.util.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.kaizen4j.common.base.Symbols.QUESTION_MARK;
public abstract class AbstractHttpClientProxy {
private static final Logger logger = LoggerFactory.getLogger(AbstractHttpClientProxy.class);
public HttpResult post(String requestUrl, Map requestParam) {
return post(requestUrl, requestParam, Maps.newHashMap());
}
/**
* 发送 POST 请求
*
* @param requestUrl 请求URL地址
* @param requestParam 请求参数
* @param requestHeader 请求头
* @return HttpResult 对象
*/
public HttpResult post(String requestUrl, Map requestParam, Map requestHeader) {
Preconditions.checkNotNull(requestParam, "Request parameter must be not null");
List nameValuePairs = Lists.newArrayList();
requestParam.forEach((name, value) -> nameValuePairs.add(new BasicNameValuePair(name, value)));
logger.info("Send POST request body: {}", JsonUtils.getJson(nameValuePairs));
HttpEntity requestEntity = new UrlEncodedFormEntity(nameValuePairs, UTF_8);
return post(requestUrl, requestHeader, requestEntity);
}
public HttpResult post(String requestUrl, String requestBody) {
return post(requestUrl, requestBody, Maps.newHashMap());
}
/**
* 发送 POST 请求
*
* @param requestUrl 请求URL地址
* @param requestBody 请求内容体
* @param requestHeader 请求头
* @return HttpResult 对象
*/
public HttpResult post(String requestUrl, String requestBody, Map requestHeader) {
Preconditions.checkNotNull(requestBody, "Request body must be not null");
logger.info("Send POST request body: {}", requestBody);
HttpEntity requestEntity = new StringEntity(requestBody, UTF_8);
return post(requestUrl, requestHeader, requestEntity);
}
/**
* 发送 GET 请求
*
* @param requestUrl 请求URL地址
* @param requestParam 请求参数
* @param requestHeader 请求头
* @return HttpResult 对象
*/
public HttpResult get(String requestUrl, Map requestParam, Map requestHeader) {
HttpGet httpGet = new HttpGet(getFullRequestUrl(requestUrl, requestParam));
return request(httpGet, requestHeader);
}
public HttpResult put(String requestUrl, Map requestParam, Map requestHeader) {
HttpPut httpPut = new HttpPut(getFullRequestUrl(requestUrl, requestParam));
return request(httpPut, requestHeader);
}
public HttpResult delete(String requestUrl, Map requestParam, Map requestHeader) {
HttpDelete httpDelete = new HttpDelete(getFullRequestUrl(requestUrl, requestParam));
return request(httpDelete, requestHeader);
}
/**
* 执行 POST 请求发送
*
* @param requestUrl 请求URL地址
* @param requestHeader 请求头
* @param requestEntity 请求实体
* @return HttpResult 对象
*/
private HttpResult post(String requestUrl, Map requestHeader, HttpEntity requestEntity) {
Preconditions.checkNotNull(requestUrl, "Request url must be not null");
Preconditions.checkNotNull(requestEntity, "HttpEntity must be not null");
HttpPost httpPost = new HttpPost(requestUrl);
/* 请求内容 */
httpPost.setEntity(requestEntity);
return request(httpPost, requestHeader);
}
private String getFullRequestUrl(String requestUrl, Map requestParam) {
Preconditions.checkNotNull(requestUrl, "Request url must be not null");
if (CollectionUtils.isEmpty(requestParam)) {
return requestUrl;
} else {
List nameValuePairs = Lists.newArrayList();
requestParam.forEach((name, value) -> nameValuePairs.add(new BasicNameValuePair(name, value)));
try {
URIBuilder uriBuilder = new URIBuilder().setCharset(UTF_8);
URI requestUri = uriBuilder.addParameters(nameValuePairs).build();
return StringUtils.join(requestUrl, QUESTION_MARK, requestUri.getQuery());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
}
protected abstract HttpResult request(HttpRequestBase httpRequestBase, Map requestHeader);
}