Example usage for org.apache.http.client.methods HttpUriRequest setHeader

List of usage examples for org.apache.http.client.methods HttpUriRequest setHeader

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpUriRequest setHeader.

Prototype

void setHeader(String str, String str2);

Source Link

Usage

From source file:org.jets3t.service.utils.SignatureUtils.java

/**
 * Calculate AWS Version 4 signature for a HTTP request and apply the
 * appropriate "Authorization" header value to authorize it.
 *
 * @param httpMethod/*from w w  w.j av  a 2  s .  c  om*/
 * the request's HTTP method just prior to sending
 * @param requestSignatureVersion
 * request signature version string, e.g. "AWS4-HMAC-SHA256"
 * @param providerCredentials
 * account holder's access and secret key credentials
 * @param requestPayloadHexSha256Hash
 * hex-encoded SHA256 hash of request's payload.
 * @param region
 * region to which the request will be sent
 * {@link "http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region"}
 */
public static void awsV4SignRequestAuthorizationHeader(String requestSignatureVersion,
        HttpUriRequest httpMethod, ProviderCredentials providerCredentials, String requestPayloadHexSha256Hash,
        String region) {
    // Ensure the required Host header is set prior to signing.
    if (httpMethod.getFirstHeader("Host") == null) {
        httpMethod.setHeader("Host", httpMethod.getURI().getHost());
    }

    // Generate AWS-flavoured ISO8601 timestamp string
    String timestampISO8601 = SignatureUtils.awsV4ParseAndFormatDate(httpMethod);

    // Apply AWS-flavoured ISO8601 timestamp string to "x-aws-date"
    // metadata, otherwise if only the Date header is present and it is
    // RFC 822 formatted S3 expects that date to be part of the string
    // to sign, not the AWS-flavoured ISO8601 timestamp as claimed by the
    // documentation.
    // TODO This shouldn't be necessary, confirm it really is...
    if (httpMethod.getFirstHeader("x-amz-date") == null) {
        httpMethod.setHeader("x-amz-date", timestampISO8601);
    }

    // Canonical request string
    String canonicalRequestString = SignatureUtils.awsV4BuildCanonicalRequestString(httpMethod,
            requestPayloadHexSha256Hash);

    // String to sign
    String stringToSign = SignatureUtils.awsV4BuildStringToSign(requestSignatureVersion, canonicalRequestString,
            timestampISO8601, region);

    // Signing key
    byte[] signingKey = SignatureUtils.awsV4BuildSigningKey(providerCredentials.getSecretKey(),
            timestampISO8601, region);

    // Request signature
    String signature = ServiceUtils
            .toHex(ServiceUtils.hmacSHA256(signingKey, ServiceUtils.stringToBytes(stringToSign)));

    // Authorization header value
    String authorizationHeaderValue = SignatureUtils.awsV4BuildAuthorizationHeaderValue(
            providerCredentials.getAccessKey(), signature, requestSignatureVersion, canonicalRequestString,
            timestampISO8601, region);

    httpMethod.setHeader("Authorization", authorizationHeaderValue);
}

From source file:appserver.grupo5.http.java

public static Response Request(Method method, String url, String content, String contentType) {
    Response response;//from   w w  w  . ja v  a2 s.c  om
    try {
        HttpClient client = new DefaultHttpClient();
        HttpUriRequest request;
        switch (method) {
        case GET:
            request = new HttpGet(url);
            break;
        case POST:
            request = new HttpPost(url);
            ((HttpPost) request).setEntity(new StringEntity(content));
            break;
        case PUT:
            request = new HttpPut(url);
            ((HttpPut) request).setEntity(new StringEntity(content));
            break;
        case DELETE:
            request = new HttpDeleteWithBody(url);
            ((HttpDeleteWithBody) request).setEntity(new StringEntity(content));
            break;
        default:
            request = new HttpGet(url);
            break;
        }
        if (method != Method.GET) {
            request.setHeader("Content-type", contentType);
        }
        response = executeRequest(client, request);
    } catch (Exception e) {
        response = Response.status(400).entity(e.getMessage()).build();
    }
    return response;
}

From source file:com.miloisbadboy.net.Utility.java

public static void setHeader(String httpMethod, HttpUriRequest request, RequestParameters authParam, String url)
        throws RequestException {
    if (!isBundleEmpty(mRequestHeader)) {
        for (int loc = 0; loc < mRequestHeader.size(); loc++) {
            String key = mRequestHeader.getKey(loc);
            request.setHeader(key, mRequestHeader.getValue(key));
        }/*from  w w w. ja  v  a2s  . co  m*/
    }
    if (!isBundleEmpty(authParam)) {
        String authHeader = "";
        if (authHeader != null) {
            request.setHeader("Authorization", authHeader);
        }
    }
    request.setHeader("User-Agent", System.getProperties().getProperty("http.agent") + " WeiboAndroidSDK");
}

