io.micronaut.http.HttpRequest.getHeaders()方法的使用及代码示例

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

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

HttpRequest.getHeaders介绍

暂无

代码示例

代码示例来源:origin: micronaut-projects/micronaut-core

@Override
default Optional<Locale> getLocale() {
  return getHeaders().findFirst(HttpHeaders.ACCEPT_LANGUAGE)
    .map((text) -> {
      int len = text.length();
      if (len == 0 || (len == 1 && text.charAt(0) == '*')) {
        return Locale.getDefault().toLanguageTag();
      }
      if (text.indexOf(';') > -1) {
        text = text.split(";")[0];
      }
      if (text.indexOf(',') > -1) {
        text = text.split(",")[0];
      }
      return text;
    })
    .map(Locale::forLanguageTag);
}

代码示例来源:origin: micronaut-projects/micronaut-core

/**
   * Obtains the web socket URL.
   *
   *
   * @param ctx The context
   * @param req The request
   * @return The socket URL
   */
  protected String getWebSocketURL(ChannelHandlerContext ctx, HttpRequest req) {
    boolean isSecure = ctx.pipeline().get(SslHandler.class) != null;
    return (isSecure ? SCHEME_SECURE_WEBSOCKET : SCHEME_WEBSOCKET) + req.getHeaders().get(HttpHeaderNames.HOST) + req.getUri() ;
  }
}

代码示例来源:origin: micronaut-projects/micronaut-core

/**
   * @param request The {@link HttpRequest} object
   * @return Return whether this request is a pre-flight request
   */
  static boolean isPreflightRequest(HttpRequest request) {
    HttpHeaders headers = request.getHeaders();
    Optional<String> origin = headers.getOrigin();
    return origin.isPresent() && headers.contains(ACCESS_CONTROL_REQUEST_METHOD) && HttpMethod.OPTIONS == request.getMethod();
  }
}

代码示例来源:origin: micronaut-projects/micronaut-core

/**
 * Creates an initial connection to the given remote host.
 *
 * @param request  The request
 * @param host     The host
 * @param port     The port
 * @param sslCtx   The SslContext instance
 * @param isStream Is the connection a stream connection
 * @return A ChannelFuture
 */
