Skip to content

Commit 03f4d9f

Browse files
author
Chris Wiechmann
authored
Merge pull request #20 from Axway-API-Management-Plus/login-retry-handling
Added Login-Retry handling and improved error handling
2 parents a60dc4c + c3cb270 commit 03f4d9f

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

modules/apim-adapter/src/main/java/com/axway/apim/adapter/APIManagerAdapter.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public void loginToAPIManager(boolean useAdminClient) throws AppException {
168168
URI uri;
169169
if(cmd.ignoreAdminAccount() && useAdminClient) return;
170170
if(hasAdminAccount && useAdminClient) return; // Already logged in with an Admin-Account.
171-
HttpResponse response = null;
171+
HttpResponse httpResponse = null;
172172
try {
173173
uri = new URIBuilder(cmd.getAPIManagerURL()).setPath(RestAPICall.API_VERSION+"/login").build();
174174
List<NameValuePair> params = new ArrayList<NameValuePair>();
@@ -191,12 +191,24 @@ public void loginToAPIManager(boolean useAdminClient) throws AppException {
191191
params.add(new BasicNameValuePair("password", password));
192192
POSTRequest loginRequest = new POSTRequest(new UrlEncodedFormEntity(params), uri, useAdminClient);
193193
loginRequest.setContentType(null);
194-
response = loginRequest.execute();
195-
int statusCode = response.getStatusLine().getStatusCode();
196-
if(statusCode == 403 || statusCode == 401){
197-
LOG.error("Login failed: " +statusCode+ ", Response: " + response);
198-
throw new AppException("Given user: '"+username+"' can't login.", ErrorCode.API_MANAGER_COMMUNICATION);
199-
}
194+
httpResponse = loginRequest.execute();
195+
int statusCode = httpResponse.getStatusLine().getStatusCode();
196+
if(statusCode < 200 || statusCode > 299){
197+
String response = EntityUtils.toString(httpResponse.getEntity());
198+
if(statusCode==403 && response.contains("Unknown API")) {
199+
LOG.warn("Login failed with statusCode: " +statusCode+ ". Got response: '"+response+"' ... Try again in 1 second.");
200+
Thread.sleep(1000);
201+
httpResponse = loginRequest.execute();
202+
statusCode = httpResponse.getStatusLine().getStatusCode();
203+
if(statusCode < 200 || statusCode > 299){
204+
LOG.error("Login finally failed with statusCode: " +statusCode+ ". Got response: '"+response+"' Got response: '"+response+"'");
205+
throw new AppException("Login finally failed with statusCode: " +statusCode, ErrorCode.API_MANAGER_COMMUNICATION);
206+
} else {
207+
LOG.info("Successfully logged in on retry. Received Status-Code: " +statusCode );
208+
}
209+
}
210+
}
211+
200212
User user = getCurrentUser(useAdminClient);
201213
if(user.getRole().equals("admin")) {
202214
this.hasAdminAccount = true;
@@ -212,8 +224,8 @@ public void loginToAPIManager(boolean useAdminClient) throws AppException {
212224
throw new AppException("Can't login to API-Manager", ErrorCode.API_MANAGER_COMMUNICATION, e);
213225
} finally {
214226
try {
215-
if(response!=null)
216-
((CloseableHttpResponse)response).close();
227+
if(httpResponse!=null)
228+
((CloseableHttpResponse)httpResponse).close();
217229
} catch (Exception ignore) {}
218230
}
219231
}

0 commit comments

Comments
 (0)