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

The following examples show how to use org.apache.http.HttpRequest#addHeader() . 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: ClientUtils.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
/**
 * Method adds required request headers.
 * TODO handle JSON request as well.
 * 
 * @param request
 */
protected void configureFhirRequest(HttpRequest request, String format, List<Header> headers) {
  request.addHeader("User-Agent", "Java FHIR Client for FHIR");

  if (format != null) {		
    request.addHeader("Accept",format);
    request.addHeader("Content-Type", format + ";charset=" + DEFAULT_CHARSET);
  }
  request.addHeader("Accept-Charset", DEFAULT_CHARSET);
  if(headers != null) {
    for(Header header : headers) {
      request.addHeader(header);
    }
  }
  setAuth(request);
}
 
Example 2
Source File: ClientUtils.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
/**
 * Method adds required request headers.
 * TODO handle JSON request as well.
 * 
 * @param request
 */
protected void configureFhirRequest(HttpRequest request, String format, List<Header> headers) {
  request.addHeader("User-Agent", "Java FHIR Client for FHIR");

  if (format != null) {		
    request.addHeader("Accept",format);
    request.addHeader("Content-Type", format + ";charset=" + DEFAULT_CHARSET);
  }
  request.addHeader("Accept-Charset", DEFAULT_CHARSET);
  if(headers != null) {
    for(Header header : headers) {
      request.addHeader(header);
    }
  }
  setAuth(request);
}
 
Example 3
Source File: ClientUtils.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
/**
 * Method adds required request headers.
 * TODO handle JSON request as well.
 * 
 * @param request
 */
 protected void configureFhirRequest(HttpRequest request, String format, List<Header> headers) {
	request.addHeader("User-Agent", "Java FHIR Client for FHIR");

	if (format != null) {		
	  request.addHeader("Accept",format);
	  request.addHeader("Content-Type", format + ";charset=" + DEFAULT_CHARSET);
	}
   request.addHeader("Accept-Charset", DEFAULT_CHARSET);
	if(headers != null) {
		for(Header header : headers) {
			request.addHeader(header);
		}
	}
   setAuth(request);
}
 
Example 4
Source File: OAuth2RequestInterceptor.java    From cyberduck with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    if(tokens.isExpired()) {
        try {
            tokens = this.refresh(tokens);
        }
        catch(BackgroundException e) {
            log.warn(String.format("Failure refreshing OAuth 2 tokens %s. %s", tokens, e));
            // Follow up error 401 handled in error interceptor
        }
    }
    if(StringUtils.isNotBlank(tokens.getAccessToken())) {
        if(log.isInfoEnabled()) {
            log.info(String.format("Authorizing service request with OAuth2 access token %s", tokens.getAccessToken()));
        }
        request.removeHeaders(HttpHeaders.AUTHORIZATION);
        request.addHeader(new BasicHeader(HttpHeaders.AUTHORIZATION, String.format("Bearer %s", tokens.getAccessToken())));
    }
}
 
Example 5
Source File: LocalRequestTest.java    From logbook with MIT License 5 votes vote down vote up
@Test
void shouldReturnContentTypeHeader() {
    final HttpRequest delegate = get("/");
    delegate.addHeader("Content-Type", "text/plain;");
    final LocalRequest unit = unit(delegate);
    assertThat(unit.getHeaders(), aMapWithSize(1));
}
 
Example 6
Source File: ConnectionReuseTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void headerRequest(HttpHost target, HttpRoute route, HttpClientConnection conn, PoolingHttpClientConnectionManager cm)
    throws IOException, HttpException {
  HttpRequest req = new BasicHttpRequest("OPTIONS", "*", HttpVersion.HTTP_1_1);

  req.addHeader("Host", target.getHostName());
  if (!conn.isOpen()) {
    // establish connection based on its route info
    cm.connect(conn, route, 1000, context);
    // and mark it as route complete
    cm.routeComplete(conn, route, context);
  }
  conn.sendRequestHeader(req);
  conn.flush();
  conn.receiveResponseHeader();
}
 
Example 7
Source File: LoggingHttpRequestExecutor.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Override
public HttpResponse execute(final HttpRequest request, final HttpClientConnection conn, final HttpContext context)
        throws IOException, HttpException {
    if(!request.containsHeader(HttpHeaders.USER_AGENT)) {
        request.addHeader(new BasicHeader(HttpHeaders.USER_AGENT, useragentProvider.get()));
    }
    return super.execute(request, conn, context);
}
 
Example 8
Source File: RestUtils.java    From directory-fortress-core with Apache License 2.0 5 votes vote down vote up
/**
 * Set these params into their associated HTTP header vars.
 *
 * @param httpRequest
 */
private static void setMethodHeaders( HttpRequest httpRequest )
{
    if ( httpRequest instanceof HttpPost || httpRequest instanceof HttpPut)
    {
        httpRequest.addHeader( "Content-Type", "application/xml" );
        httpRequest.addHeader( "Accept", "application/xml" );
    }
}
 
Example 9
Source File: HttpClientUtil.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void process(HttpRequest request, HttpContext context)
    throws HttpException, IOException {
  if (!request.containsHeader("Accept-Encoding")) {
    request.addHeader("Accept-Encoding", "gzip, deflate");
  }
}
 
