HttpClient发起请求,将响应结果(header和entity)设置到
response中返回
前⾔
HttpClient发起请求,将响应结果(header和entity)设置到response中返回
fig;
import java.io.IOException;
import flect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
aph.atlasmon.base.util.JSONUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.fig.RequestConfig;
import org.apache.hods.CloseableHttpResponse;
import org.apache.hods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.beans.factory.BeanFactory;
import org.t.support.WebApplicationContextUtils;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
//import at.util.buf.MessageBytes;
/**
* 对象服务接⼝透传只有⾮控制接⼝(/oss/*)才会进到这个
*/
public class ContentInterceptor extends HandlerInterceptorAdapter {
private static final String PATH_DELIMITER = "/";
private OssServerConfig ossServerConfig;
private MinioServerConfig minioServerConfig;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// get配置⽂件bean
BeanFactory factory = ServletContext());
ossServerConfig = (OssServerConfig) Bean("ossServerConfig");
minioServerConfig = (MinioServerConfig) Bean("minioServerConfig");
// get request uri(undertow)
String uri = RequestURI();
// uri处理(去掉context.api)
String api = Api();
if (uri.startsWith(api)) {
uri = place(api, PATH_DELIMITER);
}
// get httpclient response
String minioUrl = Endpoint() + uri;
CloseableHttpClient httpClient = ate().build();
HttpGet httpGet = new HttpGet(minioUrl);
RequestConfig config = RequestConfig.custom().setConnectTimeout(1000).setConnectionRequestTimeout(1000)
.setSocketTimeout(10 * 1000).build();
httpGet.setConfig(config);
httpGet.addHeader("content-type", "application/json");
CloseableHttpResponse closeableHttpResponse = ute(httpGet);
ServletOutputStream out = null;
try {
// 将httpClient响应的header和entity设置到httpServletResponse
Header header = FirstHeader("Content-Type");
response.Name(), Value());
HttpEntity entity = Entity();
out = OutputStream();
entity.writeTo(out);
out.flush();
out.close();
return false;
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
// 根据Field获得对应的Class
private Class getClassByName(Class classObject, String name) {
Map<Class, List<Field>> fieldMap = new HashMap<>();
Class returnClass = null;
Class tempClass = classObject;
while (tempClass != null) {
fieldMap.put(tempClass, Arrays.DeclaredFields()));            tempClass = Superclass();
}
for (Map.Entry<Class, List<Field>> entry : Set()) {
for (Field f : Value()) {
if (f.getName().equals(name)) {
returnClass = Key();
break;
}
}
}
php中header是什么意思
return returnClass;
}
// 递归遍历⽗类寻coyoteRequest Field
private Object findCoyoteRequest(Object request) throws Exception {
Class a = Class(), "request");
Field request1 = a.getDeclaredField("request");
request1.setAccessible(true);
Object b = (request);
if (Class(), "coyoteRequest") == null) {
return findCoyoteRequest(b);
} else {
return b;
}
}
private String getReqStr(String namespace, String prefix, String name) {
Map<String, String> map = new HashMap<>();
map.put("namespace", namespace);
map.put("prefix", prefix);
map.put("fileName", name);
JSONString(map);
}
}
感谢

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。