Java Code Examples for io.netty.handler.codec.http.DefaultFullHttpResponse#setStatus()

The following examples show how to use io.netty.handler.codec.http.DefaultFullHttpResponse#setStatus() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FSImageHandler.java    From hadoop with Apache License 2.0 6 votes vote down vote up
@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);
  }

  resp.headers().set(CONTENT_LENGTH, resp.content().readableBytes());
  resp.headers().set(CONNECTION, CLOSE);
  ctx.write(resp).addListener(ChannelFutureListener.CLOSE);
}
 
Example 2
Source File: FSImageHandler.java    From big-c with Apache License 2.0 6 votes vote down vote up
@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);
  }

  resp.headers().set(CONTENT_LENGTH, resp.content().readableBytes());
  resp.headers().set(CONNECTION, CLOSE);
  ctx.write(resp).addListener(ChannelFutureListener.CLOSE);
}
 
Example 3
Source File: HttpRootController.java    From happor with MIT License 5 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
		throws Exception {
	// TODO Auto-generated method stub
	if (msg instanceof FullHttpRequest) {
		request = (FullHttpRequest) msg;
		response = new DefaultFullHttpResponse(
				request.getProtocolVersion(), HttpResponseStatus.OK);
		
		HapporContext happorContext = server.getContext(request);
		if (happorContext == null) {
			logger.error("cannot find path for URI: " + request.getUri());
			response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
			ctx.channel().writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
			return;
		}
		
		HttpController lastController = null;
		
		Map<String, ControllerRegistry> controllers = happorContext.getControllers();
		for (Map.Entry<String, ControllerRegistry> entry : controllers.entrySet()) {
			ControllerRegistry registry = entry.getValue();
			String method = registry.getMethod();
			String uriPattern = registry.getUriPattern();
			UriParser uriParser = new UriParser(request.getUri());
			
			if (isMethodMatch(method) && isUriMatch(uriParser, uriPattern)) {
				HttpController controller = happorContext.getController(registry.getClazz());
				controller.setPrev(lastController);
				controller.setServer(server);
				controller.setUriParser(uriParser);
				boolean isEnd = controller.input(ctx, request, response);
				if (isEnd) {
					break;
				}
				lastController = controller;
			}
		}
	}
}