Example 10
Source File: AwsSigner4.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Sign a request.  Add the headers that authenticate the request.
 *
 * @param request The request to sign.
 * @param credentials The credentials to use when signing.
 * @param signingTime The invocation time to use;
 */
void sign(HttpRequest request, AuthConfig credentials, Date signingTime) {
    AwsSigner4Request sr = new AwsSigner4Request(region, service, request, signingTime);
    if(!request.containsHeader("X-Amz-Date")) {
        request.addHeader("X-Amz-Date", sr.getSigningDateTime());
    }
    request.addHeader("Authorization", task4(sr, credentials));
    final String securityToken = credentials.getAuth();
    if (StringUtils.isNotEmpty(securityToken)) {
        request.addHeader("X-Amz-Security-Token", securityToken);
    }
}
 
Example 11
Source File: LocalRequestTest.java    From logbook with MIT License 5 votes vote down vote up
@Test
void shouldHandleDuplicateHeaders() {
    final HttpRequest delegate = post("/");
    delegate.addHeader("Content-Type", "text/plain;");
    delegate.addHeader("Content-Type", "text/plain;");
    final LocalRequest unit = unit(delegate);
    assertThat(unit.getHeaders(), aMapWithSize(1));
    assertThat(unit.getHeaders().get("Content-Type"), hasSize(2));
}
 
Example 12
Source File: ApiProxyServlet.java    From onboard with Apache License 2.0 5 votes vote down vote up
/** Copy request headers from the servlet client to the proxy request. */
protected void copyRequestHeaders(HttpServletRequest servletRequest, HttpRequest proxyRequest) {
    // Get an Enumeration of all of the header names sent by the client
    Enumeration enumerationOfHeaderNames = servletRequest.getHeaderNames();
    while (enumerationOfHeaderNames.hasMoreElements()) {
        String headerName = (String) enumerationOfHeaderNames.nextElement();
        // Instead the content-length is effectively set via InputStreamEntity
        if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH))
            continue;
        if (hopByHopHeaders.containsHeader(headerName))
            continue;

        Enumeration headers = servletRequest.getHeaders(headerName);
        while (headers.hasMoreElements()) {// sometimes more than one value
            String headerValue = (String) headers.nextElement();
            // In case the proxy host is running multiple virtual servers,
            // rewrite the Host header to ensure that we get content from
            // the correct virtual server
            if (headerName.equalsIgnoreCase(HttpHeaders.HOST)) {
                HttpHost host = getTargetHost(servletRequest);
                headerValue = host.getHostName();
                if (host.getPort() != -1)
                    headerValue += ":" + host.getPort();
            } else if (headerName.equalsIgnoreCase(org.apache.http.cookie.SM.COOKIE)) {
                headerValue = getRealCookie(headerValue);
            }
            proxyRequest.addHeader(headerName, headerValue);
        }
    }
}
 
Example 13
Source File: FusionPipelineClient.java    From storm-solr with Apache License 2.0 4 votes vote down vote up
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
  request.addHeader(BasicScheme.authenticate(credentials, "US-ASCII", false));
}
 
Example 14
Source File: QTestHttpClient.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 4 votes vote down vote up
@Override
public void auth(HttpRequest req) throws AuthenticationException {
	req.addHeader("Authorization", authHeader);
}
 
Example 15
Source File: GzipRequestInterceptor.java    From YiBo with Apache License 2.0 4 votes vote down vote up
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
	if (!request.containsHeader("Accept-Encoding")) {
		request.addHeader("Accept-Encoding", "gzip");
	}
}
 
Example 16
Source File: ResourceBasedAuthorizationInterceptor.java    From spring-cloud-dataflow with Apache License 2.0 4 votes vote down vote up
@Override
public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException {
	final String credentials = StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8).trim();
	resource.check();
	httpRequest.addHeader(HttpHeaders.AUTHORIZATION, credentials);
}
 
Example 17
Source File: BmsCommonInterceptor.java    From spring-cloud-formula with Apache License 2.0 4 votes vote down vote up
@Override
public void process(HttpRequest request, HttpContext context) {
    request.addHeader("X-BMS-API-VERSION", VERSION);
}
 
Example 18
Source File: JsonRpcHttpAsyncClient.java    From jsonrpc4j with MIT License 2 votes vote down vote up
/**
 * Set the request headers.
 *
 * @param request the request object
 * @param headers to be used
 */
private void addHeaders(HttpRequest request, Map<String, String> headers) {
	for (Map.Entry<String, String> key : headers.entrySet()) {
		request.addHeader(key.getKey(), key.getValue());
	}
}
 
Example 19
Source File: BasicHttpClient.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 2 votes vote down vote up
/**
 * basic auth implementation
 *
 * @param req
 * @throws AuthenticationException
 */
public void auth(HttpRequest req) throws AuthenticationException {
    req.addHeader(new BasicScheme().authenticate(creds, req, context));
}
 
Example 20
Source File: AndroidHttpClient.java    From travelguide with Apache License 2.0 2 votes vote down vote up
/**
 * Modifies a request to indicate to the server that we would like a
 * gzipped response.  (Uses the "Accept-Encoding" HTTP header.)
 * @param request the request to modify
 * @see #getUngzippedContent
 */
public static void modifyRequestToAcceptGzipResponse(HttpRequest request) {
    request.addHeader("Accept-Encoding", "gzip");
}