io.netty.handler.codec.http.HttpResponse.headers()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(9.6k)|赞(0)|评价(0)|浏览(121)

本文整理了Java中io.netty.handler.codec.http.HttpResponse.headers()方法的一些代码示例,展示了HttpResponse.headers()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpResponse.headers()方法的具体详情如下:
包路径:io.netty.handler.codec.http.HttpResponse
类名称:HttpResponse
方法名:headers

HttpResponse.headers介绍

暂无

代码示例

代码示例来源:origin: lets-blade/blade

private void setGzip(HttpResponse response) {
  response.headers().set(HttpConst.CONTENT_ENCODING, "gzip");
}

代码示例来源:origin: rakam-io/rakam

@Path("/display/*")
@GET
public void display(RakamHttpRequest request) {
  if (!database.isPresent()) {
    throw new RakamException("Custom page feature is not implemented", NOT_IMPLEMENTED);
  }
  String path = request.path().substring(21);
  String[] projectCustomPage = path.split("/", 3);
  byte[] bytes;
  try {
    InputStream file = database.get().getFile(Integer.parseInt(projectCustomPage[0]), projectCustomPage[1], projectCustomPage[2]);
    if (file == null) {
      request.response(NOT_FOUND.reasonPhrase(), NOT_FOUND).end();
    }
    bytes = ByteStreams.toByteArray(file);
  } catch (IOException e) {
    throw Throwables.propagate(e);
  }
  HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
  response.headers().set(CONTENT_TYPE, "text/html");
  HttpHeaders.setContentLength(response, bytes.length);
  request.context().write(response);
  request.context().write(Unpooled.wrappedBuffer(bytes));
  ChannelFuture lastContentFuture = request.context().writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
  if (!HttpHeaders.isKeepAlive(request)) {
    lastContentFuture.addListener(ChannelFutureListener.CLOSE);
  }
}

代码示例来源:origin: lets-blade/blade

private void setGzip(HttpResponse response) {
  response.headers().set(HttpConst.CONTENT_ENCODING, "gzip");
}

代码示例来源:origin: wildfly/wildfly

private static void setVaryHeader(final HttpResponse response) {
  response.headers().set(HttpHeaderNames.VARY, HttpHeaderNames.ORIGIN);
}

代码示例来源:origin: wildfly/wildfly

private static void setOrigin(final HttpResponse response, final String origin) {
  response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, origin);
}

代码示例来源:origin: Graylog2/graylog2-server

private void writeResponse(Channel channel,
                boolean keepAlive,
                HttpVersion httpRequestVersion,
                HttpResponseStatus status,
                String origin) {
    final HttpResponse response = new DefaultHttpResponse(httpRequestVersion, status);

    response.headers().set(HttpHeaderNames.CONTENT_LENGTH, 0);
    response.headers().set(HttpHeaderNames.CONNECTION, keepAlive ? HttpHeaderValues.KEEP_ALIVE : HttpHeaderValues.CLOSE);

    if (enableCors && origin != null && !origin.isEmpty()) {
      response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, origin);
      response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, true);
      response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, "Authorization, Content-Type");
    }

    final ChannelFuture channelFuture = channel.writeAndFlush(response);

    if (!keepAlive) {
      channelFuture.addListener(ChannelFutureListener.CLOSE);
    }
  }
}

代码示例来源:origin: wildfly/wildfly

private void setAllowCredentials(final HttpResponse response) {
  if (config.isCredentialsAllowed()
      && !response.headers().get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN).equals(ANY_ORIGIN)) {
    response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
  }
}

代码示例来源:origin: wildfly/wildfly

private void setMaxAge(final HttpResponse response) {
  response.headers().set(HttpHeaderNames.ACCESS_CONTROL_MAX_AGE, config.maxAge());
}

代码示例来源:origin: wildfly/wildfly

private void setAllowMethods(final HttpResponse response) {
  response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_METHODS, config.allowedRequestMethods());
}

代码示例来源:origin: wildfly/wildfly

private void setAllowHeaders(final HttpResponse response) {
  response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, config.allowedRequestHeaders());
}

代码示例来源:origin: lets-blade/blade

/**
 * Sets the content type header for the HTTP Response
 *
 * @param response HTTP response
 * @param file     file to extract content type
 */
private static void setContentTypeHeader(HttpResponse response, File file) {
  String contentType = StringKit.mimeType(file.getName());
  if (null == contentType) {
    contentType = URLConnection.guessContentTypeFromName(file.getName());
  }
  response.headers().set(HttpConst.CONTENT_TYPE, contentType);
}

代码示例来源:origin: lets-blade/blade

/**
 * Sets the content type header for the HTTP Response
 *
 * @param response HTTP response
 * @param file     file to extract content type
 */
private static void setContentTypeHeader(HttpResponse response, File file) {
  String contentType = StringKit.mimeType(file.getName());
  if (null == contentType) {
    contentType = URLConnection.guessContentTypeFromName(file.getName());
  }
  response.headers().set(HttpConst.CONTENT_TYPE, contentType);
}

代码示例来源:origin: lets-blade/blade

/**
 * Sets the Date and Cache headers for the HTTP Response
 *
 * @param response    HTTP response
 * @param fileToCache file to extract content type
 */
private void setDateAndCacheHeaders(HttpResponse response, File fileToCache) {
  response.headers().set(HttpConst.DATE, DateKit.gmtDate());
  // Add cache headers
  if (httpCacheSeconds > 0) {
    response.headers().set(HttpConst.EXPIRES, DateKit.gmtDate(LocalDateTime.now().plusSeconds(httpCacheSeconds)));
    response.headers().set(HttpConst.CACHE_CONTROL, "private, max-age=" + httpCacheSeconds);
    if (null != fileToCache) {
      response.headers().set(HttpConst.LAST_MODIFIED, DateKit.gmtDate(new Date(fileToCache.lastModified())));
    } else {
      response.headers().set(HttpConst.LAST_MODIFIED, DateKit.gmtDate(LocalDateTime.now().plusDays(-1)));
    }
  }
}

