public String getHeader(String name) { Header header = request.getFirstHeader(name); if (header == null) { return null; } return header.getValue(); }
@Test public void testBuildRequestWithJsonPayload() throws Exception { final HttpUriRequest request = HttpUriRequestBuilder.create() .method(HttpMethods.GET) .path("/path") .jsonPayload(Optional.of("{'key1':'value1'}")) .build(); assertThat(request, notNullValue()); assertThat(request.getURI().getPath(), equalTo("/path")); assertThat(request.getURI().getScheme(), nullValue()); assertThat(request.getURI().getQuery(), nullValue()); assertThat(request.getURI().getHost(), nullValue()); assertThat(request.getMethod(), equalTo(HttpGet.METHOD_NAME)); assertThat(request.containsHeader(HttpConstants.CONTENT_TYPE), equalTo(true)); assertThat(request.getFirstHeader(HttpConstants.CONTENT_TYPE).getValue(), equalTo(HttpConstants.JSON_CONTENT_TYPE)); assertThat(request, HttpUriRequestPayloadMatcher.hasPayload("{'key1':'value1'}")); }
@Override public Header getFirstHeader(String name) { return request.getFirstHeader(name); }
public String getHeader(String name) { Header header = request.getFirstHeader(name); if (header == null) { return null; } return header.getValue(); }
@Override public List<IdentityToken> resolve(HttpUriRequest input, X509Certificate[] certificateChain) { List<IdentityToken> credentials = new ArrayList<IdentityToken>(); for (SimpleIdentityTokenName securityToken : SimpleIdentityTokenName.values()) { Header authHeaderValue = input.getFirstHeader(TOKEN_PREFIX + securityToken.name()); if (authHeaderValue != null) { credentials.add(new IdentityToken(securityToken.name(), authHeaderValue.getValue())); } } return credentials; }
@Override public ListenableFuture<EtcdResult> apply(HttpResponse response) throws Exception { if (response.getStatusLine().getStatusCode() == 500) { Header retryHeader = request.getFirstHeader(RETRY_COUNTER_HEADER); if (retryHeader != null) { Integer count = Integer.parseInt(retryHeader.getValue()); if (count > 0) { request.removeHeader(retryHeader); request.addHeader(RETRY_COUNTER_HEADER, String.valueOf(--count)); return asyncExecute(request, expectedHttpStatusCodes); } } else { request.addHeader(RETRY_COUNTER_HEADER, String.valueOf(MAX_RETRY_COUNT)); return asyncExecute(request, expectedHttpStatusCodes); } } return transformPool.submit(new TransformerWorker(response, expectedHttpStatusCodes, exceptedErrorCodes)); }
Header dateHeader = httpMethod.getFirstHeader("x-amz-date"); if (dateHeader == null) { dateHeader = httpMethod.getFirstHeader("Date");
Header dateHeader = httpMethod.getFirstHeader("x-amz-date"); if (dateHeader == null) { dateHeader = httpMethod.getFirstHeader("Date");
final Header originalXForwardedContextHeader = outboundRequest.getFirstHeader(NiFiHeaders.X_FORWARDED_CONTEXT); if (originalXForwardedContextHeader != null) { String xForwardedContextHeaderValue = originalXForwardedContextHeader.getValue();
Header requestEtagHeader = request.getFirstHeader(HeaderConstants.IF_NONE_MATCH); Header requestLastModifiedHeader = request.getFirstHeader(HeaderConstants.LAST_MODIFIED); HttpCacheEntry httpCacheEntry = null;
@Override public URI getLocationURI(HttpResponse response, HttpContext context) throws ProtocolException { String urlString = response.getFirstHeader("Location").getValue(); URI uri; try { uri = new URI(urlString); } catch (URISyntaxException e) { throw new ProtocolException("Could not parse redirect uri."); } HttpUriRequest request = OAuthHttpClient.buildRequest(urlString, HttpRequestMethod.GET, response.getEntity().toString()); OAuthConsumer token = new CommonsHttpOAuthConsumer((String) context.getAttribute("consumerKey"), (String) context.getAttribute("consumerSecret")); synchronized(this) { try { token.sign(request); } catch (OAuthMessageSignerException e) { throw new ProtocolException("Could not sign request!"); } catch (OAuthExpectationFailedException e) { throw new ProtocolException("Could not sign request!"); } catch (OAuthCommunicationException e) { throw new ProtocolException("Could not sign request!"); } } // Apache puts original headers back on the request, so we need to pass this along to the RequestInterceptor to be re-added. context.setAttribute("AuthHeader", request.getFirstHeader("Authorization")); return uri; }
@Test public void testBuildRequestWithJsonPayload() throws Exception { final HttpUriRequest request = HttpUriRequestBuilder.create() .method(HttpMethods.GET) .path("/path") .jsonPayload(Optional.of("{'key1':'value1'}")) .build(); assertThat(request, notNullValue()); assertThat(request.getURI().getPath(), equalTo("/path")); assertThat(request.getURI().getScheme(), nullValue()); assertThat(request.getURI().getQuery(), nullValue()); assertThat(request.getURI().getHost(), nullValue()); assertThat(request.getMethod(), equalTo(HttpGet.METHOD_NAME)); assertThat(request.containsHeader(HttpConstants.CONTENT_TYPE), equalTo(true)); assertThat(request.getFirstHeader(HttpConstants.CONTENT_TYPE).getValue(), equalTo(HttpConstants.JSON_CONTENT_TYPE)); assertThat(request, HttpUriRequestPayloadMatcher.hasPayload("{'key1':'value1'}")); } }
&& httpMethod.getFirstHeader("Content-Type") == null) { httpMethod.setHeader(requestEntity.getContentType());
&& httpMethod.getFirstHeader("Content-Type") == null) { httpMethod.setHeader(requestEntity.getContentType());
&& httpUriRequest.getFirstHeader(HttpHeaders.CONTENT_TYPE) == null) { httpUriRequest.addHeader(HttpHeaders.CONTENT_TYPE, contentTypeString);
&& httpUriRequest.getFirstHeader(HttpHeaders.CONTENT_TYPE) == null) { httpUriRequest.addHeader(HttpHeaders.CONTENT_TYPE, contentType.toString());
Header userAgentHeader = method.getFirstHeader( HTTP.USER_AGENT ); if ( userAgentHeader == null )
Header userAgentHeader = method.getFirstHeader( HTTP.USER_AGENT ); if ( userAgentHeader == null )
@Test public void testGetBusinessObjectDataAssertNoAuthorizationHeaderWhenNoSsl() throws Exception { HttpClientOperations mockHttpClientOperations = mock(HttpClientOperations.class); HttpClientOperations originalHttpClientOperations = (HttpClientOperations) ReflectionTestUtils.getField(downloaderWebClient, "httpClientOperations"); ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", mockHttpClientOperations); try { CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class); when(mockHttpClientOperations.execute(any(), any())).thenReturn(closeableHttpResponse); when(closeableHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK")); when(closeableHttpResponse.getEntity()).thenReturn(new StringEntity(xmlHelper.objectToXml(new BusinessObjectData()))); DownloaderInputManifestDto manifest = new DownloaderInputManifestDto(); downloaderWebClient.getRegServerAccessParamsDto().setUseSsl(false); downloaderWebClient.getBusinessObjectData(manifest); verify(mockHttpClientOperations).execute(any(), argThat(httpUriRequest -> httpUriRequest.getFirstHeader("Authorization") == null)); } finally { ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", originalHttpClientOperations); } }
@Test public void testGetBusinessObjectDataDownloadCredentialAssertNoAuthorizationHeaderWhenNoSsl() throws Exception { HttpClientOperations mockHttpClientOperations = mock(HttpClientOperations.class); HttpClientOperations originalHttpClientOperations = (HttpClientOperations) ReflectionTestUtils.getField(downloaderWebClient, "httpClientOperations"); ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", mockHttpClientOperations); try { CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class); when(mockHttpClientOperations.execute(any(), any())).thenReturn(closeableHttpResponse); when(closeableHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "SUCCESS")); when(closeableHttpResponse.getEntity()).thenReturn(new StringEntity(xmlHelper.objectToXml(new StorageUnitDownloadCredential()))); DownloaderInputManifestDto downloaderInputManifestDto = new DownloaderInputManifestDto(); String storageName = "storageName"; boolean useSsl = false; downloaderWebClient.getRegServerAccessParamsDto().setUseSsl(useSsl); downloaderWebClient.getStorageUnitDownloadCredential(downloaderInputManifestDto, storageName); verify(mockHttpClientOperations).execute(any(), argThat(httpUriRequest -> httpUriRequest.getFirstHeader("Authorization") == null)); } finally { ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", originalHttpClientOperations); } }