Update Dubbo Spring Boot Starter 2.7.1
parent
3230a61a23
commit
c12252566a
@ -1,188 +1,181 @@
|
|||||||
//package org.springframework.cloud.alibaba.dubbo.gateway;
|
package org.springframework.cloud.alibaba.dubbo.gateway;
|
||||||
//
|
|
||||||
//import org.apache.commons.lang.StringUtils;
|
import org.apache.dubbo.rpc.service.GenericException;
|
||||||
//import org.apache.dubbo.rpc.service.GenericException;
|
import org.apache.dubbo.rpc.service.GenericService;
|
||||||
//import org.apache.dubbo.rpc.service.GenericService;
|
|
||||||
//import org.springframework.cloud.alibaba.dubbo.http.MutableHttpServerRequest;
|
import org.springframework.cloud.alibaba.dubbo.http.MutableHttpServerRequest;
|
||||||
//import org.springframework.cloud.alibaba.dubbo.metadata.DubboServiceMetadata;
|
import org.springframework.cloud.alibaba.dubbo.metadata.DubboRestServiceMetadata;
|
||||||
//import org.springframework.cloud.alibaba.dubbo.metadata.DubboTransportedMetadata;
|
import org.springframework.cloud.alibaba.dubbo.metadata.RequestMetadata;
|
||||||
//import org.springframework.cloud.alibaba.dubbo.metadata.RequestMetadata;
|
import org.springframework.cloud.alibaba.dubbo.metadata.RestMethodMetadata;
|
||||||
//import org.springframework.cloud.alibaba.dubbo.metadata.RestMethodMetadata;
|
import org.springframework.cloud.alibaba.dubbo.metadata.repository.DubboServiceMetadataRepository;
|
||||||
//import org.springframework.cloud.alibaba.dubbo.metadata.repository.DubboServiceMetadataRepository;
|
import org.springframework.cloud.alibaba.dubbo.service.DubboGenericServiceExecutionContext;
|
||||||
//import org.springframework.cloud.alibaba.dubbo.service.DubboGenericServiceExecutionContext;
|
import org.springframework.cloud.alibaba.dubbo.service.DubboGenericServiceExecutionContextFactory;
|
||||||
//import org.springframework.cloud.alibaba.dubbo.service.DubboGenericServiceExecutionContextFactory;
|
import org.springframework.cloud.alibaba.dubbo.service.DubboGenericServiceFactory;
|
||||||
//import org.springframework.cloud.alibaba.dubbo.service.DubboGenericServiceFactory;
|
import org.springframework.http.HttpHeaders;
|
||||||
//import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpRequest;
|
||||||
//import org.springframework.http.HttpRequest;
|
import org.springframework.util.AntPathMatcher;
|
||||||
//import org.springframework.util.AntPathMatcher;
|
import org.springframework.util.PathMatcher;
|
||||||
//import org.springframework.util.CollectionUtils;
|
import org.springframework.util.StreamUtils;
|
||||||
//import org.springframework.util.PathMatcher;
|
import org.springframework.web.servlet.HttpServletBean;
|
||||||
//import org.springframework.util.StreamUtils;
|
import org.springframework.web.util.UriComponents;
|
||||||
//import org.springframework.web.util.UriComponents;
|
|
||||||
//
|
import javax.servlet.ServletException;
|
||||||
//import javax.servlet.ServletException;
|
import javax.servlet.ServletInputStream;
|
||||||
//import javax.servlet.ServletInputStream;
|
import javax.servlet.annotation.WebServlet;
|
||||||
//import javax.servlet.annotation.WebServlet;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
//import javax.servlet.http.HttpServlet;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
//import javax.servlet.http.HttpServletRequest;
|
import java.io.IOException;
|
||||||
//import javax.servlet.http.HttpServletResponse;
|
import java.net.URI;
|
||||||
//import java.io.IOException;
|
import java.net.URISyntaxException;
|
||||||
//import java.net.URI;
|
import java.util.Arrays;
|
||||||
//import java.net.URISyntaxException;
|
import java.util.Collections;
|
||||||
//import java.util.*;
|
import java.util.Enumeration;
|
||||||
//
|
import java.util.HashMap;
|
||||||
//import static org.springframework.web.util.UriComponentsBuilder.fromUriString;
|
import java.util.LinkedHashMap;
|
||||||
//
|
import java.util.List;
|
||||||
//@WebServlet(urlPatterns = "/dsc/*")
|
import java.util.Map;
|
||||||
//public class DubboGatewayServlet extends HttpServlet {
|
|
||||||
//
|
import static org.apache.commons.lang3.StringUtils.substringAfter;
|
||||||
// private final DubboServiceMetadataRepository repository;
|
import static org.apache.commons.lang3.StringUtils.substringBetween;
|
||||||
//
|
import static org.springframework.web.util.UriComponentsBuilder.fromUriString;
|
||||||
// private final DubboTransportedMetadata dubboTransportedMetadata;
|
|
||||||
//
|
@WebServlet(urlPatterns = "/dsc/*")
|
||||||
// private final DubboGenericServiceFactory serviceFactory;
|
public class DubboGatewayServlet extends HttpServletBean {
|
||||||
//
|
|
||||||
// private final DubboGenericServiceExecutionContextFactory contextFactory;
|
private final DubboServiceMetadataRepository repository;
|
||||||
//
|
|
||||||
// private final PathMatcher pathMatcher = new AntPathMatcher();
|
private final DubboGenericServiceFactory serviceFactory;
|
||||||
//
|
|
||||||
// public DubboGatewayServlet(DubboServiceMetadataRepository repository,
|
private final DubboGenericServiceExecutionContextFactory contextFactory;
|
||||||
// DubboGenericServiceFactory serviceFactory,
|
|
||||||
// DubboGenericServiceExecutionContextFactory contextFactory) {
|
private final PathMatcher pathMatcher = new AntPathMatcher();
|
||||||
// this.repository = repository;
|
|
||||||
// this.dubboTransportedMetadata = new DubboTransportedMetadata();
|
private final Map<String, Object> dubboTranslatedAttributes = new HashMap<>();
|
||||||
// dubboTransportedMetadata.setProtocol("dubbo");
|
|
||||||
// dubboTransportedMetadata.setCluster("failover");
|
public DubboGatewayServlet(DubboServiceMetadataRepository repository,
|
||||||
// this.serviceFactory = serviceFactory;
|
DubboGenericServiceFactory serviceFactory,
|
||||||
// this.contextFactory = contextFactory;
|
DubboGenericServiceExecutionContextFactory contextFactory) {
|
||||||
// }
|
this.repository = repository;
|
||||||
//
|
this.serviceFactory = serviceFactory;
|
||||||
// public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
this.contextFactory = contextFactory;
|
||||||
//
|
dubboTranslatedAttributes.put("protocol", "dubbo");
|
||||||
// // /g/{app-name}/{rest-path}
|
dubboTranslatedAttributes.put("cluster", "failover");
|
||||||
// String requestURI = request.getRequestURI();
|
}
|
||||||
// // /g/
|
|
||||||
// String servletPath = request.getServletPath();
|
private String resolveServiceName(HttpServletRequest request) {
|
||||||
//
|
|
||||||
// String part = StringUtils.substringAfter(requestURI, servletPath);
|
// /g/{app-name}/{rest-path}
|
||||||
//
|
String requestURI = request.getRequestURI();
|
||||||
// String serviceName = StringUtils.substringBetween(part, "/", "/");
|
// /g/
|
||||||
//
|
String servletPath = request.getServletPath();
|
||||||
// // App name= spring-cloud-alibaba-dubbo-web-provider (127.0.0.1:8080)
|
|
||||||
//
|
String part = substringAfter(requestURI, servletPath);
|
||||||
// String restPath = StringUtils.substringAfter(part, serviceName);
|
|
||||||
//
|
String serviceName = substringBetween(part, "/", "/");
|
||||||
// // 初始化 serviceName 的 REST 请求元数据
|
|
||||||
// repository.initialize(serviceName);
|
return serviceName;
|
||||||
// // 将 HttpServletRequest 转化为 RequestMetadata
|
}
|
||||||
// RequestMetadata clientMetadata = buildRequestMetadata(request, restPath);
|
|
||||||
//
|
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
||||||
// DubboServiceMetadata dubboServiceMetadata = repository.get(serviceName, clientMetadata);
|
|
||||||
//
|
String serviceName = resolveServiceName(request);
|
||||||
// if (dubboServiceMetadata == null) {
|
|
||||||
// // if DubboServiceMetadata is not found, executes next
|
String restPath = substringAfter(request.getRequestURI(), serviceName);
|
||||||
// throw new ServletException("DubboServiceMetadata can't be found!");
|
|
||||||
// }
|
// 初始化 serviceName 的 REST 请求元数据
|
||||||
//
|
repository.initialize(serviceName);
|
||||||
// RestMethodMetadata dubboRestMethodMetadata = dubboServiceMetadata.getRestMethodMetadata();
|
// 将 HttpServletRequest 转化为 RequestMetadata
|
||||||
//
|
RequestMetadata clientMetadata = buildRequestMetadata(request, restPath);
|
||||||
// GenericService genericService = serviceFactory.create(dubboServiceMetadata, dubboTransportedMetadata);
|
|
||||||
//
|
DubboRestServiceMetadata dubboRestServiceMetadata = repository.get(serviceName, clientMetadata);
|
||||||
// // TODO: Get the Request Body from HttpServletRequest
|
|
||||||
// byte[] body = getRequestBody(request);
|
if (dubboRestServiceMetadata == null) {
|
||||||
//
|
// if DubboServiceMetadata is not found, executes next
|
||||||
// MutableHttpServerRequest httpServerRequest = new MutableHttpServerRequest(new HttpRequestAdapter(request), body);
|
throw new ServletException("DubboServiceMetadata can't be found!");
|
||||||
//
|
}
|
||||||
//// customizeRequest(httpServerRequest, dubboRestMethodMetadata, clientMetadata);
|
|
||||||
//
|
RestMethodMetadata dubboRestMethodMetadata = dubboRestServiceMetadata.getRestMethodMetadata();
|
||||||
// DubboGenericServiceExecutionContext context = contextFactory.create(dubboRestMethodMetadata, httpServerRequest);
|
|
||||||
//
|
GenericService genericService = serviceFactory.create(dubboRestServiceMetadata, dubboTranslatedAttributes);
|
||||||
// Object result = null;
|
|
||||||
// GenericException exception = null;
|
// TODO: Get the Request Body from HttpServletRequest
|
||||||
//
|
byte[] body = getRequestBody(request);
|
||||||
// try {
|
|
||||||
// result = genericService.$invoke(context.getMethodName(), context.getParameterTypes(), context.getParameters());
|
MutableHttpServerRequest httpServerRequest = new MutableHttpServerRequest(new HttpRequestAdapter(request), body);
|
||||||
// } catch (GenericException e) {
|
|
||||||
// exception = e;
|
DubboGenericServiceExecutionContext context = contextFactory.create(dubboRestMethodMetadata, httpServerRequest);
|
||||||
// }
|
|
||||||
// response.getWriter().println(result);
|
Object result = null;
|
||||||
// }
|
GenericException exception = null;
|
||||||
//
|
|
||||||
// private byte[] getRequestBody(HttpServletRequest request) throws IOException {
|
try {
|
||||||
// ServletInputStream inputStream = request.getInputStream();
|
result = genericService.$invoke(context.getMethodName(), context.getParameterTypes(), context.getParameters());
|
||||||
// return StreamUtils.copyToByteArray(inputStream);
|
} catch (GenericException e) {
|
||||||
// }
|
exception = e;
|
||||||
//
|
}
|
||||||
// private static class HttpRequestAdapter implements HttpRequest {
|
response.getWriter().println(result);
|
||||||
//
|
}
|
||||||
// private final HttpServletRequest request;
|
|
||||||
//
|
private byte[] getRequestBody(HttpServletRequest request) throws IOException {
|
||||||
// private HttpRequestAdapter(HttpServletRequest request) {
|
ServletInputStream inputStream = request.getInputStream();
|
||||||
// this.request = request;
|
return StreamUtils.copyToByteArray(inputStream);
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// @Override
|
private static class HttpRequestAdapter implements HttpRequest {
|
||||||
// public String getMethodValue() {
|
|
||||||
// return request.getMethod();
|
private final HttpServletRequest request;
|
||||||
// }
|
|
||||||
//
|
private HttpRequestAdapter(HttpServletRequest request) {
|
||||||
// @Override
|
this.request = request;
|
||||||
// public URI getURI() {
|
}
|
||||||
// try {
|
|
||||||
// return new URI(request.getRequestURL().toString() + "?" + request.getQueryString());
|
@Override
|
||||||
// } catch (URISyntaxException e) {
|
public String getMethodValue() {
|
||||||
// e.printStackTrace();
|
return request.getMethod();
|
||||||
// }
|
}
|
||||||
// throw new RuntimeException();
|
|
||||||
// }
|
@Override
|
||||||
//
|
public URI getURI() {
|
||||||
// @Override
|
try {
|
||||||
// public HttpHeaders getHeaders() {
|
return new URI(request.getRequestURL().toString() + "?" + request.getQueryString());
|
||||||
// return new HttpHeaders();
|
} catch (URISyntaxException e) {
|
||||||
// }
|
e.printStackTrace();
|
||||||
// }
|
}
|
||||||
//
|
throw new RuntimeException();
|
||||||
//// protected void customizeRequest(MutableHttpServerRequest httpServerRequest,
|
}
|
||||||
//// RestMethodMetadata dubboRestMethodMetadata, RequestMetadata clientMetadata) {
|
|
||||||
////
|
@Override
|
||||||
//// RequestMetadata dubboRequestMetadata = dubboRestMethodMetadata.getRequest();
|
public HttpHeaders getHeaders() {
|
||||||
//// String pathPattern = dubboRequestMetadata.getPath();
|
return new HttpHeaders();
|
||||||
////
|
}
|
||||||
//// Map<String, String> pathVariables = pathMatcher.extractUriTemplateVariables(pathPattern, httpServerRequest.getPath());
|
}
|
||||||
////
|
|
||||||
//// if (!CollectionUtils.isEmpty(pathVariables)) {
|
private RequestMetadata buildRequestMetadata(HttpServletRequest request, String restPath) {
|
||||||
//// // Put path variables Map into query parameters Map
|
UriComponents uriComponents = fromUriString(request.getRequestURI()).build(true);
|
||||||
//// httpServerRequest.params(pathVariables);
|
RequestMetadata requestMetadata = new RequestMetadata();
|
||||||
//// }
|
requestMetadata.setPath(restPath);
|
||||||
////
|
requestMetadata.setMethod(request.getMethod());
|
||||||
//// }
|
requestMetadata.setParams(getParams(request));
|
||||||
//
|
requestMetadata.setHeaders(getHeaders(request));
|
||||||
// private RequestMetadata buildRequestMetadata(HttpServletRequest request, String restPath) {
|
return requestMetadata;
|
||||||
// UriComponents uriComponents = fromUriString(request.getRequestURI()).build(true);
|
}
|
||||||
// RequestMetadata requestMetadata = new RequestMetadata();
|
|
||||||
// requestMetadata.setPath(restPath);
|
private Map<String, List<String>> getHeaders(HttpServletRequest request) {
|
||||||
// requestMetadata.setMethod(request.getMethod());
|
Map<String, List<String>> map = new LinkedHashMap<>();
|
||||||
// requestMetadata.setParams(getParams(request));
|
Enumeration<String> headerNames = request.getHeaderNames();
|
||||||
// requestMetadata.setHeaders(getHeaders(request));
|
while (headerNames.hasMoreElements()) {
|
||||||
// return requestMetadata;
|
String headerName = headerNames.nextElement();
|
||||||
// }
|
Enumeration<String> headerValues = request.getHeaders(headerName);
|
||||||
//
|
map.put(headerName, Collections.list(headerValues));
|
||||||
// private Map<String, List<String>> getHeaders(HttpServletRequest request) {
|
}
|
||||||
// Map<String, List<String>> map = new LinkedHashMap<>();
|
return map;
|
||||||
// Enumeration<String> headerNames = request.getHeaderNames();
|
}
|
||||||
// while (headerNames.hasMoreElements()) {
|
|
||||||
// String headerName = headerNames.nextElement();
|
private Map<String, List<String>> getParams(HttpServletRequest request) {
|
||||||
// Enumeration<String> headerValues = request.getHeaders(headerName);
|
Map<String, List<String>> map = new LinkedHashMap<>();
|
||||||
// map.put(headerName, Collections.list(headerValues));
|
for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
|
||||||
// }
|
map.put(entry.getKey(), Arrays.asList(entry.getValue()));
|
||||||
// return map;
|
}
|
||||||
// }
|
return map;
|
||||||
//
|
}
|
||||||
// private Map<String, List<String>> getParams(HttpServletRequest request) {
|
}
|
||||||
// Map<String, List<String>> map = new LinkedHashMap<>();
|
|
||||||
// for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
|
|
||||||
// map.put(entry.getKey(), Arrays.asList(entry.getValue()));
|
|
||||||
// }
|
|
||||||
// return map;
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
Loading…
Reference in New Issue