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;
019
020
021import net.jcip.annotations.Immutable;
022
023import com.nimbusds.common.contenttype.ContentType;
024import com.nimbusds.oauth2.sdk.http.HTTPResponse;
025
026
027/**
028 * Pushed authorisation error response.
029 *
030 * <p>Example HTTP response:
031 *
032 * <pre>
033 * HTTP/1.1 400 Bad Request
034 * Content-Type: application/json
035 * Cache-Control: no-cache, no-store
036 *
037 * {
038 *  "error ": "invalid_request",
039 *  "error_description" : "The redirect_uri is not valid for the given client"
040 * }
041 * </pre>
042 *
043 * <p>Related specifications:
044 *
045 * <ul>
046 *     <li>OAuth 2.0 Pushed Authorization Requests (RFC 9126)
047 * </ul>
048 */
049@Immutable
050public class PushedAuthorizationErrorResponse extends PushedAuthorizationResponse implements ErrorResponse {
051        
052        
053        /**
054         * The error.
055         */
056        private final ErrorObject error;
057        
058        
059        /**
060         * Creates a new pushed authorisation error response.
061         *
062         * @param error The error. Must not be {@code null}.
063         */
064        public PushedAuthorizationErrorResponse(final ErrorObject error) {
065                
066                if (error == null)
067                        throw new IllegalArgumentException("The error must not be null");
068                
069                this.error = error;
070        }
071        
072        
073        @Override
074        public boolean indicatesSuccess() {
075                return false;
076        }
077        
078        
079        @Override
080        public ErrorObject getErrorObject() {
081                return error;
082        }
083        
084        
085        @Override
086        public HTTPResponse toHTTPResponse() {
087                return getErrorObject().toHTTPResponse();
088        }
089        
090        
091        /**
092         * Parses a pushed authorisation error response from the specified HTTP
093         * response.
094         *
095         * @param httpResponse The HTTP response. Must not be {@code null}.
096         *
097         * @return The pushed authorisation error response.
098         *
099         * @throws ParseException If the HTTP response couldn't be parsed to a
100         *                        pushed authorisation error response.
101         */
102        public static PushedAuthorizationErrorResponse parse(final HTTPResponse httpResponse)
103                throws ParseException {
104                
105                int statusCode = httpResponse.getStatusCode();
106                
107                if (statusCode == HTTPResponse.SC_CREATED || statusCode == HTTPResponse.SC_OK) {
108                        throw new ParseException("The HTTP status code must be other than 201 and 200");
109                }
110                
111                ErrorObject errorObject;
112                if (httpResponse.getEntityContentType() != null && ContentType.APPLICATION_JSON.matches(httpResponse.getEntityContentType())) {
113                        errorObject = ErrorObject.parse(httpResponse.getContentAsJSONObject());
114                } else {
115                        errorObject = new ErrorObject(null);
116                }
117                
118                return new PushedAuthorizationErrorResponse(errorObject.setHTTPStatusCode(statusCode));
119        }
120}