HttpResponse的ContentType默认值可配置

This commit is contained in:
Redkale
2018-04-12 13:30:28 +08:00
parent 6e6d0529d6
commit ef1c437191
3 changed files with 37 additions and 16 deletions

View File

@@ -229,6 +229,8 @@
<!--
【节点在<server>中唯一】
当Server为HTTP协议时, response节点才有效。
contenttype: plain值为调用finish时的ContentType; 默认值: text/plain; charset=utf-8
json值为调用finishJson时的ContentType; 默认值: application/json; charset=utf-8
defcookie 节点: 当response里输出的cookie没有指定domain 和path时使用该节点的默认值。
如果addheader、setheader 的value值以request.parameters.开头则表示从request.parameters中获取对应的parameter值
如果addheader、setheader 的value值以request.headers.开头则表示从request.headers中获取对应的header值
@@ -236,6 +238,7 @@
options 节点: 设置了该节点却auto=true当request的method=OPTIONS自动设置addheader、setheader并返回200状态码
-->
<response>
<contenttype plain="text/plain; charset=utf-8" json="application/json; charset=utf-8"/>
<defcookie domain="" path=""/>
<addheader name="Access-Control-Allow-Origin" value="request.headers.Origin" />
<setheader name="Access-Control-Allow-Headers" value="request.headers.Access-Control-Request-Headers"/>

View File

