001/* 002 * oauth2-oidc-sdk 003 * 004 * Copyright 2012-2020, 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.api; 019 020 021import net.jcip.annotations.Immutable; 022import net.minidev.json.JSONObject; 023 024import com.nimbusds.oauth2.sdk.ErrorObject; 025import com.nimbusds.oauth2.sdk.ParseException; 026import com.nimbusds.oauth2.sdk.http.HTTPResponse; 027 028 029/** 030 * Federation API error. 031 * 032 * <ul> 033 * <li>OpenID Connect Federation 1.0, section 7.5. 034 * </ul> 035 */ 036@Immutable 037public class FederationAPIError extends ErrorObject { 038 039 040 private static final long serialVersionUID = 2116693118039606386L; 041 042 043 /** 044 * Creates a new federation API error. 045 * 046 * @param code The error code, {@code null} if not specified. 047 * @param description The error description, {@code null} if not 048 * specified. 049 */ 050 public FederationAPIError(final String code, final String description) { 051 this(code, description, 0); 052 } 053 054 055 /** 056 * Creates a new federation API error. 057 * 058 * @param code The error code, {@code null} if not specified. 059 * @param description The error description, {@code null} if not 060 * specified. 061 * @param httpStatusCode The HTTP status code, zero if not specified. 062 */ 063 public FederationAPIError(final String code, 064 final String description, 065 final int httpStatusCode) { 066 super(code, description, httpStatusCode); 067 } 068 069 070 /** 071 * Returns a copy of this federation API error with the specified HTTP 072 * status code. 073 * 074 * @param httpStatusCode The HTTP status code, zero if not specified. 075 * 076 * @return The new federation API error. 077 */ 078 public FederationAPIError withStatusCode(final int httpStatusCode) { 079 return new FederationAPIError(getCode(), getDescription(), httpStatusCode); 080 } 081 082 083 /** 084 * Parses a federation API error object from the specified JSON object. 085 * 086 * @param jsonObject The JSON object to parse. Must not be 087 * {@code null}. 088 * 089 * @return The federation API error object. 090 */ 091 public static FederationAPIError parse(final JSONObject jsonObject) { 092 ErrorObject errorObject = ErrorObject.parse(jsonObject); 093 return new FederationAPIError(errorObject.getCode(), errorObject.getDescription()); 094 } 095 096 097 /** 098 * Parses a federation API error object from the specified HTTP 099 * response. 100 * 101 * @param httpResponse The HTTP response to parse. Must not be 102 * {@code null}. 103 * 104 * @return The federation API error object. 105 */ 106 public static FederationAPIError parse(final HTTPResponse httpResponse) { 107 JSONObject jsonObject; 108 try { 109 jsonObject = httpResponse.getContentAsJSONObject(); 110 } catch (ParseException e) { 111 jsonObject = new JSONObject(); 112 } 113 return FederationAPIError.parse(jsonObject).withStatusCode(httpResponse.getStatusCode()); 114 } 115}