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(Header header);

Source Link

Usage

From source file:code.google.restclient.core.Hitter.java

private HttpHandler hit(String url, HttpUriRequest request, HttpHandler handler,
        Map<String, String> requestHeaders) throws Exception {

    if (DEBUG_ENABLED)
        LOG.debug("hit() - hitting url (params encoded) --> " + url);

    HttpResponse response = null;//w  w  w.  j  a  v  a2s .c  o  m
    HttpClient client = getHttpClient();

    // set request headers
    Map<String, String> hs = new LinkedHashMap<String, String>();
    if (headers != null)
        hs.putAll(headers);
    if (requestHeaders != null)
        hs.putAll(requestHeaders);
    if (!hs.isEmpty()) {
        for (String header : hs.keySet())
            request.setHeader(new BasicHeader(header, hs.get(header)));
    }
    handler.setRequest(request);

    response = client.execute(request); // **** execute request ****
    handler.setResponse(response);
    return handler;

}

From source file:com.okta.sdk.framework.ApiClient.java

/**
 * Sets the headers in the HTTP request.
 *
 * @param  httpUriRequest {@link HttpUriRequest} Current HTTP URI request object.
 * @throws IOException                           If an input or output exception occurred.
 */// ww w .ja  v a2s. c  om
protected void setHeaders(HttpUriRequest httpUriRequest) throws IOException {
    setTokenHeader(httpUriRequest);

    // Content type and accept header are determined by the child class
    setContentTypeHeader(httpUriRequest);
    setAcceptHeader(httpUriRequest);

    // Walk through custom headers and set
    Map<String, String> headers = this.configuration.getHeaders();
    if (headers != null) {
        // Update headers
        for (Map.Entry<String, String> header : headers.entrySet()) {
            Header extraHeader = new BasicHeader(header.getKey(), header.getValue());
            httpUriRequest.setHeader(extraHeader);
        }
    }

}

From source file:org.jets3t.service.impl.rest.httpclient.RestStorageService.java

/**
 * Performs an HTTP PUT request using the {@link #performRequest} method.
 *
 * @param bucketName/*from  w w w. j  a  v a 2s  .  c o  m*/
 *        the name of the bucket the object will be stored in.
 * @param objectKey
 *        the key (name) of the object to be stored.
 * @param metadata
 *        map of name/value pairs to add as metadata to any S3 objects created.
 * @param requestParameters
 *        parameters to add to the request URL as GET params
 * @param requestEntity
 *        an HttpClient object that encapsulates the object and data contents that will be
 *        uploaded. This object supports the resending of object data, when possible.
 * @param autoRelease
 *        if true, the HTTP Method object will be released after the request has
 *        completed and the connection will be closed. If false, the object will
 *        not be released and the caller must take responsibility for doing this.
 * @return
 *        a package including the HTTP method object used to perform the request, and the
 *        content length (in bytes) of the object that was PUT to S3.
 *
 * @throws org.jets3t.service.ServiceException
 */
protected HttpResponseAndByteCount performRestPut(String bucketName, String objectKey,
        Map<String, Object> metadata, Map<String, String> requestParameters, HttpEntity requestEntity,
        boolean autoRelease) throws ServiceException {
    // Add any request parameters.
    HttpUriRequest httpMethod = setupConnection(HTTP_METHOD.PUT, bucketName, objectKey, requestParameters);

    Map<String, Object> renamedMetadata = renameMetadataKeys(metadata);
    addMetadataToHeaders(httpMethod, renamedMetadata);

    long contentLength = 0;

    if (log.isTraceEnabled()) {
        log.trace("Put request with entity: " + requestEntity);
    }
    if (requestEntity != null) {
        ((HttpPut) httpMethod).setEntity(requestEntity);

        /* Explicitly apply any latent Content-Type header from the request entity to the
         * httpMethod to ensure it is included in the request signature, since it will be
         * included in the wire request by HttpClient. But only apply the latent mimetype
         * if an explicit Content-Type is not already set. See issue #109
         */
        if (requestEntity.getContentType() != null && httpMethod.getFirstHeader("Content-Type") == null) {
            httpMethod.setHeader(requestEntity.getContentType());
        }
    }

    HttpResponse result = performRequest(httpMethod, new int[] { 200, 204 });

    if (requestEntity != null) {
        // Respond with the actual guaranteed content length of the uploaded data.
        contentLength = ((HttpPut) httpMethod).getEntity().getContentLength();
    }

    if (autoRelease) {
        releaseConnection(result);
    }

    return new HttpResponseAndByteCount(result, contentLength);
}

From source file:org.opencastproject.kernel.security.TrustedHttpClientImpl.java

/**
 * Handles the necessary handshake for digest authenticaion in the case where it isn't a GET operation.
 * /* www. j  a v a 2  s  . c om*/
 * @param httpUriRequest
 *          The request location to get the digest authentication for.
 * @param httpClient
 *          The client to send the request through.
 * @throws TrustedHttpClientException
 *           Thrown if the client cannot be shutdown.
 */
private void manuallyHandleDigestAuthentication(HttpUriRequest httpUriRequest, HttpClient httpClient)
        throws TrustedHttpClientException {
    HttpRequestBase digestRequest;
    try {
        digestRequest = (HttpRequestBase) httpUriRequest.getClass().newInstance();
    } catch (Exception e) {
        throw new IllegalStateException("Can not create a new " + httpUriRequest.getClass().getName());
    }
    digestRequest.setURI(httpUriRequest.getURI());
    digestRequest.setHeader(REQUESTED_AUTH_HEADER, DIGEST_AUTH);
    String[] realmAndNonce = getRealmAndNonce(digestRequest);

    if (realmAndNonce != null) {
        // Set the user/pass
        UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, pass);

        // Set up the digest authentication with the required values
        DigestScheme digestAuth = new DigestScheme();
        digestAuth.overrideParamter("realm", realmAndNonce[0]);
        digestAuth.overrideParamter("nonce", realmAndNonce[1]);

        // Add the authentication header
        try {
            httpUriRequest.setHeader(digestAuth.authenticate(creds, httpUriRequest));
        } catch (Exception e) {
            // close the http connection(s)
            httpClient.getConnectionManager().shutdown();
            throw new TrustedHttpClientException(e);
        }
    }
}