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

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

Introduction

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

Prototype

public void setHeader(String str, String str2) 

Source Link

Usage

From source file:com.galenframework.ide.tests.integration.controllers.ApiTestBase.java

private Response executeRequest(HttpRequestBase httpRequestBase) throws IOException {
    httpRequestBase.setHeader("Content-Type", "application/json");
    httpRequestBase.setHeader("Cookie", MockedWebApp.MOCK_KEY_COOKIE_NAME + "=" + mockUniqueKey);
    HttpResponse response = client.execute(httpRequestBase);
    int code = response.getStatusLine().getStatusCode();
    String textResponse = IOUtils.toString(response.getEntity().getContent());
    return new Response(code, textResponse);
}

From source file:io.kamax.mxisd.backend.wordpress.WordpressRestBackend.java

protected CloseableHttpResponse runRequest(HttpRequestBase request) throws IOException {
    request.setHeader("Authorization", "Bearer " + token);
    return client.execute(request);
}

From source file:org.eclipse.mylyn.internal.bugzilla.rest.core.BugzillaRestAuthenticatedGetRequest.java

@Override
protected HttpRequestBase createHttpRequestBase() {
    String bugUrl = getUrlSuffix();
    LoginToken token = ((BugzillaRestHttpClient) getClient()).getLoginToken();
    if ((!(this instanceof BugzillaRestValidateRequest)
            && !(this instanceof BugzillaRestUnauthenticatedGetRequest)) && token != null
            && bugUrl.length() > 0) {
        if (!bugUrl.endsWith("?")) {
            bugUrl += "&";
        }//from w  w  w.jav  a2 s.  c om
        bugUrl += "token=" + token.getToken();
    }
    HttpRequestBase request = new HttpGet(baseUrl() + bugUrl);
    request.setHeader(CONTENT_TYPE, TEXT_XML_CHARSET_UTF_8);
    request.setHeader(ACCEPT, APPLICATION_JSON);
    return request;
}

From source file:com.comcast.cns.io.HTTPEndpointSyncPublisher.java

private void composeHeader(HttpRequestBase httpRequest) {

    httpRequest.setHeader("x-amz-sns-message-type", this.getMessageType());
    httpRequest.setHeader("x-amz-sns-message-id", this.getMessageId());
    httpRequest.setHeader("x-amz-sns-topic-arn", this.getTopicArn());
    httpRequest.setHeader("x-amz-sns-subscription-arn", this.getSubscriptionArn());
    httpRequest.setHeader("User-Agent", "Cloud Notification Service Agent");

    if (this.getRawMessageDelivery()) {
        httpRequest.addHeader("x-amz-raw-message", "true");
    }/*from   w  ww .j a  v a  2  s . co  m*/
}

From source file:com.payu.sdk.helper.HttpClientHelper.java

/**
 * Creates a http request with the given request
 *
 * @param request/*from w  ww . j a  va 2  s . c  om*/
 *            The original request
 * @param requestMethod
 *            The request method to be sent to the server
 * @return The created http request
 * @throws URISyntaxException
 * @throws UnsupportedEncodingException
 * @throws PayUException
 * @throws ConnectionException
 */
private static HttpRequestBase createHttpRequest(Request request, RequestMethod requestMethod)
        throws URISyntaxException, UnsupportedEncodingException, PayUException, ConnectionException {

    String url = request.getRequestUrl(requestMethod);

    URI postUrl = new URI(url);

    HttpRequestBase httpMethod;

    LoggerUtil.debug("sending request...");

    String xml = Constants.EMPTY_STRING;

    switch (requestMethod) {
    case POST:
        httpMethod = new HttpPost();
        break;
    case GET:
        httpMethod = new HttpGet();
        break;
    case DELETE:
        httpMethod = new HttpDelete();
        break;
    case PUT:
        httpMethod = new HttpPut();
        break;
    default:
        throw new ConnectionException("Invalid connection method");
    }

    httpMethod.addHeader("Content-Type", MediaType.XML.getCode() + "; charset=utf-8");

    Language lng = request.getLanguage() != null ? request.getLanguage() : PayU.language;
    httpMethod.setHeader("Accept-Language", lng.name());

    // Sets the method entity
    if (httpMethod instanceof HttpEntityEnclosingRequestBase) {
        xml = request.toXml();
        ((HttpEntityEnclosingRequestBase) httpMethod)
                .setEntity(new StringEntity(xml, Constants.DEFAULT_ENCODING));
        LoggerUtil.debug("Message to send:\n {0}", xml);
    }

    httpMethod.setURI(postUrl);

    addRequestHeaders(request, httpMethod);

    LoggerUtil.debug("URL to send:\n {0}", url);

    return httpMethod;
}

