Java Code Examples for org.apache.commons.httpclient.HttpMethodBase#setRequestHeader()

The following examples show how to use org.apache.commons.httpclient.HttpMethodBase#setRequestHeader() . 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: SalesforceProvisioningConnector.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * adding OAuth authorization headers to a httpMethod
 *
 * @param httpMethod method which wants to add Authorization header
 */
private void setAuthorizationHeader(HttpMethodBase httpMethod)
        throws IdentityProvisioningException {

    boolean isDebugEnabled = log.isDebugEnabled();

    String accessToken = authenticate();
    if (StringUtils.isNotBlank(accessToken)) {
        httpMethod.setRequestHeader(SalesforceConnectorConstants.AUTHORIZATION_HEADER_NAME,
                SalesforceConnectorConstants.AUTHORIZATION_HEADER_OAUTH + " " + accessToken);

        if (isDebugEnabled) {
            log.debug("Setting authorization header for method : " + httpMethod.getName()
                    + " as follows,");
            Header authorizationHeader = httpMethod
                    .getRequestHeader(SalesforceConnectorConstants.AUTHORIZATION_HEADER_NAME);
            log.debug(authorizationHeader.getName() + ": " + authorizationHeader.getValue());
        }
    } else {
        throw new IdentityProvisioningException("Authentication failed");
    }

}
 
Example 2
Source File: BigSwitchBcfApi.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
private void setHttpHeader(final HttpMethodBase m) {
    m.setRequestHeader(CONTENT_TYPE, CONTENT_JSON);
    m.setRequestHeader(ACCEPT, CONTENT_JSON);
    m.setRequestHeader(HTTP_HEADER_INSTANCE_ID, CLOUDSTACK_INSTANCE_ID + "-" + zoneId);
    if (StringUtils.isNotEmpty(hash)) {
        m.setRequestHeader(HASH_MATCH, hash);
    }

    String authString = username + ":" + password;
    String encodedAuthString = "Basic " + Base64.encodeBase64String(authString.getBytes(Charset.forName("UTF-8")));
    m.setRequestHeader("Authorization", encodedAuthString);
}
 
Example 3
Source File: Worker.java    From zap-extensions with Apache License 2.0 4 votes vote down vote up
private int makeRequest(HttpMethodBase httpMethod)
        throws HttpException, IOException, InterruptedException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Worker[" + threadId + "]: " + httpMethod.getName() + " : " + url.toString());
    }

    // set the custom HTTP headers
    Vector HTTPheaders = manager.getHTTPHeaders();
    for (int a = 0; a < HTTPheaders.size(); a++) {
        HTTPHeader httpHeader = (HTTPHeader) HTTPheaders.elementAt(a);
        /*
         * Host header has to be set in a different way!
         */
        if (httpHeader.getHeader().startsWith("Host")) {
            httpMethod.getParams().setVirtualHost(httpHeader.getValue());
        } else {
            httpMethod.setRequestHeader(httpHeader.getHeader(), httpHeader.getValue());
        }
    }
    httpMethod.setFollowRedirects(Config.followRedirects);

    /*
     * this code is used to limit the number of request/sec
     */
    if (manager.isLimitRequests()) {
        while (manager.getTotalDone()
                        / ((System.currentTimeMillis() - manager.getTimestarted()) / 1000.0)
                > manager.getLimitRequestsTo()) {
            Thread.sleep(100);
        }
    }
    /*
     * Send the request
     */
    int code = httpclient.executeMethod(httpMethod);

    if (LOG.isDebugEnabled()) {
        LOG.debug("Worker[" + threadId + "]: " + code + " " + url.toString());
    }
    return code;
}
 
Example 4
Source File: BasicAuthStrategy.java    From rome with Apache License 2.0 4 votes vote down vote up
@Override
public void addAuthentication(final HttpClient httpClient, final HttpMethodBase method) throws ProponoException {
    httpClient.getParams().setAuthenticationPreemptive(true);
    final String header = "Basic " + credentials;
    method.setRequestHeader("Authorization", header);
}
 
Example 5
Source File: GDataAuthStrategy.java    From rome with Apache License 2.0 4 votes vote down vote up
@Override
public void addAuthentication(final HttpClient httpClient, final HttpMethodBase method) throws ProponoException {
    httpClient.getParams().setAuthenticationPreemptive(true);
    method.setRequestHeader("Authorization", authToken);
}