@@ -101,7 +101,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
private int status = 200;
private String contentType = "text/plain; charset=utf-8";
private String contentType = "";
private long contentLength = -1;
@@ -112,6 +112,10 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
private BiFunction<HttpResponse, ByteBuffer[], ByteBuffer[]> bufferHandler;
//------------------------------------------------
private final String plainContentType;
private final String jsonContentType;
private final DefaultAnyValue header = new DefaultAnyValue();
private final String[][] defaultAddHeaders;
@@ -132,9 +136,13 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
return new ObjectPool<>(creatCounter, cycleCounter, max, creator, (x) -> ((HttpResponse) x).prepare(), (x) -> ((HttpResponse) x).recycle());
}
public HttpResponse(HttpContext context, HttpRequest request, String[][] defaultAddHeaders, String[][] defaultSetHeaders,
public HttpResponse(HttpContext context, HttpRequest request,
String plainContentType, String jsonContentType,
String[][] defaultAddHeaders, String[][] defaultSetHeaders,
HttpCookie defcookie, boolean autoOptions, List< HttpRender> renders) {
super(context, request);
this.plainContentType = plainContentType == null || plainContentType.isEmpty() ? "text/plain; charset=utf-8" : plainContentType;
this.jsonContentType = jsonContentType == null || jsonContentType.isEmpty() ? "application/json; charset=utf-8" : jsonContentType;
this.defaultAddHeaders = defaultAddHeaders;
this.defaultSetHeaders = defaultSetHeaders;
this.defcookie = defcookie;
@@ -142,6 +150,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
this.renders = renders;
this.hasRender = renders != null && !renders.isEmpty();
this.onlyoneHttpRender = renders != null && renders.size() == 1 ? renders.get(0) : null;
this.contentType = this.plainContentType;
}
@Override
@@ -270,7 +279,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
* @param obj 输出对象
*/
public void finishJson(final Object obj) {
this.contentType = "application/json; charset=utf-8";
this.contentType = this.jsonContentType;
if (this.recycleListener != null) this.output = obj;
finish(request.getJsonConvert().convertTo(getBodyBufferSupplier(), obj));
}
@@ -282,7 +291,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
* @param objs 输出对象
*/
public void finishMapJson(final Object... objs) {
this.contentType = "application/json; charset=utf-8";
this.contentType = this.jsonContentType;
if (this.recycleListener != null) this.output = objs;
finish(request.getJsonConvert().convertMapTo(getBodyBufferSupplier(), objs));
}
@@ -294,7 +303,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
* @param obj 输出对象
*/
public void finishJson(final JsonConvert convert, final Object obj) {
this.contentType = "application/json; charset=utf-8";
this.contentType = this.jsonContentType;
if (this.recycleListener != null) this.output = obj;
finish(convert.convertTo(getBodyBufferSupplier(), obj));
}
@@ -307,7 +316,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
* @param objs 输出对象
*/
public void finishMapJson(final JsonConvert convert, final Object... objs) {
this.contentType = "application/json; charset=utf-8";
this.contentType = this.jsonContentType;
if (this.recycleListener != null) this.output = objs;
finish(convert.convertMapTo(getBodyBufferSupplier(), objs));
}
@@ -319,7 +328,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
* @param obj 输出对象
*/
public void finishJson(final Type type, final Object obj) {
this.contentType = "application/json; charset=utf-8";
this.contentType = this.jsonContentType;
this.output = obj;
finish(request.getJsonConvert().convertTo(getBodyBufferSupplier(), type, obj));
}
@@ -332,7 +341,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
* @param obj 输出对象
*/
public void finishJson(final JsonConvert convert, final Type type, final Object obj) {
this.contentType = "application/json; charset=utf-8";
this.contentType = this.jsonContentType;
if (this.recycleListener != null) this.output = obj;
finish(convert.convertTo(getBodyBufferSupplier(), type, obj));
}
@@ -343,7 +352,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
* @param objs 输出对象
*/
public void finishJson(final Object... objs) {
this.contentType = "application/json; charset=utf-8";
this.contentType = this.jsonContentType;
if (this.recycleListener != null) this.output = objs;
finish(request.getJsonConvert().convertTo(getBodyBufferSupplier(), objs));
}
@@ -354,7 +363,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
* @param ret RetResult输出对象
*/
public void finishJson(final org.redkale.service.RetResult ret) {
this.contentType = "application/json; charset=utf-8";
this.contentType = this.jsonContentType;
if (this.recycleListener != null) this.output = ret;
if (ret != null && !ret.isSuccess()) {
this.header.addValue("retcode", String.valueOf(ret.getRetcode()));
@@ -370,7 +379,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
* @param ret RetResult输出对象
*/
public void finishJson(final JsonConvert convert, final org.redkale.service.RetResult ret) {
this.contentType = "application/json; charset=utf-8";
this.contentType = this.jsonContentType;
if (this.recycleListener != null) this.output = ret;
if (ret != null && !ret.isSuccess()) {
this.header.addValue("retcode", String.valueOf(ret.getRetcode()));
@@ -494,9 +503,9 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
}
}
if (convert instanceof JsonConvert) {
this.contentType = "application/json; charset=utf-8";
this.contentType = this.jsonContentType;
} else if (convert instanceof TextConvert) {
this.contentType = "text/plain; charset=utf-8";
this.contentType = this.plainContentType;
}
if (this.recycleListener != null) this.output = obj;
if (obj instanceof org.redkale.service.RetResult) {
@@ -837,7 +846,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
ByteBuffer buffer = this.pollWriteReadBuffer();
buffer.put(("HTTP/1.1 " + this.status + " " + (this.status == 200 ? "OK" : httpCodes.get(this.status)) + "\r\n").getBytes());
buffer.put(("Content-Type: " + (this.contentType == null ? "text/plain; charset=utf-8" : this.contentType) + "\r\n").getBytes());
buffer.put(("Content-Type: " + (this.contentType == null ? this.plainContentType : this.contentType) + "\r\n").getBytes());
if (this.contentLength >= 0) {
buffer.put(("Content-Length: " + this.contentLength + "\r\n").getBytes());

View File

@@ -295,7 +295,8 @@ public class HttpServer extends Server<String, HttpContext, HttpRequest, HttpRes
final List<String[]> defaultAddHeaders = new ArrayList<>();
final List<String[]> defaultSetHeaders = new ArrayList<>();
boolean autoOptions = false;
String plainContentType = null;
String jsonContentType = null;
HttpCookie defaultCookie = null;
String remoteAddrHeader = null;
if (config != null) {
@@ -314,6 +315,11 @@ public class HttpServer extends Server<String, HttpContext, HttpRequest, HttpRes
AnyValue resps = config == null ? null : config.getAnyValue("response");
if (resps != null) {
AnyValue contenttypes = resps.getAnyValue("contenttype");
if (contenttypes != null) {
plainContentType = contenttypes.getValue("plain");
jsonContentType = contenttypes.getValue("json");
}
AnyValue[] addHeaders = resps.getAnyValues("addheader");
if (addHeaders.length > 0) {
for (AnyValue addHeader : addHeaders) {
@@ -363,6 +369,8 @@ public class HttpServer extends Server<String, HttpContext, HttpRequest, HttpRes
}
}
final String plainType = plainContentType;
final String jsonType = jsonContentType;
final String[][] addHeaders = defaultAddHeaders.isEmpty() ? null : defaultAddHeaders.toArray(new String[defaultAddHeaders.size()][]);
final String[][] setHeaders = defaultSetHeaders.isEmpty() ? null : defaultSetHeaders.toArray(new String[defaultSetHeaders.size()][]);
final boolean options = autoOptions;
@@ -374,7 +382,8 @@ public class HttpServer extends Server<String, HttpContext, HttpRequest, HttpRes
ObjectPool<Response> responsePool = HttpResponse.createPool(createResponseCounter, cycleResponseCounter, this.responsePoolSize, null);
HttpContext httpcontext = new HttpContext(this.serverStartTime, this.logger, executor, this.sslContext, rcapacity, bufferPool, responsePool,
this.maxbody, this.charset, this.address, this.resourceFactory, this.prepare, this.readTimeoutSecond, this.writeTimeoutSecond);
responsePool.setCreator((Object... params) -> new HttpResponse(httpcontext, new HttpRequest(httpcontext, addrHeader), addHeaders, setHeaders, defCookie, options, ((HttpPrepareServlet) prepare).renders));
responsePool.setCreator((Object... params) -> new HttpResponse(httpcontext, new HttpRequest(httpcontext, addrHeader),
plainType, jsonType, addHeaders, setHeaders, defCookie, options, ((HttpPrepareServlet) prepare).renders));
return httpcontext;
}