HttpResponse优化
This commit is contained in:
@@ -523,7 +523,7 @@ public class HttpDispatcherServlet
|
|||||||
}
|
}
|
||||||
|
|
||||||
public HttpServlet findServletByTopic(String topic) {
|
public HttpServlet findServletByTopic(String topic) {
|
||||||
return filterServlets(x -> x._reqtopic != null && x._reqtopic.equals(topic))
|
return filterServlets(x -> Objects.equals(x._reqtopic, topic))
|
||||||
.findFirst()
|
.findFirst()
|
||||||
.orElse(null);
|
.orElse(null);
|
||||||
}
|
}
|
||||||
@@ -573,9 +573,9 @@ public class HttpDispatcherServlet
|
|||||||
public final HttpServlet servlet;
|
public final HttpServlet servlet;
|
||||||
|
|
||||||
public MappingEntry(String mapping, Predicate<String> predicate, HttpServlet servlet) {
|
public MappingEntry(String mapping, Predicate<String> predicate, HttpServlet servlet) {
|
||||||
this.mapping = mapping;
|
this.mapping = Objects.requireNonNull(mapping);
|
||||||
this.predicate = predicate;
|
this.predicate = predicate;
|
||||||
this.servlet = servlet;
|
this.servlet = Objects.requireNonNull(servlet);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -28,6 +28,10 @@ public abstract class HttpFilter extends Filter<HttpContext, HttpRequest, HttpRe
|
|||||||
request.setRequestPath(path);
|
request.setRequestPath(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void setBody(HttpRequest request, byte[] body) {
|
||||||
|
request.updateBody(body);
|
||||||
|
}
|
||||||
|
|
||||||
protected void setRemoteAddr(HttpRequest request, String remoteAddr) {
|
protected void setRemoteAddr(HttpRequest request, String remoteAddr) {
|
||||||
request.setRemoteAddr(remoteAddr);
|
request.setRemoteAddr(remoteAddr);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1878,6 +1878,13 @@ public class HttpRequest extends Request<HttpContext> {
|
|||||||
return body;
|
return body;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void updateBody(byte[] body) {
|
||||||
|
this.body.clear();
|
||||||
|
if (body != null) {
|
||||||
|
this.body.put(body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
parseBody();
|
parseBody();
|
||||||
|
|||||||
@@ -5,9 +5,6 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.net.http;
|
package org.redkale.net.http;
|
||||||
|
|
||||||
import static java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME;
|
|
||||||
import static org.redkale.util.Utility.append;
|
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
@@ -15,10 +12,12 @@ import java.nio.ByteBuffer;
|
|||||||
import java.nio.channels.*;
|
import java.nio.channels.*;
|
||||||
import java.nio.file.StandardOpenOption;
|
import java.nio.file.StandardOpenOption;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
|
import static java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import java.util.function.*;
|
import java.util.function.*;
|
||||||
import java.util.logging.*;
|
import java.util.logging.*;
|
||||||
|
import org.redkale.annotation.Nonnull;
|
||||||
import org.redkale.convert.*;
|
import org.redkale.convert.*;
|
||||||
import org.redkale.convert.json.*;
|
import org.redkale.convert.json.*;
|
||||||
import org.redkale.net.*;
|
import org.redkale.net.*;
|
||||||
@@ -26,6 +25,7 @@ import org.redkale.net.Filter;
|
|||||||
import org.redkale.service.RetResult;
|
import org.redkale.service.RetResult;
|
||||||
import org.redkale.util.*;
|
import org.redkale.util.*;
|
||||||
import org.redkale.util.AnyValue.Entry;
|
import org.redkale.util.AnyValue.Entry;
|
||||||
|
import static org.redkale.util.Utility.append;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Http响应包 与javax.servlet.http.HttpServletResponse 基本类似。 <br>
|
* Http响应包 与javax.servlet.http.HttpServletResponse 基本类似。 <br>
|
||||||
@@ -293,7 +293,12 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
return httpCodes.get(status);
|
return httpCodes.get(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected HttpRequest getRequest() {
|
/**
|
||||||
|
* 获取HttpRequest
|
||||||
|
*
|
||||||
|
* @return HttpRequest
|
||||||
|
*/
|
||||||
|
public HttpRequest getRequest() {
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -973,9 +978,9 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
*
|
*
|
||||||
* @param kill kill
|
* @param kill kill
|
||||||
* @param contentType ContentType
|
* @param contentType ContentType
|
||||||
* @param bs 输出内容
|
* @param bodyContent 输出内容
|
||||||
* @param offset 偏移量
|
* @param bodyOffset 偏移量
|
||||||
* @param length 长度
|
* @param bodyLength 长度
|
||||||
* @param callback Consumer
|
* @param callback Consumer
|
||||||
* @param attachment ConvertWriter
|
* @param attachment ConvertWriter
|
||||||
* @param <A> A
|
* @param <A> A
|
||||||
@@ -983,9 +988,9 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
protected <A> void finish(
|
protected <A> void finish(
|
||||||
boolean kill,
|
boolean kill,
|
||||||
final String contentType,
|
final String contentType,
|
||||||
final byte[] bs,
|
@Nonnull final byte[] bodyContent,
|
||||||
int offset,
|
int bodyOffset,
|
||||||
int length,
|
int bodyLength,
|
||||||
Consumer<A> callback,
|
Consumer<A> callback,
|
||||||
A attachment) {
|
A attachment) {
|
||||||
if (isClosed()) {
|
if (isClosed()) {
|
||||||
@@ -995,11 +1000,11 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
if (contentType != null) {
|
if (contentType != null) {
|
||||||
this.contentType = contentType;
|
this.contentType = contentType;
|
||||||
}
|
}
|
||||||
this.contentLength = length;
|
this.contentLength = bodyLength;
|
||||||
createHeader();
|
createHeader();
|
||||||
}
|
}
|
||||||
ByteArray data = headerArray;
|
ByteArray data = headerArray;
|
||||||
data.put(bs, offset, length);
|
data.put(bodyContent, bodyOffset, bodyLength);
|
||||||
if (callback != null) {
|
if (callback != null) {
|
||||||
callback.accept(attachment);
|
callback.accept(attachment);
|
||||||
}
|
}
|
||||||
@@ -1212,11 +1217,10 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
/**
|
/**
|
||||||
* 异步输出指定内容
|
* 异步输出指定内容
|
||||||
*
|
*
|
||||||
* @param <A> 泛型
|
|
||||||
* @param buffer 输出内容
|
* @param buffer 输出内容
|
||||||
* @param handler 异步回调函数
|
* @param handler 异步回调函数
|
||||||
*/
|
*/
|
||||||
protected <A> void sendBody(ByteBuffer buffer, CompletionHandler<Integer, Void> handler) {
|
protected void sendBody(ByteBuffer buffer, CompletionHandler<Integer, Void> handler) {
|
||||||
if (this.headWritedSize < 0) {
|
if (this.headWritedSize < 0) {
|
||||||
if (this.contentLength < 0) {
|
if (this.contentLength < 0) {
|
||||||
this.contentLength = buffer == null ? 0 : buffer.remaining();
|
this.contentLength = buffer == null ? 0 : buffer.remaining();
|
||||||
@@ -1433,6 +1437,16 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
return this.header.getValue(name) != null;
|
return this.header.getValue(name) != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取Header值
|
||||||
|
*
|
||||||
|
* @param name header名
|
||||||
|
* @return header值
|
||||||
|
*/
|
||||||
|
public String getHeader(String name) {
|
||||||
|
return this.header.getValue(name);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置Header值
|
* 设置Header值
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ import java.nio.channels.ReadableByteChannel;
|
|||||||
import java.nio.charset.*;
|
import java.nio.charset.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import org.redkale.annotation.Nonnull;
|
||||||
|
import org.redkale.annotation.Nullable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 简单的byte[]操作类。
|
* 简单的byte[]操作类。
|
||||||
@@ -934,7 +936,7 @@ public final class ByteArray implements ByteTuple {
|
|||||||
* @param values 一组byte值
|
* @param values 一组byte值
|
||||||
* @return ByteArray
|
* @return ByteArray
|
||||||
*/
|
*/
|
||||||
public ByteArray put(byte... values) {
|
public ByteArray put(@Nonnull byte... values) {
|
||||||
if (values.length < 1) {
|
if (values.length < 1) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -955,7 +957,7 @@ public final class ByteArray implements ByteTuple {
|
|||||||
* @param values 一组byte值
|
* @param values 一组byte值
|
||||||
* @return ByteArray
|
* @return ByteArray
|
||||||
*/
|
*/
|
||||||
public ByteArray put(int offset, byte... values) {
|
public ByteArray put(int offset, @Nonnull byte... values) {
|
||||||
if (values.length < 1) {
|
if (values.length < 1) {
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
}
|
}
|
||||||
@@ -971,7 +973,7 @@ public final class ByteArray implements ByteTuple {
|
|||||||
* @param length 长度
|
* @param length 长度
|
||||||
* @return ByteArray
|
* @return ByteArray
|
||||||
*/
|
*/
|
||||||
public ByteArray put(byte[] values, int offset, int length) {
|
public ByteArray put(@Nonnull byte[] values, int offset, int length) {
|
||||||
if (count >= content.length - length) {
|
if (count >= content.length - length) {
|
||||||
byte[] ns = new byte[content.length + Math.max(16, length)];
|
byte[] ns = new byte[content.length + Math.max(16, length)];
|
||||||
System.arraycopy(content, 0, ns, 0, count);
|
System.arraycopy(content, 0, ns, 0, count);
|
||||||
@@ -988,7 +990,7 @@ public final class ByteArray implements ByteTuple {
|
|||||||
* @param tuple 内容
|
* @param tuple 内容
|
||||||
* @return ByteArray
|
* @return ByteArray
|
||||||
*/
|
*/
|
||||||
public ByteArray put(ByteTuple tuple) {
|
public ByteArray put(@Nonnull ByteTuple tuple) {
|
||||||
return put(tuple.content(), tuple.offset(), tuple.length());
|
return put(tuple.content(), tuple.offset(), tuple.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1001,7 +1003,7 @@ public final class ByteArray implements ByteTuple {
|
|||||||
* @param length 长度
|
* @param length 长度
|
||||||
* @return ByteArray
|
* @return ByteArray
|
||||||
*/
|
*/
|
||||||
public ByteArray put(int poffset, byte[] values, int offset, int length) {
|
public ByteArray put(int poffset, @Nonnull byte[] values, int offset, int length) {
|
||||||
System.arraycopy(values, offset, content, poffset, length);
|
System.arraycopy(values, offset, content, poffset, length);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -1011,7 +1013,7 @@ public final class ByteArray implements ByteTuple {
|
|||||||
*
|
*
|
||||||
* @param buffer ByteBuffer
|
* @param buffer ByteBuffer
|
||||||
*/
|
*/
|
||||||
public void put(ByteBuffer buffer) {
|
public void put(@Nullable ByteBuffer buffer) {
|
||||||
if (buffer == null) {
|
if (buffer == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -1039,7 +1041,7 @@ public final class ByteArray implements ByteTuple {
|
|||||||
* @param length 长度
|
* @param length 长度
|
||||||
* @return ByteArray
|
* @return ByteArray
|
||||||
*/
|
*/
|
||||||
public ByteArray put(ByteArray array, int offset, int length) {
|
public ByteArray put(@Nonnull ByteArray array, int offset, int length) {
|
||||||
if (count >= content.length - length) {
|
if (count >= content.length - length) {
|
||||||
byte[] ns = new byte[content.length + Math.max(16, length)];
|
byte[] ns = new byte[content.length + Math.max(16, length)];
|
||||||
System.arraycopy(content, 0, ns, 0, count);
|
System.arraycopy(content, 0, ns, 0, count);
|
||||||
@@ -1057,7 +1059,7 @@ public final class ByteArray implements ByteTuple {
|
|||||||
* @param length 指定长度
|
* @param length 指定长度
|
||||||
* @return ByteArray
|
* @return ByteArray
|
||||||
*/
|
*/
|
||||||
public ByteArray put(ByteBuffer buffer, int length) {
|
public ByteArray put(@Nonnull ByteBuffer buffer, int length) {
|
||||||
int len = Math.min(buffer.remaining(), length);
|
int len = Math.min(buffer.remaining(), length);
|
||||||
if (len < 1) {
|
if (len < 1) {
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
Reference in New Issue
Block a user