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.federation.config;
019
020
021import com.nimbusds.common.contenttype.ContentType;
022import com.nimbusds.oauth2.sdk.ErrorObject;
023import com.nimbusds.oauth2.sdk.ErrorResponse;
024import com.nimbusds.oauth2.sdk.ParseException;
025import com.nimbusds.oauth2.sdk.http.HTTPResponse;
026
027
028/**
029 * Federation entity configuration error response.
030 *
031 * <p>Related specifications:
032 *
033 * <ul>
034 *     <li>OpenID Connect Federation 1.0, section 5.2.
035 * </ul>
036 */
037public class FederationEntityConfigurationErrorResponse extends FederationEntityConfigurationResponse implements ErrorResponse {
038        
039        
040        /**
041         * The error.
042         */
043        private final ErrorObject error;
044        
045        
046        /**
047         * Creates a new federation entity configuration error response.
048         *
049         * @param error The error. Must not be {@code null}.
050         */
051        public FederationEntityConfigurationErrorResponse(final ErrorObject error) {
052                if (error == null) {
053                        throw new IllegalArgumentException("The error must not be null");
054                }
055                this.error = error;
056        }
057        
058        
059        @Override
060        public boolean indicatesSuccess() {
061                return false;
062        }
063        
064        
065        @Override
066        public ErrorObject getErrorObject() {
067                return error;
068        }
069        
070        
071        @Override
072        public HTTPResponse toHTTPResponse() {
073                return getErrorObject().toHTTPResponse();
074        }
075        
076        
077        /**
078         * Parses a federation entity configuration error response from the
079         * specified HTTP response.
080         *
081         * @param httpResponse The HTTP response. Must not be {@code null}.
082         *
083         * @return The federation entity configuration error response.
084         *
085         * @throws ParseException If the HTTP response couldn't be parsed to a
086         *                        federation entity configuration error
087         *                        response.
088         */
089        public static FederationEntityConfigurationErrorResponse parse(final HTTPResponse httpResponse)
090                throws ParseException {
091                
092                httpResponse.ensureStatusCodeNotOK();
093                
094                ErrorObject errorObject;
095                if (httpResponse.getEntityContentType() != null && ContentType.APPLICATION_JSON.matches(httpResponse.getEntityContentType())) {
096                        errorObject = ErrorObject.parse(httpResponse.getContentAsJSONObject());
097                } else {
098                        errorObject = new ErrorObject(null);
099                }
100                
101                errorObject = errorObject.setHTTPStatusCode(httpResponse.getStatusCode());
102                
103                return new FederationEntityConfigurationErrorResponse(errorObject);
104        }
105}