|
2 | 2 |
|
3 | 3 |
|
4 | 4 | import java.io.IOException;
|
| 5 | +import java.net.UnknownHostException; |
5 | 6 | import java.util.concurrent.TimeUnit;
|
6 | 7 | import java.util.logging.Level;
|
7 | 8 | import java.util.logging.Logger;
|
@@ -61,9 +62,9 @@ public CollectionsAuthorizationInterceptor(CollectionSession session, RequestOpt
|
61 | 62 | okhttpbuilder.addInterceptor(httpLoggingInterceptor);
|
62 | 63 |
|
63 | 64 |
|
64 |
| - okhttpbuilder.connectTimeout(30, TimeUnit.SECONDS); |
65 |
| - okhttpbuilder.readTimeout(30, TimeUnit.SECONDS); |
66 |
| - okhttpbuilder.writeTimeout(30, TimeUnit.SECONDS); |
| 65 | + okhttpbuilder.connectTimeout(60, TimeUnit.SECONDS); |
| 66 | + okhttpbuilder.readTimeout(60, TimeUnit.SECONDS); |
| 67 | + okhttpbuilder.writeTimeout(60, TimeUnit.SECONDS); |
67 | 68 |
|
68 | 69 |
|
69 | 70 | OkHttpClient httpClient = okhttpbuilder
|
@@ -113,52 +114,65 @@ private Request request(final Request initialRequest) {
|
113 | 114 | */
|
114 | 115 | @Override
|
115 | 116 | public okhttp3.Response intercept(Chain chain) throws IOException {
|
116 |
| - okhttp3.Response mainResponse = chain.proceed(request(chain.request())); |
117 |
| - Request mainRequest = chain.request(); |
118 | 117 |
|
119 | 118 |
|
120 |
| - // if response code is 401 or 403, 'mainRequest' has encountered authentication error |
121 |
| - if (mainResponse.code() == 401 || mainResponse.code() == 403) { |
| 119 | + try { |
| 120 | + okhttp3.Response mainResponse = chain.proceed(request(chain.request())); |
122 | 121 |
|
123 | 122 |
|
124 |
| - this.logger.log(Level.INFO, "<<<<<<<<<<<<<<<Getting Fresh Token"); |
| 123 | + Request mainRequest = chain.request(); |
125 | 124 |
|
126 | 125 |
|
127 |
| - String credentials = Credentials.basic(this.opts.getCollectionUserId(), |
128 |
| - this.opts.getCollectionApiSecret()); |
129 |
| - Response<AccessToken> loginResponse = this.apiService |
130 |
| - .getToken(credentials, this.opts.getCollectionPrimaryKey()).execute(); |
| 126 | + // if response code is 401 or 403, 'mainRequest' has encountered authentication error |
| 127 | + if (mainResponse.code() == 401 || mainResponse.code() == 403) { |
131 | 128 |
|
132 |
| - if (loginResponse.isSuccessful()) { |
133 |
| - // login request succeed, new token generated |
134 |
| - AccessToken token = loginResponse.body(); |
135 |
| - // save the new token |
136 |
| - this.session.saveToken(token.getToken()); |
137 |
| - // retry the 'mainRequest' which encountered an authentication error |
138 |
| - // add new token into 'mainRequest' header and request again |
139 |
| - Request.Builder builder = mainRequest.newBuilder().addHeader("Authorization", |
140 |
| - "Bearer " + this.session.getToken()) |
141 |
| - .addHeader("Ocp-Apim-Subscription-Key", this.opts.getCollectionPrimaryKey()) |
142 |
| - .addHeader("X-Target-Environment", this.opts.getTargetEnvironment()) |
143 |
| - .method(mainRequest.method(), mainRequest.body()); |
144 |
| - mainResponse = chain.proceed(builder.build()); |
145 |
| - } |
146 |
| - } else if (!mainResponse.isSuccessful()) { |
147 | 129 |
|
148 |
| - this.logger.log(Level.INFO, "<<<<<<<<<<<<<<< ETETETET " |
149 |
| - + mainResponse.code() + " .." + mainResponse.body().string()); |
| 130 | + this.logger.log(Level.INFO, "<<<<<<<<<<<<<<<Getting Fresh Token"); |
150 | 131 |
|
151 | 132 |
|
152 |
| - throw new MomoApiException(mainResponse.body().string()); |
| 133 | + String credentials = Credentials.basic(this.opts.getCollectionUserId(), |
| 134 | + this.opts.getCollectionApiSecret()); |
| 135 | + Response<AccessToken> loginResponse = this.apiService |
| 136 | + .getToken(credentials, this.opts.getCollectionPrimaryKey()).execute(); |
153 | 137 |
|
| 138 | + if (loginResponse.isSuccessful()) { |
| 139 | + // login request succeed, new token generated |
| 140 | + AccessToken token = loginResponse.body(); |
| 141 | + // save the new token |
| 142 | + this.session.saveToken(token.getToken()); |
| 143 | + // retry the 'mainRequest' which encountered an authentication error |
| 144 | + // add new token into 'mainRequest' header and request again |
| 145 | + Request.Builder builder = mainRequest.newBuilder().addHeader("Authorization", |
| 146 | + "Bearer " + this.session.getToken()) |
| 147 | + .addHeader("Ocp-Apim-Subscription-Key", this.opts.getCollectionPrimaryKey()) |
| 148 | + .addHeader("X-Target-Environment", this.opts.getTargetEnvironment()) |
| 149 | + .method(mainRequest.method(), mainRequest.body()); |
| 150 | + mainResponse = chain.proceed(builder.build()); |
| 151 | + } |
| 152 | + } else if (mainResponse.code() == 400 || mainResponse.code() == 500 || mainResponse.code() == 404) { |
| 153 | + String error = ""; |
154 | 154 |
|
155 |
| - } |
| 155 | + try { |
| 156 | + error = mainResponse.body().string(); |
| 157 | + } catch (IllegalStateException e) { |
156 | 158 |
|
157 |
| - if (this.session.getToken() == "dummy") { |
158 |
| - throw new MomoApiException(mainResponse.body().string()); |
159 |
| - } |
| 159 | + } |
| 160 | + |
| 161 | + |
| 162 | + throw new MomoApiException(error); |
| 163 | + |
| 164 | + |
| 165 | + } |
| 166 | + |
| 167 | + |
| 168 | + return mainResponse; |
| 169 | + |
| 170 | + } catch (UnknownHostException exception) { |
160 | 171 |
|
| 172 | + throw new MomoApiException("There is no internet connection."); |
161 | 173 |
|
162 |
| - return mainResponse; |
| 174 | + |
| 175 | + } |
163 | 176 | }
|
| 177 | + |
164 | 178 | }
|
0 commit comments