Java Code Examples for javax.ws.rs.client.ClientRequestContext#getProperty()

The following examples show how to use javax.ws.rs.client.ClientRequestContext#getProperty() . 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: InvokedMethodRequestFilter.java    From microprofile-rest-client with Apache License 2.0 6 votes vote down vote up
@Override
public void filter(ClientRequestContext ctx) throws IOException {
    try {
        Method m = (Method) ctx.getProperty("org.eclipse.microprofile.rest.client.invokedMethod");

        Path path = m.getAnnotation(Path.class);
        ctx.abortWith(Response.ok("OK")
                              .header("ReturnType", m.getReturnType().getName())
                              .header("POST", m.getAnnotation(POST.class) == null ? "null" : "POST")
                              .header("Path", path == null ? "null" : path.value())
                              .build());
    }
    catch (Throwable t) {
        t.printStackTrace();
        ctx.abortWith(Response.serverError().build());
    }
}
 
Example 2
Source File: MSF4JClientTracingFilter.java    From msf4j with Apache License 2.0 6 votes vote down vote up
/**
 * Intercepts the client response flow and extract response information
 * to be published to the DAS server for tracing.
 */
@Override
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
    long time = new Date().getTime();
    TraceEvent traceEvent = (TraceEvent) requestContext.getProperty(TRACE_EVENT_ATTRIBUTE);
    if (traceEvent != null) {
        TraceEvent endTraceEvent = new TraceEvent(
                TracingConstants.CLIENT_TRACE_END,
                traceEvent.getTraceId(),
                traceEvent.getOriginId(),
                time
        );
        endTraceEvent.setStatusCode(responseContext.getStatus());
        TracingUtil.pushToDAS(endTraceEvent, dasUrl);
    }
}
 
Example 3
Source File: LoggingFilter.java    From ameba with MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void filter(final ClientRequestContext requestContext, final ClientResponseContext responseContext)
        throws IOException {
    final Object requestId = requestContext.getProperty(LOGGING_ID_PROPERTY);
    final long id = requestId != null ? (Long) requestId : _id.incrementAndGet();

    StringBuilder b = (StringBuilder) requestContext.getProperty(LOGGER_BUFFER_PROPERTY);
    if (b == null) {
        b = new StringBuilder();
        requestContext.setProperty(LOGGER_BUFFER_PROPERTY, b);
    }

    printResponseLine(b, "Client response received", id, responseContext.getStatus());
    printPrefixedHeaders(b, id, RESPONSE_PREFIX, responseContext.getHeaders());

    if (printEntity && responseContext.hasEntity() && isSupportPrintType(responseContext.getMediaType())) {
        responseContext.setEntityStream(logInboundEntity(b, responseContext.getEntityStream(),
                MessageUtils.getCharset(responseContext.getMediaType())));
    }

    log(b);
}
 
Example 4
Source File: MaskingLoggingFilter.java    From gitlab4j-api with MIT License 6 votes vote down vote up
@Override
public void filter(ClientRequestContext requestContext, ClientResponseContext 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 sb = new StringBuilder();
    printResponseLine(sb, "Received server response", id, responseContext.getStatus());
    printHeaders(sb, id, RESPONSE_PREFIX, responseContext.getHeaders());
 
    if (responseContext.hasEntity() && maxEntitySize > 0) {
        responseContext.setEntityStream(logResponseEntity(sb, responseContext.getEntityStream(), 
                MessageUtils.getCharset(responseContext.getMediaType())));
    }

    log(sb);
}
 
Example 5
Source File: InvokedMethodClientRequestFilter.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Override
public void filter(ClientRequestContext ctx) throws IOException {
    try {
        Method m = (Method) ctx.getProperty("org.eclipse.microprofile.rest.client.invokedMethod");

        Path path = m.getAnnotation(Path.class);
        ctx.abortWith(Response.ok("OK")
                              .header("ReturnType", m.getReturnType().getName())
                              .header("PUT", m.getAnnotation(PUT.class) == null ? "null" : "PUT")
                              .header("Path", path == null ? "null" : path.value())
                              .header("Parm1", m.getParameters()[0].getType().getName())
                              .header("Parm1Annotation", 
                                      m.getParameters()[0].getAnnotations()[0].annotationType().getName())
                              .header("Parm2", m.getParameters()[1].getType().getName())
                              .build());
    } catch (Throwable t) {
        t.printStackTrace();
        ctx.abortWith(Response.serverError().build());
    }
}
 
Example 6
Source File: BraveClientProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void filter(final ClientRequestContext requestContext,
        final ClientResponseContext responseContext) throws IOException {
    final TraceScopeHolder<TraceScope> holder =
        (TraceScopeHolder<TraceScope>)requestContext.getProperty(TRACE_SPAN);
    super.stopTraceSpan(holder, responseContext.getStatus());
}
 
Example 7
Source File: OpenTracingClientProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void filter(final ClientRequestContext requestContext,
        final ClientResponseContext responseContext) throws IOException {
    final TraceScopeHolder<TraceScope> holder =
        (TraceScopeHolder<TraceScope>)requestContext.getProperty(TRACE_SPAN);
    super.stopTraceSpan(holder, responseContext.getStatus());
}
 
Example 8
Source File: JaxrsClientFilter.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
@Override
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) {
  HttpRequestContext context =
      (HttpRequestContext) requestContext.getProperty(OPENCENSUS_CONTEXT);
  handler.handleEnd(context, requestContext, responseContext, null);
}
 
Example 9
Source File: ClientTracingFilter.java    From java-jaxrs with Apache License 2.0 4 votes vote down vote up
@Override
public void filter(ClientRequestContext requestContext) {
    if (tracingDisabled(requestContext)) {
        log.finest("Client tracing disabled");
        return;
    }

    // in case filter is registered twice
    if (requestContext.getProperty(PROPERTY_NAME) != null) {
        return;
    }

    Tracer.SpanBuilder spanBuilder = tracer.buildSpan(requestContext.getMethod())
            .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT);

    SpanContext parentSpanContext = CastUtils.cast(requestContext.getProperty(TracingProperties.CHILD_OF),
            SpanContext.class);
    if (parentSpanContext != null) {
        spanBuilder.ignoreActiveSpan()
            .asChildOf(parentSpanContext);
    }

    /**
     * Do not create Scope - there is no way to deactivate it on UnknownHostException
     */
    final Span span = spanBuilder.start();

    if (spanDecorators != null) {
        for (ClientSpanDecorator decorator: spanDecorators) {
            decorator.decorateRequest(requestContext, span);
        }
    }

    if (log.isLoggable(Level.FINEST)) {
        log.finest("Starting client span");
    }

    tracer.inject(span.context(), Format.Builtin.HTTP_HEADERS, new ClientHeadersInjectTextMap(requestContext.getHeaders()));
    requestContext.setProperty(PROPERTY_NAME, new SpanWrapper(span, new NoopScope() {
        @Override
        public void close() {
        }
    }));
}