Skip to content

Commit eabd63c

Browse files
author
Chris Wiechmann
committed
Logout users, when deleting APIManagerAdapter instance
1 parent f8c2261 commit eabd63c

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import com.axway.apim.lib.errorHandling.ErrorState;
5555
import com.axway.apim.lib.utils.TestIndicator;
5656
import com.axway.apim.lib.utils.rest.APIMHttpClient;
57+
import com.axway.apim.lib.utils.rest.DELRequest;
5758
import com.axway.apim.lib.utils.rest.GETRequest;
5859
import com.axway.apim.lib.utils.rest.POSTRequest;
5960
import com.axway.apim.lib.utils.rest.RestAPICall;
@@ -143,7 +144,11 @@ public static synchronized void deleteInstance() throws AppException {
143144
APIManagerAdapter.cacheManager.close();
144145
LOG.debug("Closing cache end");
145146
}
146-
APIManagerAdapter.instance = null;
147+
if(APIManagerAdapter.instance!=null) {
148+
APIManagerAdapter.instance.logoutFromAPIManager(false); // Logout potentially logged in OrgAdmin
149+
APIManagerAdapter.instance.logoutFromAPIManager(true); // Logout potentially logged in Admin
150+
APIManagerAdapter.instance = null;
151+
}
147152
}
148153

149154
private APIManagerAdapter() throws AppException {
@@ -232,6 +237,29 @@ public void loginToAPIManager(boolean useAdminClient) throws AppException {
232237
}
233238
}
234239

240+
public void logoutFromAPIManager(boolean useAdminClient) throws AppException {
241+
if(useAdminClient && !hasAdminAccount()) return;
242+
URI uri;
243+
HttpResponse httpResponse = null;
244+
try {
245+
uri = new URIBuilder(cmd.getAPIManagerURL()).setPath(RestAPICall.API_VERSION+"/login").build();
246+
DELRequest logoutRequest = new DELRequest(uri, useAdminClient);
247+
httpResponse = logoutRequest.execute();
248+
int statusCode = httpResponse.getStatusLine().getStatusCode();
249+
if(statusCode != 204){
250+
String response = EntityUtils.toString(httpResponse.getEntity());
251+
LOG.warn("Logout failed with statusCode: " +statusCode+ ". Got response: '"+response+"'");
252+
}
253+
} catch (Exception e) {
254+
throw new AppException("Can't logout from API-Manager", ErrorCode.API_MANAGER_COMMUNICATION, e);
255+
} finally {
256+
try {
257+
if(httpResponse!=null)
258+
((CloseableHttpResponse)httpResponse).close();
259+
} catch (Exception ignore) {}
260+
}
261+
}
262+
235263
private String[] getAdminUsernamePassword() throws AppException {
236264
if(CommandParameters.getInstance().getAdminUsername()==null) return null;
237265
String[] usernamePassword = {CommandParameters.getInstance().getAdminUsername(), CommandParameters.getInstance().getAdminPassword()};

0 commit comments

Comments
 (0)