Example usage for org.apache.commons.imaging.util IoUtils getInputStreamBytes

List of usage examples for org.apache.commons.imaging.util IoUtils getInputStreamBytes

Introduction

In this page you can find the example usage for org.apache.commons.imaging.util IoUtils getInputStreamBytes.

Prototype

public static byte[] getInputStreamBytes(InputStream is) throws IOException 

Source Link

Document

Reads an InputStream to the end.

Usage

From source file:ru.tiis.library.service.impl.GDriveService.java

public static boolean isCredentialValid(String gdRedirectUriHost) throws IOException {
    init();//from  w  ww.  ja v  a 2s .co  m

    GoogleAuthorizationCodeFlow flow = buildAuthorizationFlow();
    Credential currentCredential = flow.loadCredential("user");
    if (null == currentCredential) {
        return false;
    }

    boolean isCredentialValid = false;
    String tokenInfoURL = String.format("https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=%s",
            currentCredential.getAccessToken());
    HttpResponse response = httpTransport.createRequestFactory().buildGetRequest(new GenericUrl(tokenInfoURL))
            .execute();
    System.out.println("Token info response: status=" + response.getStatusCode() + ", "
            + response.getStatusMessage() + ";\n content=" + response.getContent());

    // example response:
    //{
    //    "issued_to": "<client-app-id>",
    //    "audience": "<client-app-id>",
    //    "scope": "https://www.googleapis.com/auth/drive.file",
    //    "expires_in": 3433,
    //    "access_type": "online"
    //}

    if (response.isSuccessStatusCode()) {
        try {
            String responseContentString = new String(IoUtils.getInputStreamBytes(response.getContent()));

            JSONObject responseJson = JSONFactoryUtil.createJSONObject(responseContentString);
            String refreshToken = responseJson.getString("refresh_token");
            int expiresIn = responseJson.getInt("expires_in");

            if (refreshToken != null || expiresIn > 60) {
                isCredentialValid = true;
            }
        } catch (JSONException e) {
            log.error(e);
        }
    }

    return isCredentialValid;
}