001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2016, Connect2id Ltd and contributors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.oauth2.sdk.client;
019
020
021import com.nimbusds.oauth2.sdk.ParseException;
022import com.nimbusds.oauth2.sdk.Response;
023import com.nimbusds.oauth2.sdk.http.HTTPResponse;
024
025
026/**
027 * The base abstract for client registration responses.
028 *
029 * <p>Related specifications:
030 *
031 * <ul>
032 *     <li>OAuth 2.0 Dynamic Client Registration Protocol (RFC 7591)
033 * </ul>
034 */
035public abstract class ClientRegistrationResponse implements Response {
036        
037        
038        /**
039         * Casts this response to a client information response.
040         *
041         * @return The client information response.
042         */
043        public ClientInformationResponse toSuccessResponse() {
044                
045                return (ClientInformationResponse) this;
046        }
047        
048        
049        /**
050         * Casts this response to a client registration error response.
051         *
052         * @return The client registration error response.
053         */
054        public ClientRegistrationErrorResponse toErrorResponse() {
055                
056                return (ClientRegistrationErrorResponse) this;
057        }
058
059
060        /**
061         * Parses a client registration response from the specified HTTP 
062         * response.
063         *
064         * @param httpResponse The HTTP response. Must not be {@code null}.
065         *
066         * @return The client registration response.
067         *
068         * @throws ParseException If the HTTP response couldn't be parsed to a
069         *                        client registration response.
070         */
071        public static ClientRegistrationResponse parse(final HTTPResponse httpResponse)
072                throws ParseException {
073                
074                if (httpResponse.getStatusCode() == HTTPResponse.SC_CREATED ||
075                    httpResponse.getStatusCode() == HTTPResponse.SC_OK) {
076
077                        return ClientInformationResponse.parse(httpResponse);
078
079                } else {
080
081                        return ClientRegistrationErrorResponse.parse(httpResponse);
082                }
083        }
084}