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

The following examples show how to use javax.ws.rs.client.ClientRequestContext#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: LoggingFilter.java    From ameba with MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void filter(final ClientRequestContext context) throws IOException {
    final long id = _id.incrementAndGet();
    context.setProperty(LOGGING_ID_PROPERTY, id);

    final StringBuilder b = new StringBuilder();

    printRequestLine(b, "Sending client request", id, context.getMethod(), context.getUri());
    printPrefixedHeaders(b, id, REQUEST_PREFIX, context.getStringHeaders());

    if (printEntity && context.hasEntity() && isSupportPrintType(context.getMediaType())) {
        final OutputStream stream = new LoggingStream(b, context.getEntityStream());
        context.setEntityStream(stream);
        context.setProperty(ENTITY_LOGGER_PROPERTY, stream);
        // not calling log(b) here - it will be called by the interceptor
    } else {
        log(b);
    }
}
 
Example 2
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 3
Source File: MaskingLoggingFilter.java    From gitlab4j-api with MIT License 6 votes vote down vote up
@Override
public void filter(ClientRequestContext requestContext) throws IOException {

    if (!logger.isLoggable(level)) {
        return;
    }

    final long id = _id.incrementAndGet();
    requestContext.setProperty(LOGGING_ID_PROPERTY, id);

    final StringBuilder sb = new StringBuilder();
    printRequestLine(sb, "Sending client request", id, requestContext.getMethod(), requestContext.getUri());
    printHeaders(sb, id, REQUEST_PREFIX, requestContext.getStringHeaders());

    if (requestContext.hasEntity() && maxEntitySize > 0) {
        final OutputStream stream = new LoggingStream(sb, requestContext.getEntityStream());
        requestContext.setEntityStream(stream);
        requestContext.setProperty(ENTITY_STREAM_PROPERTY, stream);
    } else {
        log(sb);
    }
}
 
Example 4
Source File: LoggingFilter.java    From docker-java with Apache License 2.0 6 votes vote down vote up
@Override
public void filter(final ClientRequestContext context) throws IOException {
    final long id = aid.incrementAndGet();
    final StringBuilder b = new StringBuilder();

    printRequestLine(b, "Sending client request", id, context.getMethod(), context.getUri());
    printPrefixedHeaders(b, id, REQUEST_PREFIX, context.getStringHeaders());

    if (printEntity && context.hasEntity()) {
        final OutputStream stream = new LoggingStream(b, context.getEntityStream());
        context.setEntityStream(stream);
        context.setProperty(ENTITY_LOGGER_PROPERTY, stream);
        // not calling log(b) here - it will be called by the interceptor
    } else {
        log(b);
    }
}
 
Example 5
Source File: LoggingInterceptor.java    From karate with MIT License 5 votes vote down vote up
@Override
public void filter(ClientRequestContext request) throws IOException {
    if (request.hasEntity() && isPrintable(request.getMediaType())) {
        LoggingFilterOutputStream out = new LoggingFilterOutputStream(request.getEntityStream());
        request.setEntityStream(out);
        request.setProperty(LoggingFilterOutputStream.KEY, out);
    }
    HttpRequest actual = new HttpRequest();
    context.setPrevRequest(actual);
    actual.startTimer();
}
 
Example 6
Source File: LogbookClientFilter.java    From logbook with MIT License 5 votes vote down vote up
@Override
public void filter(final ClientRequestContext context) throws IOException {
    final LocalRequest request = new LocalRequest(context);
    final RequestWritingStage stage = logbook.process(request);

    if (context.hasEntity()) {
        context.setProperty("request", request);
        context.setProperty("write-request", stage);
    } else {
        context.setProperty("process-response", stage.write());
    }

    request.expose();
}
 
Example 7
Source File: MSF4JClientTracingFilter.java    From msf4j with Apache License 2.0 5 votes vote down vote up
/**
 * Intercepts the client request flow and extract request information
 * to be published to the DAS for tracing.
 */
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
    long time = new Date().getTime();
    String clientTraceId;
    String traceOriginId;
    String traceParentId = null;
    if (this.parentEvent == null) {
        traceOriginId = TracingUtil.generateUniqueId();
        clientTraceId = traceOriginId;
    } else {
        traceOriginId = parentEvent.getOriginId();
        clientTraceId = TracingUtil.generateUniqueId();
        traceParentId = parentEvent.getTraceId();
    }
    TraceEvent clientTraceEvent = new TraceEvent(
            TracingConstants.CLIENT_TRACE_START,
            clientTraceId,
            traceOriginId,
            time
    );
    clientTraceEvent.setInstanceId(instanceId);
    clientTraceEvent.setInstanceName(instanceName);
    clientTraceEvent.setParentId(traceParentId);
    clientTraceEvent.setHttpMethod(requestContext.getMethod());
    clientTraceEvent.setUrl(requestContext.getUri().toString());
    requestContext.setProperty(TRACE_EVENT_ATTRIBUTE, clientTraceEvent);
    requestContext.getHeaders().putSingle(TracingConstants.TRACE_ID_HEADER, clientTraceId);
    requestContext.getHeaders().putSingle(TracingConstants.TRACE_ORIGIN_ID_HEADER, traceOriginId);
    TracingUtil.pushToDAS(clientTraceEvent, dasUrl);
}
 
