Java Code Examples for org.glassfish.jersey.server.ContainerRequest#setProperty()

The following examples show how to use org.glassfish.jersey.server.ContainerRequest#setProperty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JerseyHandlerFilter.java    From aws-serverless-java-container with Apache License 2.0 6 votes vote down vote up
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
        throws IOException, ServletException {
    Timer.start("JERSEY_FILTER_DOFILTER");
    // we use a latch to make the processing inside Jersey synchronous
    CountDownLatch jerseyLatch = new CountDownLatch(1);

    ContainerRequest req = servletRequestToContainerRequest(servletRequest);
    req.setWriter(new JerseyServletResponseWriter(servletResponse, jerseyLatch));

    req.setProperty(JERSEY_SERVLET_RESPONSE_PROPERTY, servletResponse);

    jersey.handle(req);
    try {
        jerseyLatch.await();
    } catch (InterruptedException e) {
        log.error("Interrupted while processing request", e);
        throw new InternalServerErrorException(e);
    }
    Timer.stop("JERSEY_FILTER_DOFILTER");
    filterChain.doFilter(servletRequest, servletResponse);
}
 
Example 2
Source File: GatewayRequestHandler.java    From jrestless with Apache License 2.0 6 votes vote down vote up
@Override
protected void extendActualJerseyContainerRequest(ContainerRequest actualContainerRequest,
		JRestlessContainerRequest containerRequest, GatewayRequestAndLambdaContext requestAndLambdaContext) {
	GatewayRequest request = requestAndLambdaContext.getGatewayRequest();
	Context lambdaContext = requestAndLambdaContext.getLambdaContext();
	actualContainerRequest.setRequestScopedInitializer(locator -> {
		Ref<GatewayRequest> gatewayRequestRef = locator
				.<Ref<GatewayRequest>>getInstance(GATEWAY_REQUEST_TYPE);
		if (gatewayRequestRef != null) {
			gatewayRequestRef.set(request);
		} else {
			LOG.error("GatewayFeature has not been registered. GatewayRequest injection won't work.");
		}
		Ref<Context> contextRef = locator
				.<Ref<Context>>getInstance(AbstractLambdaContextReferencingBinder.LAMBDA_CONTEXT_TYPE);
		if (contextRef != null) {
			contextRef.set(lambdaContext);
		} else {
			LOG.error("AwsFeature has not been registered. Context injection won't work.");
		}
	});
	actualContainerRequest.setProperty(GatewayBinaryReadInterceptor.PROPERTY_BASE_64_ENCODED_REQUEST,
			request.isBase64Encoded());
}
 
Example 3
Source File: StructuredEventFilterTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Test
void filterWithResponse() throws IOException {
    MultivaluedMap<String, String> headersMap = createRequestHeader();
    ContainerRequest requestContext = createRequestContext(headersMap);
    underTest.filter(requestContext);

    requestContext.setProperty("structuredevent.loggingEnabled", Boolean.TRUE);

    ArgumentCaptor<StructuredRestCallEvent> structuredEventCaptor = ArgumentCaptor.forClass(StructuredRestCallEvent.class);
    doNothing().when(structuredEventClient).sendStructuredEvent(structuredEventCaptor.capture());

    ContainerResponseContext responseContext = new ContainerResponse(requestContext, Response.accepted().build());
    underTest.filter(requestContext, responseContext);

    StructuredRestCallEvent captorValue = structuredEventCaptor.getValue();
    headersMap.forEach((key, value) -> assertEquals(value.get(0), captorValue.getRestCall().getRestRequest().getHeaders().get(key)));
}
 
Example 4
Source File: SpanCustomizingApplicationEventListener.java    From brave with Apache License 2.0 6 votes vote down vote up
@Override public void onEvent(RequestEvent event) {
  // Note: until REQUEST_MATCHED, we don't know metadata such as if the request is async or not
  if (event.getType() != FINISHED) return;
  ContainerRequest request = event.getContainerRequest();
  Object maybeSpan = request.getProperty(SpanCustomizer.class.getName());
  if (!(maybeSpan instanceof SpanCustomizer)) return;

  // Set the HTTP route attribute so that TracingFilter can see it
  request.setProperty("http.route", route(request));

  Throwable error = unwrapError(event);
  // Set the error attribute so that TracingFilter can see it
  if (error != null && request.getProperty("error") == null) request.setProperty("error", error);

  parser.requestMatched(event, (SpanCustomizer) maybeSpan);
}
 
Example 5
Source File: SpanCustomizingApplicationEventListener.java    From wingtips with Apache License 2.0 5 votes vote down vote up
@Override
public void onEvent(RequestEvent event) {
    // We only care about the REQUEST_MATCHED event.
    if (event.getType() != REQUEST_MATCHED) {
        return;
    }

    ContainerRequest request = event.getContainerRequest();
    // Setting the http.route as a setProperty() on this ContainerRequest will bubble out to the
    //      HttpServletRequest as a request attribute.
    request.setProperty(KnownZipkinTags.HTTP_ROUTE, route(request));
}