use of io.apicurio.datamodels.asyncapi.models.AaiChannelItem in project carbon-apimgt by wso2.
the class AsyncApiParser method getAsyncApiDefinitionForStore.
/**
* Update AsyncAPI definition for store
*
* @param api API
* @param asyncAPIDefinition AsyncAPI definition
* @param hostsWithSchemes host addresses with protocol mapping
* @return AsyncAPI definition
* @throws APIManagementException throws if an error occurred
*/
public String getAsyncApiDefinitionForStore(API api, String asyncAPIDefinition, Map<String, String> hostsWithSchemes) throws APIManagementException {
Aai20Document aai20Document = (Aai20Document) Library.readDocumentFromJSONString(asyncAPIDefinition);
String channelName = api.getContext();
String transports = api.getTransports();
String url = StringUtils.EMPTY;
String[] apiTransports = transports.split(",");
if (ArrayUtils.contains(apiTransports, APIConstants.WSS_PROTOCOL) && hostsWithSchemes.get(APIConstants.WSS_PROTOCOL) != null) {
url = hostsWithSchemes.get(APIConstants.WSS_PROTOCOL).trim().replace(APIConstants.WSS_PROTOCOL_URL_PREFIX, "");
}
if (ArrayUtils.contains(apiTransports, APIConstants.WSS_PROTOCOL) && hostsWithSchemes.get(APIConstants.WS_PROTOCOL) != null) {
if (StringUtils.isEmpty(url)) {
url = hostsWithSchemes.get(APIConstants.WS_PROTOCOL).trim().replace(APIConstants.WS_PROTOCOL_URL_PREFIX, "");
}
}
Aai20Server server = (Aai20Server) aai20Document.getServers().get(0);
server.url = url;
Map<String, AaiChannelItem> channels = aai20Document.channels;
Aai20ChannelItem channelDetails = null;
for (String x : channels.keySet()) {
channelDetails = (Aai20ChannelItem) channels.get(x);
aai20Document.channels.remove(x);
}
assert channelDetails != null;
channelDetails._name = channelName;
aai20Document.channels.put(channelName, channelDetails);
return Library.writeDocumentToJSONString(aai20Document);
}
use of io.apicurio.datamodels.asyncapi.models.AaiChannelItem in project carbon-apimgt by wso2.
the class SolaceAdminApis method buildAPIProductRequestBody.
/**
* Build the request body for API product creation request
*
* @param aai20Document Async definition of the Solace API
* @param environment name of the Environment in Solace
* @param apiNameWithContext name of the API with context
* @param registeredApiName name of the API in Solace
* @return org.json.JSON Object of request body
*/
private org.json.JSONObject buildAPIProductRequestBody(Aai20Document aai20Document, String environment, String apiNameWithContext, String registeredApiName) {
org.json.JSONObject requestBody = new org.json.JSONObject();
org.json.JSONArray apiName = new org.json.JSONArray();
apiName.put(registeredApiName);
requestBody.put("apis", apiName);
requestBody.put("approvalType", "auto");
if (aai20Document.info.description != null) {
requestBody.put("description", aai20Document.info.description);
} else {
requestBody.put("description", apiNameWithContext);
}
requestBody.put("displayName", apiNameWithContext);
requestBody.put("pubResources", new org.json.JSONArray());
requestBody.put("subResources", new org.json.JSONArray());
requestBody.put("name", apiNameWithContext);
org.json.JSONArray environments = new org.json.JSONArray();
environments.put(environment);
requestBody.put("environments", environments);
HashSet<String> parameters1 = new HashSet<>();
org.json.JSONArray attributes1 = new org.json.JSONArray();
for (AaiChannelItem channel : aai20Document.getChannels()) {
if (channel.parameters != null) {
Set<String> parameterKeySet = channel.parameters.keySet();
for (String parameterName : parameterKeySet) {
if (!parameters1.contains(parameterName)) {
AaiParameter parameterObject = channel.parameters.get(parameterName);
if (parameterObject.schema != null) {
org.json.JSONObject attributeObj = getAttributesFromParameterSchema(parameterName, parameterObject);
if (attributeObj != null) {
attributes1.put(attributeObj);
parameters1.add(parameterName);
}
} else if (parameterObject.$ref != null) {
if (aai20Document.components.parameters != null) {
AaiParameter parameterObject1 = aai20Document.components.parameters.get(parameterName);
if (parameterObject1.schema != null) {
org.json.JSONObject attributeObj = getAttributesFromParameterSchema(parameterName, parameterObject1);
if (attributeObj != null) {
attributes1.put(attributeObj);
parameters1.add(parameterName);
}
}
}
}
}
}
}
}
requestBody.put("attributes", attributes1);
HashSet<String> protocolsHashSet = new HashSet<>();
for (AaiChannelItem channel : aai20Document.getChannels()) {
protocolsHashSet.addAll(AsyncApiParser.getProtocols(channel));
}
org.json.JSONArray protocols = new org.json.JSONArray();
for (String protocol : protocolsHashSet) {
org.json.JSONObject protocolObject = new org.json.JSONObject();
protocolObject.put("name", protocol);
protocolObject.put("version", getProtocolVersion(protocol));
protocols.put(protocolObject);
}
requestBody.put("protocols", protocols);
return requestBody;
}
use of io.apicurio.datamodels.asyncapi.models.AaiChannelItem in project carbon-apimgt by wso2.
the class AsyncApiParser method getTransportProtocolsForAsyncAPI.
/**
* Get available transport protocols for the Async API
*
* @param definition Async API Definition
* @return List<String> List of available transport protocols
* @throws APIManagementException If the async env configuration if not provided properly
*/
public static List<String> getTransportProtocolsForAsyncAPI(String definition) throws APIManagementException {
Aai20Document aai20Document = (Aai20Document) Library.readDocumentFromJSONString(definition);
HashSet<String> asyncTransportProtocols = new HashSet<>();
for (AaiChannelItem channel : aai20Document.getChannels()) {
asyncTransportProtocols.addAll(getProtocols(channel));
}
ArrayList<String> asyncTransportProtocolsList = new ArrayList<>(asyncTransportProtocols);
return asyncTransportProtocolsList;
}
use of io.apicurio.datamodels.asyncapi.models.AaiChannelItem in project carbon-apimgt by wso2.
the class AsyncApiParser method getURITemplates.
public Set<URITemplate> getURITemplates(String apiDefinition, boolean includePublish) throws APIManagementException {
Set<URITemplate> uriTemplates = new HashSet<>();
Set<Scope> scopes = getScopes(apiDefinition);
Aai20Document document = (Aai20Document) Library.readDocumentFromJSONString(apiDefinition);
if (document.channels != null && document.channels.size() > 0) {
for (Map.Entry<String, AaiChannelItem> entry : document.channels.entrySet()) {
Aai20ChannelItem channel = (Aai20ChannelItem) entry.getValue();
if (includePublish && channel.publish != null) {
uriTemplates.add(buildURITemplate(entry.getKey(), APIConstants.HTTP_VERB_PUBLISH, (Aai20Operation) channel.publish, scopes, channel));
}
if (channel.subscribe != null) {
uriTemplates.add(buildURITemplate(entry.getKey(), APIConstants.HTTP_VERB_SUBSCRIBE, (Aai20Operation) channel.subscribe, scopes, channel));
}
}
}
return uriTemplates;
}
use of io.apicurio.datamodels.asyncapi.models.AaiChannelItem in project carbon-apimgt by wso2.
the class AsyncApiParser method buildWSUriMapping.
public Map<String, String> buildWSUriMapping(String apiDefinition) {
Map<String, String> wsUriMapping = new HashMap<>();
Aai20Document document = (Aai20Document) Library.readDocumentFromJSONString(apiDefinition);
for (Map.Entry<String, AaiChannelItem> entry : document.channels.entrySet()) {
AaiOperation publishOperation = entry.getValue().publish;
if (publishOperation != null) {
Extension xUriMapping = publishOperation.getExtension("x-uri-mapping");
if (xUriMapping != null) {
wsUriMapping.put("PUBLISH_" + entry.getKey(), xUriMapping.value.toString());
}
}
AaiOperation subscribeOperation = entry.getValue().subscribe;
if (subscribeOperation != null) {
Extension xUriMapping = subscribeOperation.getExtension("x-uri-mapping");
if (xUriMapping != null) {
wsUriMapping.put("SUBSCRIBE_" + entry.getKey(), xUriMapping.value.toString());
}
}
}
return wsUriMapping;
}
Aggregations