javax.ws.rs.container.ContainerRequestContext.getProperty()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(10.9k)|赞(0)|评价(0)|浏览(179)

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

ContainerRequestContext.getProperty介绍

[英]Returns the property with the given name registered in the current request/response exchange context, or null if there is no property by that name.

A property allows a JAX-RS filters and interceptors to exchange additional custom information not already provided by this interface.

A list of supported properties can be retrieved using #getPropertyNames(). Custom property names should follow the same convention as package names.

In a Servlet container, the properties are synchronized with the ServletRequestand expose all the attributes available in the ServletRequest. Any modifications of the properties are also reflected in the set of properties of the associated ServletRequest.
[中]返回在当前请求/响应交换上下文中注册的具有给定名称的属性,如果没有该名称的属性,则返回null。
属性允许JAX-RS过滤器和拦截器交换此接口尚未提供的其他自定义信息。
可以使用#getPropertyNames()检索受支持属性的列表。自定义属性名称应遵循与包名称相同的约定。
在Servlet容器中,属性与ServletRequest同步,并公开ServletRequest中可用的所有属性。属性的任何修改也会反映在相关ServletRequest的属性集中。

代码示例

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

@Override
public Object getAttribute(String name, int scope) {
  return requestContext.getProperty(name);
}

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

@Override
  public void filter(final ContainerRequestContext request,
      final ContainerResponseContext response) throws IOException {

    final Long startTime = (Long) request.getProperty(RUNTIME_PROPERTY);
    if (startTime != null) {
      final float seconds = (currentTimeProvider.get() - startTime) / NANOS_IN_SECOND;
      response.getHeaders().putSingle(RUNTIME_HEADER, String.format(Locale.ROOT, "%.6f", seconds));
    }
  }
}

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

/**
 * Resolve resource-level span.
 * <p>
 * If open tracing is enabled and {@link GlobalTracer} is registered, resource-level span should be stored in the
 * {@link OpenTracingFeature#SPAN_CONTEXT_PROPERTY}. This span is resolved and returned as an {@link Optional}.
 *
 * @param context {@link ContainerRequestContext} instance, can be obtained via {@code @Context} injection
 * @return {@link Optional} of the resolved span, if found; empty optional if not
 */
public static Optional<Span> getRequestSpan(final ContainerRequestContext context) {
  if (context != null) {
    final Object spanProperty = context.getProperty(OpenTracingFeature.SPAN_CONTEXT_PROPERTY);
    if (spanProperty != null && spanProperty instanceof Span) {
      return Optional.of((Span) spanProperty);
    }
  }
  return Optional.empty();
}

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

@Override
  public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
    final Timer.Context context = (Timer.Context) requestContext.getProperty("metricsTimerContext");
    if (context == null) return;
    final long elapsedNanos = context.stop();
    responseContext.getHeaders().add("X-Runtime-Microseconds", TimeUnit.NANOSECONDS.toMicros(elapsedNanos));
  }
}

代码示例来源:origin: openzipkin/brave

@Override public void filter(ContainerRequestContext request) {
  SpanCustomizer span = (SpanCustomizer) request.getProperty(SpanCustomizer.class.getName());
  if (span != null && resourceInfo != null) {
   parser.resourceInfo(resourceInfo, span);
  }
 }
}

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

@Override
  public void resetAttributes(final ContainerRequestContext requestContext) {
    final AbstractRequestAttributes attributes =
        (AbstractRequestAttributes) requestContext.getProperty(REQUEST_ATTRIBUTES_PROPERTY);
    RequestContextHolder.resetRequestAttributes();
    attributes.requestCompleted();
  }
} : EMPTY_ATTRIBUTE_CONTROLLER;

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

