This commit is contained in:
wentch
2016-01-18 12:19:17 +08:00
parent dc15a75f0e
commit 4463cc92a8
17 changed files with 108 additions and 78 deletions

View File

@@ -107,12 +107,8 @@ public final class NodeHttpServer extends NodeServer {
if (ws == null || ws.value().length == 0) continue;
final HttpServlet servlet = clazz.newInstance();
factory.inject(servlet, this);
String[] mappings = ws.value();
if (ws.repair() && !prefix.isEmpty()) {
for (int i = 0; i < mappings.length; i++) {
mappings[i] = prefix + mappings[i];
}
}
final String[] mappings = ws.value();
String pref = ws.repair() ? prefix : "";
DefaultAnyValue servletConf = (DefaultAnyValue) en.getProperty();
WebInitParam[] webparams = ws.initParams();
if (webparams.length > 0) {
@@ -121,8 +117,13 @@ public final class NodeHttpServer extends NodeServer {
servletConf.addValue(webparam.name(), webparam.value());
}
}
this.httpServer.addHttpServlet(servlet, servletConf, mappings);
if (ss != null) ss.add(new AbstractMap.SimpleEntry<>(clazz.getName(), mappings));
this.httpServer.addHttpServlet(servlet, pref, servletConf, mappings);
if (ss != null) {
for (int i = 0; i < mappings.length; i++) {
mappings[i] = pref + mappings[i];
}
ss.add(new AbstractMap.SimpleEntry<>(clazz.getName(), mappings));
}
}
if (ss != null) {
Collections.sort(ss, (AbstractMap.SimpleEntry<String, String[]> o1, AbstractMap.SimpleEntry<String, String[]> o2) -> o1.getKey().compareTo(o2.getKey()));

View File

@@ -13,12 +13,15 @@ import java.util.logging.*;
/**
*
* <p> 详情见: http://www.redkale.org
* <p>
* 详情见: http://www.redkale.org
*
* @author zhangjx
* @param <C> Context的子类型
* @param <R> Request的子类型
* @param <P> Response的子类型
*/
public abstract class PrepareServlet<R extends Request, P extends Response<R>> extends Servlet<R, P> {
public abstract class PrepareServlet<C extends Context, R extends Request<C>, P extends Response<R>> extends Servlet<C, R, P> {
protected final AtomicLong executeCounter = new AtomicLong(); //执行请求次数

View File

@@ -17,9 +17,9 @@ import org.redkale.convert.json.*;
*
* @author zhangjx
*/
public abstract class Request {
public abstract class Request<C extends Context> {
protected final Context context;
protected final C context;
protected final BsonConvert bsonConvert;
@@ -39,7 +39,7 @@ public abstract class Request {
protected final Map<String, Object> attributes = new HashMap<>();
protected Request(Context context) {
protected Request(C context) {
this.context = context;
this.bsonConvert = context.getBsonConvert();
this.jsonConvert = context.getJsonConvert();
@@ -104,7 +104,7 @@ public abstract class Request {
return attributes;
}
public Context getContext() {
public C getContext() {
return this.context;
}

View File

@@ -14,17 +14,18 @@ import java.io.IOException;
* 详情见: http://www.redkale.org
*
* @author zhangjx
* @param <C> Context的子类型
* @param <R> Request的子类型
* @param <P> Response的子类型
*/
public abstract class Servlet<R extends Request, P extends Response<R>> {
public abstract class Servlet<C extends Context, R extends Request<? extends C>, P extends Response<R>> {
public void init(Context context, AnyValue config) {
public void init(C context, AnyValue config) {
}
public abstract void execute(R request, P response) throws IOException;
public void destroy(Context context, AnyValue config) {
public void destroy(C context, AnyValue config) {
}
}

View File

@@ -7,7 +7,6 @@ package org.redkale.net.http;
import org.redkale.net.Response;
import org.redkale.net.Request;
import org.redkale.net.Context;
import org.redkale.util.AnyValue;
import java.io.IOException;
import java.lang.annotation.*;
@@ -120,8 +119,8 @@ public abstract class BasedHttpServlet extends HttpServlet {
}
@Override
public void init(Context context, AnyValue config) {
String path = ((HttpContext) context).getContextPath();
public void init(HttpContext context, AnyValue config) {
String path = _prefix == null ? "" : _prefix;
WebServlet ws = this.getClass().getAnnotation(WebServlet.class);
if (ws != null && !ws.repair()) path = "";
HashMap<String, Entry> map = load();
@@ -194,7 +193,7 @@ public abstract class BasedHttpServlet extends HttpServlet {
FieldVisitor fv;
MethodVisitor mv;
AnnotationVisitor av0;
final String factfield = "factServlet";
final String factfield = "_factServlet";
cw.visit(V1_8, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, newDynName, null, supDynName, null);
{
fv = cw.visitField(ACC_PUBLIC, factfield, interDesc, null, null);

View File

@@ -17,26 +17,22 @@ import org.redkale.watch.*;
/**
*
* <p> 详情见: http://www.redkale.org
* <p>
* 详情见: http://www.redkale.org
*
* @author zhangjx
*/
public class HttpContext extends Context {
protected final String contextPath;
protected final SecureRandom random = new SecureRandom();
public HttpContext(long serverStartTime, Logger logger, ExecutorService executor, int bufferCapacity, ObjectPool<ByteBuffer> bufferPool,
ObjectPool<Response> responsePool, int maxbody, Charset charset, InetSocketAddress address, PrepareServlet prepare,
WatchFactory watch, int readTimeoutSecond, int writeTimeoutSecond, String contextPath) {
WatchFactory watch, int readTimeoutSecond, int writeTimeoutSecond) {
super(serverStartTime, logger, executor, bufferCapacity, bufferPool, responsePool, maxbody, charset,
address, prepare, watch, readTimeoutSecond, writeTimeoutSecond);
this.contextPath = contextPath;
random.setSeed(Math.abs(System.nanoTime()));
}
public String getContextPath() {
return this.contextPath;
random.setSeed(Math.abs(System.nanoTime()));
}
protected String createSessionid() {

View File

@@ -23,7 +23,7 @@ import org.redkale.watch.*;
*
* @author zhangjx
*/
public final class HttpPrepareServlet extends PrepareServlet<HttpRequest, HttpResponse<HttpRequest>> {
public final class HttpPrepareServlet extends PrepareServlet<HttpContext, HttpRequest, HttpResponse<HttpRequest>> {
private final List<HttpServlet> servlets = new ArrayList<>();
@@ -34,11 +34,11 @@ public final class HttpPrepareServlet extends PrepareServlet<HttpRequest, HttpRe
private HttpServlet resourceHttpServlet = new HttpResourceServlet();
@Override
public void init(Context context, AnyValue config) {
public void init(HttpContext context, AnyValue config) {
this.servlets.stream().forEach(s -> {
s.init(context, s._conf);
});
final WatchFactory watch = ((HttpContext) context).getWatchFactory();
final WatchFactory watch = context.getWatchFactory();
if (watch != null) {
this.servlets.stream().forEach(s -> {
watch.inject(s);
@@ -83,14 +83,15 @@ public final class HttpPrepareServlet extends PrepareServlet<HttpRequest, HttpRe
}
}
public void addHttpServlet(HttpServlet servlet, AnyValue conf, String... mappings) {
public void addHttpServlet(HttpServlet servlet, String prefix, AnyValue conf, String... mappings) {
if (prefix == null) prefix = "";
for (String mapping : mappings) {
if (contains(mapping, '.', '*', '{', '[', '(', '|', '^', '$', '+', '?', '\\')) { //是否是正则表达式))
if (mapping.charAt(0) != '^') mapping = '^' + mapping;
if (mapping.endsWith("/*")) {
mapping = mapping.substring(0, mapping.length() - 1) + ".*";
mapping = prefix + mapping.substring(0, mapping.length() - 1) + ".*";
} else {
mapping += "$";
mapping = prefix + mapping + "$";
}
if (regArray == null) {
regArray = new SimpleEntry[1];
@@ -100,10 +101,11 @@ public final class HttpPrepareServlet extends PrepareServlet<HttpRequest, HttpRe
regArray[regArray.length - 1] = new SimpleEntry<>(Pattern.compile(mapping).asPredicate(), servlet);
}
} else if (mapping != null && !mapping.isEmpty()) {
strmaps.put(mapping, servlet);
strmaps.put(prefix + mapping, servlet);
}
}
servlet._conf = conf;
servlet._prefix = prefix == null ? "" : prefix;
this.servlets.add(servlet);
}
@@ -124,7 +126,7 @@ public final class HttpPrepareServlet extends PrepareServlet<HttpRequest, HttpRe
}
@Override
public void destroy(Context context, AnyValue config) {
public void destroy(HttpContext context, AnyValue config) {
this.resourceHttpServlet.destroy(context, config);
this.servlets.stream().forEach(s -> {
s.destroy(context, s._conf);

View File

@@ -23,19 +23,17 @@ import org.redkale.net.*;
* 获取页号: int page = request.getRequstURIPath("page:", 1);
* 获取行数: int size = request.getRequstURIPath("size:", 10);
*
* <p> 详情见: http://www.redkale.org
* <p>
* 详情见: http://www.redkale.org
*
* @author zhangjx
*/
public class HttpRequest extends Request {
public class HttpRequest extends Request<HttpContext> {
protected static final Charset UTF8 = Charset.forName("UTF-8");
protected static final String SESSIONID_NAME = "JSESSIONID";
private static final byte[] flashRequestContent1 = "<policy-file-request/>\0".getBytes();
private static final byte[] flashRequestContent2 = "<policy-file-request/>".getBytes();
private String method;
private String protocol;
@@ -68,7 +66,7 @@ public class HttpRequest extends Request {
private final String remoteAddrHeader;
public HttpRequest(Context context, String remoteAddrHeader) {
public HttpRequest(HttpContext context, String remoteAddrHeader) {
super(context);
this.remoteAddrHeader = remoteAddrHeader;
}
@@ -226,11 +224,6 @@ public class HttpRequest extends Request {
super.removeProperty(name);
}
@Override
public HttpContext getContext() {
return (HttpContext) this.context;
}
public String getRemoteAddr() {
if (remoteAddrHeader != null) {
String val = getHeader(remoteAddrHeader);

View File

@@ -16,12 +16,13 @@ import java.util.concurrent.atomic.*;
import java.util.function.*;
import java.util.logging.*;
import java.util.regex.*;
import org.redkale.net.*;
import org.redkale.util.*;
/**
*
* <p> 详情见: http://www.redkale.org
* <p>
* 详情见: http://www.redkale.org
*
* @author zhangjx
*/
public final class HttpResourceServlet extends HttpServlet {
@@ -98,7 +99,7 @@ public final class HttpResourceServlet extends HttpServlet {
protected Predicate<String> ranges;
@Override
public void init(Context context, AnyValue config) {
public void init(HttpContext context, AnyValue config) {
if (config != null) {
String rootstr = config.getValue("webroot", "root");
if (rootstr.indexOf(':') < 0 && rootstr.indexOf('/') != 0 && System.getProperty("APP_HOME") != null) {
@@ -140,7 +141,7 @@ public final class HttpResourceServlet extends HttpServlet {
}
@Override
public void destroy(Context context, AnyValue config) {
public void destroy(HttpContext context, AnyValue config) {
if (this.watchThread != null) {
try {
this.watchThread.watcher.close();

View File

@@ -15,13 +15,13 @@ import org.redkale.watch.*;
/**
*
* <p> 详情见: http://www.redkale.org
* <p>
* 详情见: http://www.redkale.org
*
* @author zhangjx
*/
public final class HttpServer extends Server {
private String contextPath;
public HttpServer() {
this(System.currentTimeMillis(), null);
}
@@ -34,11 +34,10 @@ public final class HttpServer extends Server {
public void init(AnyValue config) throws Exception {
super.init(config);
AnyValue conf = config == null ? null : config.getAnyValue("servlets");
this.contextPath = conf == null ? "" : conf.getValue("path", "");
}
public void addHttpServlet(HttpServlet servlet, AnyValue conf, String... mappings) {
((HttpPrepareServlet) this.prepare).addHttpServlet(servlet, conf, mappings);
public void addHttpServlet(HttpServlet servlet, final String prefix, AnyValue conf, String... mappings) {
((HttpPrepareServlet) this.prepare).addHttpServlet(servlet, prefix, conf, mappings);
}
@Override
@@ -120,7 +119,7 @@ public final class HttpServer extends Server {
AtomicLong cycleResponseCounter = watch == null ? new AtomicLong() : watch.createWatchNumber("HTTP_" + port + ".Response.cycleCounter");
ObjectPool<Response> responsePool = HttpResponse.createPool(createResponseCounter, cycleResponseCounter, this.responsePoolSize, null);
HttpContext httpcontext = new HttpContext(this.serverStartTime, this.logger, executor, rcapacity, bufferPool, responsePool,
this.maxbody, this.charset, this.address, this.prepare, this.watch, this.readTimeoutSecond, this.writeTimeoutSecond, contextPath);
this.maxbody, this.charset, this.address, this.prepare, this.watch, this.readTimeoutSecond, this.writeTimeoutSecond);
responsePool.setCreator((Object... params) -> new HttpResponse(httpcontext, new HttpRequest(httpcontext, addrHeader), addHeaders, setHeaders, defCookie));
return httpcontext;
}

View File

@@ -10,13 +10,17 @@ import org.redkale.util.*;
/**
*
* <p> 详情见: http://www.redkale.org
* <p>
* 详情见: http://www.redkale.org
*
* @author zhangjx
*/
public abstract class HttpServlet extends Servlet<HttpRequest, HttpResponse<HttpRequest>> {
public abstract class HttpServlet extends Servlet<HttpContext, HttpRequest, HttpResponse<HttpRequest>> {
AnyValue _conf; //当前HttpServlet的配置
String _prefix = ""; //当前HttpServlet的path前缀
@Override
public final boolean equals(Object obj) {
return obj != null && obj.getClass() == this.getClass();

View File

@@ -13,7 +13,6 @@ import java.security.*;
import java.util.*;
import java.util.logging.*;
import javax.annotation.*;
import org.redkale.net.*;
import org.redkale.util.*;
/**
@@ -64,7 +63,7 @@ public abstract class WebSocketServlet extends HttpServlet {
protected WebSocketEngine engine;
@Override
public void init(Context context, AnyValue conf) {
public void init(HttpContext context, AnyValue conf) {
InetSocketAddress addr = context.getServerAddress();
this.engine = new WebSocketEngine(addr.getHostString() + ":" + addr.getPort() + "-[" + name() + "]", this.node, logger);
this.node.putWebSocketEngine(engine);
@@ -73,7 +72,7 @@ public abstract class WebSocketServlet extends HttpServlet {
}
@Override
public void destroy(Context context, AnyValue conf) {
public void destroy(HttpContext context, AnyValue conf) {
this.node.destroy(conf);
super.destroy(context, conf);
engine.close();

View File

@@ -0,0 +1,29 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.redkale.net.sncp;
import java.net.*;
import java.nio.*;
import java.nio.charset.*;
import java.util.concurrent.*;
import java.util.logging.*;
import org.redkale.net.*;
import org.redkale.util.*;
import org.redkale.watch.*;
/**
*
* @author zhangjx
*/
public class SncpContext extends Context {
public SncpContext(long serverStartTime, Logger logger, ExecutorService executor, int bufferCapacity, ObjectPool<ByteBuffer> bufferPool,
ObjectPool<Response> responsePool, int maxbody, Charset charset, InetSocketAddress address, PrepareServlet prepare,
WatchFactory watch, int readTimeoutSecond, int writeTimeoutSecond) {
super(serverStartTime, logger, executor, bufferCapacity, bufferPool, responsePool, maxbody, charset,
address, prepare, watch, readTimeoutSecond, writeTimeoutSecond);
}
}

View File

@@ -6,7 +6,6 @@
package org.redkale.net.sncp;
import org.redkale.net.PrepareServlet;
import org.redkale.net.Context;
import org.redkale.util.AnyValue;
import java.io.IOException;
import java.nio.ByteBuffer;
@@ -15,10 +14,12 @@ import org.redkale.util.*;
/**
*
* <p> 详情见: http://www.redkale.org
* <p>
* 详情见: http://www.redkale.org
*
* @author zhangjx
*/
public class SncpPrepareServlet extends PrepareServlet<SncpRequest, SncpResponse> {
public class SncpPrepareServlet extends PrepareServlet<SncpContext, SncpRequest, SncpResponse> {
private static final ByteBuffer pongBuffer = ByteBuffer.wrap("PONG".getBytes()).asReadOnlyBuffer();
@@ -50,7 +51,7 @@ public class SncpPrepareServlet extends PrepareServlet<SncpRequest, SncpResponse
}
@Override
public void init(Context context, AnyValue config) {
public void init(SncpContext context, AnyValue config) {
Collection<Map<DLong, SncpServlet>> values = this.maps.values();
values.stream().forEach((en) -> {
en.values().stream().forEach(s -> s.init(context, s.conf));
@@ -58,7 +59,7 @@ public class SncpPrepareServlet extends PrepareServlet<SncpRequest, SncpResponse
}
@Override
public void destroy(Context context, AnyValue config) {
public void destroy(SncpContext context, AnyValue config) {
Collection<Map<DLong, SncpServlet>> values = this.maps.values();
values.stream().forEach((en) -> {
en.values().stream().forEach(s -> s.destroy(context, s.conf));

View File

@@ -16,7 +16,7 @@ import org.redkale.util.*;
* <p> 详情见: http://www.redkale.org
* @author zhangjx
*/
public final class SncpRequest extends Request {
public final class SncpRequest extends Request<SncpContext> {
public static final int HEADER_SIZE = 72;
@@ -42,7 +42,7 @@ public final class SncpRequest extends Request {
private byte[] bufferbytes = new byte[6];
protected SncpRequest(Context context) {
protected SncpRequest(SncpContext context) {
super(context);
this.convert = context.getBsonConvert();
}

View File

@@ -62,7 +62,7 @@ public final class SncpServer extends Server {
AtomicLong createResponseCounter = watch == null ? new AtomicLong() : watch.createWatchNumber("SNCP_" + port + ".Response.creatCounter");
AtomicLong cycleResponseCounter = watch == null ? new AtomicLong() : watch.createWatchNumber("SNCP_" + port + ".Response.cycleCounter");
ObjectPool<Response> responsePool = SncpResponse.createPool(createResponseCounter, cycleResponseCounter, this.responsePoolSize, null);
Context sncpcontext = new Context(this.serverStartTime, this.logger, executor, rcapacity, bufferPool, responsePool,
SncpContext sncpcontext = new SncpContext(this.serverStartTime, this.logger, executor, rcapacity, bufferPool, responsePool,
this.maxbody, this.charset, this.address, this.prepare, this.watch, this.readTimeoutSecond, this.writeTimeoutSecond);
responsePool.setCreator((Object... params) -> new SncpResponse(sncpcontext, new SncpRequest(sncpcontext)));
return sncpcontext;

View File

@@ -5,15 +5,17 @@
*/
package org.redkale.net.sncp;
import org.redkale.net.Servlet;
import org.redkale.net.*;
import org.redkale.util.*;
/**
*
* <p> 详情见: http://www.redkale.org
* <p>
* 详情见: http://www.redkale.org
*
* @author zhangjx
*/
public abstract class SncpServlet extends Servlet<SncpRequest, SncpResponse> implements Comparable<SncpServlet> {
public abstract class SncpServlet extends Servlet<SncpContext, SncpRequest, SncpResponse> implements Comparable<SncpServlet> {
AnyValue conf;
@@ -30,7 +32,7 @@ public abstract class SncpServlet extends Servlet<SncpRequest, SncpResponse> imp
public final int hashCode() {
return this.getClass().hashCode();
}
@Override
public int compareTo(SncpServlet o) {
return 0;