@Override
 public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
   HttpServerChannelHandler handler = (HttpServerChannelHandler) ctx.getAttachment();
   if (handler != null) {
     handler.exceptionCaught(ctx, e);
   } else {
     throw new IllegalStateException(
         "HttpServerChannelHandler not found as attachment. Cannot handle caught exception.",
         e.getCause());
   }
 }
  @Override
  public void messageReceived(ChannelHandlerContext ctx, MessageEvent messageEvent)
      throws Exception {
    // store request, as this channel handler is created per pipeline
    HttpRequest request = (HttpRequest) messageEvent.getMessage();

    LOG.debug("Message received: {}", request);

    HttpServerChannelHandler handler = getHandler(request);
    if (handler != null) {
      // store handler as attachment
      ctx.setAttachment(handler);
      handler.messageReceived(ctx, messageEvent);
    } else {
      // this service is not available, so send empty response back
      HttpResponse response = new DefaultHttpResponse(HTTP_1_1, SERVICE_UNAVAILABLE);
      response.setHeader(Exchange.CONTENT_TYPE, "text/plain");
      response.setHeader(Exchange.CONTENT_LENGTH, 0);
      response.setContent(ChannelBuffers.copiedBuffer(new byte[] {}));
      messageEvent.getChannel().write(response);
    }
  }