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.openid.connect.sdk;
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 class for UserInfo success and error responses.
028 *
029 * <p>Related specifications:
030 *
031 * <ul>
032 *     <li>OpenID Connect Core 1.0, section 5.3.2 and 5.3.3.
033 *     <li>OAuth 2.0 Bearer Token Usage (RFC 6750), section 3.1.
034 * </ul>
035 */
036public abstract class UserInfoResponse implements Response {
037        
038        
039        /**
040         * Casts this response to a UserInfo success response.
041         *
042         * @return The UserInfo success response.
043         */
044        public UserInfoSuccessResponse toSuccessResponse() {
045                return (UserInfoSuccessResponse) this;
046        }
047        
048        
049        /**
050         * Casts this response to a UserInfo error response.
051         *
052         * @return The UserInfo error response.
053         */
054        public UserInfoErrorResponse toErrorResponse() {
055                return (UserInfoErrorResponse) this;
056        }
057
058
059        /**
060         * Parses a UserInfo response from the specified HTTP response.
061         *
062         * @param httpResponse The HTTP response. Must not be {@code null}.
063         *
064         * @return The UserInfo success or error response.
065         *
066         * @throws ParseException If the HTTP response couldn't be parsed to a 
067         *                        UserInfo response.
068         */
069        public static UserInfoResponse parse(final HTTPResponse httpResponse)
070                throws ParseException {
071                
072                if (httpResponse.getStatusCode() == HTTPResponse.SC_OK)
073                        return UserInfoSuccessResponse.parse(httpResponse);
074                else
075                        return UserInfoErrorResponse.parse(httpResponse);
076        }
077}