-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverseProxyProvider.java
159 lines (133 loc) · 5.75 KB
/
ReverseProxyProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.web.provider;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StreamUtils;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.ResponseExtractor;
import org.springframework.web.client.RestTemplate;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;
import org.springframework.core.io.Resource;
@Component
public class ReverseProxyProvider {
@Value("${targetServer:}")
public String targetServer;
public MultiValueMap<String, String> copyForm(HttpServletRequest request) {
MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
Map<String, String[]> srcReq = request.getParameterMap();
for (String k : srcReq.keySet()) {
form.put(k, Arrays.asList(srcReq.get(k)));
}
return form;
}
public HttpHeaders copyHeaders(HttpServletRequest request) {
// 复制请求头部
HttpHeaders httpHeaders = new HttpHeaders();
Enumeration<String> hne = request.getHeaderNames();
while (hne.hasMoreElements()) {
String hk = hne.nextElement();
httpHeaders.add(hk, request.getHeader(hk));
}
return httpHeaders;
}
public String getTargetUrl(HttpServletRequest request) {
// 去除api前缀
String urlTo = targetServer + request.getRequestURI().substring(4);
if (request.getQueryString() != null) {
urlTo = urlTo + "?" + request.getQueryString();
}
return urlTo;
}
public String readReqBody(HttpServletRequest request) throws IOException {
BufferedReader br = request.getReader();
StringBuilder wholeStr = new StringBuilder();
String str;
while ((str = br.readLine()) != null) {
wholeStr.append(str);
}
return wholeStr.toString();
}
@Deprecated
public ResponseEntity<Resource> exchange(String url, String method, HttpEntity<Object> reqEntity) {
RestTemplate restTemplate = new RestTemplate();
return restTemplate.exchange(url, Objects.requireNonNull(HttpMethod.resolve(method)), reqEntity, Resource.class);
}
@Deprecated
public void sendRsp(ResponseEntity<Resource> reqRsp, HttpServletResponse response) throws IOException {
for (String k : reqRsp.getHeaders().keySet()) {
for (String v : Objects.requireNonNull(reqRsp.getHeaders().get(k))) {
response.setHeader(k, v);
}
}
//这里理解错误了 Resource 还是把数据加载到内存里面了,暂时先不考虑内存的问题。
InputStream input = Objects.requireNonNull(reqRsp.getBody()).getInputStream();
OutputStream output = response.getOutputStream();
StreamUtils.copy(input, output);
input.close();
output.close();
}
public void copyRspHeaders(HttpServletResponse response, HttpHeaders headers) {
if (headers == null) {
return;
}
for (String k : headers.keySet()) {
for (String v : Objects.requireNonNull(headers.get(k))) {
response.setHeader(k, v);
}
}
}
public void proxyWithoutBuffering(String url, String method, HttpServletResponse response, HttpEntity<Object> reqBody) throws IOException {
RestTemplate restTemplate = new RestTemplate();
final RequestCallback requestCallBack = restTemplate.httpEntityCallback(reqBody, null);
final ResponseExtractor responseExtractor = (ClientHttpResponse clientHttpResponse) -> {
copyRspHeaders(response, clientHttpResponse.getHeaders());
StreamUtils.copy(clientHttpResponse.getBody(), response.getOutputStream());
return null;
};
restTemplate.execute(url, HttpMethod.resolve(method), requestCallBack, responseExtractor);
}
public void proxy(HttpServletRequest request, HttpServletResponse response, Map<String, String> addFrom) throws Exception {
String urlTo = this.getTargetUrl(request);
// 复制请求头部
HttpHeaders httpHeaders = copyHeaders(request);
// 复制表单
MultiValueMap<String, String> toReq = this.copyForm(request);
// 额外追加的表单参数
if (addFrom != null) {
for (String fk : addFrom.keySet()) {
toReq.put(fk, Collections.singletonList(addFrom.get(fk)));
}
}
HttpEntity<Object> reqEntity = new HttpEntity<>(toReq, httpHeaders);
// json body
String wholeStr = this.readReqBody(request);
if (!wholeStr.equals("")) {
reqEntity = new HttpEntity<>(wholeStr, httpHeaders);
}
String m = request.getMethod();
try {
/*
ResponseEntity<Resource> rsp = this.exchange(urlTo, m, reqEntity);
this.sendRsp(rsp, response);
*/
proxyWithoutBuffering(urlTo, m, response, reqEntity);
} catch (HttpStatusCodeException e) {
response.setStatus(e.getRawStatusCode());
copyRspHeaders(response, e.getResponseHeaders());
response.getWriter().write(e.getResponseBodyAsString());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}