@@ -814,72 +814,30 @@ private JsonNode importFromSwagger(API api) throws URISyntaxException, AppExcept
814814 }
815815 }
816816
817- public void upgradeAccessToNewerAPI (API newAPI , API oldAPI ) throws AppException {
818- if (newAPI .getState ().equals (API .STATE_UNPUBLISHED )) {
819- LOG .debug ("No need to grant access to newly created API, as desired state of API is unpublished." );
820- return ;
821- }
822- LOG .info ("Upgrade access & subscriptions to newly created API." );
823-
817+ public void upgradeAccessToNewerAPI (API apiToUpgrade , API referenceAPI ) throws AppException {
818+ upgradeAccessToNewerAPI (apiToUpgrade , referenceAPI , null , null , null );
819+ // Existing applications now got access to the new API, hence we have to update the internal state
820+ // APIManagerAdapter.getInstance().addClientApplications(inTransitState, actualState);
821+ // Additionally we need to preserve existing (maybe manually created) application quotas
824822 URI uri ;
825823 HttpEntity entity ;
826824 RestAPICall request ;
827825 HttpResponse httpResponse = null ;
828-
829- try {
830- uri = new URIBuilder (cmd .getAPIManagerURL ()).setPath (RestAPICall .API_VERSION +"/proxies/upgrade/" +oldAPI .getId ()).build ();
831-
832- List <NameValuePair > params = new Vector <NameValuePair >();
833- params .add (new BasicNameValuePair ("upgradeApiId" , newAPI .getId ()));
834-
835- entity = new UrlEncodedFormEntity (params , "UTF-8" );
836-
837- request = new POSTRequest (entity , uri , true );
838- request .setContentType ("application/x-www-form-urlencoded" );
839-
840- httpResponse = request .execute ();
841- int statusCode = httpResponse .getStatusLine ().getStatusCode ();
842- if (statusCode != 204 ){
843- String response = EntityUtils .toString (httpResponse .getEntity ());
844- if (statusCode ==403 && response .contains ("Unknown API" )) {
845- LOG .warn ("Got unexpected error: 'Unknown API' while granting access to newer API ... Try again in 1 second." );
846- Thread .sleep (1000 );
847- httpResponse = request .execute ();
848- statusCode = httpResponse .getStatusLine ().getStatusCode ();
849- if (statusCode != 204 ) {
850- LOG .error ("Error upgrading access to newer API. Received Status-Code: " +statusCode + ", Response: " + EntityUtils .toString (httpResponse .getEntity ()));
851- throw new AppException ("Error upgrading access to newer API. Received Status-Code: " +statusCode , ErrorCode .CANT_CREATE_BE_API );
852- } else {
853- LOG .info ("Successfully granted access to newer API on retry. Received Status-Code: " +statusCode );
854- }
855- }
856- }
857- } catch (Exception e ) {
858- throw new AppException ("Can't upgrade access to newer API!" , ErrorCode .CANT_UPGRADE_API_ACCESS , e );
859- } finally {
860- try {
861- if (httpResponse !=null )
862- ((CloseableHttpResponse )httpResponse ).close ();
863- } catch (Exception ignore ) {}
864- }
865- // Existing applications now got access to the new API, hence we have to update the internal state
866- // APIManagerAdapter.getInstance().addClientApplications(inTransitState, actualState);
867- // Additionally we need to preserve existing (maybe manually created) application quotas
868826 boolean updateAppQuota = false ;
869- if (oldAPI .getApplications ().size ()!=0 ) {
870- LOG .debug ("Found: " +oldAPI .getApplications ().size ()+" subscribed applications for this API. Taking over potentially configured quota configuration." );
871- for (ClientApplication app : oldAPI .getApplications ()) {
827+ if (referenceAPI .getApplications ().size ()!=0 ) {
828+ LOG .debug ("Found: " +referenceAPI .getApplications ().size ()+" subscribed applications for this API. Taking over potentially configured quota configuration." );
829+ for (ClientApplication app : referenceAPI .getApplications ()) {
872830 if (app .getAppQuota ()==null ) continue ;
873831 // REST-API for App-Quota is also returning Default-Quotas, but we have to ignore them here!
874832 if (app .getAppQuota ().getId ().equals (APIManagerAdapter .APPLICATION_DEFAULT_QUOTA ) || app .getAppQuota ().getId ().equals (APIManagerAdapter .SYSTEM_API_QUOTA )) continue ;
875833 for (QuotaRestriction restriction : app .getAppQuota ().getRestrictions ()) {
876- if (restriction .getApi ().equals (oldAPI .getId ())) { // This application has a restriction for this specific API
834+ if (restriction .getApi ().equals (referenceAPI .getId ())) { // This application has a restriction for this specific API
877835 updateAppQuota = true ;
878- restriction .setApi (newAPI .getId ()); // Take over the quota config to new API
836+ restriction .setApi (apiToUpgrade .getId ()); // Take over the quota config to new API
879837 if (!restriction .getMethod ().equals ("*" )) { // The restriction is for a specific method
880- String originalMethodName = APIManagerAdapter .getInstance ().methodAdapter .getMethodForId (oldAPI .getId (), restriction .getMethod ()).getName ();
838+ String originalMethodName = APIManagerAdapter .getInstance ().methodAdapter .getMethodForId (referenceAPI .getId (), restriction .getMethod ()).getName ();
881839 // Try to find the same operation for the newly created API based on the name
882- String newMethodId = APIManagerAdapter .getInstance ().methodAdapter .getMethodForName (newAPI .getId (), originalMethodName ).getId ();
840+ String newMethodId = APIManagerAdapter .getInstance ().methodAdapter .getMethodForName (apiToUpgrade .getId (), originalMethodName ).getId ();
883841 restriction .setMethod (newMethodId );
884842 }
885843 }
@@ -911,6 +869,67 @@ public void upgradeAccessToNewerAPI(API newAPI, API oldAPI) throws AppException
911869 }
912870 }
913871
872+ public boolean upgradeAccessToNewerAPI (API apiToUpgrade , API referenceAPI , Boolean deprecateRefApi , Boolean retireRefApi , Long retirementDateRefAPI ) throws AppException {
873+ if (apiToUpgrade .getState ().equals (API .STATE_UNPUBLISHED )) {
874+ LOG .info ("API to upgrade has state unpublished." );
875+ return false ;
876+ }
877+ if (apiToUpgrade .getId ().equals (referenceAPI .getId ())) {
878+ LOG .warn ("API to upgrade: " +Utils .getAPILogString (apiToUpgrade )+" and "
879+ + "reference/old API: " +Utils .getAPILogString (referenceAPI )+" are the same. Skip upgrade access to newer API." );
880+ return false ;
881+ }
882+ LOG .debug ("Upgrade access & subscriptions to API: " + apiToUpgrade .getName () + " " + apiToUpgrade .getVersion () + "(" +apiToUpgrade .getId ()+")" );
883+
884+ URI uri ;
885+ HttpEntity entity ;
886+ RestAPICall request ;
887+ HttpResponse httpResponse = null ;
888+ try {
889+ uri = new URIBuilder (cmd .getAPIManagerURL ()).setPath (RestAPICall .API_VERSION +"/proxies/upgrade/" +referenceAPI .getId ()).build ();
890+
891+ List <NameValuePair > params = new Vector <NameValuePair >();
892+ params .add (new BasicNameValuePair ("upgradeApiId" , apiToUpgrade .getId ()));
893+ if (deprecateRefApi != null ) params .add (new BasicNameValuePair ("deprecate" , deprecateRefApi .toString ()));
894+ if (retireRefApi != null ) params .add (new BasicNameValuePair ("retire" , retireRefApi .toString ()));
895+ if (retirementDateRefAPI != null ) params .add (new BasicNameValuePair ("retirementDate" , formatRetirementDate (retirementDateRefAPI )));
896+
897+ entity = new UrlEncodedFormEntity (params , "UTF-8" );
898+
899+ request = new POSTRequest (entity , uri , true );
900+ request .setContentType ("application/x-www-form-urlencoded" );
901+
902+ httpResponse = request .execute ();
903+ int statusCode = httpResponse .getStatusLine ().getStatusCode ();
904+ if (statusCode != 204 ){
905+ String response = EntityUtils .toString (httpResponse .getEntity ());
906+ if (statusCode ==403 && response .contains ("Unknown API" )) {
907+ LOG .warn ("Got unexpected error: 'Unknown API' while granting access to newer API ... Try again in 1 second." );
908+ Thread .sleep (1000 );
909+ httpResponse = request .execute ();
910+ statusCode = httpResponse .getStatusLine ().getStatusCode ();
911+ if (statusCode != 204 ) {
912+ LOG .error ("Error upgrading access to newer API. Received Status-Code: " +statusCode + ", Response: " + EntityUtils .toString (httpResponse .getEntity ()));
913+ throw new AppException ("Error upgrading access to newer API. Received Status-Code: " +statusCode , ErrorCode .CANT_CREATE_BE_API );
914+ } else {
915+ LOG .info ("Successfully granted access to newer API on retry. Received Status-Code: " +statusCode );
916+ }
917+ } else {
918+ LOG .error ("Error upgrading access to newer API. Received Status-Code: " +statusCode + ", Response: " + response );
919+ throw new AppException ("Error upgrading access to newer API. Received Status-Code: " +statusCode , ErrorCode .CANT_CREATE_BE_API );
920+ }
921+ }
922+ return true ;
923+ } catch (Exception e ) {
924+ throw new AppException ("Can't upgrade access to newer API!" , ErrorCode .CANT_UPGRADE_API_ACCESS , e );
925+ } finally {
926+ try {
927+ if (httpResponse !=null )
928+ ((CloseableHttpResponse )httpResponse ).close ();
929+ } catch (Exception ignore ) {}
930+ }
931+ }
932+
914933 public void grantClientOrganization (List <Organization > grantAccessToOrgs , API api , boolean allOrgs ) throws AppException {
915934 URI uri ;
916935 HttpEntity entity ;
0 commit comments