001    package com.nimbusds.oauth2.sdk.client;
002    
003    
004    import net.jcip.annotations.Immutable;
005    
006    import com.nimbusds.oauth2.sdk.ParseException;
007    import com.nimbusds.oauth2.sdk.SuccessResponse;
008    import com.nimbusds.oauth2.sdk.http.CommonContentTypes;
009    import com.nimbusds.oauth2.sdk.http.HTTPResponse;
010    
011    
012    /**
013     * Client information response. This class is immutable.
014     *
015     * <p>Example HTTP response:
016     *
017     * <pre>
018     * HTTP/1.1 200 OK
019     * Content-Type: application/json
020     * Cache-Control: no-store
021     * Pragma: no-cache
022     *
023     * {
024     *  "registration_access_token"  : "reg-23410913-abewfq.123483",
025     *  "registration_client_uri"    : "https://server.example.com/register/s6BhdRkqt3",
026     *  "client_id"                  : "s6BhdRkqt3",
027     *  "client_secret"              : "cf136dc3c1fc93f31185e5885805d",
028     *  "client_id_issued_at"        : 2893256800
029     *  "client_secret_expires_at"   : 2893276800
030     *  "client_name"                : "My Example Client",
031     *  "client_name#ja-Jpan-JP"     : "\u30AF\u30E9\u30A4\u30A2\u30F3\u30C8\u540D",
032     *  "redirect_uris"              : ["https://client.example.org/callback",
033     *                                  "https://client.example.org/callback2"]
034     *  "scope"                      : "read write dolphin",
035     *  "grant_types"                : ["authorization_code", "refresh_token"]
036     *  "token_endpoint_auth_method" : "client_secret_basic",
037     *  "logo_uri"                   : "https://client.example.org/logo.png",
038     *  "jwks_uri"                   : "https://client.example.org/my_public_keys.jwks"
039     * }
040     * </pre>
041     *
042     * <p>Related specifications:
043     *
044     * <ul>
045     *     <li>OAuth 2.0 Dynamic Client Registration Protocol 
046     *         (draft-ietf-oauth-dyn-reg-12), section 5.1.
047     * </ul>
048     *
049     * @author Vladimir Dzhuvinov
050     */
051    @Immutable
052    public class ClientInformationResponse 
053            extends ClientRegistrationResponse
054            implements SuccessResponse {
055    
056    
057            /**
058             * The client information.
059             */
060            private ClientInformation clientInfo;
061    
062    
063            /**
064             * Creates a new client information response.
065             *
066             * @param clientInfo The client information. Must not be {@code null}.
067             */
068            public ClientInformationResponse(final ClientInformation clientInfo) {
069    
070                    if (clientInfo == null)
071                            throw new IllegalArgumentException("The client information must not be null");
072    
073                    this.clientInfo = clientInfo;
074            }
075    
076    
077            /**
078             * Gets the client information.
079             *
080             * @return The client information.
081             */
082            public ClientInformation getClientInformation() {
083    
084                    return clientInfo;
085            }
086    
087    
088            @Override
089            public HTTPResponse toHTTPResponse() {
090            
091                    HTTPResponse httpResponse = new HTTPResponse(HTTPResponse.SC_OK);
092                    httpResponse.setContentType(CommonContentTypes.APPLICATION_JSON);
093                    httpResponse.setCacheControl("no-store");
094                    httpResponse.setPragma("no-cache");
095                    httpResponse.setContent(clientInfo.toJSONObject().toString());
096                    return httpResponse;
097            }
098    
099    
100            /**
101             * Parses a client information response from the specified 
102             * HTTP response.
103             *
104             * @param httpResponse The HTTP response. Must not be {@code null}.
105             *
106             * @return The client information response.
107             *
108             * @throws ParseException If the HTTP response couldn't be parsed to a
109             *                        client information response.
110             */
111            public static ClientInformationResponse parse(final HTTPResponse httpResponse)
112                    throws ParseException {
113    
114                    httpResponse.ensureStatusCode(HTTPResponse.SC_OK);
115                    ClientInformation clientInfo = ClientInformation.parse(httpResponse.getContentAsJSONObject());
116                    return new ClientInformationResponse(clientInfo);
117            }
118    }