Response增加output字段,便于RecycleListener获取输出结果
This commit is contained in:
@@ -29,7 +29,9 @@ public abstract class Response<C extends Context, R extends Request<C>> {
|
|||||||
|
|
||||||
private boolean inited = true;
|
private boolean inited = true;
|
||||||
|
|
||||||
protected BiConsumer<R, Response<C, R>> recycleListener;
|
protected Object output; //输出的结果对象
|
||||||
|
|
||||||
|
protected BiConsumer<R, Response<C, R>> recycleListener;
|
||||||
|
|
||||||
private final CompletionHandler finishHandler = new CompletionHandler<Integer, ByteBuffer>() {
|
private final CompletionHandler finishHandler = new CompletionHandler<Integer, ByteBuffer>() {
|
||||||
|
|
||||||
@@ -113,6 +115,7 @@ public abstract class Response<C extends Context, R extends Request<C>> {
|
|||||||
}
|
}
|
||||||
recycleListener = null;
|
recycleListener = null;
|
||||||
}
|
}
|
||||||
|
this.output = null;
|
||||||
request.recycle();
|
request.recycle();
|
||||||
if (channel != null) {
|
if (channel != null) {
|
||||||
if (keepAlive) {
|
if (keepAlive) {
|
||||||
@@ -143,6 +146,10 @@ public abstract class Response<C extends Context, R extends Request<C>> {
|
|||||||
this.recycleListener = recycleListener;
|
this.recycleListener = recycleListener;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Object getOutput() {
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
public void finish() {
|
public void finish() {
|
||||||
this.finish(false);
|
this.finish(false);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -225,6 +225,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
*/
|
*/
|
||||||
public void finishJson(final Object obj) {
|
public void finishJson(final Object obj) {
|
||||||
this.contentType = "text/plain; charset=utf-8";
|
this.contentType = "text/plain; charset=utf-8";
|
||||||
|
if (this.recycleListener != null) this.output = obj;
|
||||||
finish(request.getJsonConvert().convertTo(context.getBufferSupplier(), obj));
|
finish(request.getJsonConvert().convertTo(context.getBufferSupplier(), obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -236,6 +237,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
*/
|
*/
|
||||||
public void finishJson(final JsonConvert convert, final Object obj) {
|
public void finishJson(final JsonConvert convert, final Object obj) {
|
||||||
this.contentType = "text/plain; charset=utf-8";
|
this.contentType = "text/plain; charset=utf-8";
|
||||||
|
if (this.recycleListener != null) this.output = obj;
|
||||||
finish(convert.convertTo(context.getBufferSupplier(), obj));
|
finish(convert.convertTo(context.getBufferSupplier(), obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,6 +249,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
*/
|
*/
|
||||||
public void finishJson(final Type type, final Object obj) {
|
public void finishJson(final Type type, final Object obj) {
|
||||||
this.contentType = "text/plain; charset=utf-8";
|
this.contentType = "text/plain; charset=utf-8";
|
||||||
|
this.output = obj;
|
||||||
finish(request.getJsonConvert().convertTo(context.getBufferSupplier(), type, obj));
|
finish(request.getJsonConvert().convertTo(context.getBufferSupplier(), type, obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -259,6 +262,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
*/
|
*/
|
||||||
public void finishJson(final JsonConvert convert, final Type type, final Object obj) {
|
public void finishJson(final JsonConvert convert, final Type type, final Object obj) {
|
||||||
this.contentType = "text/plain; charset=utf-8";
|
this.contentType = "text/plain; charset=utf-8";
|
||||||
|
if (this.recycleListener != null) this.output = obj;
|
||||||
finish(convert.convertTo(context.getBufferSupplier(), type, obj));
|
finish(convert.convertTo(context.getBufferSupplier(), type, obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -269,6 +273,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
*/
|
*/
|
||||||
public void finishJson(final Object... objs) {
|
public void finishJson(final Object... objs) {
|
||||||
this.contentType = "text/plain; charset=utf-8";
|
this.contentType = "text/plain; charset=utf-8";
|
||||||
|
if (this.recycleListener != null) this.output = objs;
|
||||||
finish(request.getJsonConvert().convertTo(context.getBufferSupplier(), objs));
|
finish(request.getJsonConvert().convertTo(context.getBufferSupplier(), objs));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -279,6 +284,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
*/
|
*/
|
||||||
public void finishJson(final org.redkale.service.RetResult ret) {
|
public void finishJson(final org.redkale.service.RetResult ret) {
|
||||||
this.contentType = "text/plain; charset=utf-8";
|
this.contentType = "text/plain; charset=utf-8";
|
||||||
|
if (this.recycleListener != null) this.output = ret;
|
||||||
if (ret != null && !ret.isSuccess()) {
|
if (ret != null && !ret.isSuccess()) {
|
||||||
this.header.addValue("retcode", String.valueOf(ret.getRetcode()));
|
this.header.addValue("retcode", String.valueOf(ret.getRetcode()));
|
||||||
this.header.addValue("retinfo", ret.getRetinfo());
|
this.header.addValue("retinfo", ret.getRetinfo());
|
||||||
@@ -294,6 +300,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
*/
|
*/
|
||||||
public void finishJson(final JsonConvert convert, final org.redkale.service.RetResult ret) {
|
public void finishJson(final JsonConvert convert, final org.redkale.service.RetResult ret) {
|
||||||
this.contentType = "text/plain; charset=utf-8";
|
this.contentType = "text/plain; charset=utf-8";
|
||||||
|
if (this.recycleListener != null) this.output = ret;
|
||||||
if (ret != null && !ret.isSuccess()) {
|
if (ret != null && !ret.isSuccess()) {
|
||||||
this.header.addValue("retcode", String.valueOf(ret.getRetcode()));
|
this.header.addValue("retcode", String.valueOf(ret.getRetcode()));
|
||||||
this.header.addValue("retinfo", ret.getRetinfo());
|
this.header.addValue("retinfo", ret.getRetinfo());
|
||||||
@@ -309,6 +316,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
*/
|
*/
|
||||||
public void finishJsResult(String var, Object result) {
|
public void finishJsResult(String var, Object result) {
|
||||||
this.contentType = "application/javascript; charset=utf-8";
|
this.contentType = "application/javascript; charset=utf-8";
|
||||||
|
if (this.recycleListener != null) this.output = result;
|
||||||
finish("var " + var + " = " + request.getJsonConvert().convertTo(result) + ";");
|
finish("var " + var + " = " + request.getJsonConvert().convertTo(result) + ";");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -321,6 +329,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
*/
|
*/
|
||||||
public void finishJsResult(JsonConvert jsonConvert, String var, Object result) {
|
public void finishJsResult(JsonConvert jsonConvert, String var, Object result) {
|
||||||
this.contentType = "application/javascript; charset=utf-8";
|
this.contentType = "application/javascript; charset=utf-8";
|
||||||
|
if (this.recycleListener != null) this.output = result;
|
||||||
finish("var " + var + " = " + jsonConvert.convertTo(result) + ";");
|
finish("var " + var + " = " + jsonConvert.convertTo(result) + ";");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -330,6 +339,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
* @param obj 输出内容
|
* @param obj 输出内容
|
||||||
*/
|
*/
|
||||||
public void finish(String obj) {
|
public void finish(String obj) {
|
||||||
|
if (this.recycleListener != null) this.output = obj;
|
||||||
if (obj == null || obj.isEmpty()) {
|
if (obj == null || obj.isEmpty()) {
|
||||||
final ByteBuffer headbuf = createHeader();
|
final ByteBuffer headbuf = createHeader();
|
||||||
headbuf.flip();
|
headbuf.flip();
|
||||||
|
|||||||
Reference in New Issue
Block a user