Polish spring-cloud-incubator/spring-cloud-alibaba#510 : Dubbo Spring Cloud Non-Web Provider Registration issue
parent
cda6d4ac6a
commit
4b111e4e46
@ -0,0 +1,199 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.cloud.alibaba.dubbo.registry.env;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.env.EnvironmentPostProcessor;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.MutablePropertySources;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.apache.dubbo.common.Constants.DEFAULT_PROTOCOL;
|
||||
import static org.apache.dubbo.config.spring.util.PropertySourcesUtils.getSubProperties;
|
||||
|
||||
/**
|
||||
* Dubbo {@link WebApplicationType#NONE Non-Web Application} {@link EnvironmentPostProcessor}
|
||||
*
|
||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
*/
|
||||
public class DubboNonWebApplicationEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered {
|
||||
|
||||
private static final String DOT = ".";
|
||||
|
||||
/**
|
||||
* The name of default {@link PropertySource} defined in SpringApplication#configurePropertySources method.
|
||||
*/
|
||||
private static final String PROPERTY_SOURCE_NAME = "defaultProperties";
|
||||
|
||||
private static final String SERVER_PORT_PROPERTY_NAME = "server.port";
|
||||
|
||||
private static final String PORT_PROPERTY_NAME = "port";
|
||||
|
||||
private static final String PROTOCOL_PROPERTY_NAME_PREFIX = "dubbo.protocol";
|
||||
|
||||
private static final String PROTOCOL_NAME_PROPERTY_NAME_SUFFIX = DOT + "name";
|
||||
|
||||
private static final String PROTOCOL_PORT_PROPERTY_NAME_SUFFIX = DOT + PORT_PROPERTY_NAME;
|
||||
|
||||
private static final String PROTOCOL_PORT_PROPERTY_NAME = PROTOCOL_PROPERTY_NAME_PREFIX + PROTOCOL_PORT_PROPERTY_NAME_SUFFIX;
|
||||
|
||||
private static final String PROTOCOL_NAME_PROPERTY_NAME = PROTOCOL_PROPERTY_NAME_PREFIX + PROTOCOL_NAME_PROPERTY_NAME_SUFFIX;
|
||||
|
||||
private static final String PROTOCOLS_PROPERTY_NAME_PREFIX = "dubbo.protocols";
|
||||
|
||||
private static final String REST_PROTOCOL = "rest";
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(DubboNonWebApplicationEnvironmentPostProcessor.class);
|
||||
|
||||
@Override
|
||||
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
|
||||
WebApplicationType webApplicationType = application.getWebApplicationType();
|
||||
|
||||
if (!WebApplicationType.NONE.equals(webApplicationType)) { // Just works in Non-Web Application
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Current application is a Web Application, the process will be ignored.");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
resetServerPort(environment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset server port property if it's absent, whose value is configured by "dubbbo.protocol.port"
|
||||
* or "dubbo.protcols.rest.port"
|
||||
*
|
||||
* @param environment
|
||||
*/
|
||||
private void resetServerPort(ConfigurableEnvironment environment) {
|
||||
|
||||
String serverPort = environment.getProperty(SERVER_PORT_PROPERTY_NAME, environment.getProperty(PORT_PROPERTY_NAME));
|
||||
|
||||
if (serverPort != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
serverPort = getRestPortFromProtocolProperty(environment);
|
||||
|
||||
if (serverPort == null) {
|
||||
serverPort = getRestPortFromProtocolsProperties(environment);
|
||||
}
|
||||
|
||||
setServerPort(environment, serverPort);
|
||||
|
||||
}
|
||||
|
||||
private String getRestPortFromProtocolProperty(ConfigurableEnvironment environment) {
|
||||
|
||||
String protocol = environment.getProperty(PROTOCOL_NAME_PROPERTY_NAME, DEFAULT_PROTOCOL);
|
||||
|
||||
return isRestProtocol(protocol) ?
|
||||
environment.getProperty(PROTOCOL_PORT_PROPERTY_NAME) :
|
||||
null;
|
||||
}
|
||||
|
||||
private String getRestPortFromProtocolsProperties(ConfigurableEnvironment environment) {
|
||||
|
||||
String restPort = null;
|
||||
|
||||
Map<String, Object> subProperties = getSubProperties(environment, PROTOCOLS_PROPERTY_NAME_PREFIX);
|
||||
|
||||
Properties properties = new Properties();
|
||||
|
||||
properties.putAll(subProperties);
|
||||
|
||||
for (String propertyName : properties.stringPropertyNames()) {
|
||||
if (propertyName.endsWith(PROTOCOL_NAME_PROPERTY_NAME_SUFFIX)) { // protocol name property
|
||||
String protocol = properties.getProperty(propertyName);
|
||||
if (isRestProtocol(protocol)) {
|
||||
String beanName = resolveBeanName(propertyName);
|
||||
if (StringUtils.hasText(beanName)) {
|
||||
restPort = properties.getProperty(beanName + PROTOCOL_PORT_PROPERTY_NAME_SUFFIX);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return restPort;
|
||||
}
|
||||
|
||||
private String resolveBeanName(String propertyName) {
|
||||
int index = propertyName.indexOf(DOT);
|
||||
return index > -1 ? propertyName.substring(0, index) : null;
|
||||
}
|
||||
|
||||
private void setServerPort(ConfigurableEnvironment environment, String serverPort) {
|
||||
if (serverPort == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
MutablePropertySources propertySources = environment.getPropertySources();
|
||||
|
||||
Map<String, Object> properties = new HashMap<>();
|
||||
properties.put(SERVER_PORT_PROPERTY_NAME, String.valueOf(serverPort));
|
||||
|
||||
addOrReplace(propertySources, properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy from BusEnvironmentPostProcessor#addOrReplace(MutablePropertySources, Map)
|
||||
*
|
||||
* @param propertySources {@link MutablePropertySources}
|
||||
* @param map Default Dubbo Properties
|
||||
*/
|
||||
private void addOrReplace(MutablePropertySources propertySources,
|
||||
Map<String, Object> map) {
|
||||
MapPropertySource target = null;
|
||||
if (propertySources.contains(PROPERTY_SOURCE_NAME)) {
|
||||
PropertySource<?> source = propertySources.get(PROPERTY_SOURCE_NAME);
|
||||
if (source instanceof MapPropertySource) {
|
||||
target = (MapPropertySource) source;
|
||||
for (String key : map.keySet()) {
|
||||
if (!target.containsProperty(key)) {
|
||||
target.getSource().put(key, map.get(key));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (target == null) {
|
||||
target = new MapPropertySource(PROPERTY_SOURCE_NAME, map);
|
||||
}
|
||||
if (!propertySources.contains(PROPERTY_SOURCE_NAME)) {
|
||||
propertySources.addLast(target);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() { // Keep LOWEST_PRECEDENCE
|
||||
return LOWEST_PRECEDENCE;
|
||||
}
|
||||
|
||||
private static boolean isRestProtocol(String protocol) {
|
||||
return REST_PROTOCOL.equalsIgnoreCase(protocol);
|
||||
}
|
||||
}
|
@ -1,188 +1,188 @@
|
||||
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.GenericService;
|
||||
import org.springframework.cloud.alibaba.dubbo.http.MutableHttpServerRequest;
|
||||
import org.springframework.cloud.alibaba.dubbo.metadata.DubboServiceMetadata;
|
||||
import org.springframework.cloud.alibaba.dubbo.metadata.DubboTransportedMetadata;
|
||||
import org.springframework.cloud.alibaba.dubbo.metadata.RequestMetadata;
|
||||
import org.springframework.cloud.alibaba.dubbo.metadata.RestMethodMetadata;
|
||||
import org.springframework.cloud.alibaba.dubbo.metadata.repository.DubboServiceMetadataRepository;
|
||||
import org.springframework.cloud.alibaba.dubbo.service.DubboGenericServiceExecutionContext;
|
||||
import org.springframework.cloud.alibaba.dubbo.service.DubboGenericServiceExecutionContextFactory;
|
||||
import org.springframework.cloud.alibaba.dubbo.service.DubboGenericServiceFactory;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.PathMatcher;
|
||||
import org.springframework.util.StreamUtils;
|
||||
import org.springframework.web.util.UriComponents;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletInputStream;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.*;
|
||||
|
||||
import static org.springframework.web.util.UriComponentsBuilder.fromUriString;
|
||||
|
||||
@WebServlet(urlPatterns = "/dsc/*")
|
||||
public class DubboGatewayServlet extends HttpServlet {
|
||||
|
||||
private final DubboServiceMetadataRepository repository;
|
||||
|
||||
private final DubboTransportedMetadata dubboTransportedMetadata;
|
||||
|
||||
private final DubboGenericServiceFactory serviceFactory;
|
||||
|
||||
private final DubboGenericServiceExecutionContextFactory contextFactory;
|
||||
|
||||
private final PathMatcher pathMatcher = new AntPathMatcher();
|
||||
|
||||
public DubboGatewayServlet(DubboServiceMetadataRepository repository,
|
||||
DubboGenericServiceFactory serviceFactory,
|
||||
DubboGenericServiceExecutionContextFactory contextFactory) {
|
||||
this.repository = repository;
|
||||
this.dubboTransportedMetadata = new DubboTransportedMetadata();
|
||||
dubboTransportedMetadata.setProtocol("dubbo");
|
||||
dubboTransportedMetadata.setCluster("failover");
|
||||
this.serviceFactory = serviceFactory;
|
||||
this.contextFactory = contextFactory;
|
||||
}
|
||||
|
||||
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
||||
|
||||
// /g/{app-name}/{rest-path}
|
||||
String requestURI = request.getRequestURI();
|
||||
// /g/
|
||||
String servletPath = request.getServletPath();
|
||||
|
||||
String part = StringUtils.substringAfter(requestURI, servletPath);
|
||||
|
||||
String serviceName = StringUtils.substringBetween(part, "/", "/");
|
||||
|
||||
// App name= spring-cloud-alibaba-dubbo-web-provider (127.0.0.1:8080)
|
||||
|
||||
String restPath = StringUtils.substringAfter(part, serviceName);
|
||||
|
||||
// 初始化 serviceName 的 REST 请求元数据
|
||||
repository.initialize(serviceName);
|
||||
// 将 HttpServletRequest 转化为 RequestMetadata
|
||||
RequestMetadata clientMetadata = buildRequestMetadata(request, restPath);
|
||||
|
||||
DubboServiceMetadata dubboServiceMetadata = repository.get(serviceName, clientMetadata);
|
||||
|
||||
if (dubboServiceMetadata == null) {
|
||||
// if DubboServiceMetadata is not found, executes next
|
||||
throw new ServletException("DubboServiceMetadata can't be found!");
|
||||
}
|
||||
|
||||
RestMethodMetadata dubboRestMethodMetadata = dubboServiceMetadata.getRestMethodMetadata();
|
||||
|
||||
GenericService genericService = serviceFactory.create(dubboServiceMetadata, dubboTransportedMetadata);
|
||||
|
||||
// TODO: Get the Request Body from HttpServletRequest
|
||||
byte[] body = getRequestBody(request);
|
||||
|
||||
MutableHttpServerRequest httpServerRequest = new MutableHttpServerRequest(new HttpRequestAdapter(request), body);
|
||||
|
||||
// customizeRequest(httpServerRequest, dubboRestMethodMetadata, clientMetadata);
|
||||
|
||||
DubboGenericServiceExecutionContext context = contextFactory.create(dubboRestMethodMetadata, httpServerRequest);
|
||||
|
||||
Object result = null;
|
||||
GenericException exception = null;
|
||||
|
||||
try {
|
||||
result = genericService.$invoke(context.getMethodName(), context.getParameterTypes(), context.getParameters());
|
||||
} catch (GenericException e) {
|
||||
exception = e;
|
||||
}
|
||||
response.getWriter().println(result);
|
||||
}
|
||||
|
||||
private byte[] getRequestBody(HttpServletRequest request) throws IOException {
|
||||
ServletInputStream inputStream = request.getInputStream();
|
||||
return StreamUtils.copyToByteArray(inputStream);
|
||||
}
|
||||
|
||||
private static class HttpRequestAdapter implements HttpRequest {
|
||||
|
||||
private final HttpServletRequest request;
|
||||
|
||||
private HttpRequestAdapter(HttpServletRequest request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMethodValue() {
|
||||
return request.getMethod();
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getURI() {
|
||||
try {
|
||||
return new URI(request.getRequestURL().toString() + "?" + request.getQueryString());
|
||||
} catch (URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
return new HttpHeaders();
|
||||
}
|
||||
}
|
||||
|
||||
// protected void customizeRequest(MutableHttpServerRequest httpServerRequest,
|
||||
// RestMethodMetadata dubboRestMethodMetadata, RequestMetadata clientMetadata) {
|
||||
//
|
||||
// RequestMetadata dubboRequestMetadata = dubboRestMethodMetadata.getRequest();
|
||||
// String pathPattern = dubboRequestMetadata.getPath();
|
||||
//
|
||||
// Map<String, String> pathVariables = pathMatcher.extractUriTemplateVariables(pathPattern, httpServerRequest.getPath());
|
||||
//
|
||||
// if (!CollectionUtils.isEmpty(pathVariables)) {
|
||||
// // Put path variables Map into query parameters Map
|
||||
// httpServerRequest.params(pathVariables);
|
||||
//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.GenericService;
|
||||
//import org.springframework.cloud.alibaba.dubbo.http.MutableHttpServerRequest;
|
||||
//import org.springframework.cloud.alibaba.dubbo.metadata.DubboServiceMetadata;
|
||||
//import org.springframework.cloud.alibaba.dubbo.metadata.DubboTransportedMetadata;
|
||||
//import org.springframework.cloud.alibaba.dubbo.metadata.RequestMetadata;
|
||||
//import org.springframework.cloud.alibaba.dubbo.metadata.RestMethodMetadata;
|
||||
//import org.springframework.cloud.alibaba.dubbo.metadata.repository.DubboServiceMetadataRepository;
|
||||
//import org.springframework.cloud.alibaba.dubbo.service.DubboGenericServiceExecutionContext;
|
||||
//import org.springframework.cloud.alibaba.dubbo.service.DubboGenericServiceExecutionContextFactory;
|
||||
//import org.springframework.cloud.alibaba.dubbo.service.DubboGenericServiceFactory;
|
||||
//import org.springframework.http.HttpHeaders;
|
||||
//import org.springframework.http.HttpRequest;
|
||||
//import org.springframework.util.AntPathMatcher;
|
||||
//import org.springframework.util.CollectionUtils;
|
||||
//import org.springframework.util.PathMatcher;
|
||||
//import org.springframework.util.StreamUtils;
|
||||
//import org.springframework.web.util.UriComponents;
|
||||
//
|
||||
//import javax.servlet.ServletException;
|
||||
//import javax.servlet.ServletInputStream;
|
||||
//import javax.servlet.annotation.WebServlet;
|
||||
//import javax.servlet.http.HttpServlet;
|
||||
//import javax.servlet.http.HttpServletRequest;
|
||||
//import javax.servlet.http.HttpServletResponse;
|
||||
//import java.io.IOException;
|
||||
//import java.net.URI;
|
||||
//import java.net.URISyntaxException;
|
||||
//import java.util.*;
|
||||
//
|
||||
//import static org.springframework.web.util.UriComponentsBuilder.fromUriString;
|
||||
//
|
||||
//@WebServlet(urlPatterns = "/dsc/*")
|
||||
//public class DubboGatewayServlet extends HttpServlet {
|
||||
//
|
||||
// private final DubboServiceMetadataRepository repository;
|
||||
//
|
||||
// private final DubboTransportedMetadata dubboTransportedMetadata;
|
||||
//
|
||||
// private final DubboGenericServiceFactory serviceFactory;
|
||||
//
|
||||
// private final DubboGenericServiceExecutionContextFactory contextFactory;
|
||||
//
|
||||
// private final PathMatcher pathMatcher = new AntPathMatcher();
|
||||
//
|
||||
// public DubboGatewayServlet(DubboServiceMetadataRepository repository,
|
||||
// DubboGenericServiceFactory serviceFactory,
|
||||
// DubboGenericServiceExecutionContextFactory contextFactory) {
|
||||
// this.repository = repository;
|
||||
// this.dubboTransportedMetadata = new DubboTransportedMetadata();
|
||||
// dubboTransportedMetadata.setProtocol("dubbo");
|
||||
// dubboTransportedMetadata.setCluster("failover");
|
||||
// this.serviceFactory = serviceFactory;
|
||||
// this.contextFactory = contextFactory;
|
||||
// }
|
||||
//
|
||||
// public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
||||
//
|
||||
// // /g/{app-name}/{rest-path}
|
||||
// String requestURI = request.getRequestURI();
|
||||
// // /g/
|
||||
// String servletPath = request.getServletPath();
|
||||
//
|
||||
// String part = StringUtils.substringAfter(requestURI, servletPath);
|
||||
//
|
||||
// String serviceName = StringUtils.substringBetween(part, "/", "/");
|
||||
//
|
||||
// // App name= spring-cloud-alibaba-dubbo-web-provider (127.0.0.1:8080)
|
||||
//
|
||||
// String restPath = StringUtils.substringAfter(part, serviceName);
|
||||
//
|
||||
// // 初始化 serviceName 的 REST 请求元数据
|
||||
// repository.initialize(serviceName);
|
||||
// // 将 HttpServletRequest 转化为 RequestMetadata
|
||||
// RequestMetadata clientMetadata = buildRequestMetadata(request, restPath);
|
||||
//
|
||||
// DubboServiceMetadata dubboServiceMetadata = repository.get(serviceName, clientMetadata);
|
||||
//
|
||||
// if (dubboServiceMetadata == null) {
|
||||
// // if DubboServiceMetadata is not found, executes next
|
||||
// throw new ServletException("DubboServiceMetadata can't be found!");
|
||||
// }
|
||||
//
|
||||
// RestMethodMetadata dubboRestMethodMetadata = dubboServiceMetadata.getRestMethodMetadata();
|
||||
//
|
||||
// GenericService genericService = serviceFactory.create(dubboServiceMetadata, dubboTransportedMetadata);
|
||||
//
|
||||
// // TODO: Get the Request Body from HttpServletRequest
|
||||
// byte[] body = getRequestBody(request);
|
||||
//
|
||||
// MutableHttpServerRequest httpServerRequest = new MutableHttpServerRequest(new HttpRequestAdapter(request), body);
|
||||
//
|
||||
//// customizeRequest(httpServerRequest, dubboRestMethodMetadata, clientMetadata);
|
||||
//
|
||||
// DubboGenericServiceExecutionContext context = contextFactory.create(dubboRestMethodMetadata, httpServerRequest);
|
||||
//
|
||||
// Object result = null;
|
||||
// GenericException exception = null;
|
||||
//
|
||||
// try {
|
||||
// result = genericService.$invoke(context.getMethodName(), context.getParameterTypes(), context.getParameters());
|
||||
// } catch (GenericException e) {
|
||||
// exception = e;
|
||||
// }
|
||||
// response.getWriter().println(result);
|
||||
// }
|
||||
//
|
||||
// private byte[] getRequestBody(HttpServletRequest request) throws IOException {
|
||||
// ServletInputStream inputStream = request.getInputStream();
|
||||
// return StreamUtils.copyToByteArray(inputStream);
|
||||
// }
|
||||
//
|
||||
// private static class HttpRequestAdapter implements HttpRequest {
|
||||
//
|
||||
// private final HttpServletRequest request;
|
||||
//
|
||||
// private HttpRequestAdapter(HttpServletRequest request) {
|
||||
// this.request = request;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public String getMethodValue() {
|
||||
// return request.getMethod();
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public URI getURI() {
|
||||
// try {
|
||||
// return new URI(request.getRequestURL().toString() + "?" + request.getQueryString());
|
||||
// } catch (URISyntaxException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// throw new RuntimeException();
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public HttpHeaders getHeaders() {
|
||||
// return new HttpHeaders();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//// protected void customizeRequest(MutableHttpServerRequest httpServerRequest,
|
||||
//// RestMethodMetadata dubboRestMethodMetadata, RequestMetadata clientMetadata) {
|
||||
////
|
||||
//// RequestMetadata dubboRequestMetadata = dubboRestMethodMetadata.getRequest();
|
||||
//// String pathPattern = dubboRequestMetadata.getPath();
|
||||
////
|
||||
//// Map<String, String> pathVariables = pathMatcher.extractUriTemplateVariables(pathPattern, httpServerRequest.getPath());
|
||||
////
|
||||
//// if (!CollectionUtils.isEmpty(pathVariables)) {
|
||||
//// // Put path variables Map into query parameters Map
|
||||
//// httpServerRequest.params(pathVariables);
|
||||
//// }
|
||||
////
|
||||
//// }
|
||||
//
|
||||
// private RequestMetadata buildRequestMetadata(HttpServletRequest request, String restPath) {
|
||||
// UriComponents uriComponents = fromUriString(request.getRequestURI()).build(true);
|
||||
// RequestMetadata requestMetadata = new RequestMetadata();
|
||||
// requestMetadata.setPath(restPath);
|
||||
// requestMetadata.setMethod(request.getMethod());
|
||||
// requestMetadata.setParams(getParams(request));
|
||||
// requestMetadata.setHeaders(getHeaders(request));
|
||||
// return requestMetadata;
|
||||
// }
|
||||
//
|
||||
// private Map<String, List<String>> getHeaders(HttpServletRequest request) {
|
||||
// Map<String, List<String>> map = new LinkedHashMap<>();
|
||||
// Enumeration<String> headerNames = request.getHeaderNames();
|
||||
// while (headerNames.hasMoreElements()) {
|
||||
// String headerName = headerNames.nextElement();
|
||||
// Enumeration<String> headerValues = request.getHeaders(headerName);
|
||||
// map.put(headerName, Collections.list(headerValues));
|
||||
// }
|
||||
// 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;
|
||||
// }
|
||||
|
||||
private RequestMetadata buildRequestMetadata(HttpServletRequest request, String restPath) {
|
||||
UriComponents uriComponents = fromUriString(request.getRequestURI()).build(true);
|
||||
RequestMetadata requestMetadata = new RequestMetadata();
|
||||
requestMetadata.setPath(restPath);
|
||||
requestMetadata.setMethod(request.getMethod());
|
||||
requestMetadata.setParams(getParams(request));
|
||||
requestMetadata.setHeaders(getHeaders(request));
|
||||
return requestMetadata;
|
||||
}
|
||||
|
||||
private Map<String, List<String>> getHeaders(HttpServletRequest request) {
|
||||
Map<String, List<String>> map = new LinkedHashMap<>();
|
||||
Enumeration<String> headerNames = request.getHeaderNames();
|
||||
while (headerNames.hasMoreElements()) {
|
||||
String headerName = headerNames.nextElement();
|
||||
Enumeration<String> headerValues = request.getHeaders(headerName);
|
||||
map.put(headerName, Collections.list(headerValues));
|
||||
}
|
||||
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