From source file:com.haoqee.chat.net.Utility.java

public static void setHeader(String httpMethod, HttpUriRequest request, Parameters authParam, String url) {
    if (!isBundleEmpty(mRequestHeader)) {
        for (int loc = 0; loc < mRequestHeader.size(); loc++) {
            String key = mRequestHeader.getKey(loc);
            request.setHeader(key, mRequestHeader.getValue(key));
        }//  w ww  .jav  a 2 s .co  m
    }
    if (!isBundleEmpty(authParam) && mAuth != null) {
        String authHeader = mAuth.getWeiboAuthHeader(httpMethod, url, authParam);
        if (authHeader != null) {
            request.setHeader("Authorization", authHeader);
        }
    }
    request.setHeader("User-Agent", System.getProperties().getProperty("http.agent") + " WeiboAndroidSDK");
}

From source file:com.dongfang.dicos.sina.UtilSina.java

public static void setHeader(String httpMethod, HttpUriRequest request, WeiboParameters authParam, String url,
        Token token) throws WeiboException {
    if (!isBundleEmpty(mRequestHeader)) {
        for (int loc = 0; loc < mRequestHeader.size(); loc++) {
            String key = mRequestHeader.getKey(loc);
            request.setHeader(key, mRequestHeader.getValue(key));
        }/*  ww  w.j  av a  2s  .co  m*/
    }
    if (!isBundleEmpty(authParam) && mAuth != null) {
        String authHeader = mAuth.getWeiboAuthHeader(httpMethod, url, authParam, Weibo.getAppKey(),
                Weibo.getAppSecret(), token);
        if (authHeader != null) {
            request.setHeader("Authorization", authHeader);
        }
    }
    request.setHeader("User-Agent", System.getProperties().getProperty("http.agent") + " WeiboAndroidSDK");
}

From source file:com.research.net.Utility.java

public static void setHeader(String httpMethod, HttpUriRequest request, ResearchParameters authParam,
        String url) {//from  ww w . j  a  v a 2  s .  co m
    if (!isBundleEmpty(mRequestHeader)) {
        for (int loc = 0; loc < mRequestHeader.size(); loc++) {
            String key = mRequestHeader.getKey(loc);
            request.setHeader(key, mRequestHeader.getValue(key));
        }
    }
    if (!isBundleEmpty(authParam) && mAuth != null) {
        String authHeader = mAuth.getWeiboAuthHeader(httpMethod, url, authParam);
        if (authHeader != null) {
            request.setHeader("Authorization", authHeader);
        }
    }
    request.setHeader("User-Agent", System.getProperties().getProperty("http.agent") + " WeiboAndroidSDK");
}

From source file:net.fischboeck.discogs.security.OAuthAuthorizationStrategy.java

public HttpUriRequest authorize(HttpUriRequest request) {
    request.setHeader(HttpHeaders.AUTHORIZATION, "");
    return request;
}

From source file:net.fischboeck.discogs.security.TokenAuthenticationStrategy.java

/**
 * Modifies the provided request to be authenticated.
 * This is done by adding the HTTP Authorization header with the given api token
 * @param request The request to be modified
 * @return The modified request//from  w w  w  .  j av a2  s  . c om
 */
public HttpUriRequest authorize(HttpUriRequest request) {
    request.setHeader(HttpHeaders.AUTHORIZATION, "Discogs token=" + this.token);
    return request;
}

From source file:ee.ioc.phon.netspeechapi.AbstractUserAgent.java

/**
 * <p>Sets the User-Agent field in the given request.</p>
 * /*w ww  .j a v a 2 s.  com*/
 * @param request HTTP request (e.g. GET or POST)
 */
protected void setUserAgent(HttpUriRequest request) {
    request.setHeader("User-Agent", mUserAgent);
}

From source file:net.fischboeck.discogs.security.UsernamePasswordAuthenticationStrategy.java

/**
 * Modifies the HTTP request by appending the HTTP header 'AUTHORIZATION' with the specified username and password.
 * @param request The HTTP request to be modified
 * @return The modified HTTP request/*from   w w w .ja va 2s .  co m*/
 */
public HttpUriRequest authorize(HttpUriRequest request) {
    request.setHeader(HttpHeaders.AUTHORIZATION, "Discogs key=" + this.username + ", secret=" + this.password);
    return request;
}