Code example for HttpResponse

Methods: getEntity, getFirstHeader, getStatusLine

0
        throws JSONException, InvalidRequestException, 
               InvalidClientException, InvalidGrantException, 
               UnauthorizedClientException, UnsupportedGrantTypeException, 
               InvalidScopeException, OAuthException { 
 
        StatusLine statusLine = this.httpResponse.getStatusLine();
        this.statusCode = statusLine.getStatusCode();
        try { 
            getStringEntity(); 
        } catch (IOException ex) {
            Logger.getLogger(Response.class.getName()).log(Level.SEVERE, null, ex);
        } 
 
        if (parseStringEntityForJson()) { 
            JsonParser.parseForErrors(jsonResponse);
        } else if (parseStringEntityForParameters()) { 
        } 
 
        switch (statusCode) {
            case (200): { 
                return true; 
            } 
            case (401): { 
                OAuthException unauthorizedClientException = new UnauthorizedClientException(statusLine.getReasonPhrase());
                unauthorizedClientException.setErrorDescription(this.httpResponse.getFirstHeader("WWW-Authenticate").getValue());
                return false; 
            } 
        } 
        return true; 
    } 
 
    public void parseForTokens(TokenTypeDefinition tokenTypeDefinition) throws InvalidTokenTypeException {
        this.accessToken = JsonParser.parseForAccessToken(jsonResponse, tokenTypeDefinition);
        this.refreshToken = JsonParser.parseForRefreshToken(jsonResponse);
    } 
 
    private void getStringEntity() 
        throws IOException { 
        HttpEntity entity = httpResponse.getEntity();
        this.stringResponse = EntityUtils.toString(entity, EntityUtils.getContentCharSet(entity));
    } 
 
    private boolean parseStringEntityForJson() { 
        if (httpResponse.getFirstHeader("Content-Type").getValue().equalsIgnoreCase("application/json")) {
            if (stringResponse != null && !stringResponse.isEmpty()) {
                try { 
                    jsonResponse = new JSONObject(stringResponse);
                } catch (JSONException ex) {
                    return false; 
                } 
                return true; 
            } 
        } 
        return false; 
    } 
 
    private boolean parseStringEntityForParameters() 
        throws JSONException, InvalidRequestException, 
               InvalidClientException, InvalidGrantException, 
               UnauthorizedClientException, UnsupportedGrantTypeException, 
               InvalidScopeException, OAuthException { 
        if (httpResponse.getFirstHeader("Content-Type").getValue().equalsIgnoreCase("application/x-www-form-urlencoded")) {
            if (stringResponse != null && !stringResponse.isEmpty()) {
                this.parameterList = StringParser.parseForParameters(stringResponse);
            } 
        } 
        return this.parameterList != null;