/**
 * Create and start ad-hoc custom span with a custom name as a child span of the request span (if available).
 *
 * @param context  {@link ContainerRequestContext} instance, can be obtained via {@code @Context} injection
 * @param spanName name to be used for the created span
 * @return If parent span ("request span") instance is stored in the {@code ContainerRequestContext}, new span is created
 * as a child span of the found span. If no parent span found, new "root" span is created. In both cases, the returned span
 * is already started. In order to successfully store the tracing, {@link Span#finish()} needs to be invoked explicitly,
 * after the traced code finishes.
 */
public static Span getRequestChildSpan(final ContainerRequestContext context, final String spanName) {
  Tracer.SpanBuilder spanBuilder = GlobalTracer.get().buildSpan(spanName);
  if (context != null) {
    final Object spanProperty = context.getProperty(OpenTracingFeature.SPAN_CONTEXT_PROPERTY);
    if (spanProperty != null && spanProperty instanceof Span) {
      spanBuilder = spanBuilder.asChildOf((Span) spanProperty);
    }
  }
  return spanBuilder.startManual();
}

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

@Override
  public void filter(final ContainerRequestContext requestContext, final ContainerResponseContext responseContext)
      throws IOException {
    if (!logger.isLoggable(level)) {
      return;
    }
    final Object requestId = requestContext.getProperty(LOGGING_ID_PROPERTY);
    final long id = requestId != null ? (Long) requestId : _id.incrementAndGet();

    final StringBuilder b = new StringBuilder();

    printResponseLine(b, "Server responded with a response", id, responseContext.getStatus());
    printPrefixedHeaders(b, id, RESPONSE_PREFIX, responseContext.getStringHeaders());

    if (responseContext.hasEntity() && printEntity(verbosity, responseContext.getMediaType())) {
      final OutputStream stream = new LoggingStream(b, responseContext.getEntityStream());
      responseContext.setEntityStream(stream);
      requestContext.setProperty(ENTITY_LOGGER_PROPERTY, stream);
      // not calling log(b) here - it will be called by the interceptor
    } else {
      log(b);
    }
  }
}

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

@Override
  public void filter(final ContainerRequestContext requestContext, final ContainerResponseContext responseContext)
      throws IOException {
    if (!logger.isLoggable(level)) {
      return;
    }
    final Object requestId = requestContext.getProperty(LOGGING_ID_PROPERTY);
    final long id = requestId != null ? (Long) requestId : _id.incrementAndGet();

    final StringBuilder b = new StringBuilder();

    printResponseLine(b, "Server responded with a response", id, responseContext.getStatus());
    printPrefixedHeaders(b, id, RESPONSE_PREFIX, responseContext.getStringHeaders());

    if (responseContext.hasEntity() && printEntity(verbosity, responseContext.getMediaType())) {
      final OutputStream stream = new LoggingStream(b, responseContext.getEntityStream());
      responseContext.setEntityStream(stream);
      requestContext.setProperty(ENTITY_LOGGER_PROPERTY, stream);
      // not calling log(b) here - it will be called by the interceptor
    } else {
      log(b);
    }
  }
}

代码示例来源:origin: oracle/helidon

@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
  Span span = (Span) requestContext.getProperty(SPAN_PROPERTY);
  if (span == null) {
    return; // unknown state
  }
  if (responseContext.getStatus() >= 500) {
    Tags.ERROR.set(span, true);
    span.log(CollectionsHelper.mapOf(
        "event", "error",
        "status", responseContext.getStatus()
    ));
  }
  span.finish();
  TracingContext.remove();
}

代码示例来源:origin: oracle/helidon