Example 8
Source File: BraveClientProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public void filter(final ClientRequestContext requestContext) throws IOException {
    final TraceScopeHolder<TraceScope> holder = super.startTraceSpan(requestContext.getStringHeaders(),
        requestContext.getUri(), requestContext.getMethod());

    if (holder != null) {
        requestContext.setProperty(TRACE_SPAN, holder);
    }
}
 
Example 9
Source File: OpenTracingClientProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public void filter(final ClientRequestContext requestContext) throws IOException {
    final TraceScopeHolder<TraceScope> holder = super.startTraceSpan(requestContext.getStringHeaders(),
        requestContext.getUri(), requestContext.getMethod());

    if (holder != null) {
        requestContext.setProperty(TRACE_SPAN, holder);
    }
}
 
Example 10
Source File: JaxrsClientFilter.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
@Override
public void filter(ClientRequestContext requestContext) {
  HttpRequestContext context = handler.handleStart(null, requestContext, requestContext);
  requestContext.setProperty(OPENCENSUS_CONTEXT, context);
}
 
Example 11
Source File: RequestMetricsClientRequestFilter.java    From cf-java-logging-support with Apache License 2.0 4 votes vote down vote up
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
    requestContext.setProperty(REQ_METRICS_KEY, handler.handle(new ClientRequestContextAdapter(requestContext)));
}
 
Example 12
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() {
        }
    }));
}
 
Example 13
Source File: CacheControlClientRequestFilter.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Override
public void filter(final ClientRequestContext request) throws IOException {
    if (!HttpMethod.GET.equals(request.getMethod())) {
        //TODO: Review the possibility of supporting POST responses, example,
        //      POST create request may get a created entity representation returned
        request.setProperty(NO_CACHE_PROPERTY, "true");
        return;
    }
    final URI uri = request.getUri();
    final String accepts = request.getHeaderString(HttpHeaders.ACCEPT);
    final Key key = new Key(uri, accepts);
    Entry entry = cache.get(key);
    if (entry != null) {
        //TODO: do the extra validation against the conditional headers
        //      which may be contained in the current request
        if (entry.isOutDated()) {
            String ifNoneMatchHeader = entry.getCacheHeaders().get(HttpHeaders.IF_NONE_MATCH);
            String ifModifiedSinceHeader = entry.getCacheHeaders().get(HttpHeaders.IF_MODIFIED_SINCE);

            if (StringUtils.isEmpty(ifNoneMatchHeader) && StringUtils.isEmpty(ifModifiedSinceHeader)) {
                cache.remove(key, entry);
            } else {
                request.getHeaders().add(HttpHeaders.IF_NONE_MATCH, ifNoneMatchHeader);
                request.getHeaders().add(HttpHeaders.IF_MODIFIED_SINCE, ifModifiedSinceHeader);
                request.setProperty(CACHED_ENTITY_PROPERTY, entry.getData());
            }
        } else {
            Object cachedEntity = entry.getData();
            Response.ResponseBuilder ok = Response.ok(cachedEntity);
            if (entry.getHeaders() != null) {
                for (Map.Entry<String, List<String>> h : entry.getHeaders().entrySet()) {
                    for (final Object instance : h.getValue()) {
                        ok = ok.header(h.getKey(), instance);
                    }
                }
            }
            request.setProperty(CACHED_ENTITY_PROPERTY, cachedEntity);
            request.abortWith(ok.build());
        }
    }
    // Should the map of all request headers shared ?
    request.setProperty(CLIENT_ACCEPTS, accepts);
    request.setProperty(CLIENT_CACHE_CONTROL, request.getHeaderString(HttpHeaders.CACHE_CONTROL));
}
 
Example 14
Source File: RequestClientFilter.java    From tutorials with MIT License 4 votes vote down vote up
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
    LOG.info("Request client filter");

    requestContext.setProperty("test", "test client request filter");
}
 
Example 15
Source File: TracingClientFilter.java    From brave with Apache License 2.0 4 votes vote down vote up
@Override public void filter(ClientRequestContext request) {
  Span span = handler.handleSend(new ClientRequestContextWrapper(request));
  request.setProperty(SpanInScope.class.getName(), tracer.withSpanInScope(span));
}