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 * 019 * @author Vladimir Dzhuvinov 020 */ 021public abstract class TokenResponse implements Response { 022 023 024 /** 025 * Parses a token response from the specified JSON object. 026 * 027 * @param jsonObject The JSON object to parse. Must not be 028 * @code null}. 029 * 030 * @return The access token or token error response. 031 * 032 * @throws ParseException If the JSON object couldn't be parsed to a 033 * token response. 034 */ 035 public static TokenResponse parse(JSONObject jsonObject) 036 throws ParseException{ 037 038 if (jsonObject.containsKey("access_token")) 039 return AccessTokenResponse.parse(jsonObject); 040 else 041 return TokenErrorResponse.parse(jsonObject); 042 } 043 044 045 /** 046 * Parses a token response from the specified HTTP response. 047 * 048 * @param httpResponse The HTTP response. Must not be {@code null}. 049 * 050 * @return The access token or token error response. 051 * 052 * @throws ParseException If the HTTP response couldn't be parsed to a 053 * token response. 054 */ 055 public static TokenResponse parse(final HTTPResponse httpResponse) 056 throws ParseException { 057 058 if (httpResponse.getStatusCode() == HTTPResponse.SC_OK) 059 return AccessTokenResponse.parse(httpResponse); 060 else 061 return TokenErrorResponse.parse(httpResponse); 062 } 063}