Skip to content

Commit 877f9d6

Browse files
committed
2nd set of required changes
1 parent de9cdc9 commit 877f9d6

File tree

2 files changed

+265
-56
lines changed

2 files changed

+265
-56
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
/*
2+
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
3+
*
4+
* WSO2 LLC. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
package org.wso2.carbon.apimgt.impl.dto.devportal;
20+
21+
import java.util.List;
22+
23+
public class ApiMetaDataDTO {
24+
25+
private ApiInfo apiInfo;
26+
private List<String> subscriptionPolicies;
27+
private EndPoints endPoints;
28+
29+
public static class ApiInfo {
30+
private String referenceID;
31+
private String apiName;
32+
private String orgName;
33+
private String provider;
34+
private String apiCategory;
35+
private String apiDescription;
36+
private String visibility;
37+
private List<String> visibleGroups;
38+
private Owners owners;
39+
private String apiVersion;
40+
private String apiType;
41+
42+
public String getReferenceID() {
43+
return referenceID;
44+
}
45+
46+
public void setReferenceID(String referenceID) {
47+
this.referenceID = referenceID;
48+
}
49+
50+
public String getApiName() {
51+
return apiName;
52+
}
53+
54+
public void setApiName(String apiName) {
55+
this.apiName = apiName;
56+
}
57+
58+
public String getOrgName() {
59+
return orgName;
60+
}
61+
62+
public void setOrgName(String orgName) {
63+
this.orgName = orgName;
64+
}
65+
66+
public String getProvider() {
67+
return provider;
68+
}
69+
70+
public void setProvider(String provider) {
71+
this.provider = provider;
72+
}
73+
74+
public String getApiCategory() {
75+
return apiCategory;
76+
}
77+
78+
public void setApiCategory(String apiCategory) {
79+
this.apiCategory = apiCategory;
80+
}
81+
82+
public String getApiDescription() {
83+
return apiDescription;
84+
}
85+
86+
public void setApiDescription(String apiDescription) {
87+
this.apiDescription = apiDescription;
88+
}
89+
90+
public String getVisibility() {
91+
return visibility;
92+
}
93+
94+
public void setVisibility(String visibility) {
95+
this.visibility = visibility;
96+
}
97+
98+
public List<String> getVisibleGroups() {
99+
return visibleGroups;
100+
}
101+
102+
public void setVisibleGroups(List<String> visibleGroups) {
103+
this.visibleGroups = visibleGroups;
104+
}
105+
106+
public Owners getOwners() {
107+
return owners;
108+
}
109+
110+
public void setOwners(Owners owners) {
111+
this.owners = owners;
112+
}
113+
114+
public String getApiVersion() {
115+
return apiVersion;
116+
}
117+
118+
public void setApiVersion(String apiVersion) {
119+
this.apiVersion = apiVersion;
120+
}
121+
122+
public String getApiType() {
123+
return apiType;
124+
}
125+
126+
public void setApiType(String apiType) {
127+
this.apiType = apiType;
128+
}
129+
130+
public static class Owners {
131+
private String technicalOwner;
132+
private String technicalOwnerEmail;
133+
private String businessOwner;
134+
private String businessOwnerEmail;
135+
136+
public String getTechnicalOwner() {
137+
return technicalOwner;
138+
}
139+
140+
public void setTechnicalOwner(String technicalOwner) {
141+
this.technicalOwner = technicalOwner;
142+
}
143+
144+
public String getTechnicalOwnerEmail() {
145+
return technicalOwnerEmail;
146+
}
147+
148+
public void setTechnicalOwnerEmail(String technicalOwnerEmail) {
149+
this.technicalOwnerEmail = technicalOwnerEmail;
150+
}
151+
152+
public String getBusinessOwner() {
153+
return businessOwner;
154+
}
155+
156+
public void setBusinessOwner(String businessOwner) {
157+
this.businessOwner = businessOwner;
158+
}
159+
160+
public String getBusinessOwnerEmail() {
161+
return businessOwnerEmail;
162+
}
163+
164+
public void setBusinessOwnerEmail(String businessOwnerEmail) {
165+
this.businessOwnerEmail = businessOwnerEmail;
166+
}
167+
}
168+
}
169+
170+
public static class EndPoints {
171+
private String sandboxURL;
172+
private String productionURL;
173+
174+
175+
public String getSandboxURL() {
176+
return sandboxURL;
177+
}
178+
179+
public void setSandboxURL(String sandboxURL) {
180+
this.sandboxURL = sandboxURL;
181+
}
182+
183+
public String getProductionURL() {
184+
return productionURL;
185+
}
186+
187+
public void setProductionURL(String productionURL) {
188+
this.productionURL = productionURL;
189+
}
190+
}
191+
192+
public ApiInfo getApiInfo() {
193+
return apiInfo;
194+
}
195+
196+
public void setApiInfo(ApiInfo apiInfo) {
197+
this.apiInfo = apiInfo;
198+
}
199+
200+
public List<String> getSubscriptionPolicies() {
201+
return subscriptionPolicies;
202+
}
203+
204+
public void setSubscriptionPolicies(List<String> subscriptionPolicies) {
205+
this.subscriptionPolicies = subscriptionPolicies;
206+
}
207+
208+
public EndPoints getEndPoints() {
209+
return endPoints;
210+
}
211+
212+
public void setEndPoints(EndPoints endPoints) {
213+
this.endPoints = endPoints;
214+
}
215+
}

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/NewDevPortalHandler.java

+50-56
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
import org.apache.http.impl.client.HttpClients;
3232
import org.apache.http.ssl.SSLContexts;
3333
import org.apache.http.util.EntityUtils;
34-
import org.json.simple.JSONArray;
35-
import org.json.simple.JSONObject;
3634
import org.wso2.carbon.apimgt.api.APIConsumer;
3735
import org.wso2.carbon.apimgt.impl.APIConstants;
3836
import org.wso2.carbon.apimgt.impl.APIManagerConfiguration;
@@ -42,8 +40,8 @@
4240
import org.wso2.carbon.apimgt.api.model.API;
4341
import org.wso2.carbon.apimgt.api.model.ApiTypeWrapper;
4442
import org.wso2.carbon.apimgt.api.model.Tier;
43+
import org.wso2.carbon.apimgt.impl.dto.devportal.ApiMetaDataDTO;
4544
import com.fasterxml.jackson.core.JsonProcessingException;
46-
import java.io.File;
4745
import java.io.FileInputStream;
4846
import java.io.IOException;
4947
import java.net.URISyntaxException;
@@ -52,15 +50,21 @@
5250
import java.util.Map;
5351
import java.util.ArrayList;
5452
import java.util.List;
55-
import java.util.Collections;
56-
import java.util.HashMap;
53+
import java.util.Objects;
5754
import java.util.Arrays;
58-
55+
import java.util.concurrent.ConcurrentHashMap;
5956
import com.fasterxml.jackson.databind.ObjectMapper;
6057
import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder;
6158
import org.wso2.carbon.base.ServerConfiguration;
6259

60+
/**
61+
* This class used to handle newly introduced 2025 version of Developer Portal's configuration with APIM.
62+
*/
6363
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<>();
6468

