top.jfunc.http.smart.AbstractSmartHttpClient Maven / Gradle / Ivy
package top.jfunc.http.smart;
import top.jfunc.http.HttpRequestHttpClient;
import top.jfunc.http.SimpleHttpClient;
import top.jfunc.http.SmartHttpClient;
import top.jfunc.http.base.*;
import top.jfunc.http.component.*;
import top.jfunc.http.config.Config;
import top.jfunc.http.config.ConfigFrozen;
import top.jfunc.http.request.HttpRequest;
import top.jfunc.http.request.StringBodyRequest;
import top.jfunc.http.request.UploadRequest;
import top.jfunc.http.response.ClientHttpResponseConverter;
import top.jfunc.http.util.ResponseExtractor;
import top.jfunc.common.utils.*;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;
/**
* 提供了{@link SmartHttpClient}接口的实现
* @see SimpleHttpClient
* @see HttpRequestHttpClient
* @author xiongshiyan at 2019/5/8 , contact me with email [email protected] or phone 15208384257
*/
public abstract class AbstractSmartHttpClient implements SmartHttpClient, SmartHttpTemplate {
/**一堆参数组装为{@link HttpRequest}、{@link StringBodyRequest}、{@link UploadRequest}*/
private AssemblingFactory assemblingFactory;
/**处理一般包含body的*/
private ContentCallbackCreator bodyContentCallbackCreator;
/**处理文件上传*/
private ContentCallbackCreator uploadContentCallbackCreator;
/**配置冻结器*/
private ConfigFrozen configFrozen = new ConfigFrozen();
/**保存系统默认配置*/
private Config config = Config.defaultConfig();
protected AbstractSmartHttpClient(ContentCallbackCreator bodyContentCallbackCreator,
ContentCallbackCreator uploadContentCallbackCreator){
this.assemblingFactory = new DefaultSimpleAssemblingFactory();
this.bodyContentCallbackCreator = bodyContentCallbackCreator;
this.uploadContentCallbackCreator = uploadContentCallbackCreator;
}
protected AbstractSmartHttpClient(AssemblingFactory assemblingFactory,
ContentCallbackCreator bodyContentCallbackCreator,
ContentCallbackCreator uploadContentCallbackCreator){
this.assemblingFactory = assemblingFactory;
this.bodyContentCallbackCreator = bodyContentCallbackCreator;
this.uploadContentCallbackCreator = uploadContentCallbackCreator;
}
@Override
public R http(HttpRequest httpRequest, Method method, ClientHttpResponseConverter clientHttpResponseConverter) throws IOException {
init(httpRequest , method);
ContentCallback contentCallback = getBodyContentCallbackCreator().create(httpRequest);
return template(httpRequest, contentCallback, clientHttpResponseConverter);
}
@Override
public R upload(UploadRequest uploadRequest , ClientHttpResponseConverter clientHttpResponseConverter) throws IOException {
init(uploadRequest, Method.POST);
ContentCallback contentCallback = getUploadContentCallbackCreator().create(uploadRequest);
return template(uploadRequest, contentCallback, clientHttpResponseConverter);
}
protected void init(HttpRequest httpRequest , Method method){
if(null == httpRequest.getConfig()){
httpRequest.setConfig(getConfig());
}
if(null == httpRequest.getMethod()){
httpRequest.setMethod(method);
}
}
/////////////////////////////////////////////////////SimpleHttpClient///////////////////////////////////////////////////////////////
@Override
public String get(String url, Map queryParams, Map headers,
int connectTimeout, int readTimeout, String resultCharset) throws IOException{
MultiValueMap q = ArrayListMultiValueMap.fromMap(queryParams);
MultiValueMap h = ArrayListMultiValueMap.fromMap(headers);
HttpRequest httpRequest = getAssemblingFactory()
.create(url, q, h, connectTimeout, readTimeout, resultCharset);
return get(httpRequest , ResponseExtractor::extractString);
}
@Override
public String post(String url, Map queryParams, String body, String contentType, Map headers,
int connectTimeout, int readTimeout, String bodyCharset, String resultCharset) throws IOException {
MultiValueMap q = ArrayListMultiValueMap.fromMap(queryParams);
MultiValueMap h = ArrayListMultiValueMap.fromMap(headers);
StringBodyRequest stringBodyRequest = getAssemblingFactory()
.create(url, q, body, contentType, h, connectTimeout, readTimeout, bodyCharset, resultCharset);
return post(stringBodyRequest , ResponseExtractor::extractString);
}
@Override
public byte[] getAsBytes(String url, MultiValueMap headers, int connectTimeout, int readTimeout) throws IOException {
HttpRequest httpRequest = getAssemblingFactory()
.create(url, null, headers, connectTimeout, readTimeout, null);
return get(httpRequest , ResponseExtractor::extractBytes);
}
@Override
public File getAsFile(String url, MultiValueMap headers, File file, int connectTimeout, int readTimeout) throws IOException {
HttpRequest httpRequest = getAssemblingFactory()
.create(url, null, headers, connectTimeout, readTimeout, null);
return get(httpRequest, (chr, r) -> IoUtil.copy2File(chr.getBody(), file));
}
@Override
public String upload(String url, MultiValueMap headers,
int connectTimeout, int readTimeout, String resultCharset, FormFile... formFiles) throws IOException{
UploadRequest uploadRequest = getAssemblingFactory()
.create(url, null, formFiles, headers, connectTimeout, readTimeout, resultCharset);
return upload(uploadRequest , ResponseExtractor::extractString);
}
@Override
public String upload(String url, MultiValueMap formParams, MultiValueMap headers,
int connectTimeout, int readTimeout, String resultCharset, FormFile... formFiles) throws IOException {
UploadRequest uploadRequest = getAssemblingFactory()
.create(url, formParams, formFiles, headers, connectTimeout, readTimeout, resultCharset);
return upload(uploadRequest , ResponseExtractor::extractString);
}
protected String calculateResultCharset(HttpRequest httpRequest) {
Config config = getConfig();
return ObjectUtil.defaultIfNull(httpRequest.getResultCharset(), config.getDefaultResultCharset());
}
public ContentCallbackCreator getBodyContentCallbackCreator() {
return bodyContentCallbackCreator;
}
public ContentCallbackCreator getUploadContentCallbackCreator() {
return uploadContentCallbackCreator;
}
public AssemblingFactory getAssemblingFactory() {
return assemblingFactory;
}
@Override
public Config getConfig() {
return config;
}
@Override
public void setConfig(Config config) {
configFrozen.ensureConfigNotFreeze();
this.config = Objects.requireNonNull(config);
}
@Override
public void freezeConfig() {
//本身冻结
configFrozen.freezeConfig();
//Config冻结
config.freezeConfig();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy