This commit is contained in:
@@ -73,9 +73,11 @@ public abstract class PrepareServlet<K extends Serializable, C extends Context,
|
|||||||
|
|
||||||
protected void removeMapping(K key) {
|
protected void removeMapping(K key) {
|
||||||
synchronized (lock2) {
|
synchronized (lock2) {
|
||||||
Map<K, S> newmappings = new HashMap<>(mappings);
|
if (mappings.containsKey(key)) {
|
||||||
newmappings.remove(key);
|
Map<K, S> newmappings = new HashMap<>(mappings);
|
||||||
this.mappings = newmappings;
|
newmappings.remove(key);
|
||||||
|
this.mappings = newmappings;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import java.util.function.*;
|
|||||||
import java.util.logging.*;
|
import java.util.logging.*;
|
||||||
import java.util.regex.*;
|
import java.util.regex.*;
|
||||||
import org.redkale.net.*;
|
import org.redkale.net.*;
|
||||||
|
import org.redkale.net.http.Rest.RestDynSourceType;
|
||||||
import org.redkale.util.*;
|
import org.redkale.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -27,14 +28,14 @@ public class HttpPrepareServlet extends PrepareServlet<String, HttpContext, Http
|
|||||||
|
|
||||||
protected final Logger logger = Logger.getLogger(this.getClass().getSimpleName());
|
protected final Logger logger = Logger.getLogger(this.getClass().getSimpleName());
|
||||||
|
|
||||||
|
protected HttpServlet resourceHttpServlet = new HttpResourceServlet();
|
||||||
|
|
||||||
protected MappingEntry[] regArray = null; //regArray 包含 regWsArray
|
protected MappingEntry[] regArray = null; //regArray 包含 regWsArray
|
||||||
|
|
||||||
protected MappingEntry[] regWsArray = null;
|
protected MappingEntry[] regWsArray = null;
|
||||||
|
|
||||||
protected Map<String, WebSocketServlet> wsmappings = new HashMap<>(); //super.mappings 包含 wsmappings
|
protected Map<String, WebSocketServlet> wsmappings = new HashMap<>(); //super.mappings 包含 wsmappings
|
||||||
|
|
||||||
protected HttpServlet resourceHttpServlet = new HttpResourceServlet();
|
|
||||||
|
|
||||||
protected final Map<String, Class> allMapStrings = new HashMap<>();
|
protected final Map<String, Class> allMapStrings = new HashMap<>();
|
||||||
|
|
||||||
private final Object excludeLock = new Object();
|
private final Object excludeLock = new Object();
|
||||||
@@ -43,28 +44,78 @@ public class HttpPrepareServlet extends PrepareServlet<String, HttpContext, Http
|
|||||||
|
|
||||||
private BiPredicate<String, String>[] forbidURIPredicates; //禁用的URL的Predicate, 必须与 forbidURIMaps 保持一致
|
private BiPredicate<String, String>[] forbidURIPredicates; //禁用的URL的Predicate, 必须与 forbidURIMaps 保持一致
|
||||||
|
|
||||||
public HttpServlet removeHttpServlet(HttpServlet servlet) {
|
private List<HttpServlet> removeHttpServlet(final Predicate<MappingEntry> predicateEntry, final Predicate<Map.Entry<String, WebSocketServlet>> predicateFilter) {
|
||||||
HttpServlet rs = null;
|
List<HttpServlet> servlets = new ArrayList<>();
|
||||||
synchronized (allMapStrings) {
|
synchronized (allMapStrings) {
|
||||||
//待开发
|
List<String> keys = new ArrayList<>();
|
||||||
|
if (regArray != null) {
|
||||||
|
for (MappingEntry me : regArray) {
|
||||||
|
if (predicateEntry.test(me)) {
|
||||||
|
servlets.add(me.servlet);
|
||||||
|
keys.add(me.mapping);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (regWsArray != null) {
|
||||||
|
for (MappingEntry me : regWsArray) {
|
||||||
|
if (predicateEntry.test(me)) {
|
||||||
|
servlets.add(me.servlet);
|
||||||
|
keys.add(me.mapping);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Map<String, WebSocketServlet> newwsmappings = new HashMap<>();
|
||||||
|
for (Map.Entry<String, WebSocketServlet> en : wsmappings.entrySet()) {
|
||||||
|
if (predicateFilter.test(en)) {
|
||||||
|
servlets.add(en.getValue());
|
||||||
|
keys.add(en.getKey());
|
||||||
|
} else {
|
||||||
|
newwsmappings.put(en.getKey(), en.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (newwsmappings.size() != wsmappings.size()) this.wsmappings = newwsmappings;
|
||||||
|
if (!keys.isEmpty()) {
|
||||||
|
this.regArray = Utility.remove(this.regArray, predicateEntry);
|
||||||
|
this.regWsArray = Utility.remove(this.regWsArray, predicateEntry);
|
||||||
|
for (HttpServlet rs : servlets) {
|
||||||
|
super.removeServlet(rs);
|
||||||
|
}
|
||||||
|
for (String key : keys) {
|
||||||
|
super.removeMapping(key);
|
||||||
|
allMapStrings.remove(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return rs;
|
return servlets;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T extends HttpServlet> HttpServlet removeHttpServlet(Class<T> servletType) {
|
public HttpServlet removeHttpServlet(final HttpServlet servlet) {
|
||||||
HttpServlet rs = null;
|
Predicate<MappingEntry> predicateEntry = (t) -> t.servlet == servlet;
|
||||||
synchronized (allMapStrings) {
|
Predicate<Map.Entry<String, WebSocketServlet>> predicateFilter = (t) -> t.getValue() == servlet;
|
||||||
//待开发
|
removeHttpServlet(predicateEntry, predicateFilter);
|
||||||
}
|
return servlet;
|
||||||
return rs;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpServlet removeHttpServlet(String mapping) {
|
public <T extends HttpServlet> List<HttpServlet> removeHttpServlet(final Class<T> servletOrServiceOrWsType) {
|
||||||
HttpServlet rs = null;
|
Predicate<MappingEntry> predicateEntry = (t) -> {
|
||||||
synchronized (allMapStrings) {
|
Class type = t.servlet.getClass();
|
||||||
//待开发
|
if (type == servletOrServiceOrWsType) return true;
|
||||||
}
|
RestDynSourceType rdt = (RestDynSourceType) type.getAnnotation(RestDynSourceType.class);
|
||||||
return rs;
|
return (rdt != null && rdt.value() == servletOrServiceOrWsType);
|
||||||
|
};
|
||||||
|
Predicate<Map.Entry<String, WebSocketServlet>> predicateFilter = (t) -> {
|
||||||
|
Class type = t.getValue().getClass();
|
||||||
|
if (type == servletOrServiceOrWsType) return true;
|
||||||
|
RestDynSourceType rdt = (RestDynSourceType) type.getAnnotation(RestDynSourceType.class);
|
||||||
|
return (rdt != null && rdt.value() == servletOrServiceOrWsType);
|
||||||
|
};
|
||||||
|
return removeHttpServlet(predicateEntry, predicateFilter);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<HttpServlet> removeHttpServlet(final String mapping) {
|
||||||
|
Predicate<MappingEntry> predicateEntry = (t) -> t.mapping.equals(mapping);
|
||||||
|
Predicate<Map.Entry<String, WebSocketServlet>> predicateFilter = (t) -> t.getKey().equals(mapping);
|
||||||
|
return removeHttpServlet(predicateEntry, predicateFilter);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean addForbidURIReg(final String urlreg) {
|
public boolean addForbidURIReg(final String urlreg) {
|
||||||
|
|||||||
@@ -71,13 +71,12 @@ public class HttpServer extends Server<String, HttpContext, HttpRequest, HttpRes
|
|||||||
/**
|
/**
|
||||||
* 删除HttpServlet
|
* 删除HttpServlet
|
||||||
*
|
*
|
||||||
* @param <T> 泛型
|
* @param servletOrServiceOrWsType Class
|
||||||
* @param servletType Class
|
|
||||||
*
|
*
|
||||||
* @return HttpServlet
|
* @return HttpServlet
|
||||||
*/
|
*/
|
||||||
public <T extends HttpServlet> HttpServlet removeHttpServlet(Class<T> servletType) {
|
public List<HttpServlet> removeHttpServlet(Class servletOrServiceOrWsType) {
|
||||||
return ((HttpPrepareServlet) this.prepare).removeHttpServlet(servletType);
|
return ((HttpPrepareServlet) this.prepare).removeHttpServlet(servletOrServiceOrWsType);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -87,7 +86,7 @@ public class HttpServer extends Server<String, HttpContext, HttpRequest, HttpRes
|
|||||||
*
|
*
|
||||||
* @return HttpServlet
|
* @return HttpServlet
|
||||||
*/
|
*/
|
||||||
public HttpServlet removeHttpServlet(String mapping) {
|
public List<HttpServlet> removeHttpServlet(String mapping) {
|
||||||
return ((HttpPrepareServlet) this.prepare).removeHttpServlet(mapping);
|
return ((HttpPrepareServlet) this.prepare).removeHttpServlet(mapping);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -58,6 +58,18 @@ public final class Rest {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用于标记由Rest.createRestServlet 方法创建的RestServlet
|
||||||
|
*/
|
||||||
|
@Inherited
|
||||||
|
@Documented
|
||||||
|
@Target({TYPE})
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
public static @interface RestDynSourceType {
|
||||||
|
|
||||||
|
Class value();
|
||||||
|
}
|
||||||
|
|
||||||
private Rest() {
|
private Rest() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,6 +198,11 @@ public final class Rest {
|
|||||||
AnnotationVisitor av0;
|
AnnotationVisitor av0;
|
||||||
cw.visit(V1_8, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, newDynName, null, supDynName, null);
|
cw.visit(V1_8, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, newDynName, null, supDynName, null);
|
||||||
|
|
||||||
|
{ //RestDynSourceType
|
||||||
|
av0 = cw.visitAnnotation(Type.getDescriptor(RestDynSourceType.class), true);
|
||||||
|
av0.visit("value", Type.getType(Type.getDescriptor(webSocketType)));
|
||||||
|
av0.visitEnd();
|
||||||
|
}
|
||||||
{ //注入 @WebServlet 注解
|
{ //注入 @WebServlet 注解
|
||||||
String urlpath = (rws.catalog().isEmpty() ? "/" : ("/" + rws.catalog() + "/")) + rws.name();
|
String urlpath = (rws.catalog().isEmpty() ? "/" : ("/" + rws.catalog() + "/")) + rws.name();
|
||||||
av0 = cw.visitAnnotation(webServletDesc, true);
|
av0 = cw.visitAnnotation(webServletDesc, true);
|
||||||
@@ -521,7 +538,11 @@ public final class Rest {
|
|||||||
av0 = cw.visitAnnotation(Type.getDescriptor(RestDynamic.class), true);
|
av0 = cw.visitAnnotation(Type.getDescriptor(RestDynamic.class), true);
|
||||||
av0.visitEnd();
|
av0.visitEnd();
|
||||||
}
|
}
|
||||||
|
{ //RestDynSourceType
|
||||||
|
av0 = cw.visitAnnotation(Type.getDescriptor(RestDynSourceType.class), true);
|
||||||
|
av0.visit("value", Type.getType(Type.getDescriptor(serviceType)));
|
||||||
|
av0.visitEnd();
|
||||||
|
}
|
||||||
{ //注入 @WebServlet 注解
|
{ //注入 @WebServlet 注解
|
||||||
String urlpath = (catalog.isEmpty() ? "/" : ("/" + catalog + "/")) + defmodulename + "/*";
|
String urlpath = (catalog.isEmpty() ? "/" : ("/" + catalog + "/")) + defmodulename + "/*";
|
||||||
int moduleid = controller == null ? 0 : controller.moduleid();
|
int moduleid = controller == null ? 0 : controller.moduleid();
|
||||||
|
|||||||
Reference in New Issue
Block a user