代码示例来源:origin: lets-blade/blade

/**
 * Sets the Date and Cache headers for the HTTP Response
 *
 * @param response    HTTP response
 * @param fileToCache file to extract content type
 */
private void setDateAndCacheHeaders(HttpResponse response, File fileToCache) {
  response.headers().set(HttpConst.DATE, DateKit.gmtDate());
  // Add cache headers
  if (httpCacheSeconds > 0) {
    response.headers().set(HttpConst.EXPIRES, DateKit.gmtDate(LocalDateTime.now().plusSeconds(httpCacheSeconds)));
    response.headers().set(HttpConst.CACHE_CONTROL, "private, max-age=" + httpCacheSeconds);
    if (null != fileToCache) {
      response.headers().set(HttpConst.LAST_MODIFIED, DateKit.gmtDate(new Date(fileToCache.lastModified())));
    } else {
      response.headers().set(HttpConst.LAST_MODIFIED, DateKit.gmtDate(LocalDateTime.now().plusDays(-1)));
    }
  }
}

代码示例来源:origin: wildfly/wildfly

private static void forbidden(final ChannelHandlerContext ctx, final HttpRequest request) {
  HttpResponse response = new DefaultFullHttpResponse(request.protocolVersion(), FORBIDDEN);
  response.headers().set(HttpHeaderNames.CONTENT_LENGTH, HttpHeaderValues.ZERO);
  release(request);
  respond(ctx, request, response);
}

代码示例来源:origin: wildfly/wildfly

/**
   * Return that we need cannot not support the web socket version
   */
  public static ChannelFuture sendUnsupportedVersionResponse(Channel channel, ChannelPromise promise) {
    HttpResponse res = new DefaultFullHttpResponse(
        HttpVersion.HTTP_1_1,
        HttpResponseStatus.UPGRADE_REQUIRED);
    res.headers().set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, WebSocketVersion.V13.toHttpHeaderValue());
    HttpUtil.setContentLength(res, 0);
    return channel.writeAndFlush(res, promise);
  }
}

代码示例来源:origin: wildfly/wildfly

private void setExposeHeaders(final HttpResponse response) {
  if (!config.exposedHeaders().isEmpty()) {
    response.headers().set(HttpHeaderNames.ACCESS_CONTROL_EXPOSE_HEADERS, config.exposedHeaders());
  }
}

代码示例来源:origin: blynkkk/blynk-server

/**
 * Sets the Date and Cache headers for the HTTP Response
 *
 * @param response
 *            HTTP response
 * @param fileToCache
 *            file to extract content type
 */
private static void setDateAndCacheHeaders(io.netty.handler.codec.http.HttpResponse response, File fileToCache) {
  SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
  dateFormatter.setTimeZone(TimeZone.getTimeZone(HTTP_DATE_GMT_TIMEZONE));
  // Date header
  Calendar time = new GregorianCalendar();
  response.headers().set(DATE, dateFormatter.format(time.getTime()));
  // Add cache headers
  time.add(Calendar.SECOND, HTTP_CACHE_SECONDS);
  response.headers()
      .set(EXPIRES, dateFormatter.format(time.getTime()))
      .set(CACHE_CONTROL, "private, max-age=" + HTTP_CACHE_SECONDS)
      .set(LAST_MODIFIED, dateFormatter.format(new Date(fileToCache.lastModified())));
}

代码示例来源:origin: lets-blade/blade

@Override
public void close() throws IOException {
  try {
    this.flush();
    FileChannel file       = new FileInputStream(this.file).getChannel();
    long        fileLength = file.size();
    HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    httpResponse.headers().set(HttpConst.CONTENT_LENGTH, fileLength);
    httpResponse.headers().set(HttpConst.DATE, DateKit.gmtDate());
    httpResponse.headers().set(HttpConst.SERVER, "blade/" + Const.VERSION);
    boolean keepAlive = WebContext.request().keepAlive();
    if (keepAlive) {
      httpResponse.headers().set(HttpConst.CONNECTION, HttpConst.KEEP_ALIVE);
    }
    // Write the initial line and the header.
    ctx.write(httpResponse);
    ctx.write(new DefaultFileRegion(file, 0, fileLength), ctx.newProgressivePromise());
    // Write the end marker.
    ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
  } finally {
    if(null != outputStream){
      outputStream.close();
    }
  }
}

代码示例来源:origin: lets-blade/blade

@Override
public void close() throws IOException {
  try {
    this.flush();
    FileChannel file       = new FileInputStream(this.file).getChannel();
    long        fileLength = file.size();
    HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    httpResponse.headers().set(HttpConst.CONTENT_LENGTH, fileLength);
    httpResponse.headers().set(HttpConst.DATE, DateKit.gmtDate());
    httpResponse.headers().set(HttpConst.SERVER, "blade/" + Const.VERSION);
    boolean keepAlive = WebContext.request().keepAlive();
    if (keepAlive) {
      httpResponse.headers().set(HttpConst.CONNECTION, HttpConst.KEEP_ALIVE);
    }
    // Write the initial line and the header.
    ctx.write(httpResponse);
    ctx.write(new DefaultFileRegion(file, 0, fileLength), ctx.newProgressivePromise());
    // Write the end marker.
    ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
  } finally {
    if(null != outputStream){
      outputStream.close();
    }
  }
}

相关文章