FilterContext fc = (FilterContext) requestContext.getProperty(PROP_FILTER_CONTEXT);
SecurityDefinition methodSecurity = jerseySecurityContext.methodSecurity();
SecurityContext securityContext = jerseySecurityContext.securityContext();
  if ((Boolean) requestContext.getProperty(SecurityPreMatchingFilter.PROP_CLOSE_PARENT_SPAN)) {
    finishSpan((Span) requestContext.getProperty(SecurityPreMatchingFilter.PROP_PARENT_SPAN),
          CollectionsHelper.listOf());

代码示例来源:origin: resteasy/Resteasy

@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException
{
 String origin = requestContext.getHeaderString(CorsHeaders.ORIGIN);
 if (origin == null || requestContext.getMethod().equalsIgnoreCase("OPTIONS") || requestContext.getProperty("cors.failure") != null)
 {
   // don't do anything if origin is null, its an OPTIONS request, or cors.failure is set
   return;
 }
 responseContext.getHeaders().putSingle(CorsHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, origin);
 responseContext.getHeaders().putSingle(CorsHeaders.VARY, CorsHeaders.ORIGIN);
 if (allowCredentials) responseContext.getHeaders().putSingle(CorsHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
 if (exposedHeaders != null) {
   responseContext.getHeaders().putSingle(CorsHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, exposedHeaders);
 }
}

代码示例来源:origin: resteasy/Resteasy

KeyRepository repository = (KeyRepository) context.getProperty(KeyRepository.class.getName());
if (repository == null)

代码示例来源:origin: ch.exense.step/core

@POST
@Secured
@Path("/logout")
public void logout(@Context ContainerRequestContext crc) {
  Session session = (Session) crc.getProperty("session");
  if(session != null) {
    sessions.remove(session.getToken());
  }
}

代码示例来源:origin: apache/cxf

@Override
public void filter(ContainerRequestContext context) throws IOException {
  if (!"true".equals(context.getProperty("FirstPrematchingFilter"))
    || !"true".equals(context.getProperty("DynamicPrematchingFilter"))
    || !"true".equals(servletRequest.getAttribute("FirstPrematchingFilter"))
    || !"true".equals(servletRequest.getAttribute("DynamicPrematchingFilter"))) {
    throw new RuntimeException();
  }
  context.getHeaders().add("BOOK", "12");
}

代码示例来源:origin: apache/cxf

@Override
public void filter(ContainerRequestContext context) throws IOException {
  if (!"true".equals(context.getProperty("FirstPrematchingFilter"))) {
    throw new RuntimeException();
  }
  context.setProperty("DynamicPrematchingFilter", "true");
}

代码示例来源:origin: apache/cxf

@SuppressWarnings("unchecked")
@Override
public void filter(final ContainerRequestContext requestContext,
    final ContainerResponseContext responseContext) throws IOException {
  super.stopTraceSpan(requestContext.getHeaders(), responseContext.getHeaders(),
    responseContext.getStatus(), (TraceScopeHolder<TraceScope>)requestContext.getProperty(TRACE_SPAN));
}

代码示例来源:origin: apache/cxf

@SuppressWarnings("unchecked")
@Override
public void filter(final ContainerRequestContext requestContext,
    final ContainerResponseContext responseContext) throws IOException {
  super.stopTraceSpan(requestContext.getHeaders(), responseContext.getHeaders(),
    responseContext.getStatus(), (TraceScopeHolder<TraceScope>)requestContext.getProperty(TRACE_SPAN));
}

代码示例来源:origin: apache/cxf

@Override
public void filter(ContainerRequestContext reqCtx, ContainerResponseContext respCtx) throws IOException {
  if (Boolean.TRUE == reqCtx.getProperty(OPEN_API_PROPERTY)) {
    final Object entity = respCtx.getEntity();
    // Right before 1.5.18, the entity was always a String but became a model object
    // (io.swagger.models.Swagger) after. For now, let us serialize it to JSON manually.
    String swaggerJson = entity instanceof String ? (String)entity : Json.pretty(entity);
    String openApiJson = SwaggerToOpenApiConversionUtils.getOpenApiFromSwaggerJson(
        createMessageContext(), swaggerJson, openApiConfig);
    respCtx.setEntity(openApiJson);
  }
}

代码示例来源:origin: apache/cxf

@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
  throws IOException {
  if (PropertyUtils.isTrue(requestContext.getProperty("blocked"))) {
    throw new BlockedException();
  }
}

相关文章

微信公众号

最新文章

更多