in dubbo-remoting-extensions/dubbo-remoting-http/src/main/java/org/apache/dubbo/remoting/http/restclient/HttpClientRestClient.java [62:132]
public CompletableFuture<RestResult> send(RequestTemplate requestTemplate) {
HttpRequestBase httpRequest = null;
String httpMethod = requestTemplate.getHttpMethod();
httpRequest = createHttpUriRequest(httpMethod, requestTemplate);
if (httpRequest instanceof HttpEntityEnclosingRequest) {
((HttpEntityEnclosingRequestBase) httpRequest)
.setEntity(new ByteArrayEntity(requestTemplate.getSerializedBody()));
}
Map<String, Collection<String>> allHeaders = requestTemplate.getAllHeaders();
allHeaders.remove("Content-Length");
// header
for (String headerName : allHeaders.keySet()) {
Collection<String> headerValues = allHeaders.get(headerName);
for (String headerValue : headerValues) {
httpRequest.addHeader(headerName, headerValue);
}
}
httpRequest.setConfig(getRequestConfig(httpClientConfig));
CompletableFuture<RestResult> future = new CompletableFuture<>();
try {
CloseableHttpResponse response = closeableHttpClient.execute(httpRequest);
future.complete(new RestResult() {
@Override
public String getContentType() {
Header header = response.getFirstHeader("Content-Type");
return header == null ? null : header.getValue();
}
@Override
public byte[] getBody() throws IOException {
if (response.getEntity() == null) {
return new byte[0];
}
return IOUtils.toByteArray(response.getEntity().getContent());
}
@Override
public Map<String, List<String>> headers() {
return Arrays.stream(response.getAllHeaders())
.collect(Collectors.toMap(Header::getName, h -> Collections.singletonList(h.getValue())));
}
@Override
public byte[] getErrorResponse() throws IOException {
return getBody();
}
@Override
public int getResponseCode() {
return response.getStatusLine().getStatusCode();
}
@Override
public String getMessage() throws IOException {
return appendErrorMessage(
response.getStatusLine().getReasonPhrase(), new String(getErrorResponse()));
}
});
} catch (IOException e) {
future.completeExceptionally(e);
}
return future;
}