コード例 #1
0
 /*
  * Create an HTTP 400 bad request response.
  */
 public static FullHttpResponse badRequest(String msg) {
   DefaultFullHttpResponse resp = new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST);
   resp.content().writeBytes(msg.getBytes());
   resp.headers().set(CONTENT_TYPE, "text/plain");
   resp.headers().set(CONTENT_LENGTH, resp.content().readableBytes());
   return resp;
 }
コード例 #2
0
 /**
  * Respond to errors occurring on a Reactor by redirecting them to the client via an HTTP 500
  * error response.
  *
  * @param channel the channel on which to send an HTTP response
  * @return a consumer to handle HTTP requests
  */
 public static Consumer<Throwable> errorHandler(
     NetChannel<FullHttpRequest, FullHttpResponse> channel) {
   return ev -> {
     DefaultFullHttpResponse resp =
         new DefaultFullHttpResponse(
             HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
     resp.content().writeBytes(ev.getMessage().getBytes());
     resp.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
     resp.headers().set(HttpHeaders.Names.CONTENT_LENGTH, resp.content().readableBytes());
     channel.send(resp);
   };
 }
コード例 #3
0
  /*
   * Create an HTTP 200 response that contains the data of the thumbnailed image.
   */
  public static FullHttpResponse serveImage(Path path) throws IOException {
    DefaultFullHttpResponse resp = new DefaultFullHttpResponse(HTTP_1_1, OK);

    RandomAccessFile f = new RandomAccessFile(path.toString(), "r");
    resp.headers().set(CONTENT_TYPE, "image/jpeg");
    resp.headers().set(CONTENT_LENGTH, f.length());

    byte[] bytes = Files.readAllBytes(path);
    resp.content().writeBytes(bytes);

    return resp;
  }