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

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

Introduction

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

Prototype

public void setHeader(String str, String str2) 

Source Link

Usage

From source file:com.microsoft.office365.meetingmgr.HttpHelper.java

@Override
protected CloseableHttpResponse executeRequest(HttpRequestBaseHC4 httpRequest)
        throws ExecutionException, InterruptedException, IOException {
    User user = Manager.Instance.getUser();
    if (user != null) {
        httpRequest.setHeader("AnchorMailbox", user.id);
    }// ww  w .  ja  va2s.co m

    setAuthHeader(httpRequest, false);
    CloseableHttpResponse response = super.executeRequest(httpRequest);

    if (needsTokenRefresh(response)) {
        setAuthHeader(httpRequest, true);

        // Repeat failed request with refreshed token
        response = super.executeRequest(httpRequest);
    }

    return response;
}

From source file:com.microsoft.office365.meetingmgr.HttpHelperBase.java

private <TBody, TResult> TResult doHttp(HttpRequestBaseHC4 httpRequest, TBody body, Class<TResult> clazz) {
    String requestBody;//  w  ww.  j ava2 s  .c  om
    String requestString;

    if (body == null) {
        requestString = requestBody = null;
    } else if (body instanceof String) {
        requestString = requestBody = (String) body;
        httpRequest.setHeader("Content-Type", "application/x-www-form-urlencoded");
    } else {
        requestBody = mJson.serialize(body);
        requestString = mJson.prettyPrint(body);
        httpRequest.setHeader("Content-Type", "application/json");
    }

    traceRequest(httpRequest, requestString);

    try {
        if (requestBody != null) {
            HttpEntity entityBody = new ByteArrayEntity(requestBody.getBytes(CHARSET_NAME));
            ((HttpEntityEnclosingRequestBaseHC4) httpRequest).setEntity(entityBody);
        }

        try (CloseableHttpResponse result = executeRequest(httpRequest)) {
            return getResult(result, clazz);
        }
    } catch (IOException | ExecutionException | InterruptedException e) {
        ErrorLogger.log(e);
    }
    return null;
}

From source file:com.microsoft.office365.meetingmgr.HttpHelper.java

private void setAuthHeader(HttpRequestBaseHC4 httpRequest, boolean refreshToken) {
    String uri = httpRequest.getURI().toString();
    String resourceId = resourceIdFromUri(uri);

    if (resourceId != null) {
        String accessToken = getAccessToken(resourceId, refreshToken);
        httpRequest.setHeader("Authorization", "Bearer " + accessToken);
    }//from   w  ww .  j ava2  s .  c  o m
}