protected ChannelFuture doConnect(
    io.micronaut.http.HttpRequest<?> request,
    String host,
    int port,
    @Nullable SslContext sslCtx,
    boolean isStream) {
  Bootstrap localBootstrap = this.bootstrap.clone();
  localBootstrap.handler(new HttpClientInitializer(
      sslCtx,
      host,
      port,
      isStream,
      request.getHeaders().get(io.micronaut.http.HttpHeaders.ACCEPT, String.class).map(ct -> ct.equals(MediaType.TEXT_EVENT_STREAM)).orElse(false))
  );
  return doConnect(localBootstrap, host, port);
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Override
public BindingResult<T> bind(ArgumentConversionContext<T> argument, HttpRequest<?> source) {
  ConvertibleMultiValues<String> parameters = source.getHeaders();
  AnnotationMetadata annotationMetadata = argument.getAnnotationMetadata();
  String parameterName = annotationMetadata.getValue(Header.class, String.class).orElse(argument.getArgument().getName());
  return doBind(argument, parameters, parameterName);
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Override
public Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, ServerFilterChain chain) {
  boolean originHeaderPresent = request.getHeaders().getOrigin().isPresent();
  if (originHeaderPresent) {
    Optional<MutableHttpResponse<?>> response = handleRequest(request);
    if (response.isPresent()) {
      return Publishers.just(response.get());
    } else {
      return Publishers.map(chain.proceed(request), mutableHttpResponse -> {
        handleResponse(request, mutableHttpResponse);
        return mutableHttpResponse;
      });
    }
  } else {
    return chain.proceed(request);
  }
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Override
protected NettyRxWebSocketSession createWebSocketSession(ChannelHandlerContext ctx) {
  String id = originatingRequest.getHeaders().get(HttpHeaderNames.SEC_WEBSOCKET_KEY);
  final Channel channel = ctx.channel();

代码示例来源:origin: micronaut-projects/micronaut-core

HttpHeaders headers = request.getHeaders();
Optional<String> originHeader = headers.getOrigin();
if (originHeader.isPresent()) {

代码示例来源:origin: micronaut-projects/micronaut-core

private void writeFinalNettyResponse(MutableHttpResponse<?> message, AtomicReference<HttpRequest<?>> requestReference, ChannelHandlerContext context) {
  NettyMutableHttpResponse nettyHttpResponse = (NettyMutableHttpResponse) message;
  FullHttpResponse nettyResponse = nettyHttpResponse.getNativeResponse();
  HttpRequest<?> httpRequest = requestReference.get();
  io.netty.handler.codec.http.HttpHeaders nettyHeaders = nettyResponse.headers();
  // default Connection header if not set explicitly
  if (!nettyHeaders.contains(HttpHeaderNames.CONNECTION)) {
    boolean expectKeepAlive = nettyResponse.protocolVersion().isKeepAliveDefault() || httpRequest.getHeaders().isKeepAlive();
    HttpStatus status = nettyHttpResponse.status();
    if (!expectKeepAlive || status.getCode() > 299) {
      nettyHeaders.add(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
    } else {
      nettyHeaders.add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    }
  }
  // default to Transfer-Encoding: chunked if Content-Length not set or not already set
  if (!nettyHeaders.contains(HttpHeaderNames.CONTENT_LENGTH) && !nettyHeaders.contains(HttpHeaderNames.TRANSFER_ENCODING)) {
    nettyHeaders.add(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
  }
  final Object body = message.body();
  if (body instanceof NettyCustomizableResponseTypeHandlerInvoker) {
    NettyCustomizableResponseTypeHandlerInvoker handler = (NettyCustomizableResponseTypeHandlerInvoker) body;
    handler.invoke(httpRequest, nettyHttpResponse, context);
  } else {
    // close handled by HttpServerKeepAliveHandler
    context.writeAndFlush(nettyResponse);
    context.read();
  }
}

代码示例来源:origin: micronaut-projects/micronaut-core

registerDefaultAnnotationBinders(byAnnotation);
byType.put(Argument.of(HttpHeaders.class).typeHashCode(), (RequestArgumentBinder<HttpHeaders>) (argument, source) -> () -> Optional.of(source.getHeaders()));
byType.put(Argument.of(HttpRequest.class).typeHashCode(), (RequestArgumentBinder<HttpRequest>) (argument, source) -> () -> Optional.of(source));
byType.put(Argument.of(HttpParameters.class).typeHashCode(), (RequestArgumentBinder<HttpParameters>) (argument, source) -> () -> Optional.of(source.getParameters()));

代码示例来源:origin: micronaut-projects/micronaut-core

/**
 * Handles a CORS response.
 *
 * @param request  The {@link HttpRequest} object
 * @param response The {@link MutableHttpResponse} object
 */
protected void handleResponse(HttpRequest<?> request, MutableHttpResponse<?> response) {
  HttpHeaders headers = request.getHeaders();
  Optional<String> originHeader = headers.getOrigin();
  originHeader.ifPresent(requestOrigin -> {
    Optional<CorsOriginConfiguration> optionalConfig = getConfiguration(requestOrigin);
    if (optionalConfig.isPresent()) {
      CorsOriginConfiguration config = optionalConfig.get();
      if (CorsUtil.isPreflightRequest(request)) {
        Optional<HttpMethod> result = headers.getFirst(ACCESS_CONTROL_REQUEST_METHOD, HttpMethod.class);
        setAllowMethods(result.get(), response);
        Argument<List> type = Argument.of(List.class, String.class);
        Optional<List> allowedHeaders = headers.get(ACCESS_CONTROL_REQUEST_HEADERS, type);
        allowedHeaders.ifPresent(val ->
          setAllowHeaders(val, response)
        );
        setMaxAge(config.getMaxAge(), response);
      }
      setOrigin(requestOrigin, response);
      setVary(response);
      setExposeHeaders(config.getExposedHeaders(), response);
      setAllowCredentials(config, response);
    }
  });
}

代码示例来源:origin: micronaut-projects/micronaut-core

ZonedDateTime ifModifiedSince = request.getHeaders().getDate(HttpHeaders.IF_MODIFIED_SINCE);
if (ifModifiedSince != null) {

代码示例来源:origin: io.micronaut/micronaut-http

@Override
default Optional<Locale> getLocale() {
  return getHeaders().findFirst(HttpHeaders.ACCEPT_LANGUAGE)
    .map((text) -> {
      int len = text.length();
      if (len == 0 || (len == 1 && text.charAt(0) == '*')) {
        return Locale.getDefault().toLanguageTag();
      }
      if (text.indexOf(';') > -1) {
        text = text.split(";")[0];
      }
      if (text.indexOf(',') > -1) {
        text = text.split(",")[0];
      }
      return text;
    })
    .map(Locale::forLanguageTag);
}

代码示例来源:origin: micronaut-projects/micronaut-spring

private static HttpHeaders initHeaders(HttpRequest<?> request) {
  final HttpHeaders httpHeaders = new HttpHeaders();
  final io.micronaut.http.HttpHeaders micronautHeaders = request.getHeaders();
  micronautHeaders.forEach(entry -> {
    final String key = entry.getKey();
    final List<String> value = entry.getValue();
    httpHeaders.addAll(key, value);
  });
  return httpHeaders;
}

代码示例来源:origin: io.micronaut/http-server

/**
   * @param request The {@link HttpRequest} object
   * @return Return whether this request is a pre-flight request
   */
  static boolean isPreflightRequest(HttpRequest request) {
    HttpHeaders headers = request.getHeaders();
    Optional<String> origin = headers.getOrigin();
    return origin.isPresent() && headers.contains(ACCESS_CONTROL_REQUEST_METHOD) && HttpMethod.OPTIONS == request.getMethod();
  }
}

代码示例来源:origin: io.micronaut/http-client

/**
 * Creates an initial connection to the given remote host.
 *
 * @param request  The request
 * @param host     The host
 * @param port     The port
 * @param sslCtx   The SslContext instance
 * @param isStream Is the connection a stream connection
 * @return A ChannelFuture
 */
protected ChannelFuture doConnect(
    io.micronaut.http.HttpRequest<?> request,
    String host,
    int port,
    @Nullable SslContext sslCtx,
    boolean isStream) {
  Bootstrap localBootstrap = this.bootstrap.clone();
  localBootstrap.handler(new HttpClientInitializer(
      sslCtx,
      host,
      port,
      isStream,
      request.getHeaders().get(io.micronaut.http.HttpHeaders.ACCEPT, String.class).map(ct -> ct.equals(MediaType.TEXT_EVENT_STREAM)).orElse(false))
  );
  return doConnect(localBootstrap, host, port);
}

代码示例来源:origin: io.micronaut/micronaut-http-client

/**
 * Creates an initial connection to the given remote host.
 *
 * @param request  The request
 * @param host     The host
 * @param port     The port
 * @param sslCtx   The SslContext instance
 * @param isStream Is the connection a stream connection
 * @return A ChannelFuture
 */
protected ChannelFuture doConnect(
    io.micronaut.http.HttpRequest<?> request,
    String host,
    int port,
    @Nullable SslContext sslCtx,
    boolean isStream) {
  Bootstrap localBootstrap = this.bootstrap.clone();
  localBootstrap.handler(new HttpClientInitializer(
      sslCtx,
      host,
      port,
      isStream,
      request.getHeaders().get(io.micronaut.http.HttpHeaders.ACCEPT, String.class).map(ct -> ct.equals(MediaType.TEXT_EVENT_STREAM)).orElse(false))
  );
  return doConnect(localBootstrap, host, port);
}

代码示例来源:origin: io.micronaut/micronaut-http

@Override
public BindingResult<T> bind(ArgumentConversionContext<T> argument, HttpRequest<?> source) {
  ConvertibleMultiValues<String> parameters = source.getHeaders();
  AnnotationMetadata annotationMetadata = argument.getAnnotationMetadata();
  String parameterName = annotationMetadata.getValue(Header.class, String.class).orElse(argument.getArgument().getName());
  return doBind(argument, parameters, parameterName);
}

代码示例来源:origin: io.micronaut/http-server

@Override
public Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, ServerFilterChain chain) {
  boolean originHeaderPresent = request.getHeaders().getOrigin().isPresent();
  if (originHeaderPresent) {
    Optional<MutableHttpResponse<?>> response = handleRequest(request);
    if (response.isPresent()) {
      return Publishers.just(response.get());
    } else {
      return Publishers.map(chain.proceed(request), mutableHttpResponse -> {
        handleResponse(request, mutableHttpResponse);
        return mutableHttpResponse;
      });
    }
  } else {
    return chain.proceed(request);
  }
}

代码示例来源:origin: io.micronaut/http-server

/**
 * Handles a CORS response.
 *
 * @param request  The {@link HttpRequest} object
 * @param response The {@link MutableHttpResponse} object
 */
protected void handleResponse(HttpRequest<?> request, MutableHttpResponse<?> response) {
  HttpHeaders headers = request.getHeaders();
  Optional<String> originHeader = headers.getOrigin();
  originHeader.ifPresent(requestOrigin -> {
    Optional<CorsOriginConfiguration> optionalConfig = getConfiguration(requestOrigin);
    if (optionalConfig.isPresent()) {
      CorsOriginConfiguration config = optionalConfig.get();
      if (CorsUtil.isPreflightRequest(request)) {
        Optional<HttpMethod> result = headers.getFirst(ACCESS_CONTROL_REQUEST_METHOD, HttpMethod.class);
        setAllowMethods(result.get(), response);
        Argument<List> type = Argument.of(List.class, String.class);
        Optional<List> allowedHeaders = headers.get(ACCESS_CONTROL_REQUEST_HEADERS, type);
        allowedHeaders.ifPresent(val ->
          setAllowHeaders(val, response)
        );
        setMaxAge(config.getMaxAge(), response);
      }
      setOrigin(requestOrigin, response);
      setVary(response);
      setExposeHeaders(config.getExposedHeaders(), response);
      setAllowCredentials(config, response);
    }
  });
}

相关文章