org.apache.http.client.methods.HttpExecutionAware Java Examples
The following examples show how to use
org.apache.http.client.methods.HttpExecutionAware.
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: SignatureExec.java From wechatpay-apache-httpclient with Apache License 2.0 | 6 votes |
private CloseableHttpResponse executeWithSignature(HttpRoute route, HttpRequestWrapper request, HttpClientContext context, HttpExecutionAware execAware) throws IOException, HttpException { // 上传类不需要消耗两次故不做转换 if (!(request.getOriginal() instanceof WechatPayUploadHttpPost)) { convertToRepeatableRequestEntity(request); } // 添加认证信息 request.addHeader("Authorization", credentials.getSchema() + " " + credentials.getToken(request)); // 执行 CloseableHttpResponse response = mainExec.execute(route, request, context, execAware); // 对成功应答验签 StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() >= 200 && statusLine.getStatusCode() < 300) { convertToRepeatableResponseEntity(response); if (!validator.validate(response)) { throw new HttpException("应答的微信支付签名验证失败"); } } return response; }
Example #2
Source File: ProxyFeedBackClientExecChain.java From vscrawler with Apache License 2.0 | 6 votes |
@Override public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request, HttpClientContext clientContext, HttpExecutionAware execAware) throws IOException, HttpException { Proxy proxy = (Proxy) clientContext.getAttribute(VSCrawlerConstant.VSCRAWLER_AVPROXY_KEY); if (proxy != null) { proxy.recordUsage(); } try { return delegate.execute(route, request, clientContext, execAware); } catch (IOException ioe) { if (proxy != null) { proxy.recordFailed(); } throw ioe; } }
Example #3
Source File: TracingMainExec.java From brave with Apache License 2.0 | 6 votes |
@Override public CloseableHttpResponse execute(HttpRoute route, org.apache.http.client.methods.HttpRequestWrapper request, HttpClientContext context, HttpExecutionAware execAware) throws IOException, HttpException { Span span = (Span) context.removeAttribute(Span.class.getName()); if (span != null) { handler.handleSend(new HttpRequestWrapper(request, route.getTargetHost()), span); } CloseableHttpResponse response = mainExec.execute(route, request, context, execAware); if (span != null) { if (isRemote(context, span)) { if (serverName != null) span.remoteServiceName(serverName); parseTargetAddress(route.getTargetHost(), span); } else { span.kind(null); // clear as cache hit } } return response; }
Example #4
Source File: TracingProtocolExec.java From brave with Apache License 2.0 | 6 votes |
@Override public CloseableHttpResponse execute(HttpRoute route, org.apache.http.client.methods.HttpRequestWrapper req, HttpClientContext context, HttpExecutionAware execAware) throws IOException, HttpException { HttpRequestWrapper request = new HttpRequestWrapper(req, context.getTargetHost()); Span span = tracer.nextSpan(httpSampler, request); context.setAttribute(Span.class.getName(), span); CloseableHttpResponse response = null; Throwable error = null; try (SpanInScope ws = tracer.withSpanInScope(span)) { return response = protocolExec.execute(route, req, context, execAware); } catch (Throwable e) { error = e; throw e; } finally { handler.handleReceive(new HttpResponseWrapper(response, context, error), span); } }
Example #5
Source File: SignatureExec.java From wechatpay-apache-httpclient with Apache License 2.0 | 5 votes |
@Override public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request, HttpClientContext context, HttpExecutionAware execAware) throws IOException, HttpException { if (request.getURI().getHost().endsWith(".mch.weixin.qq.com")) { return executeWithSignature(route, request, context, execAware); } else { return mainExec.execute(route, request, context, execAware); } }
Example #6
Source File: WingtipsHttpClientBuilderTest.java From wingtips with Apache License 2.0 | 5 votes |
@Override public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request, HttpClientContext clientContext, HttpExecutionAware execAware) throws IOException, HttpException { capturedSpan = Tracer.getInstance().getCurrentSpan(); doReturn(statusLineMock).when(response).getStatusLine(); if (exceptionToThrow != null) { throw exceptionToThrow; } // Return a graceful 500 from the response doReturn(500).when(statusLineMock).getStatusCode(); return response; }
Example #7
Source File: ProxyingHttpClientBuilder.java From esigate with Apache License 2.0 | 5 votes |
/** * Decorate with fetch event managements * * @param wrapped * @return the decorated ClientExecChain */ private ClientExecChain addFetchEvent(final ClientExecChain wrapped) { return new ClientExecChain() { @Override public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request, HttpClientContext httpClientContext, HttpExecutionAware execAware) throws IOException, HttpException { OutgoingRequestContext context = OutgoingRequestContext.adapt(httpClientContext); // Create request event FetchEvent fetchEvent = new FetchEvent(context, request); eventManager.fire(EventManager.EVENT_FETCH_PRE, fetchEvent); if (fetchEvent.isExit()) { if (fetchEvent.getHttpResponse() == null) { // Provide an error page in order to avoid a NullPointerException fetchEvent.setHttpResponse(HttpErrorPage.generateHttpResponse( HttpStatus.SC_INTERNAL_SERVER_ERROR, "An extension stopped the processing of the request without providing a response")); } } else { try { fetchEvent.setHttpResponse(wrapped.execute(route, request, context, execAware)); eventManager.fire(EventManager.EVENT_FETCH_POST, fetchEvent); } catch (IOException | HttpException e) { fetchEvent.setHttpResponse(HttpErrorPage.generateHttpResponse(e)); // Usually we want to render and cache the exception but we let an extension decide fetchEvent.setExit(true); eventManager.fire(EventManager.EVENT_FETCH_POST, fetchEvent); if (!fetchEvent.isExit()) throw e; // Throw the exception and let http client process it (may retry) } } return fetchEvent.getHttpResponse(); } }; }
Example #8
Source File: WingtipsHttpClientBuilderTest.java From wingtips with Apache License 2.0 | 4 votes |
private void verifyDecoratedClientExecChainPerformsTracingLogic( ClientExecChain decoratedCec, SpanCapturingClientExecChain origCecSpy, Span parentSpan, boolean expectSubspan, Throwable expectedError ) throws IOException, HttpException { // given HttpRoute httpRoute = new HttpRoute(new HttpHost("localhost")); HttpRequestWrapper requestWrapperSpy = spy(HttpRequestWrapper.wrap(requestMock)); HttpClientContext httpClientContextMock = mock(HttpClientContext.class); HttpExecutionAware httpExecutionAwareMock = mock(HttpExecutionAware.class); assertThat(origCecSpy.capturedSpan).isNull(); assertThat(spanRecorder.completedSpans).isEmpty(); // when CloseableHttpResponse result = null; Throwable exFromChain = null; try { result = decoratedCec.execute( httpRoute, requestWrapperSpy, httpClientContextMock, httpExecutionAwareMock ); } catch (Throwable ex) { exFromChain = ex; } // then verify(origCecSpy).execute(httpRoute, requestWrapperSpy, httpClientContextMock, httpExecutionAwareMock); if (origCecSpy.exceptionToThrow == null) { assertThat(result).isSameAs(origCecSpy.response); } else { assertThat(exFromChain).isSameAs(origCecSpy.exceptionToThrow); } // The only time the capturedSpan should be null is if expectSubspan is false and parentSpan is null, and then // no tracing propagation headers should have been set. // Otherwise, the tracing propagation headers should match capturedSpan. if (origCecSpy.capturedSpan == null) { assertThat(expectSubspan).isFalse(); assertThat(parentSpan).isNull(); verify(requestWrapperSpy, never()).setHeader(anyString(), anyString()); } else { verify(requestWrapperSpy).setHeader(TRACE_ID, origCecSpy.capturedSpan.getTraceId()); verify(requestWrapperSpy).setHeader(SPAN_ID, origCecSpy.capturedSpan.getSpanId()); verify(requestWrapperSpy).setHeader( TRACE_SAMPLED, convertSampleableBooleanToExpectedB3Value(origCecSpy.capturedSpan.isSampleable()) ); if (origCecSpy.capturedSpan.getParentSpanId() == null) { verify(requestWrapperSpy, never()).setHeader(eq(PARENT_SPAN_ID), anyString()); } else { verify(requestWrapperSpy).setHeader(PARENT_SPAN_ID, origCecSpy.capturedSpan.getParentSpanId()); } } // If we have a subspan, then it should have been completed. Otherwise, no spans should have been completed. // Also, if we have a subspan, then request and response tagging should have been done. if (expectSubspan) { assertThat(spanRecorder.completedSpans).containsExactly(origCecSpy.capturedSpan); // Verify the request tags were set assertThat(strategyInitialSpanNameMethodCalled.get()).isTrue(); assertThat(strategyInitialSpanNameArgs.get()).isNotNull(); strategyInitialSpanNameArgs.get().verifyArgs(requestWrapperSpy, tagAndNamingAdapterMock); assertThat(strategyRequestTaggingMethodCalled.get()).isTrue(); assertThat(strategyRequestTaggingArgs.get()).isNotNull(); strategyRequestTaggingArgs.get().verifyArgs( origCecSpy.capturedSpan, requestWrapperSpy, tagAndNamingAdapterMock ); // Verify the response tags were set assertThat(strategyResponseTaggingAndFinalSpanNameMethodCalled.get()).isTrue(); assertThat(strategyResponseTaggingArgs.get()).isNotNull(); strategyResponseTaggingArgs.get().verifyArgs( origCecSpy.capturedSpan, requestWrapperSpy, result, expectedError, tagAndNamingAdapterMock ); } else { assertThat(spanRecorder.completedSpans).isEmpty(); // None of the tag/naming stuff should have been called since there was no subspan. assertThat(strategyInitialSpanNameMethodCalled.get()).isFalse(); assertThat(strategyInitialSpanNameArgs.get()).isNull(); assertThat(strategyRequestTaggingMethodCalled.get()).isFalse(); assertThat(strategyRequestTaggingArgs.get()).isNull(); assertThat(strategyResponseTaggingAndFinalSpanNameMethodCalled.get()).isFalse(); assertThat(strategyResponseTaggingArgs.get()).isNull(); } }
Example #9
Source File: CacheAdapter.java From esigate with Apache License 2.0 | 4 votes |
public ClientExecChain wrapCachingHttpClient(final ClientExecChain wrapped) { return new ClientExecChain() { /** * Removes client http cache directives like "Cache-control" and "Pragma". Users must not be able to bypass * the cache just by making a refresh in the browser. Generates X-cache header. * */ @Override public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request, HttpClientContext httpClientContext, HttpExecutionAware execAware) throws IOException, HttpException { OutgoingRequestContext context = OutgoingRequestContext.adapt(httpClientContext); // Switch route for the cache to generate the right cache key CloseableHttpResponse response = wrapped.execute(route, request, context, execAware); // Remove previously added Cache-control header if (request.getRequestLine().getMethod().equalsIgnoreCase("GET") && (staleWhileRevalidate > 0 || staleIfError > 0)) { response.removeHeader(response.getLastHeader("Cache-control")); } // Add X-cache header if (xCacheHeader) { if (context != null) { CacheResponseStatus cacheResponseStatus = (CacheResponseStatus) context.getAttribute(HttpCacheContext.CACHE_RESPONSE_STATUS); String xCacheString; if (cacheResponseStatus.equals(CacheResponseStatus.CACHE_HIT)) { xCacheString = "HIT"; } else if (cacheResponseStatus.equals(CacheResponseStatus.VALIDATED)) { xCacheString = "VALIDATED"; } else { xCacheString = "MISS"; } xCacheString += " from " + route.getTargetHost().toHostString(); xCacheString += " (" + request.getRequestLine().getMethod() + " " + request.getRequestLine().getUri() + ")"; response.addHeader("X-Cache", xCacheString); } } // Remove Via header if (!viaHeader && response.containsHeader("Via")) { response.removeHeaders("Via"); } return response; } }; }