From source file:org.aerogear.android.impl.core.HttpRestProvider.java

private HeaderAndBody execute(HttpRequestBase method) throws IOException {
    method.setHeader("Accept", "application/json");
    method.setHeader("Content-type", "application/json");

    for (Entry<String, String> entry : defaultHeaders.entrySet()) {
        method.setHeader(entry.getKey(), entry.getValue());
    }//from w ww  .j a  v  a 2 s. c  om

    HttpResponse response = client.execute(method);

    int statusCode = response.getStatusLine().getStatusCode();
    byte[] data = EntityUtils.toByteArray(response.getEntity());
    response.getEntity().consumeContent();
    if (statusCode != 200) {
        throw new HttpException(data, statusCode);
    }

    Header[] headers = response.getAllHeaders();
    HeaderAndBody result = new HeaderAndBody(data, new HashMap<String, Object>(headers.length));

    for (Header header : headers) {
        result.setHeader(header.getName(), header.getValue());
    }

    return result;
}

From source file:com.cloudera.livy.client.http.LivyConnection.java

private <V> V sendJSONRequest(HttpRequestBase req, Class<V> retType, String uri, Object... uriParams)
        throws Exception {
    req.setHeader(HttpHeaders.ACCEPT, APPLICATION_JSON);
    req.setHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON);
    req.setHeader(HttpHeaders.CONTENT_ENCODING, "UTF-8");
    return sendRequest(req, retType, uri, uriParams);
}

From source file:co.cask.cdap.client.rest.RestClient.java

/**
 * Method for execute HttpRequest with authorized headers, if need.
 *
 * @param request {@link HttpRequestBase} initiated http request with entity, headers, request uri and all another
 *                required properties for successfully request
 * @return {@link CloseableHttpResponse} as a result of http request execution.
 * @throws IOException in case of a problem or the connection was aborted
 *///from  w ww . ja v a  2  s  .c  o  m
public CloseableHttpResponse execute(HttpRequestBase request) throws IOException {
    if (config.isAuthEnabled()) {
        request.setHeader(HTTP_HEADER_AUTHORIZATION, config.getAuthTokenType() + " " + config.getAuthToken());
    }
    if (StringUtils.isNotEmpty(config.getAPIKey())) {
        request.setHeader(CONTINUUITY_API_KEY_HEADER_NAME, config.getAPIKey());
    }
    LOG.debug("Execute Http Request: {}", request);
    return httpClient.execute(request);
}

From source file:com.github.tmyroadctfig.icloud4j.IdmsaService.java

/**
 * Populates the HTTP request headers for idmsa requests.
 *
 * @param request the request to populate.
 *//*  w w  w  .j a  v a 2  s . co m*/
public void populateIdmsaRequestHeadersParameters(HttpRequestBase request) {
    request.setHeader("Origin", idmsaEndPoint);
    request.setHeader("Referer", idmsaEndPoint + "/");
    request.setHeader("User-Agent",
            "Mozilla/5.0 (iPad; CPU OS 9_3_4 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13G35 Safari/601.1");

    // Seems to be required to get a session token
    request.setHeader("X-Apple-Widget-Key", "83545bf919730e51dbfba24e7e8a78d2");

    if (!Strings.isNullOrEmpty(appleIdSessionId)) {
        request.setHeader("X-Apple-ID-Session-Id", appleIdSessionId);
    }

    if (!Strings.isNullOrEmpty(scnt)) {
        request.setHeader("scnt", scnt);
    }
}

From source file:com.dnastack.bob.service.processor.impl.BeaconizerBeaconProcessor.java

@Override
@Asynchronous/*from w w  w  .ja va  2 s  .  c  om*/
public Future<String> getQueryResponse(Beacon beacon, Query query) {
    String res = null;

    // should be POST, but the server accepts GET as well
    try {
        HttpRequestBase request = createRequest(getQueryUrl(beacon.getId(), query.getChromosome().toString(),
                query.getPosition(), query.getAllele()), false, null);
        request.setHeader("Accept", "application/json");
        res = executeRequest(request);
    } catch (MalformedURLException | UnsupportedEncodingException ex) {
        // ignore, already null
    }

    return new AsyncResult<>(res);
}