1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.libraries.entitlement; 18 19 /** 20 * Indicates errors happened in retrieving service entitlement configuration. 21 */ 22 public class ServiceEntitlementException extends Exception { 23 /** 24 * Unknown error. 25 */ 26 public static final int ERROR_UNKNOWN = 0; 27 28 // Android telephony related failures 29 /** 30 * Android telephony is unable to provide info like IMSI, e.g. when modem crashed. 31 */ 32 public static final int ERROR_PHONE_NOT_AVAILABLE = 10; 33 34 // EAP-AKA authentication related falures 35 /** 36 * SIM not returning a response to the EAP-AKA challenge, e.g. when the challenge is invalid. 37 * This can happen only when an embedded EAP-AKA challange is conducted, as per GMSA spec TS.43 38 * section 2.6.1. 39 */ 40 public static final int ERROR_ICC_AUTHENTICATION_NOT_AVAILABLE = 20; 41 /** 42 * EAP-AKA synchronization failure that cannot be recoverd even after the "Sequence number 43 * synchronization" procedure as defined in RFC 4187. 44 */ 45 public static final int ERROR_EAP_AKA_SYNCHRONIZATION_FAILURE = 21; 46 47 // HTTP related failures 48 /** 49 * Cannot connect to the entitlment server, e.g. due to weak mobile network and Wi-Fi 50 * connection. 51 */ 52 public static final int ERROR_SERVER_NOT_CONNECTABLE = 30; 53 /** 54 * HTTP response received with a status code indicating failure, e.g. 4xx and 5xx. Use {@link 55 * #getHttpStatus} to get the status code and {@link #getMessage} the error message in the 56 * response body. 57 */ 58 public static final int ERROR_HTTP_STATUS_NOT_SUCCESS = 31; 59 /** 60 * HTTP response received with a malformed format. e.g. the response with content-type JSON but 61 * failing JSON parser. 62 */ 63 public static final int ERROR_MALFORMED_HTTP_RESPONSE = 32; 64 65 /** 66 * Default HTTP status if not been specified. 67 */ 68 private static final int HTTP_STATUS_UNSPECIFIED = 0; 69 70 /** 71 * An empty string if Retry-After header in HTTP response not been specified. 72 */ 73 private static final String RETRY_AFTER_UNSPECIFIED = ""; 74 75 private int mError; 76 private int mHttpStatus; 77 private String mRetryAfter; 78 ServiceEntitlementException(int error, String message)79 public ServiceEntitlementException(int error, String message) { 80 this(error, HTTP_STATUS_UNSPECIFIED, RETRY_AFTER_UNSPECIFIED, message); 81 } 82 ServiceEntitlementException(int error, int httpStatus, String message)83 public ServiceEntitlementException(int error, int httpStatus, String message) { 84 this(error, httpStatus, RETRY_AFTER_UNSPECIFIED, message); 85 } 86 ServiceEntitlementException( int error, int httpStatus, String retryAfter, String message)87 public ServiceEntitlementException( 88 int error, int httpStatus, String retryAfter, String message) { 89 super(message); 90 this.mError = error; 91 this.mHttpStatus = httpStatus; 92 this.mRetryAfter = retryAfter; 93 } 94 ServiceEntitlementException(int error, String message, Throwable cause)95 public ServiceEntitlementException(int error, String message, Throwable cause) { 96 this(error, HTTP_STATUS_UNSPECIFIED, RETRY_AFTER_UNSPECIFIED, message, cause); 97 } 98 ServiceEntitlementException(int error, int httpStatus, String message, Throwable cause)99 public ServiceEntitlementException(int error, int httpStatus, String message, Throwable cause) { 100 this(error, httpStatus, RETRY_AFTER_UNSPECIFIED, message, cause); 101 } 102 ServiceEntitlementException( int error, int httpStatus, String retryAfter, String message, Throwable cause)103 public ServiceEntitlementException( 104 int error, int httpStatus, String retryAfter, String message, Throwable cause) { 105 super(message, cause); 106 this.mError = error; 107 this.mHttpStatus = httpStatus; 108 this.mRetryAfter = retryAfter; 109 } 110 111 /** 112 * Returns the error code, see {@link #ERROR_*}. {@link #ERROR_UNKNOWN} if not been specified. 113 */ getErrorCode()114 public int getErrorCode() { 115 return mError; 116 } 117 118 /** 119 * Returns the HTTP status code returned by entitlement server; {@link #HTTP_STATUS_UNSPECIFIED} 120 * if not been specified. 121 */ getHttpStatus()122 public int getHttpStatus() { 123 return mHttpStatus; 124 } 125 126 /** 127 * Returns the "Retry-After" header in HTTP response, often set with HTTP status code 503; an 128 * empty string if unavailable. 129 * 130 * @return the HTTP-date or a number of seconds to delay, as defiend in RFC 7231: 131 * https://tools.ietf.org/html/rfc7231#section-7.1.3 132 */ getRetryAfter()133 public String getRetryAfter() { 134 return mRetryAfter; 135 } 136 } 137