Java Code Examples for org.apache.http.HttpRequest#setHeader()

The following examples show how to use org.apache.http.HttpRequest#setHeader() . 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: UserAgentHttpRequestInterceptorTest.java    From gerrit-rest-java-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testHeaderIsModified() {
    HttpRequest req = EasyMock.createMock(HttpRequest.class);
    Header header = EasyMock.createMock(Header.class);
    EasyMock.expect(header.getValue()).andReturn("BaseAgent").once();
    EasyMock.expect(req.getFirstHeader(HttpHeaders.USER_AGENT))
        .andReturn(header).once();
    req.setHeader(EasyMock.matches("^User-Agent$"),
        EasyMock.matches("^gerrit-rest-java-client/.* using BaseAgent$"));
    EasyMock.replay(header, req);
    HttpContext ctx = EasyMock.createMock(HttpContext.class);
    UserAgentHttpRequestInterceptor interceptor = new UserAgentHttpRequestInterceptor();

    interceptor.process(req, ctx);

    EasyMock.verify(header, req);
}
 
Example 2
Source File: ZosmfSchemeTest.java    From api-layer with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void givenZosmfToken_whenApplyToRequest_thenTestJwtToken() {
    Calendar calendar = Calendar.getInstance();
    QueryResponse queryResponse = new QueryResponse("domain", "username", calendar.getTime(), calendar.getTime(), QueryResponse.Source.ZOSMF);
    AuthConfigurationProperties.CookieProperties cookieProperties = mock(AuthConfigurationProperties.CookieProperties.class);
    when(cookieProperties.getCookieName()).thenReturn("apimlAuthenticationToken");
    when(authConfigurationProperties.getCookieProperties()).thenReturn(cookieProperties);
    RequestContext requestContext = spy(new RequestContext());
    RequestContext.testSetCurrentContext(requestContext);

    HttpRequest httpRequest = new HttpGet("/test/request");
    httpRequest.setHeader(COOKIE_HEADER, "cookie1=1");
    Authentication authentication = new Authentication(AuthenticationScheme.ZOSMF, null);
    HttpServletRequest request = new MockHttpServletRequest();
    requestContext.setRequest(request);

    when(authenticationService.getJwtTokenFromRequest(request)).thenReturn(Optional.of("jwtToken2"));
    when(authenticationService.parseJwtToken("jwtToken2")).thenReturn(queryResponse);
    when(authConfigurationProperties.getCookieProperties().getCookieName()).thenReturn("apimlAuthenticationToken");

    zosmfScheme.createCommand(authentication, () -> queryResponse).applyToRequest(httpRequest);

    assertEquals("cookie1=1;jwtToken=jwtToken2", httpRequest.getFirstHeader("cookie").getValue());
}
 
Example 3
Source File: HttpBasicPassTicketSchemeTest.java    From api-layer with Eclipse Public License 2.0 6 votes vote down vote up
@Test
void givenJwtInCookie_whenApplyToRequest_thenJwtIsRemoved() {
    AuthenticationCommand command = getPassTicketCommand();
    HttpRequest httpRequest = new HttpGet();
    httpRequest.setHeader("cookie",
        authConfigurationProperties.getCookieProperties().getCookieName() + "=jwt;" +
        "abc=def"
    );

    command.applyToRequest(httpRequest);

    Header[] headers = httpRequest.getHeaders("cookie");
    assertNotNull(headers);
    assertEquals(1, headers.length);
    assertEquals("abc=def", headers[0].getValue());
}
 
Example 4
Source File: ApiProxyServlet.java    From onboard with Apache License 2.0 5 votes vote down vote up
private void setXForwardedForHeader(HttpServletRequest servletRequest, HttpRequest proxyRequest) {
    String headerName = "X-Forwarded-For";
    if (doForwardIP) {
        String newHeader = servletRequest.getRemoteAddr();
        String existingHeader = servletRequest.getHeader(headerName);
        if (existingHeader != null) {
            newHeader = existingHeader + ", " + newHeader;
        }
        proxyRequest.setHeader(headerName, newHeader);
    }
}
 
Example 5
Source File: ZosmfSchemeTest.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void givenRequestWithSetCookie_whenApplyToRequest_thenAppendSetCookie() {
    HttpRequest httpRequest = new HttpGet("/test/request");
    httpRequest.setHeader(COOKIE_HEADER, "cookie1=1");
    prepareAuthenticationService("ltpa2");

    zosmfScheme.createCommand(authentication, () -> queryResponse).applyToRequest(httpRequest);

    assertEquals("cookie1=1;LtpaToken2=ltpa2", httpRequest.getFirstHeader("cookie").getValue());
}
 
Example 6
Source File: TestableApi.java    From ezScrum with GNU General Public License v2.0 5 votes vote down vote up
protected void setHeaders(HttpRequest request, long accountId, String platformType) {
	TokenObject token = TokenObject.getByPlatform(accountId, platformType);
	long currentTime = System.currentTimeMillis();
	request.setHeader("user_id", String.valueOf(accountId));
	request.setHeader("public_token", token.getPublicToken());
	request.setHeader("disposable_token", token.getDisposableToken(currentTime));
	request.setHeader("timestamp", String.valueOf(currentTime));
}
 
