5454import com .axway .apim .lib .errorHandling .ErrorState ;
5555import com .axway .apim .lib .utils .TestIndicator ;
5656import com .axway .apim .lib .utils .rest .APIMHttpClient ;
57+ import com .axway .apim .lib .utils .rest .DELRequest ;
5758import com .axway .apim .lib .utils .rest .GETRequest ;
5859import com .axway .apim .lib .utils .rest .POSTRequest ;
5960import 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 {
@@ -197,19 +202,17 @@ public void loginToAPIManager(boolean useAdminClient) throws AppException {
197202 loginRequest .setContentType (null );
198203 httpResponse = loginRequest .execute ();
199204 int statusCode = httpResponse .getStatusLine ().getStatusCode ();
200- if (statusCode < 200 || statusCode > 299 ){
205+ if (statusCode != 303 ){
201206 String response = EntityUtils .toString (httpResponse .getEntity ());
202- if (statusCode ==403 && response .contains ("Unknown API" )) {
203- LOG .warn ("Login failed with statusCode: " +statusCode + ". Got response: '" +response +"' ... Try again in 1 second." );
204- Thread .sleep (1000 );
205- httpResponse = loginRequest .execute ();
206- statusCode = httpResponse .getStatusLine ().getStatusCode ();
207- if (statusCode < 200 || statusCode > 299 ){
208- LOG .error ("Login finally failed with statusCode: " +statusCode + ". Got response: '" +response +"' Got response: '" +response +"'" );
209- throw new AppException ("Login finally failed with statusCode: " +statusCode , ErrorCode .API_MANAGER_COMMUNICATION );
210- } else {
211- LOG .info ("Successfully logged in on retry. Received Status-Code: " +statusCode );
212- }
207+ LOG .warn ("Login failed with statusCode: " +statusCode + ". Got response: '" +response +"' ... Try again in 1 second." );
208+ Thread .sleep (1000 );
209+ httpResponse = loginRequest .execute ();
210+ statusCode = httpResponse .getStatusLine ().getStatusCode ();
211+ if (statusCode != 303 ){
212+ LOG .error ("Login finally failed with statusCode: " +statusCode + ". Got response: '" +response +"' Got response: '" +response +"'" );
213+ throw new AppException ("Login finally failed with statusCode: " +statusCode , ErrorCode .API_MANAGER_COMMUNICATION );
214+ } else {
215+ LOG .info ("Successfully logged in on retry. Received Status-Code: " +statusCode );
213216 }
214217 }
215218
@@ -234,6 +237,29 @@ public void loginToAPIManager(boolean useAdminClient) throws AppException {
234237 }
235238 }
236239
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+
237263 private String [] getAdminUsernamePassword () throws AppException {
238264 if (CommandParameters .getInstance ().getAdminUsername ()==null ) return null ;
239265 String [] usernamePassword = {CommandParameters .getInstance ().getAdminUsername (), CommandParameters .getInstance ().getAdminPassword ()};
0 commit comments