aboutsummaryrefslogtreecommitdiff
path: root/src/java/com/android/ims/rcs/uce/request/SubscriptionTerminatedHelper.java
blob: 8ae25271cc608cbb7f203da57a96a4e8cd77cb0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright (c) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ims.rcs.uce.request;

import android.telephony.ims.RcsUceAdapter;
import android.telephony.ims.RcsUceAdapter.ErrorCode;
import android.text.TextUtils;
import android.util.Log;

import com.android.ims.rcs.uce.util.UceUtils;

import java.util.Optional;

/**
 * The helper class to analyze the result of the callback onTerminated to determine whether the
 * subscription request should be retried or not.
 */
public class SubscriptionTerminatedHelper {

    private static final String LOG_TAG = UceUtils.getLogPrefix() + "SubscriptionTerminated";

    // The terminated reasons defined in RFC 3265 3.2.4
    private static final String REASON_DEACTIVATED = "deactivated";
    private static final String REASON_PROBATION = "probation";
    private static final String REASON_REJECTED = "rejected";
    private static final String REASON_TIMEOUT = "timeout";
    private static final String REASON_GIVEUP = "giveup";
    private static final String REASON_NORESOURCE = "noresource";

    /**
     * The analysis result of the callback onTerminated.
     */
    static class TerminatedResult {
        private final @ErrorCode Optional<Integer> mErrorCode;
        private final long mRetryAfterMillis;

        public TerminatedResult(@ErrorCode Optional<Integer> errorCode, long retryAfterMillis) {
            mErrorCode = errorCode;
            mRetryAfterMillis = retryAfterMillis;
        }

        /**
         * @return the error code when the request is failed. Optional.empty if the request is
         * successful.
         */
        public Optional<Integer> getErrorCode() {
            return mErrorCode;
        }

        public long getRetryAfterMillis() {
            return mRetryAfterMillis;
        }

        public String toString() {
            StringBuilder builder = new StringBuilder();
            builder.append("TerminatedResult: ")
                    .append("errorCode=").append(mErrorCode)
                    .append(", retryAfterMillis=").append(mRetryAfterMillis);
            return builder.toString();
        }
    }

    /**
     * According to the RFC 3265, Check the given reason to see whether clients should retry the
     * subscribe request.
     * <p>
     * See RFC 3265 3.2.4 for the detail.
     *
     * @param reason The reason why the subscribe request is terminated. The reason is given by the
     * network and it could be empty.
     * @param retryAfterMillis How long should clients wait before retrying.
     */
    public static TerminatedResult getAnalysisResult(String reason, long retryAfterMillis) {
        TerminatedResult result = null;
        if (TextUtils.isEmpty(reason)) {
            /*
             * When the value of retryAfterMillis is larger then zero, the client should retry.
             */
            if (retryAfterMillis > 0L) {
                result = new TerminatedResult(Optional.of(RcsUceAdapter.ERROR_GENERIC_FAILURE),
                        retryAfterMillis);
            }
        } else if (REASON_DEACTIVATED.equalsIgnoreCase(reason)) {
            /*
             * When the reason is "deactivated", clients should retry immediately.
             */
            long retry = getRequestRetryAfterMillis(retryAfterMillis);
            result = new TerminatedResult(Optional.of(RcsUceAdapter.ERROR_GENERIC_FAILURE), retry);
        } else if (REASON_PROBATION.equalsIgnoreCase(reason)) {
            /*
             * When the reason is "probation", it means that the subscription has been terminated,
             * but the client should retry at some later time.
             */
            long retry = getRequestRetryAfterMillis(retryAfterMillis);
            result = new TerminatedResult(Optional.of(RcsUceAdapter.ERROR_GENERIC_FAILURE), retry);
        } else if (REASON_REJECTED.equalsIgnoreCase(reason)) {
            /*
             * When the reason is "rejected", it means that the subscription has been terminated
             * due to chang in authorization policy. Clients should NOT retry.
             */
            result = new TerminatedResult(Optional.of(RcsUceAdapter.ERROR_NOT_AUTHORIZED), 0L);
        } else if (REASON_TIMEOUT.equalsIgnoreCase(reason) && retryAfterMillis > 0L) {
            /*
             * The subscription has been terminated because it was not refreshed before it expired.
             * The request completes successfully when the retryAfter is not set. Otherwise, the
             * request should retry if the retryAfter is set.
             */
            long retry = getRequestRetryAfterMillis(retryAfterMillis);
            result = new TerminatedResult(Optional.of(RcsUceAdapter.ERROR_REQUEST_TIMEOUT), retry);
        } else if (REASON_GIVEUP.equalsIgnoreCase(reason)) {
            /*
             * The subscription has been terminated because the notifier could no obtain
             * authorization in a timely fashion. Clients could retry the subscribe request.
             */
            long retry = getRequestRetryAfterMillis(retryAfterMillis);
            result = new TerminatedResult(Optional.of(RcsUceAdapter.ERROR_NOT_AUTHORIZED), retry);
        } else if (REASON_NORESOURCE.equalsIgnoreCase(reason)) {
            /*
             * The subscription has been terminated because the resource is no longer exists.
             * Clients should NOT retry.
             */
            result = new TerminatedResult(Optional.of(RcsUceAdapter.ERROR_NOT_FOUND), 0L);
        } else if (retryAfterMillis > 0L) {
            /*
             * Even if the reason is not listed above, clients should retry the request as long as
             * the value of retry is non-zero.
             */
            long retry = getRequestRetryAfterMillis(retryAfterMillis);
            result = new TerminatedResult(Optional.of(RcsUceAdapter.ERROR_GENERIC_FAILURE), retry);
        }

        // The request should be successful. when the terminated is not in the above cases
        if (result == null) {
            result = new TerminatedResult(Optional.empty(), 0L);
        }

        Log.d(LOG_TAG, "getAnalysisResult: reason=" + reason + ", retry=" + retryAfterMillis +
                ", " + result);
        return result;
    }

    /*
     * Get the appropriated retryAfterMillis for the subscribe request.
     */
    private static long getRequestRetryAfterMillis(long retryAfterMillis) {
        // Return the minimum retry after millis if the given retryAfterMillis is less than the
        // minimum value.
        long minRetryAfterMillis = UceUtils.getMinimumRequestRetryAfterMillis();
        return (retryAfterMillis < minRetryAfterMillis) ? minRetryAfterMillis : retryAfterMillis;
    }
}