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