- java.lang.Object
-
- org.redkale.net.Response<HttpContext,HttpRequest>
-
- org.redkale.net.http.HttpResponse
-
public class HttpResponse extends Response<HttpContext,HttpRequest>
Http响应包 与javax.servlet.http.HttpServletResponse 基本类似。
同时提供发送json的系列接口: public void finishJson(Type type, Object obj)
Redkale提倡http+json的接口风格, 所以主要输出的数据格式为json, 同时提供异步接口。
详情见: https://redkale.org
- 作者:
- zhangjx
-
-
嵌套类概要
嵌套类 修饰符和类型 类 说明 static class
HttpResponse.HttpResponseConfig
-
字段概要
字段 修饰符和类型 字段 说明 protected static byte[]
bytes304
protected static byte[]
bytes404
protected static byte[]
bytes405
protected static byte[]
bytes500
protected static byte[]
bytes504
protected static byte[]
connectAliveBytes
protected static byte[]
connectCloseBytes
protected static String
CONTENT_TYPE_HTML_UTF8
protected ConvertBytesHandler
convertHandler
protected static byte[]
EMPTY_BTYES
protected static byte[]
LINE
protected static byte[]
serverNameBytes
protected static byte[]
status200Bytes
-
从类继承的字段 org.redkale.net.Response
afterFinishListeners, channel, context, errorHandler, filter, inNonBlocking, output, readRegistered, recycleListener, request, responseConsumer, responseSupplier, servlet, thread, workExecutor
-
-
构造器概要
构造器 构造器 说明 HttpResponse(HttpContext context, HttpRequest request, HttpResponse.HttpResponseConfig config)
-
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 HttpResponse
addCookie(HttpCookie... cookies)
增加Cookie值HttpResponse
addCookie(Collection<HttpCookie> cookies)
增加Cookie值HttpResponse
addHeader(String name, Object value)
添加Header值HttpResponse
addHeader(Map<String,?> map)
添加Header值CompletionHandler
createAsyncHandler()
创建CompletionHandler实例<H extends CompletionHandler>
HcreateAsyncHandler(Class<H> handlerClass)
创建CompletionHandler子类的实例
传入的CompletionHandler子类必须是public,且保证其子类可被继承且completed、failed可被重载且包含空参数的构造函数。protected void
createHeader()
protected void
defaultError(Throwable t)
protected AnyValueWriter
duplicateHeader()
boolean
existsHeader(String name)
判断是否存在Header值void
finish(boolean kill, byte[] bs, int offset, int length)
protected void
finish(boolean kill, String contentType, byte[] bs, int offset, int length)
将指定byte[]按响应结果输出protected <A> void
finish(boolean kill, String contentType, byte[] bodyContent, int bodyOffset, int bodyLength, Consumer<A> callback, A attachment)
将指定byte[]按响应结果输出<A> void
finish(byte[] bs, int offset, int length, Consumer<A> callback, A attachment)
void
finish(int status, String message)
以指定响应码附带内容输出void
finish(File file)
将指定文件按响应结果输出void
finish(Object obj)
将结果对象输出void
finish(Type type, Object obj)
将结果对象输出void
finish(Type resultType, HttpResult result)
将HttpResult对象输出void
finish(Type type, RetResult ret)
将RetResult对象输出void
finish(String obj)
将指定字符串以响应结果输出void
finish(String contentType, byte[] bs)
将指定byte[]按响应结果输出void
finish(String fileName, File file)
将文件按指定文件名输出void
finish(String contentType, ByteTuple array)
将ByteTuple按响应结果输出void
finish(Convert convert, Object obj)
将结果对象输出void
finish(Convert convert, Type type, Object obj)
将结果对象输出void
finish(Convert convert, Type resultType, HttpResult result)
将HttpResult对象输出void
finish(Convert convert, Type type, RetResult ret)
将RetResult对象输出void
finish(Convert convert, HttpScope result)
将HttpScope对象输出void
finish(HttpScope result)
将HttpScope对象输出void
finish304()
以304状态码输出void
finish404()
以404状态码输出void
finish405()
以405状态码输出void
finish500()
以500状态码输出void
finish504()
以504状态码输出protected void
finishFile(File file, ByteArray fileBody)
将指定文件句柄或文件内容按响应结果输出,若fileBody不为null则只输出fileBody内容protected void
finishFile(String fileName, File file, ByteArray fileBody)
将指定文件句柄或文件内容按指定文件名输出,若fileBody不为null则只输出fileBody内容 file 与 fileBody 不能同时为空 file 与 fileName 也不能同时为空void
finishFuture(Type valueType, CompletionStage future)
将CompletionStage对象输出void
finishFuture(Convert convert, Type valueType, CompletionStage future)
将CompletionStage对象输出void
finishJson(Object obj)
将对象以JSON格式输出void
finishJson(Type type, Object obj)
将对象以JSON格式输出void
finishJson(Convert convert, Object obj)
将对象以JSON格式输出void
finishJson(Convert convert, Type type, Object obj)
将对象以JSON格式输出void
finishJsonFuture(Type valueType, CompletionStage future)
将CompletionStage对象输出void
finishJsonFuture(Convert convert, Type valueType, CompletionStage future)
将CompletionStage对象输出void
finishPublisher(Type valueType, Object publisher)
将第三方类Flow.Publisher对象(如: Mono/Flux)输出<T> void
finishPublisher(Type valueType, Flow.Publisher<T> publisher)
将Flow.Publisher对象输出void
finishPublisher(Convert convert, Type valueType, Object publisher)
将第三方类Flow.Publisher对象(如: Mono/Flux)输出<T> void
finishPublisher(Convert convert, Type valueType, Flow.Publisher<T> publisher)
将Flow.Publisher对象输出void
finishScopeFuture(CompletionStage<HttpScope> future)
将HttpScope对象输出void
finishScopeFuture(Convert convert, CompletionStage<HttpScope> future)
将HttpScope对象输出protected BiConsumer<HttpResponse,byte[]>
getCacheHandler()
获取输出时的拦截器protected AsyncConnection
getChannel()
long
getContentLength()
获取内容长度String
getContentType()
获取 ContentTypeString
getHeader(String name)
获取Header值protected String
getHttpCode(int status)
获取状态码对应的状态描述protected String
getHttpCode(int status, String defValue)
HttpRequest
getRequest()
获取HttpRequestprotected BiFunction<HttpRequest,RetResult,RetResult>
getRetResultHandler()
获取输出RetResult时的拦截器protected BiFunction<HttpResponse,ByteArray,ByteArray>
getSendHandler()
获取输出RetResult时的拦截器int
getStatus()
获取状态码protected ExecutorService
getWorkExecutor()
protected void
init(AsyncConnection channel)
protected boolean
inNonBlocking()
protected boolean
isAutoOptions()
protected void
prepare()
protected boolean
recycle()
protected AsyncConnection
removeChannel()
void
retResultHandler(BiFunction<HttpRequest,RetResult,RetResult> retResultHandler)
设置输出RetResult时的拦截器protected void
sendBody(ByteBuffer buffer, CompletionHandler<Integer,Void> handler)
异步输出指定内容void
sendHandler(BiFunction<HttpResponse,ByteArray,ByteArray> sendHandler)
设置输出结果时的拦截器protected void
setCacheHandler(BiConsumer<HttpResponse,byte[]> cacheHandler)
设置输出时的拦截器HttpResponse
setContentLength(long contentLength)
设置内容长度HttpResponse
setContentType(String contentType)
设置 ContentTypeprotected void
setFilter(Filter filter)
HttpResponse
setHeader(String name, Object value)
设置Header值HttpResponse
setStatus(int status)
设置状态码HttpResponse
skipHeader()
跳过header的输出 通常应用场景是,调用者的输出内容里已经包含了HTTP的响应头信息,因此需要调用此方法避免重复输出HTTP响应头信息。protected void
thenEvent(Filter filter)
protected void
thenEvent(Servlet servlet)
protected void
updateNonBlocking(boolean nonBlocking)
-
从类继承的方法 org.redkale.net.Response
addAfterFinishListener, codecError, completeFinishBytes, errorHandler, finish, finish, finish, finish, finish, finish, finishBuffer, finishBuffer, finishBuffers, finishBuffers, finishError, getContext, getOutput, isClosed, nextEvent, recycleListener, refuseAlive, send, send, send
-
-
-
-
字段详细资料
-
EMPTY_BTYES
protected static final byte[] EMPTY_BTYES
-
bytes304
protected static final byte[] bytes304
-
bytes404
protected static final byte[] bytes404
-
bytes405
protected static final byte[] bytes405
-
bytes500
protected static final byte[] bytes500
-
bytes504
protected static final byte[] bytes504
-
status200Bytes
protected static final byte[] status200Bytes
-
LINE
protected static final byte[] LINE
-
serverNameBytes
protected static final byte[] serverNameBytes
-
connectCloseBytes
protected static final byte[] connectCloseBytes
-
connectAliveBytes
protected static final byte[] connectAliveBytes
-
convertHandler
protected final ConvertBytesHandler convertHandler
-
-
构造器详细资料
-
HttpResponse
public HttpResponse(HttpContext context, HttpRequest request, HttpResponse.HttpResponseConfig config)
-
-
方法详细资料
-
removeChannel
protected AsyncConnection removeChannel()
- 覆盖:
removeChannel
在类中Response<HttpContext,HttpRequest>
-
getChannel
protected AsyncConnection getChannel()
-
prepare
protected void prepare()
- 覆盖:
prepare
在类中Response<HttpContext,HttpRequest>
-
recycle
protected boolean recycle()
- 覆盖:
recycle
在类中Response<HttpContext,HttpRequest>
-
getWorkExecutor
protected ExecutorService getWorkExecutor()
- 覆盖:
getWorkExecutor
在类中Response<HttpContext,HttpRequest>
-
updateNonBlocking
protected void updateNonBlocking(boolean nonBlocking)
- 覆盖:
updateNonBlocking
在类中Response<HttpContext,HttpRequest>
-
inNonBlocking
protected boolean inNonBlocking()
- 覆盖:
inNonBlocking
在类中Response<HttpContext,HttpRequest>
-
init
protected void init(AsyncConnection channel)
- 覆盖:
init
在类中Response<HttpContext,HttpRequest>
-
getHttpCode
protected String getHttpCode(int status)
获取状态码对应的状态描述- 参数:
status
- 状态码- 返回:
- 状态描述
-
getRequest
public HttpRequest getRequest()
获取HttpRequest- 返回:
- HttpRequest
-
setFilter
protected void setFilter(Filter filter)
- 覆盖:
setFilter
在类中Response<HttpContext,HttpRequest>
-
thenEvent
protected void thenEvent(Servlet servlet)
- 覆盖:
thenEvent
在类中Response<HttpContext,HttpRequest>
-
thenEvent
protected void thenEvent(Filter filter)
- 覆盖:
thenEvent
在类中Response<HttpContext,HttpRequest>
-
isAutoOptions
protected boolean isAutoOptions()
-
addCookie
public HttpResponse addCookie(HttpCookie... cookies)
增加Cookie值- 参数:
cookies
- cookie- 返回:
- HttpResponse
-
addCookie
public HttpResponse addCookie(Collection<HttpCookie> cookies)
增加Cookie值- 参数:
cookies
- cookie- 返回:
- HttpResponse
-
createAsyncHandler
public CompletionHandler createAsyncHandler()
创建CompletionHandler实例- 返回:
- CompletionHandler
-
defaultError
protected void defaultError(Throwable t)
- 覆盖:
defaultError
在类中Response<HttpContext,HttpRequest>
-
createAsyncHandler
public <H extends CompletionHandler> H createAsyncHandler(Class<H> handlerClass)
创建CompletionHandler子类的实例
传入的CompletionHandler子类必须是public,且保证其子类可被继承且completed、failed可被重载且包含空参数的构造函数。- 类型参数:
H
- 泛型- 参数:
handlerClass
- CompletionHandler子类- 返回:
- CompletionHandler
-
finishJson
public final void finishJson(Object obj)
将对象以JSON格式输出- 参数:
obj
- 输出对象
-
finishJson
public final void finishJson(Convert convert, Object obj)
将对象以JSON格式输出- 参数:
convert
- 指定的JsonConvertobj
- 输出对象
-
finishJson
public final void finishJson(Type type, Object obj)
将对象以JSON格式输出- 参数:
type
- 指定的类型obj
- 输出对象
-
finishJson
public void finishJson(Convert convert, Type type, Object obj)
将对象以JSON格式输出- 参数:
convert
- 指定的Converttype
- 指定的类型obj
- 输出对象
-
finish
public final void finish(Type type, RetResult ret)
将RetResult对象输出- 参数:
type
- 指定的RetResult泛型类型ret
- RetResult输出对象
-
finish
public void finish(Convert convert, Type type, RetResult ret)
将RetResult对象输出- 参数:
convert
- 指定的Converttype
- 指定的RetResult泛型类型ret
- RetResult输出对象
-
finish
public final void finish(Type resultType, HttpResult result)
将HttpResult对象输出- 参数:
resultType
- HttpResult.result的泛型类型result
- HttpResult输出对象
-
finish
public void finish(Convert convert, Type resultType, HttpResult result)
将HttpResult对象输出- 参数:
convert
- 指定的ConvertresultType
- HttpResult.result的泛型类型result
- HttpResult输出对象
-
finishFuture
public final void finishFuture(Type valueType, CompletionStage future)
将CompletionStage对象输出- 参数:
valueType
- CompletionFuture.value的泛型类型future
- CompletionStage输出对象
-
finishFuture
public void finishFuture(Convert convert, Type valueType, CompletionStage future)
将CompletionStage对象输出- 参数:
convert
- 指定的ConvertvalueType
- CompletionFuture.value的泛型类型future
- CompletionStage输出对象
-
finishJsonFuture
public final void finishJsonFuture(Type valueType, CompletionStage future)
将CompletionStage对象输出- 参数:
valueType
- CompletionFuture.value的泛型类型future
- CompletionStage输出对象
-
finishJsonFuture
public void finishJsonFuture(Convert convert, Type valueType, CompletionStage future)
将CompletionStage对象输出- 参数:
convert
- 指定的ConvertvalueType
- CompletionFuture.value的泛型类型future
- CompletionStage输出对象
-
finishPublisher
public final <T> void finishPublisher(Type valueType, Flow.Publisher<T> publisher)
将Flow.Publisher对象输出- 类型参数:
T
- 泛型- 参数:
valueType
- Publisher的泛型类型publisher
- Publisher输出对象
-
finishPublisher
public final <T> void finishPublisher(Convert convert, Type valueType, Flow.Publisher<T> publisher)
将Flow.Publisher对象输出- 类型参数:
T
- 泛型- 参数:
convert
- 指定的ConvertvalueType
- Publisher的泛型类型publisher
- Publisher输出对象
-
finishPublisher
public final void finishPublisher(Type valueType, Object publisher)
将第三方类Flow.Publisher对象(如: Mono/Flux)输出- 参数:
valueType
- Publisher的泛型类型publisher
- Publisher输出对象
-
finishPublisher
public final void finishPublisher(Convert convert, Type valueType, Object publisher)
将第三方类Flow.Publisher对象(如: Mono/Flux)输出- 参数:
convert
- 指定的ConvertvalueType
- Publisher的泛型类型publisher
- Publisher输出对象
-
finishScopeFuture
public final void finishScopeFuture(CompletionStage<HttpScope> future)
将HttpScope对象输出- 参数:
future
- HttpScope输出异步对象
-
finishScopeFuture
public void finishScopeFuture(Convert convert, CompletionStage<HttpScope> future)
将HttpScope对象输出- 参数:
convert
- 指定的Convertfuture
- HttpScope输出异步对象
-
finish
public final void finish(HttpScope result)
将HttpScope对象输出- 参数:
result
- HttpScope输出对象
-
finish
public void finish(Convert convert, HttpScope result)
将HttpScope对象输出- 参数:
convert
- 指定的Convertresult
- HttpScope输出对象
-
finish
public final void finish(Object obj)
将结果对象输出- 参数:
obj
- 输出对象
-
finish
public final void finish(Convert convert, Object obj)
将结果对象输出- 参数:
convert
- 指定的Convertobj
- 输出对象
-
finish
public final void finish(Type type, Object obj)
将结果对象输出- 参数:
type
- 指定的类型, 不一定是obj的数据类型,必然obj为CompletableFuture, type应该为Future的元素类型obj
- 输出对象
-
finish
public void finish(Convert convert, Type type, Object obj)
将结果对象输出- 参数:
convert
- 指定的Converttype
- 指定的类型, 不一定是obj的数据类型,必然obj为CompletionStage, type应该为Future的元素类型obj
- 输出对象
-
finish
public void finish(String obj)
将指定字符串以响应结果输出- 参数:
obj
- 输出内容
-
finish
public void finish(int status, String message)
以指定响应码附带内容输出- 参数:
status
- 响应码message
- 输出内容
-
finish
public void finish(boolean kill, byte[] bs, int offset, int length)
- 覆盖:
finish
在类中Response<HttpContext,HttpRequest>
-
finish
public <A> void finish(byte[] bs, int offset, int length, Consumer<A> callback, A attachment)
-
finish
public void finish(String contentType, byte[] bs)
将指定byte[]按响应结果输出- 参数:
contentType
- ContentTypebs
- 输出内容
-
finish
public void finish(String contentType, ByteTuple array)
将ByteTuple按响应结果输出- 参数:
contentType
- ContentTypearray
- 输出内容
-
finish
protected void finish(boolean kill, String contentType, byte[] bs, int offset, int length)
将指定byte[]按响应结果输出- 参数:
kill
- killcontentType
- ContentTypebs
- 输出内容offset
- 偏移量length
- 长度
-
finish
protected <A> void finish(boolean kill, String contentType, @Nonnull byte[] bodyContent, int bodyOffset, int bodyLength, Consumer<A> callback, A attachment)
将指定byte[]按响应结果输出- 类型参数:
A
- A- 参数:
kill
- killcontentType
- ContentTypebodyContent
- 输出内容bodyOffset
- 偏移量bodyLength
- 长度callback
- Consumerattachment
- ConvertWriter
-
finish304
public void finish304()
以304状态码输出
-
finish404
public void finish404()
以404状态码输出
-
finish405
public void finish405()
以405状态码输出
-
finish500
public void finish500()
以500状态码输出
-
finish504
public void finish504()
以504状态码输出
-
createHeader
protected void createHeader()
-
sendBody
protected void sendBody(ByteBuffer buffer, CompletionHandler<Integer,Void> handler)
异步输出指定内容- 参数:
buffer
- 输出内容handler
- 异步回调函数
-
finish
public void finish(File file) throws IOException
将指定文件按响应结果输出- 参数:
file
- 输出文件- 抛出:
IOException
- IO异常
-
finish
public void finish(String fileName, File file) throws IOException
将文件按指定文件名输出- 参数:
fileName
- 输出文件名file
- 输出文件- 抛出:
IOException
- IO异常
-
finishFile
protected void finishFile(File file, ByteArray fileBody) throws IOException
将指定文件句柄或文件内容按响应结果输出,若fileBody不为null则只输出fileBody内容- 参数:
file
- 输出文件fileBody
- 文件内容, 没有则输出file- 抛出:
IOException
- IO异常
-
finishFile
protected void finishFile(String fileName, File file, ByteArray fileBody) throws IOException
将指定文件句柄或文件内容按指定文件名输出,若fileBody不为null则只输出fileBody内容 file 与 fileBody 不能同时为空 file 与 fileName 也不能同时为空- 参数:
fileName
- 输出文件名file
- 输出文件fileBody
- 文件内容, 没有则输出file- 抛出:
IOException
- IO异常
-
skipHeader
public HttpResponse skipHeader()
跳过header的输出 通常应用场景是,调用者的输出内容里已经包含了HTTP的响应头信息,因此需要调用此方法避免重复输出HTTP响应头信息。- 返回:
- HttpResponse
-
duplicateHeader
protected AnyValueWriter duplicateHeader()
-
existsHeader
public boolean existsHeader(String name)
判断是否存在Header值- 参数:
name
- header-name- 返回:
- 是否存在
-
setHeader
public HttpResponse setHeader(String name, Object value)
设置Header值- 参数:
name
- header名value
- header值- 返回:
- HttpResponse
-
addHeader
public HttpResponse addHeader(String name, Object value)
添加Header值- 参数:
name
- header名value
- header值- 返回:
- HttpResponse
-
addHeader
public HttpResponse addHeader(Map<String,?> map)
添加Header值- 参数:
map
- header值- 返回:
- HttpResponse
-
setStatus
public HttpResponse setStatus(int status)
设置状态码- 参数:
status
- 状态码- 返回:
- HttpResponse
-
getStatus
public int getStatus()
获取状态码- 返回:
- 状态码
-
getContentType
public String getContentType()
获取 ContentType- 返回:
- ContentType
-
setContentType
public HttpResponse setContentType(String contentType)
设置 ContentType- 参数:
contentType
- ContentType- 返回:
- HttpResponse
-
getContentLength
public long getContentLength()
获取内容长度- 返回:
- 内容长度
-
setContentLength
public HttpResponse setContentLength(long contentLength)
设置内容长度- 参数:
contentLength
- 内容长度- 返回:
- HttpResponse
-
getCacheHandler
protected BiConsumer<HttpResponse,byte[]> getCacheHandler()
获取输出时的拦截器- 返回:
- 拦截器
-
setCacheHandler
protected void setCacheHandler(BiConsumer<HttpResponse,byte[]> cacheHandler)
设置输出时的拦截器- 参数:
cacheHandler
- 拦截器
-
getRetResultHandler
protected BiFunction<HttpRequest,RetResult,RetResult> getRetResultHandler()
获取输出RetResult时的拦截器- 返回:
- 拦截器
-
retResultHandler
public void retResultHandler(BiFunction<HttpRequest,RetResult,RetResult> retResultHandler)
设置输出RetResult时的拦截器- 参数:
retResultHandler
- 拦截器
-
getSendHandler
protected BiFunction<HttpResponse,ByteArray,ByteArray> getSendHandler()
获取输出RetResult时的拦截器- 返回:
- 拦截器
-
sendHandler
public void sendHandler(BiFunction<HttpResponse,ByteArray,ByteArray> sendHandler)
设置输出结果时的拦截器- 参数:
sendHandler
- 拦截器
-
-