org.apache.http.client.methods.HttpRequestWrapper Java Examples
The following examples show how to use
org.apache.http.client.methods.HttpRequestWrapper.
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: RedirectStrategy.java From NetDiscovery with Apache License 2.0 | 7 votes |
@Override public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { URI uri = getLocationURI(request, response, context); String method = request.getRequestLine().getMethod(); if ("post".equalsIgnoreCase(method)) { try { HttpRequestWrapper httpRequestWrapper = (HttpRequestWrapper) request; httpRequestWrapper.setURI(uri); httpRequestWrapper.removeHeaders("Content-Length"); return httpRequestWrapper; } catch (Exception e) { log.error("强转为HttpRequestWrapper出错"); } return new HttpPost(uri); } else { return new HttpGet(uri); } }
Example #2
Source File: CustomRedirectStrategy.java From webmagic with Apache License 2.0 | 6 votes |
@Override public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { URI uri = getLocationURI(request, response, context); String method = request.getRequestLine().getMethod(); if ("post".equalsIgnoreCase(method)) { try { HttpRequestWrapper httpRequestWrapper = (HttpRequestWrapper) request; httpRequestWrapper.setURI(uri); httpRequestWrapper.removeHeaders("Content-Length"); return httpRequestWrapper; } catch (Exception e) { logger.error("强转为HttpRequestWrapper出错"); } return new HttpPost(uri); } else { return new HttpGet(uri); } }
Example #3
Source File: ApacheHttpClientTagAdapter.java From wingtips with Apache License 2.0 | 6 votes |
@Override public @Nullable String getRequestUrl(@Nullable HttpRequest request) { if (request == null) { return null; } String uri = request.getRequestLine().getUri(); if (request instanceof HttpRequestWrapper && uri.startsWith("/")) { HttpRequestWrapper wrapper = (HttpRequestWrapper) request; HttpHost target = wrapper.getTarget(); if (target != null) { uri = wrapper.getTarget().toURI() + uri; } } return uri; }
Example #4
Source File: ApacheHttpClientTagAdapterTest.java From wingtips with Apache License 2.0 | 6 votes |
@DataProvider(value = { "http://foo.bar/some/path | /some/path", "http://foo.bar/some/path?thing=stuff | /some/path", "/some/path | /some/path", "/some/path?thing=stuff | /some/path", "http://foo.bar/ | /", "http://foo.bar/?thing=stuff | /", "/ | /", "/?thing=stuff | /", }, splitBy = "\\|") @Test public void getRequestPath_works_as_expected_for_a_request_that_is_an_HttpRequestWrapper( String uriString, String expectedResult ) { // given HttpRequestWrapper requestWrapperMock = mock(HttpRequestWrapper.class); doReturn(URI.create(uriString)).when(requestWrapperMock).getURI(); // when String result = implSpy.getRequestPath(requestWrapperMock); // then assertThat(result).isEqualTo(expectedResult); }
Example #5
Source File: ApacheHttpClientTagAdapterTest.java From wingtips with Apache License 2.0 | 6 votes |
GetRequestUrlScenario( boolean requestIsNull, boolean requestIsWrapper, Object baseUri, boolean targetIsNull, String targetUri, String expectedResult ) { this.expectedResult = expectedResult; HttpRequest request = null; if (!requestIsNull) { request = (requestIsWrapper) ? mock(HttpRequestWrapper.class) : mock(HttpRequest.class); RequestLine requestLine = mock(RequestLine.class); doReturn(requestLine).when(request).getRequestLine(); doReturn(baseUri).when(requestLine).getUri(); if (!targetIsNull) { HttpHost target = HttpHost.create(targetUri); doReturn(target).when((HttpRequestWrapper) request).getTarget(); } } this.requestMock = request; }
Example #6
Source File: Http2Curl.java From curl-logger with BSD 3-Clause "New" or "Revised" License | 6 votes |
private String inferUri(HttpRequest request) { String inferredUri = request.getRequestLine().getUri(); if (!isValidUrl(inferredUri)) { // Missing schema and domain name String host = getHost(request); String inferredScheme = "http"; if (host.endsWith(":443")) { inferredScheme = "https"; } else if ((request instanceof RequestWrapper) || (request instanceof HttpRequestWrapper)) { if (getOriginalRequestUri(request).startsWith("https")) { // This is for original URL, so if during redirects we go out of HTTPs, this might be a wrong guess inferredScheme = "https"; } } if ("CONNECT".equals(request.getRequestLine().getMethod())) { inferredUri = String.format("%s://%s", inferredScheme, host); } else { inferredUri = String.format("%s://%s/%s", inferredScheme, host, inferredUri) .replaceAll("(?<!http(s)?:)//", "/"); } } return inferredUri; }
Example #7
Source File: TwitterSecurityTest.java From streams with Apache License 2.0 | 6 votes |
@Test public void testProcess() throws Exception { URI testURI = new URIBuilder() .setPath("/1/statuses/update.json") .setParameter("include_entities", "true") .build(); HttpPost testRequest = new HttpPost(testURI); testRequest.setEntity(new StringEntity("status="+security.encode("Hello Ladies + Gentlemen, a signed OAuth request!"))); HttpHost host = new HttpHost("api.twitter.com", -1, "https"); HttpRequestWrapper wrapper = HttpRequestWrapper.wrap(testRequest, host); TwitterOAuthConfiguration testOauthConfiguration = new TwitterOAuthConfiguration() .withConsumerKey("xvz1evFS4wEEPTGEFPHBog") .withConsumerSecret("kAcSOqF21Fu85e7zjz7ZN2U4ZRhfV3WpwPAoE3Z7kBw") .withAccessToken("370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb") .withAccessTokenSecret("LswwdoUaIvS8ltyTt5jkRh4J50vUPVVHtR2YPi5kE"); TwitterOAuthRequestInterceptor interceptor = Mockito.spy(new TwitterOAuthRequestInterceptor(testOauthConfiguration)); Mockito.when(interceptor.generateNonce()).thenReturn("kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg"); Mockito.when(interceptor.generateTimestamp()).thenReturn("1318622958"); interceptor.process(wrapper, new HttpCoreContext()); assertEquals(1, wrapper.getHeaders("Authorization").length); String actual = wrapper.getFirstHeader("Authorization").getValue(); String expected = "OAuth oauth_consumer_key=\"xvz1evFS4wEEPTGEFPHBog\", oauth_nonce=\"kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg\", oauth_signature=\"tnnArxj06cWHq44gCs1OSKk%2FjLY%3D\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1318622958\", oauth_token=\"370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb\", oauth_version=\"1.0\""; assertEquals(expected, actual); }
Example #8
Source File: EverySessionPlanner.java From vscrawler with Apache License 2.0 | 6 votes |
@Override public Proxy determineProxy(HttpHost host, HttpRequest request, HttpContext context, IPPool ipPool, CrawlerSession crawlerSession) { HttpClientContext httpClientContext = HttpClientContext.adapt(context); Proxy proxy = (Proxy) crawlerSession.getExtInfo(VSCRAWLER_AVPROXY_KEY); if (proxy == null) { String accessUrl = null; if (request instanceof HttpRequestWrapper || request instanceof HttpGet) { accessUrl = HttpUriRequest.class.cast(request).getURI().toString(); } if (!PoolUtil.isDungProxyEnabled(httpClientContext)) { log.info("{}不会被代理", accessUrl); return null; } proxy = ipPool.getIP(host.getHostName(), accessUrl); if (proxy == null) { return null; } crawlerSession.setExtInfo(VSCRAWLER_AVPROXY_KEY, proxy); } return proxy; }
Example #9
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 #10
Source File: ApacheHttpClientInstrumentation.java From apm-agent-java with Apache License 2.0 | 6 votes |
@Advice.OnMethodEnter(suppress = Throwable.class) private static void onBeforeExecute(@Advice.Argument(0) HttpRoute route, @Advice.Argument(1) HttpRequestWrapper request, @Advice.Local("span") Span span) { if (tracer == null || tracer.getActive() == null) { return; } final AbstractSpan<?> parent = tracer.getActive(); span = HttpClientHelper.startHttpClientSpan(parent, request.getMethod(), request.getURI(), route.getTargetHost().getHostName()); TextHeaderSetter<HttpRequest> headerSetter = headerSetterHelperClassManager.getForClassLoaderOfClass(HttpRequest.class); TextHeaderGetter<HttpRequest> headerGetter = headerGetterHelperClassManager.getForClassLoaderOfClass(HttpRequest.class); if (span != null) { span.activate(); if (headerSetter != null) { span.propagateTraceContext(request, headerSetter); } } else if (headerGetter != null && !TraceContext.containsTraceContextTextHeaders(request, headerGetter) && headerSetter != null && parent != null) { // re-adds the header on redirects parent.propagateTraceContext(request, headerSetter); } }
Example #11
Source File: WechatPay2Credentials.java From wechatpay-apache-httpclient with Apache License 2.0 | 6 votes |
protected final String buildMessage(String nonce, long timestamp, HttpRequestWrapper request) throws IOException { URI uri = request.getURI(); String canonicalUrl = uri.getRawPath(); if (uri.getQuery() != null) { canonicalUrl += "?" + uri.getRawQuery(); } String body = ""; // PATCH,POST,PUT if (request.getOriginal() instanceof WechatPayUploadHttpPost) { body = ((WechatPayUploadHttpPost) request.getOriginal()).getMeta(); } else if (request instanceof HttpEntityEnclosingRequest) { body = EntityUtils.toString(((HttpEntityEnclosingRequest) request).getEntity()); } return request.getRequestLine().getMethod() + "\n" + canonicalUrl + "\n" + timestamp + "\n" + nonce + "\n" + body + "\n"; }
Example #12
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 #13
Source File: AbstractHttpClientGenerator.java From cetty with Apache License 2.0 | 6 votes |
@Override public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { URI uri = getLocationURI(request, response, context); String method = request.getRequestLine().getMethod(); if (HttpConstants.POST.equalsIgnoreCase(method)) { try { HttpRequestWrapper httpRequestWrapper = (HttpRequestWrapper) request; httpRequestWrapper.setURI(uri); httpRequestWrapper.removeHeaders("Content-Length"); return httpRequestWrapper; } catch (Exception e) { e.printStackTrace(); } return new HttpPost(uri); } else { return new HttpGet(uri); } }
Example #14
Source File: WechatPay2Credentials.java From wechatpay-apache-httpclient with Apache License 2.0 | 6 votes |
@Override public final String getToken(HttpRequestWrapper request) throws IOException { String nonceStr = generateNonceStr(); long timestamp = generateTimestamp(); String message = buildMessage(nonceStr, timestamp, request); log.debug("authorization message=[{}]", message); Signer.SignatureResult signature = signer.sign(message.getBytes(StandardCharsets.UTF_8)); String token = "mchid=\"" + getMerchantId() + "\"," + "nonce_str=\"" + nonceStr + "\"," + "timestamp=\"" + timestamp + "\"," + "serial_no=\"" + signature.certificateSerialNumber + "\"," + "signature=\"" + signature.sign + "\""; log.debug("authorization token=[{}]", token); return token; }
Example #15
Source File: LocalRequest.java From logbook with MIT License | 5 votes |
private static URI getOriginalRequestUri(final HttpRequest request) { if (request instanceof HttpRequestWrapper) { return extractRequestUri(HttpRequestWrapper.class.cast(request).getOriginal()); } else if (request instanceof HttpUriRequest) { return HttpUriRequest.class.cast(request).getURI(); } else { return extractRequestUri(request); } }
Example #16
Source File: CurlFakeTest.java From curl with The Unlicense | 5 votes |
@Test public void curlWithDefaultUserAgent () { this.curl ("https://put.anything.in.this.url:1337", context -> assertEquals (Curl.class.getPackage ().getName () + "/" + Curl.getVersion () + VersionInfo.getUserAgent (", Apache-HttpClient", "org.apache.http.client", CurlFakeTest.class), ((HttpRequestWrapper) context.getAttribute ("http.request")) .getLastHeader ("User-Agent").getValue ())); }
Example #17
Source File: BasicHttpSolrClientTest.java From lucene-solr with Apache License 2.0 | 5 votes |
@Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { log.info("Intercepted params: {}", context); HttpRequestWrapper wrapper = (HttpRequestWrapper) request; URIBuilder uribuilder = new URIBuilder(wrapper.getURI()); uribuilder.addParameter("b", "\u4321"); try { wrapper.setURI(uribuilder.build()); } catch (URISyntaxException ex) { throw new HttpException("Invalid request URI", ex); } }
Example #18
Source File: CurlFakeTest.java From curl with The Unlicense | 5 votes |
@Test public void curlVerifyUserAgentAndAOptionTogether () { this.curl ("-H 'User-Agent: toto' -A titi https://put.anything.in.this.url:1337", context -> assertEquals ("toto", ((HttpRequestWrapper) context.getAttribute ("http.request")) .getLastHeader ("User-Agent").getValue ())); }
Example #19
Source File: ApacheHttpClientEdgeGridRequestSigner.java From AkamaiOPEN-edgegrid-java with Apache License 2.0 | 5 votes |
private void setRequestUri(HttpRequest request, URI uri) { if (request instanceof HttpRequestWrapper) { setRequestUri(((HttpRequestWrapper) request).getOriginal(), uri); } else if (request instanceof RequestWrapper) { setRequestUri(((RequestWrapper) request).getOriginal(), uri); } else { ((HttpRequestBase) request).setURI(uri); } }
Example #20
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 #21
Source File: Http2Curl.java From curl-logger with BSD 3-Clause "New" or "Revised" License | 5 votes |
@SuppressWarnings("deprecation") private static String getOriginalRequestUri(HttpRequest request) { if (request instanceof HttpRequestWrapper) { return ((HttpRequestWrapper) request).getOriginal().getRequestLine().getUri(); } else if (request instanceof RequestWrapper) { return ((RequestWrapper) request).getOriginal().getRequestLine().getUri(); } else { throw new IllegalArgumentException("Unsupported request class type: " + request.getClass()); } }
Example #22
Source File: AWSSigningRequestInterceptor.java From aws-signing-request-interceptor with MIT License | 5 votes |
private Multimap<String, String> params(HttpRequest request) throws IOException { final String rawQuery = ((HttpRequestWrapper) request).getURI().getRawQuery(); if (Strings.isNullOrEmpty(rawQuery)) return ImmutableListMultimap.of(); return params(URLDecoder.decode(rawQuery, StandardCharsets.UTF_8.name())); }
Example #23
Source File: AWSSigningRequestInterceptor.java From aws-signing-request-interceptor with MIT License | 5 votes |
private Optional<byte[]> body(HttpRequest request) throws IOException { final HttpRequest original = ((HttpRequestWrapper) request).getOriginal(); if (! HttpEntityEnclosingRequest.class.isAssignableFrom(original.getClass())) { return Optional.absent(); } return Optional.fromNullable(((HttpEntityEnclosingRequest) original).getEntity()).transform(TO_BYTE_ARRAY); }
Example #24
Source File: SeimiRedirectStrategy.java From SeimiCrawler with Apache License 2.0 | 5 votes |
@Override public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { URI uri = getLocationURI(request, response, context); String method = request.getRequestLine().getMethod(); if (HttpPost.METHOD_NAME.equalsIgnoreCase(method)&& request instanceof HttpRequestWrapper) { HttpRequestWrapper httpRequestWrapper = (HttpRequestWrapper) request; httpRequestWrapper.setURI(uri); httpRequestWrapper.removeHeaders("Content-Length"); return httpRequestWrapper; } else { return super.getRedirect(request,response,context); } }
Example #25
Source File: HttpAsyncRequestExecutorInterceptor.java From skywalking with Apache License 2.0 | 5 votes |
@Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable { HttpContext context = CONTEXT_LOCAL.get(); CONTEXT_LOCAL.remove(); if (context == null) { return; } final ContextCarrier contextCarrier = new ContextCarrier(); HttpRequestWrapper requestWrapper = (HttpRequestWrapper) context.getAttribute(HttpClientContext.HTTP_REQUEST); HttpHost httpHost = (HttpHost) context.getAttribute(HttpClientContext.HTTP_TARGET_HOST); RequestLine requestLine = requestWrapper.getRequestLine(); String uri = requestLine.getUri(); String operationName = uri.startsWith("http") ? new URL(uri).getPath() : uri; int port = httpHost.getPort(); AbstractSpan span = ContextManager.createExitSpan(operationName, contextCarrier, httpHost.getHostName() + ":" + (port == -1 ? 80 : port)); span.setComponent(ComponentsDefine.HTTP_ASYNC_CLIENT); Tags.URL.set(span, requestWrapper.getOriginal().getRequestLine().getUri()); Tags.HTTP.METHOD.set(span, requestLine.getMethod()); SpanLayer.asHttp(span); CarrierItem next = contextCarrier.items(); while (next.hasNext()) { next = next.next(); requestWrapper.setHeader(next.getHeadKey(), next.getHeadValue()); } }
Example #26
Source File: PreemptiveAuthHttpRequestInterceptor.java From gerrit-rest-java-client with Apache License 2.0 | 5 votes |
/** * Checks if request is intended for Gerrit host. */ private boolean isForGerritHost(HttpRequest request) { if (!(request instanceof HttpRequestWrapper)) return false; HttpRequest originalRequest = ((HttpRequestWrapper) request).getOriginal(); if (!(originalRequest instanceof HttpRequestBase)) return false; URI uri = ((HttpRequestBase) originalRequest).getURI(); URI authDataUri = URI.create(authData.getHost()); if (uri == null || uri.getHost() == null) return false; boolean hostEquals = uri.getHost().equals(authDataUri.getHost()); boolean portEquals = uri.getPort() == authDataUri.getPort(); return hostEquals && portEquals; }
Example #27
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 #28
Source File: BotsHttpClient.java From geoportal-server-harvester with Apache License 2.0 | 5 votes |
@Override public CloseableHttpResponse execute(HttpHost target, HttpRequest request) throws IOException, ClientProtocolException { HttpRequestWrapper wrap = HttpRequestWrapper.wrap(request, target); adviseRobotsTxt(wrap.getURI()); wrap.setURI(applyPHP(wrap.getURI())); return client.execute(wrap); }
Example #29
Source File: SignatureExec.java From wechatpay-apache-httpclient with Apache License 2.0 | 5 votes |
protected void convertToRepeatableRequestEntity(HttpRequestWrapper request) throws IOException { if (request instanceof HttpEntityEnclosingRequest) { HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity(); if (entity != null) { ((HttpEntityEnclosingRequest) request).setEntity(new BufferedHttpEntity(entity)); } } }
Example #30
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); } }