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