001/**
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 2008 Sun Microsystems Inc. All Rights Reserved
005 *
006 * The contents of this file are subject to the terms
007 * of the Common Development and Distribution License
008 * (the License). You may not use this file except in
009 * compliance with the License.
010 *
011 * You can obtain a copy of the License at
012 * https://opensso.dev.java.net/public/CDDLv1.0.html or
013 * opensso/legal/CDDLv1.0.txt
014 * See the License for the specific language governing
015 * permission and limitations under the License.
016 *
017 * When distributing Covered Code, include this CDDL
018 * Header Notice in each file and include the License file
019 * at opensso/legal/CDDLv1.0.txt.
020 * If applicable, add the following below the CDDL Header,
021 * with the fields enclosed by brackets [] replaced by
022 * your own identifying information:
023 * "Portions Copyrighted [year] [name of copyright owner]"
024 *
025 * $Id: EntitlementException.java,v 1.2 2009/09/03 17:06:23 veiming Exp $
026 *
027 * Portions copyright 2010-2016 ForgeRock AS.
028 */
029
030package com.sun.identity.entitlement;
031
032import java.text.MessageFormat;
033import java.util.Locale;
034import java.util.ResourceBundle;
035
036/**
037 * Entitlement related exception.
038 *
039 * @supported.all.api
040 */
041public class EntitlementException extends Exception {
042    public static final String RES_BUNDLE_NAME = "EntitlementException";
043
044    /*
045     * Selected error code constants. See EntitlementException.properties for full list.
046     */
047
048    public static final int UNABLE_TO_CREATE_POLICY = 1;
049    public static final int INVALID_PRIVILEGE_CLASS = 2;
050    public static final int EMPTY_PRIVILEGE_NAME = 3;
051    public static final int NULL_ENTITLEMENT = 4;
052    public static final int UNSUPPORTED_OPERATION = 5;
053    public static final int INVALID_APPLICATION_CLASS = 6;
054    public static final int INVALID_XML = 7;
055    public static final int INVALID_WSDL_LOCATION = 8;
056    public static final int MISSING_PRIVILEGE_JSON = 9;
057    public static final int SESSION_HAS_EXPIRED = 10;
058    public static final int INVALID_JSON = 11;
059    public static final int MISSING_PRIVILEGE_NAME = 12;
060    public static final int POLICY_NAME_MISMATCH = 13;
061    public static final int RESOURCE_LIST_EMPTY = 14;
062    public static final int RESOURCE_TYPE_ID_MISMATCH = 15;
063
064    public static final int UNSUPPORTED_SUBJECT_INDEX = 57;
065    public static final int UNABLE_TO_CREATE_SUBJECT_INDEX = 58;
066    public static final int UNKNOWN_IDENTITY = 59;
067    public static final int UNABLE_TO_QUERY_POLICY_INVALID_UID = 60;
068
069    public static final int UNABLE_TO_SERIALIZE_OBJECT = 200;
070    public static final int NO_SUCH_POLICY = 203;
071    public static final int UNABLE_SEARCH_PRIVILEGES = 215;
072    public static final int UNABLE_SEARCH_PRIVILEGES_MISSING_TOKEN = 216;
073    public static final int POLICY_ALREADY_EXISTS = 217;
074    public static final int RESOURCE_TYPE_ALREADY_EXISTS = 218;
075    public static final int NO_SUCH_RESOURCE_TYPE = 219;
076    public static final int RESOURCE_TYPE_IN_USE = 223;
077    public static final int RESOURCE_TYPE_RETRIEVAL_ERROR = 224;
078    public static final int APPLICATION_ALREADY_EXISTS = 228;
079    public static final int APPLICATION_NAME_MISMATCH = 229;
080    public static final int REMOVE_APPLICATION_FAIL = 230;
081    public static final int MODIFY_APPLICATION_FAIL = 231;
082    public static final int REMOVE_RESOURCE_TYPE_FAIL = 232;
083    public static final int MODIFY_RESOURCE_TYPE_FAIL = 233;
084    public static final int RESOURCE_TYPE_REFERENCED = 234;
085    public static final int POLICY_DEFINES_INVALID_RESOURCE_TYPE = 235;
086    public static final int MISSING_RESOURCE_TYPE = 236;
087    public static final int INVALID_RESOURCE_TYPE = 237;
088    public static final int NO_RESOURCE_TYPE_EXPECTED = 238;
089    public static final int MISSING_APPLICATION_NAME = 239;
090    public static final int APP_RETRIEVAL_ERROR = 248;
091    public static final int MISSING_RESOURCE_TYPE_NAME = 249;
092    public static final int NO_SUCH_REFERRAL_PRIVILEGE = 263;
093    public static final int NO_RESOURCE_TYPE_MATCH_FOUND = 242;
094
095    public static final int INCONSISTENT_WILDCARDS = 300;
096    public static final int INVALID_PORT = 301;
097    public static final int MALFORMED_URL = 302;
098    public static final int INVALID_RESOURCE = 303;
099    public static final int INVALID_ENTITLEMENT_SUBJECT_CLASS = 310;
100    public static final int INVALID_CLASS = 311;
101
102    public static final int INVALID_APP_TYPE = 317;
103    public static final int INVALID_APP_REALM = 318;
104    public static final int APPLICATION_TYPE_NOT_FOUND = 319;
105
106    public static final int NO_SUCH_APPLICATION = 321;
107    public static final int NOT_FOUND = 325;
108
109    public static final int PERMISSION_DENIED = 326;
110
111    public static final int SUBJECT_REQUIRED = 327;
112    public static final int INVALID_SEARCH_FILTER = 328;
113    public static final int UNKNOWN_POLICY_CLASS = 329;
114    public static final int UNKNOWN_RESOURCE_ATTRIBUTE_CLASS = 330;
115    public static final int POLICY_CLASS_CAST_EXCEPTION = 331;
116    public static final int POLICY_CLASS_NOT_INSTANTIABLE = 332;
117    public static final int POLICY_CLASS_NOT_ACCESSIBLE = 333;
118    public static final int INVALID_PROPERTY_VALUE = 400;
119    public static final int INVALID_VALUE = 401;
120    public static final int START_DATE_AFTER_END_DATE = 402;
121    public static final int APP_NOT_CREATED_POLICIES_EXIST = 404;
122    public static final int INVALID_PROPERTY_VALUE_UNKNOWN_VALUE = 405;
123    public static final int IP_CONDITION_CONFIGURATION_REQUIRED = 406;
124
125    public static final int MISSING_RESOURCE = 420;
126    public static final int JSON_PARSE_ERROR = 425;
127    public static final int AUTHENTICATION_ERROR = 434;
128    public static final int CLIENT_IP_EMPTY = 437;
129    public static final int RESOURCE_ENV_NOT_KNOWN = 438;
130    public static final int APPLICATION_SEARCH_FAILED = 450;
131    public static final int INVALID_QUERY_FILTER = 452;
132    public static final int INVALID_QUERY_ID = 454;
133    public static final int QUERY_ID_MISSING_UID = 456;
134
135    public static final int CONDITION_EVALUATION_FAILED = 510;
136
137    public static final int UNABLE_TO_RETRIEVE_SUBJECT_ATTRIBUTE = 600;
138
139    public static final int INVALID_OAUTH2_SCOPE = 700;
140    public static final int AUTH_LEVEL_NOT_INTEGER = 710;
141    public static final int PROPERTY_VALUE_NOT_DEFINED = 711;
142    public static final int AUTH_LEVEL_NOT_INT_OR_SET = 712;
143    public static final int AUTH_SCHEME_NOT_FOUND = 713;
144    public static final int INVALID_ADMIN = 720;
145    public static final int AM_ID_SUBJECT_MEMBERSHIP_EVALUATION_ERROR = 721;
146    public static final int UNABLE_TO_PARSE_SSOTOKEN_AUTHINSTANT = 730;
147    public static final int AT_LEAST_ONE_OF_TIME_PROPS_SHOULD_BE_DEFINED = 740;
148    public static final int PAIR_PROPERTY_NOT_DEFINED = 741;
149    public static final int END_IP_BEFORE_START_IP = 750;
150    public static final int INVALID_SCRIPT_ID = 760;
151
152    public static final int PROPERTY_IS_NOT_AN_INTEGER = 800;
153    public static final int PROPERTY_IS_NOT_A_SET = 801;
154    public static final int PROPERTY_CONTAINS_BLANK_VALUE = 802;
155
156    public static final int INTERNAL_ERROR = 900;
157    public static final int REALM_NOT_FOUND = 901;
158
159    private int errorCode;
160    private String message;
161    private Object[] params;
162
163    /**
164     * Creates an entitlement exception.
165     *
166     * @param errorCode Error code.
167     */
168    public EntitlementException(int errorCode) {
169        this.errorCode = errorCode;
170        this.message = getLocalizedMessage(Locale.getDefault());
171    }
172
173    /**
174     * Creates an entitlement exception.
175     *
176     * @param errorCode Error code.
177     * @param params Parameters for formatting the message string.
178     */
179    public EntitlementException(int errorCode, Object... params) {
180        this.errorCode = errorCode;
181        this.params = params;
182        this.message = getLocalizedMessage(Locale.getDefault());
183    }
184
185    /**
186     * Creates an entitlement exception.
187     *
188     * @param errorCode Error code.
189     * @param cause Root cause.
190     */
191    public EntitlementException(int errorCode, Throwable cause) {
192        super(cause);
193        this.errorCode = errorCode;
194        this.message = getLocalizedMessage(Locale.getDefault());
195    }
196
197    /**
198     * Creates an entitlement exception.
199     *
200     * @param errorCode Error code.
201     * @param params Parameters for formatting the message string.
202     * @param cause Root cause.
203     */
204    public EntitlementException(int errorCode, Object[] params, Throwable cause)
205    {
206        super(cause);
207        this.errorCode = errorCode;
208        this.params = params;
209        this.message = getLocalizedMessage(Locale.getDefault());
210    }
211
212    public EntitlementException(int errorCode, Throwable cause, Object...params) {
213        this(errorCode, params, cause);
214    }
215
216    /**
217     * Returns error code.
218     *
219     * @return error code.
220     */
221    public int getErrorCode() {
222        return errorCode;
223    }
224
225    /**
226     * Returns exception message.
227     *
228     * @return exception message.
229     */
230    @Override
231    public String getMessage() {
232        return message;
233    }
234
235    /**
236     * Returns localized exception message.
237     *
238     * @return localized exception message.
239     */
240    @Override
241    public String getLocalizedMessage() {
242        return message;
243    }
244
245    /**
246     * Returns localized exception message using the errorCode as key.
247     *
248     * @param locale Locale of the message.
249     * @return localized exception message.
250     */
251    public String getLocalizedMessage(Locale locale) {
252        ResourceBundle rb = ResourceBundle.getBundle(RES_BUNDLE_NAME, locale);
253        String msg = rb.getString(Integer.toString(errorCode));
254        return (params != null) ? MessageFormat.format(msg, params) : msg;
255    }
256}