Example 7
Source File: BasicAuthPlugin.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean interceptInternodeRequest(HttpRequest httpRequest, HttpContext httpContext) {
  if (forwardCredentials) {
    if (httpContext instanceof HttpClientContext) {
      HttpClientContext httpClientContext = (HttpClientContext) httpContext;
      if (httpClientContext.getUserToken() instanceof BasicAuthUserPrincipal) {
        BasicAuthUserPrincipal principal = (BasicAuthUserPrincipal) httpClientContext.getUserToken();
        String userPassBase64 = Base64.encodeBase64String((principal.getName() + ":" + principal.getPassword()).getBytes(StandardCharsets.UTF_8));
        httpRequest.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + userPassBase64);
        return true;
      }
    }
  }
  return false;
}
 
Example 8
Source File: CsrfHttpClient.java    From multiapps-controller with Apache License 2.0 5 votes vote down vote up
private void setCrsfToken(HttpRequest request) throws IOException {
    if ((request == null) || !isProtectionRequired(request)) {
        return;
    }

    initializeToken(false);
    if (csrfToken != null) {
        request.setHeader(CSRF_TOKEN_HEADER_NAME, csrfToken);
    }
}
 
Example 9
Source File: ClientUtils.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private void setAuth(HttpRequest httpget) {
  if (password != null) {
    try {
      byte[] b = Base64.encodeBase64((username+":"+password).getBytes("ASCII"));
      String b64 = new String(b, StandardCharsets.US_ASCII);
      httpget.setHeader("Authorization", "Basic " + b64);
    } catch (UnsupportedEncodingException e) {
    }
  }
}
 
Example 10
Source File: JWTAuthPlugin.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean interceptInternodeRequest(HttpRequest httpRequest, HttpContext httpContext) {
  if (httpContext instanceof HttpClientContext) {
    HttpClientContext httpClientContext = (HttpClientContext) httpContext;
    if (httpClientContext.getUserToken() instanceof JWTPrincipal) {
      JWTPrincipal jwtPrincipal = (JWTPrincipal) httpClientContext.getUserToken();
      httpRequest.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + jwtPrincipal.getToken());
      return true;
    }
  }
  return false;
}
 
Example 11
Source File: BaseRequestInterceptor.java    From clouddisk with MIT License 5 votes vote down vote up
public void requestInit(final HttpRequest request, final HttpContext context)throws HttpException, IOException {
	request.setHeader(C.ACCEPT_KEY, C.ACCEPT_VAL);
	request.setHeader(C.ACCEPT_ENCODING_KEY, C.ACCEPT_ENCODING_VAL);
	request.setHeader(C.ACCEPT_LANGUAGE_KEY, C.ACCEPT_LANGUAGE_VAL);
	request.setHeader(C.CONNECTION_KEY, C.CONNECTION_VAL);
	request.setHeader(C.CONTENT_TYPE_KEY, C.CONTENT_TYPE_VAL);
	request.setHeader(C.USER_AGENT_KEY, C.USER_AGENT_VAL);
}
 
Example 12
Source File: ClientUtils.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private void setAuth(HttpRequest httpget) {
  if (password != null) {
    try {
      byte[] b = Base64.encodeBase64((username+":"+password).getBytes("ASCII"));
      String b64 = new String(b, StandardCharsets.US_ASCII);
      httpget.setHeader("Authorization", "Basic " + b64);
    } catch (UnsupportedEncodingException e) {
    }
  }
}
 
Example 13
Source File: HttpWebConnection.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    request.setHeader(HttpHeader.USER_AGENT, value_);
}
 
Example 14
Source File: HttpWebConnection.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    request.setHeader(HttpHeader.ACCEPT, value_);
}
 
Example 15
Source File: CsrfHttpClient.java    From multiapps-controller with Apache License 2.0 4 votes vote down vote up
private void setHttpRequestHeaders(HttpRequest request) {
    for (Entry<String, String> httpRequestHeaderEntry : httpRequestHeaders.entrySet()) {
        request.setHeader(httpRequestHeaderEntry.getKey(), httpRequestHeaderEntry.getValue());
    }
}
 
Example 16
Source File: HttpWebConnection.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    request.setHeader(HttpHeader.UPGRADE_INSECURE_REQUESTS, value_);
}
 
Example 17
Source File: HttpWebConnection.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    request.setHeader("Accept-Encoding", value_);
}
 
Example 18
Source File: ApacheHttpClientEdgeGridRequestSigner.java    From AkamaiOPEN-edgegrid-java with Apache License 2.0 4 votes vote down vote up
@Override
protected void setAuthorization(HttpRequest request, String signature) {
    request.setHeader("Authorization", signature);
}
 
Example 19
Source File: HttpWebConnection.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    request.setHeader(HttpHeader.SEC_FETCH_SITE, value_);
}
 
Example 20
Source File: HttpWebConnection.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    request.setHeader(HttpHeader.SEC_FETCH_DEST, value_);
}