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.oauth2.sdk.http.CommonContentTypes;
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 401 Unauthorized
034 * Date: Tue, 2 May 2017 15:22:31 GMT
035 * </pre>
036 *
037 * <p>Related specifications:
038 *
039 * <ul>
040 *     <li>OAuth 2.0 Pushed Authorization Requests
041 *         (draft-lodderstedt-oauth-par-01)
042 * </ul>
043 */
044@Immutable
045public class PushedAuthorizationErrorResponse extends PushedAuthorizationResponse implements ErrorResponse {
046        
047        
048        /**
049         * The error.
050         */
051        private final ErrorObject error;
052        
053        
054        /**
055         * Creates a new pushed authorisation error response.
056         *
057         * @param error The error. Must not be {@code null}.
058         */
059        public PushedAuthorizationErrorResponse(final ErrorObject error) {
060                
061                if (error == null)
062                        throw new IllegalArgumentException("The error must not be null");
063                
064                this.error = error;
065        }
066        
067        
068        @Override
069        public boolean indicatesSuccess() {
070                return false;
071        }
072        
073        
074        @Override
075        public ErrorObject getErrorObject() {
076                return error;
077        }
078        
079        
080        @Override
081        public HTTPResponse toHTTPResponse() {
082                
083                int statusCode = (error.getHTTPStatusCode() > 0) ? error.getHTTPStatusCode() : HTTPResponse.SC_BAD_REQUEST;
084                HTTPResponse httpResponse = new HTTPResponse(statusCode);
085                httpResponse.setCacheControl("no-store");
086                httpResponse.setPragma("no-cache");
087                
088                if (getErrorObject().getCode() != null) {
089                        httpResponse.setContentType(CommonContentTypes.APPLICATION_JSON);
090                        httpResponse.setContent(getErrorObject().toJSONObject().toJSONString());
091                }
092                
093                return httpResponse;
094        }
095        
096        
097        /**
098         * Parses a pushed authorisation error response from the specified HTTP
099         * response.
100         *
101         * @param httpResponse The HTTP response. Must not be {@code null}.
102         *
103         * @return The pushed authorisation error response.
104         *
105         * @throws ParseException If the HTTP response couldn't be parsed to a
106         *                        pushed authorisation error response.
107         */
108        public static PushedAuthorizationErrorResponse parse(final HTTPResponse httpResponse)
109                throws ParseException {
110                
111                int statusCode = httpResponse.getStatusCode();
112                
113                if (statusCode == HTTPResponse.SC_CREATED || statusCode == HTTPResponse.SC_OK) {
114                        throw new ParseException("The HTTP status code must be other than 201 and 200");
115                }
116                
117                ErrorObject errorObject;
118                if (httpResponse.getContentType() != null && CommonContentTypes.APPLICATION_JSON.getBaseType().equals(httpResponse.getContentType().getBaseType())) {
119                        errorObject = ErrorObject.parse(httpResponse.getContentAsJSONObject());
120                } else {
121                        errorObject = new ErrorObject(null);
122                }
123                
124                return new PushedAuthorizationErrorResponse(errorObject);
125        }
126}