/*
  * 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;
 }
 @Override
 public FullHttpResponse duplicate() {
   DefaultFullHttpResponse duplicate =
       new DefaultFullHttpResponse(protocolVersion(), status(), content().duplicate());
   duplicate.headers().set(headers());
   duplicate.trailingHeaders().set(trailingHeaders());
   return duplicate;
 }
 /**
  * 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);
   };
 }
  /*
   * 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;
  }
 /**
  * Copy this object
  *
  * @param copyContent
  *     <ul>
  *       <li>{@code true} if this object's {@link #content()} should be used to copy.
  *       <li>{@code false} if {@code newContent} should be used instead.
  *     </ul>
  *
  * @param newContent
  *     <ul>
  *       <li>if {@code copyContent} is false then this will be used in the copy's content.
  *       <li>if {@code null} then a default buffer of 0 size will be selected
  *     </ul>
  *
  * @return A copy of this object
  */
 private FullHttpResponse copy(boolean copyContent, ByteBuf newContent) {
   DefaultFullHttpResponse copy =
       new DefaultFullHttpResponse(
           protocolVersion(),
           status(),
           copyContent
               ? content().copy()
               : newContent == null ? Unpooled.buffer(0) : newContent);
   copy.headers().set(headers());
   copy.trailingHeaders().set(trailingHeaders());
   return copy;
 }
 private void setCookies(HttpResponse response, DefaultFullHttpResponse httpServletResponse) {
   if (response.getCookies() != null) {
     List<Cookie> cookieValues = new ArrayList<Cookie>();
     for (org.mockserver.model.Cookie cookie : response.getCookies()) {
       cookieValues.add(
           new DefaultCookie(cookie.getName().getValue(), cookie.getValue().getValue()));
     }
     if (!cookieValues.isEmpty()) {
       httpServletResponse.headers().add(SET_COOKIE, ServerCookieEncoder.LAX.encode(cookieValues));
     }
   }
 }
  private void setHeaders(HttpResponse response, DefaultFullHttpResponse httpServletResponse) {
    if (response.getHeaders() != null) {
      for (Header header : response.getHeaders()) {
        for (NottableString value : header.getValues()) {
          httpServletResponse.headers().add(header.getName().getValue(), value.getValue());
        }
      }
    }

    if (Strings.isNullOrEmpty(response.getFirstHeader(CONTENT_TYPE))) {
      if (response.getBody() != null && !Strings.isNullOrEmpty(response.getBody().toString())) {
        Charset bodyCharset = response.getBody().getCharset(null);
        String bodyContentType = response.getBody().getContentType();
        if (bodyCharset != null) {
          httpServletResponse
              .headers()
              .set(CONTENT_TYPE, bodyContentType + "; charset=" + bodyCharset.name().toLowerCase());
        } else if (bodyContentType != null) {
          httpServletResponse.headers().set(CONTENT_TYPE, bodyContentType);
        }
      }
    }
  }
 /*
  * Create an HTTP 301 redirect response.
  */
 public static FullHttpResponse redirect() {
   DefaultFullHttpResponse resp = new DefaultFullHttpResponse(HTTP_1_1, MOVED_PERMANENTLY);
   resp.headers().set(CONTENT_LENGTH, 0);
   resp.headers().set(LOCATION, IMG_THUMBNAIL_URI);
   return resp;
 }