31
31
import org .apache .http .impl .client .HttpClients ;
32
32
import org .apache .http .ssl .SSLContexts ;
33
33
import org .apache .http .util .EntityUtils ;
34
- import org .json .simple .JSONArray ;
35
- import org .json .simple .JSONObject ;
36
34
import org .wso2 .carbon .apimgt .api .APIConsumer ;
37
35
import org .wso2 .carbon .apimgt .impl .APIConstants ;
38
36
import org .wso2 .carbon .apimgt .impl .APIManagerConfiguration ;
42
40
import org .wso2 .carbon .apimgt .api .model .API ;
43
41
import org .wso2 .carbon .apimgt .api .model .ApiTypeWrapper ;
44
42
import org .wso2 .carbon .apimgt .api .model .Tier ;
43
+ import org .wso2 .carbon .apimgt .impl .dto .devportal .ApiMetaDataDTO ;
45
44
import com .fasterxml .jackson .core .JsonProcessingException ;
46
- import java .io .File ;
47
45
import java .io .FileInputStream ;
48
46
import java .io .IOException ;
49
47
import java .net .URISyntaxException ;
52
50
import java .util .Map ;
53
51
import java .util .ArrayList ;
54
52
import java .util .List ;
55
- import java .util .Collections ;
56
- import java .util .HashMap ;
53
+ import java .util .Objects ;
57
54
import java .util .Arrays ;
58
-
55
+ import java . util . concurrent . ConcurrentHashMap ;
59
56
import com .fasterxml .jackson .databind .ObjectMapper ;
60
57
import org .wso2 .carbon .apimgt .impl .internal .ServiceReferenceHolder ;
61
58
import org .wso2 .carbon .base .ServerConfiguration ;
62
59
60
+ /**
61
+ * This class used to handle newly introduced 2025 version of Developer Portal's configuration with APIM.
62
+ */
63
63
public class NewDevPortalHandler {
64
+ private static final Log log = LogFactory .getLog (NewDevPortalHandler .class );
65
+ private static final String baseUrl = getNewPortalURL ();
66
+ private static final ObjectMapper objectMapper = new ObjectMapper ();
67
+ private static final Map <String , String > orgIdCache = new ConcurrentHashMap <>();
64
68
65
69
private static class HttpResponseData {
66
70
private final int statusCode ;
@@ -80,11 +84,6 @@ private String getResponseBody() {
80
84
}
81
85
}
82
86
83
- private static final Log log = LogFactory .getLog (NewDevPortalHandler .class );
84
- private static final String baseUrl = getNewPortalURL ();
85
- private static final ObjectMapper objectMapper = new ObjectMapper ();
86
- private static final Map <String , String > orgIdCache = new HashMap <>();
87
-
88
87
public static boolean isNewPortalEnabled () {
89
88
return Boolean .parseBoolean (getConfigProperty (APIConstants .API_STORE_NEW_PORTAL_ENABLED , "false" ));
90
89
}
@@ -256,38 +255,40 @@ private static String getApiId(String orgId, String apiName, String apiVersion,
256
255
}
257
256
}
258
257
259
- // Data Structuring Related Methods
260
-
261
258
private static String getApiMetaData (ApiTypeWrapper apiTypeWrapper ) throws APIManagementException {
262
259
API api = apiTypeWrapper .getApi ();
263
-
264
- JSONObject apiInfo = new JSONObject ();
265
- apiInfo . put ( "referenceID" , defaultString ( apiTypeWrapper . getUuid ()) );
266
- apiInfo .put ( "provider" , "WSO2" ); // DEV PORTAL expects WSO2 as Provider when API coming from WSO2 API Manager
267
- apiInfo .put ( "tags" , new ArrayList <>( api . getTags ()));
268
- apiInfo .put ( "apiName" , defaultString (apiTypeWrapper .getName ()));
269
- apiInfo .put ( "apiDescription" , defaultString (api .getDescription ()));
270
- if (defaultString (api .getVisibility ()).equals ("public" )){
271
- apiInfo .put ( "visibility" , "PUBLIC" );
260
+ ApiMetaDataDTO apiMetaDataDTO = new ApiMetaDataDTO ();
261
+
262
+ ApiMetaDataDTO . ApiInfo apiInfo = new ApiMetaDataDTO . ApiInfo ( );
263
+ apiInfo .setReferenceID ( Objects . toString ( apiTypeWrapper . getUuid () , "" ));
264
+ apiInfo .setProvider ( "WSO2" ); // DEV PORTAL expects WSO2 as Provider when API coming from WSO2 API Manager
265
+ apiInfo .setApiName ( Objects . toString (apiTypeWrapper .getName (), "" ));
266
+ apiInfo .setApiDescription ( Objects . toString (api .getDescription (), "" ));
267
+ if (Objects . toString (api .getVisibility (), "" ).equals ("public" )) {
268
+ apiInfo .setVisibility ( "PUBLIC" );
272
269
} else {
273
- // If visibility is not PUBLIC, DEV PORTAL expects visibility Groups as well
274
- apiInfo .put ("visibility" , defaultString (api .getVisibility ()));
275
- apiInfo .put ("visibleGroups" , generateVisibleGroupsArray (api ));
270
+ apiInfo .setVisibility (Objects .toString (api .getVisibility (), "" ));
271
+ apiInfo .setVisibleGroups (generateVisibleGroupsArray (api ));
276
272
}
277
- apiInfo .put ("owners" , generateOwnersObject (api ));
278
- apiInfo .put ("apiVersion" , defaultString (api .getId ().getVersion ()));
279
- apiInfo .put ("apiType" , getType (api .getType ())); // IF type is HTTP, DEV PORTAL expects REST As Type
273
+ apiInfo .setOwners (generateOwnersObject (api ));
274
+ apiInfo .setApiVersion (Objects .toString (api .getId ().getVersion (), "" ));
275
+ apiInfo .setApiType (getType (api .getType ()));
276
+ apiMetaDataDTO .setApiInfo (apiInfo );
277
+
278
+ apiMetaDataDTO .setSubscriptionPolicies (convertToSubscriptionPolicies (api .getAvailableTiers ().toArray ()));
280
279
281
- JSONObject endPoints = new JSONObject ();
282
- endPoints .put ( "sandboxURL" , getSandboxEndpoint (api .getEndpointConfig ()));
283
- endPoints .put ( "productionURL" , getProductionEndpoint (api .getEndpointConfig ()));
280
+ ApiMetaDataDTO . EndPoints endPoints = new ApiMetaDataDTO . EndPoints ();
281
+ endPoints .setSandboxURL ( getSandboxEndpoint (api .getEndpointConfig ()));
282
+ endPoints .setProductionURL ( getProductionEndpoint (api .getEndpointConfig ()));
284
283
285
- JSONObject response = new JSONObject ();
286
- response .put ("apiInfo" , apiInfo );
287
- response .put ("subscriptionPolicies" , convertToSubscriptionPolicies (api .getAvailableTiers ().toArray ()));
288
- response .put ("endPoints" , endPoints );
284
+ apiMetaDataDTO .setEndPoints (endPoints );
289
285
290
- return response .toJSONString ();
286
+ try {
287
+ ObjectMapper objectMapper = new ObjectMapper ();
288
+ return objectMapper .writeValueAsString (apiMetaDataDTO );
289
+ } catch (JsonProcessingException e ) {
290
+ throw new APIManagementException ("Error while converting ApiMetaDataDTO to JSON: " + e .getMessage (), e );
291
+ }
291
292
}
292
293
293
294
private static String getSandboxEndpoint (String jsonString ) throws APIManagementException {
@@ -328,41 +329,34 @@ private static String getType(String type) {
328
329
}
329
330
}
330
331
331
- private static List <Map < String , String > > convertToSubscriptionPolicies (Object [] tiers ) {
332
- List <Map < String , String > > subscriptionPolicies = new ArrayList <>();
332
+ private static List <String > convertToSubscriptionPolicies (Object [] tiers ) {
333
+ List <String > subscriptionPolicies = new ArrayList <>();
333
334
for (Object tier : tiers ) {
334
335
if (tier instanceof Tier ) {
335
336
Tier tierObject = (Tier ) tier ;
336
337
String name = tierObject .getName ();
337
338
if (name != null ) {
338
- subscriptionPolicies .add (Collections . singletonMap ( "policyName" , name ));
339
+ subscriptionPolicies .add (name ); // Add tier name directly
339
340
}
340
341
}
341
342
}
342
343
return subscriptionPolicies ;
343
344
}
344
345
345
- private static String defaultString (String value ) {
346
- return value != null ? value : "" ;
347
- }
348
-
349
- private static JSONArray generateVisibleGroupsArray (API api ) {
350
- JSONArray visibleGroupsArray = new JSONArray ();
346
+ private static List <String > generateVisibleGroupsArray (API api ) {
347
+ List <String > visibleGroupsList = new ArrayList <>();
351
348
if (api .getVisibleRoles () != null ) {
352
- for (String role : api .getVisibleRoles ().split ("," )) {
353
- visibleGroupsArray .add (role );
354
- }
349
+ visibleGroupsList = Arrays .asList (api .getVisibleRoles ().split ("," ));
355
350
}
356
- return visibleGroupsArray ;
351
+ return visibleGroupsList ;
357
352
}
358
353
359
- private static JSONObject generateOwnersObject (API api ) {
360
- JSONObject owners = new JSONObject ();
361
- // TODO: verify below data
362
- owners .put ("technicalOwner" , defaultString (api .getTechnicalOwner ()));
363
- owners .put ("technicalOwnerEmail" , defaultString (api .getTechnicalOwnerEmail ()));
364
- owners .put ("businessOwner" , defaultString (api .getBusinessOwner ()));
365
- owners .put ("businessOwnerEmail" , defaultString (api .getBusinessOwnerEmail ()));
354
+ private static ApiMetaDataDTO .ApiInfo .Owners generateOwnersObject (API api ) {
355
+ ApiMetaDataDTO .ApiInfo .Owners owners = new ApiMetaDataDTO .ApiInfo .Owners ();
356
+ owners .setTechnicalOwner (Objects .toString (api .getTechnicalOwner (), "" ));
357
+ owners .setTechnicalOwnerEmail (Objects .toString (api .getTechnicalOwnerEmail (), "" ));
358
+ owners .setBusinessOwner (Objects .toString (api .getBusinessOwner (), "" ));
359
+ owners .setBusinessOwnerEmail (Objects .toString (api .getBusinessOwnerEmail (), "" ));
366
360
return owners ;
367
361
}
368
362
0 commit comments