Example usage for io.netty.handler.codec.http DefaultFullHttpResponse setStatus

List of usage examples for io.netty.handler.codec.http DefaultFullHttpResponse setStatus

Introduction

In this page you can find the example usage for io.netty.handler.codec.http DefaultFullHttpResponse setStatus.

Prototype

@Override
    public FullHttpResponse setStatus(HttpResponseStatus status) 

Source Link

Usage

From source file:org.apache.hadoop.hdfs.tools.offlineImageViewer.FSImageHandler.java

License:Apache License

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    Exception e = cause instanceof Exception ? (Exception) cause : new Exception(cause);
    final String output = JsonUtil.toJsonString(e);
    ByteBuf content = Unpooled.wrappedBuffer(output.getBytes(Charsets.UTF_8));
    final DefaultFullHttpResponse resp = new DefaultFullHttpResponse(HTTP_1_1, INTERNAL_SERVER_ERROR, content);

    resp.headers().set(CONTENT_TYPE, APPLICATION_JSON_UTF8);
    if (e instanceof IllegalArgumentException) {
        resp.setStatus(BAD_REQUEST);
    } else if (e instanceof FileNotFoundException) {
        resp.setStatus(NOT_FOUND);/*from   w w  w. jav a  2  s  .c  o  m*/
    } else if (e instanceof IOException) {
        resp.setStatus(FORBIDDEN);
    }
    resp.headers().set(CONTENT_LENGTH, resp.content().readableBytes());
    resp.headers().set(CONNECTION, CLOSE);
    ctx.write(resp).addListener(ChannelFutureListener.CLOSE);
}