mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
新增restful模式
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
package com.gitee.sop.support.constant;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* @author 六如
|
||||
*/
|
||||
public final class SopConstants {
|
||||
|
||||
private SopConstants() {
|
||||
}
|
||||
|
||||
public static final Charset CHARSET_UTF8 = StandardCharsets.UTF_8;
|
||||
public static final String UTF8 = "UTF-8";
|
||||
public static final String NULL = "null";
|
||||
public static final String DUBBO_TAG = "dubbo.tag";
|
||||
public static final String OPEN_CONTEXT = "sop.open-context";
|
||||
public static final String WEB_CONTEXT = "sop.web-context";
|
||||
public static final String DEFAULT_VERSION = "1.0";
|
||||
|
||||
}
|
@@ -5,7 +5,6 @@ import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author 六如
|
||||
@@ -55,11 +54,6 @@ public class DefaultOpenContext extends OpenContext implements Serializable {
|
||||
*/
|
||||
private Locale locale;
|
||||
|
||||
/**
|
||||
* 请求头
|
||||
*/
|
||||
private Map<String, String> headers;
|
||||
|
||||
public void initContext() {
|
||||
this.setContext(this);
|
||||
}
|
||||
|
@@ -0,0 +1,66 @@
|
||||
package com.gitee.sop.support.context;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author 六如
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class DefaultWebContext extends WebContext implements Serializable {
|
||||
private static final long serialVersionUID = -6070543618514206391L;
|
||||
|
||||
private String method;
|
||||
private String pathInfo;
|
||||
private String pathTranslated;
|
||||
private String contextPath;
|
||||
private String queryString;
|
||||
private String requestURI;
|
||||
private StringBuffer requestURL;
|
||||
private String servletPath;
|
||||
private int contentLength;
|
||||
private String contentType;
|
||||
private String remoteAddr;
|
||||
private String remoteHost;
|
||||
private int remotePort;
|
||||
private Locale locale;
|
||||
private Map<String, String> headers;
|
||||
private Map<String, String[]> paramtreMap;
|
||||
|
||||
@Override
|
||||
public String getParameter(String name) {
|
||||
String[] value = paramtreMap.get(name);
|
||||
return value == null || value.length == 0 ? null : String.join(",", value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getParameterNames() {
|
||||
return new ArrayList<>(paramtreMap.keySet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getParameterValues(String name) {
|
||||
return paramtreMap.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String[]> getParameterMap() {
|
||||
return paramtreMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getHeaders() {
|
||||
return headers;
|
||||
}
|
||||
|
||||
public void initContext() {
|
||||
this.setContext(this);
|
||||
}
|
||||
}
|
@@ -3,7 +3,6 @@ package com.gitee.sop.support.context;
|
||||
import com.alibaba.ttl.TransmittableThreadLocal;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author 六如
|
||||
@@ -52,11 +51,6 @@ public abstract class OpenContext {
|
||||
*/
|
||||
public abstract Locale getLocale();
|
||||
|
||||
/**
|
||||
* 获取请求头
|
||||
*/
|
||||
public abstract Map<String, String> getHeaders();
|
||||
|
||||
protected void setContext(OpenContext openContext) {
|
||||
THREAD_LOCAL.set(openContext);
|
||||
}
|
||||
|
@@ -0,0 +1,73 @@
|
||||
package com.gitee.sop.support.context;
|
||||
|
||||
import com.alibaba.ttl.TransmittableThreadLocal;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author 六如
|
||||
*/
|
||||
public abstract class WebContext {
|
||||
|
||||
private static final ThreadLocal<WebContext> THREAD_LOCAL = new TransmittableThreadLocal<>();
|
||||
|
||||
|
||||
public abstract String getMethod();
|
||||
|
||||
public abstract String getPathInfo();
|
||||
|
||||
public abstract String getPathTranslated();
|
||||
|
||||
public abstract String getContextPath();
|
||||
|
||||
public abstract String getQueryString();
|
||||
|
||||
public abstract String getRequestURI();
|
||||
|
||||
public abstract StringBuffer getRequestURL();
|
||||
|
||||
public abstract String getServletPath();
|
||||
|
||||
public abstract int getContentLength();
|
||||
|
||||
public abstract String getContentType();
|
||||
|
||||
public abstract String getRemoteAddr();
|
||||
|
||||
public abstract String getRemoteHost();
|
||||
|
||||
public abstract int getRemotePort();
|
||||
|
||||
public abstract String getParameter(String name);
|
||||
|
||||
public abstract List<String> getParameterNames();
|
||||
|
||||
public abstract String[] getParameterValues(String name);
|
||||
|
||||
public abstract Map<String, String[]> getParameterMap();
|
||||
|
||||
/**
|
||||
* 获取locale
|
||||
*/
|
||||
public abstract Locale getLocale();
|
||||
|
||||
/**
|
||||
* 获取请求头
|
||||
*/
|
||||
public abstract Map<String, String> getHeaders();
|
||||
|
||||
protected void setContext(WebContext openContext) {
|
||||
THREAD_LOCAL.set(openContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前WebContext
|
||||
*
|
||||
* @return 返回WebContext
|
||||
*/
|
||||
public static WebContext current() {
|
||||
return THREAD_LOCAL.get();
|
||||
}
|
||||
}
|
@@ -1,7 +1,10 @@
|
||||
package com.gitee.sop.support.dubbo;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.gitee.sop.support.constant.SopConstants;
|
||||
import com.gitee.sop.support.context.DefaultOpenContext;
|
||||
import com.gitee.sop.support.context.DefaultWebContext;
|
||||
import com.gitee.sop.support.context.WebContext;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.dubbo.common.constants.CommonConstants;
|
||||
import org.apache.dubbo.common.extension.Activate;
|
||||
@@ -21,11 +24,10 @@ import org.apache.dubbo.rpc.RpcServiceContext;
|
||||
@Activate(group = {CommonConstants.PROVIDER})
|
||||
public class DubboProviderTraceFilter implements Filter {
|
||||
|
||||
private static final String OPEN_CONTEXT = "open.context";
|
||||
|
||||
@Override
|
||||
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
|
||||
initOpenContext();
|
||||
initWebContext();
|
||||
long startTime = System.currentTimeMillis();
|
||||
long endTime = 0;
|
||||
try {
|
||||
@@ -40,10 +42,19 @@ public class DubboProviderTraceFilter implements Filter {
|
||||
private void initOpenContext() {
|
||||
// 从 ServerAttachment 中读取的参数是从 Client 中传递过来的
|
||||
RpcContextAttachment serverAttachment = RpcContext.getServerAttachment();
|
||||
String attachment = serverAttachment.getAttachment(OPEN_CONTEXT);
|
||||
if (attachment != null) {
|
||||
DefaultOpenContext defaultOpenContext = JSON.parseObject(attachment, DefaultOpenContext.class);
|
||||
defaultOpenContext.initContext();
|
||||
Object objectAttachment = serverAttachment.getObjectAttachment(SopConstants.OPEN_CONTEXT);
|
||||
if (objectAttachment instanceof DefaultOpenContext) {
|
||||
DefaultOpenContext openContext = (DefaultOpenContext) objectAttachment;
|
||||
openContext.initContext();
|
||||
}
|
||||
}
|
||||
|
||||
private void initWebContext() {
|
||||
RpcContextAttachment serverAttachment = RpcContext.getServerAttachment();
|
||||
Object objectAttachment = serverAttachment.getObjectAttachment(SopConstants.WEB_CONTEXT);
|
||||
if (objectAttachment instanceof DefaultWebContext) {
|
||||
DefaultWebContext webContext = (DefaultWebContext) objectAttachment;
|
||||
webContext.initContext();
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,2 +1,3 @@
|
||||
com.gitee.sop.support.dto.CommonFileData
|
||||
com.gitee.sop.support.context.DefaultOpenContext
|
||||
com.gitee.sop.support.context.DefaultWebContext
|
Reference in New Issue
Block a user