001package com.nimbusds.oauth2.sdk;
002
003
004import net.minidev.json.JSONObject;
005
006import com.nimbusds.oauth2.sdk.http.HTTPResponse;
007
008
009/**
010 * Token endpoint response. This is the base abstract class for access token
011 * (success) and token error responses.
012 *
013 * <p>Related specifications:
014 *
015 * <ul>
016 *     <li>OAuth 2.0 (RFC 6749), section 3.2.
017 * </ul>
018 */
019public abstract class TokenResponse implements Response {
020
021
022        /**
023         * Parses a token response from the specified JSON object.
024         *
025         * @param jsonObject The JSON object to parse. Must not be 
026         *                   @code null}.
027         *
028         * @return The access token or token error response.
029         *
030         * @throws ParseException If the JSON object couldn't be parsed to a
031         *                        token response.
032         */
033        public static TokenResponse parse(JSONObject jsonObject)
034                throws ParseException{
035
036                if (jsonObject.containsKey("access_token"))
037                        return AccessTokenResponse.parse(jsonObject);
038                else
039                        return TokenErrorResponse.parse(jsonObject);
040        }
041
042
043        /**
044         * Parses a token response from the specified HTTP response.
045         *
046         * @param httpResponse The HTTP response. Must not be {@code null}.
047         *
048         * @return The access token or token error response.
049         *
050         * @throws ParseException If the HTTP response couldn't be parsed to a 
051         *                        token response.
052         */
053        public static TokenResponse parse(final HTTPResponse httpResponse)
054                throws ParseException {
055                
056                if (httpResponse.getStatusCode() ==  HTTPResponse.SC_OK)
057                        return AccessTokenResponse.parse(httpResponse);
058                else
059                        return TokenErrorResponse.parse(httpResponse);
060        }
061}