6569
private static class HttpResponseData {
6670
private final int statusCode;
@@ -80,11 +84,6 @@ private String getResponseBody() {
8084
}
8185
}
8286

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-
8887
public static boolean isNewPortalEnabled() {
8988
return Boolean.parseBoolean(getConfigProperty(APIConstants.API_STORE_NEW_PORTAL_ENABLED, "false"));
9089
}
@@ -256,38 +255,40 @@ private static String getApiId(String orgId, String apiName, String apiVersion,
256255
}
257256
}
258257

259-
// Data Structuring Related Methods
260-
261258
private static String getApiMetaData(ApiTypeWrapper apiTypeWrapper) throws APIManagementException {
262259
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");
272269
} 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));
276272
}
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()));
280279

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()));
284283

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);
289285

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+
}
291292
}
292293

293294
private static String getSandboxEndpoint(String jsonString) throws APIManagementException {
@@ -328,41 +329,34 @@ private static String getType(String type) {
328329
}
329330
}
330331

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<>();
333334
for (Object tier : tiers) {
334335
if (tier instanceof Tier) {
335336
Tier tierObject = (Tier) tier;
336337
String name = tierObject.getName();
337338
if (name != null) {
338-
subscriptionPolicies.add(Collections.singletonMap("policyName", name));
339+
subscriptionPolicies.add(name); // Add tier name directly
339340
}
340341
}
341342
}
342343
return subscriptionPolicies;
343344
}
344345

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<>();
351348
if (api.getVisibleRoles() != null) {
352-
for (String role : api.getVisibleRoles().split(",")) {
353-
visibleGroupsArray.add(role);
354-
}
349+
visibleGroupsList = Arrays.asList(api.getVisibleRoles().split(","));
355350
}
356-
return visibleGroupsArray;
351+
return visibleGroupsList;
357352
}
358353

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(), ""));
366360
return owners;
367361
}
368362

0 commit comments

Comments
 (0)