Search in sources :

Example 1 with Aai20Document

use of io.apicurio.datamodels.asyncapi.v2.models.Aai20Document 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);
}
Also used : AaiChannelItem(io.apicurio.datamodels.asyncapi.models.AaiChannelItem) Aai20Document(io.apicurio.datamodels.asyncapi.v2.models.Aai20Document) Aai20Server(io.apicurio.datamodels.asyncapi.v2.models.Aai20Server) Aai20ChannelItem(io.apicurio.datamodels.asyncapi.v2.models.Aai20ChannelItem)

Example 2 with Aai20Document

use of io.apicurio.datamodels.asyncapi.v2.models.Aai20Document in project carbon-apimgt by wso2.

the class AsyncApiParser method updateAsyncAPIDefinition.

public String updateAsyncAPIDefinition(String oldDefinition, API apiToUpdate) {
    Aai20Document document = (Aai20Document) Library.readDocumentFromJSONString(oldDefinition);
    if (document.components == null) {
        document.components = document.createComponents();
    }
    // add scopes
    if (document.components.securitySchemes == null) {
        document.components.securitySchemes = new HashMap<>();
    }
    Aai20SecurityScheme oauth2SecurityScheme = new Aai20SecurityScheme(document.components, APIConstants.DEFAULT_API_SECURITY_OAUTH2);
    oauth2SecurityScheme.type = APIConstants.DEFAULT_API_SECURITY_OAUTH2;
    if (oauth2SecurityScheme.flows == null) {
        oauth2SecurityScheme.flows = new Aai20OAuthFlows(oauth2SecurityScheme);
    }
    if (oauth2SecurityScheme.flows.implicit == null) {
        oauth2SecurityScheme.flows.implicit = new Aai20ImplicitOAuthFlow(oauth2SecurityScheme.flows);
    }
    oauth2SecurityScheme.flows.implicit.authorizationUrl = "http://localhost:9999";
    Map<String, String> scopes = new HashMap<>();
    Map<String, String> scopeBindings = new HashMap<>();
    Iterator<Scope> iterator = apiToUpdate.getScopes().iterator();
    while (iterator.hasNext()) {
        Scope scope = iterator.next();
        scopes.put(scope.getName(), scope.getDescription());
        scopeBindings.put(scope.getName(), scope.getRoles());
    }
    oauth2SecurityScheme.flows.implicit.scopes = scopes;
    Extension xScopeBindings = oauth2SecurityScheme.flows.implicit.createExtension();
    xScopeBindings.name = APIConstants.SWAGGER_X_SCOPES_BINDINGS;
    xScopeBindings.value = scopeBindings;
    oauth2SecurityScheme.flows.implicit.addExtension(APIConstants.SWAGGER_X_SCOPES_BINDINGS, xScopeBindings);
    document.components.securitySchemes.put(APIConstants.DEFAULT_API_SECURITY_OAUTH2, oauth2SecurityScheme);
    return Library.writeDocumentToJSONString(document);
}
Also used : Extension(io.apicurio.datamodels.core.models.Extension) Aai20SecurityScheme(io.apicurio.datamodels.asyncapi.v2.models.Aai20SecurityScheme) Aai20ImplicitOAuthFlow(io.apicurio.datamodels.asyncapi.v2.models.Aai20ImplicitOAuthFlow) Scope(org.wso2.carbon.apimgt.api.model.Scope) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Aai20Document(io.apicurio.datamodels.asyncapi.v2.models.Aai20Document) Aai20OAuthFlows(io.apicurio.datamodels.asyncapi.v2.models.Aai20OAuthFlows)

Example 3 with Aai20Document

use of io.apicurio.datamodels.asyncapi.v2.models.Aai20Document in project carbon-apimgt by wso2.

the class SolaceApiParser method getVendorFromExtension.

/**
 * This method will extract the vendor provider or the API specification form the extensions list
 *
 * @param definition String
 * @return String
 */
@Override
public String getVendorFromExtension(String definition) {
    Aai20Document aai20Document = (Aai20Document) Library.readDocumentFromJSONString(definition);
    Extension origin = aai20Document.info.getExtension("x-origin");
    if (origin != null) {
        ObjectMapper objectMapper = new ObjectMapper();
        Map originMap = objectMapper.convertValue(origin.value, Map.class);
        if (originMap.containsKey("vendor")) {
            if (SolaceConstants.SOLACE_ENVIRONMENT.equalsIgnoreCase(originMap.get("vendor").toString())) {
                return SolaceConstants.SOLACE_ENVIRONMENT;
            }
        }
    }
    return null;
}
Also used : Extension(io.apicurio.datamodels.core.models.Extension) Aai20Document(io.apicurio.datamodels.asyncapi.v2.models.Aai20Document) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with Aai20Document

use of io.apicurio.datamodels.asyncapi.v2.models.Aai20Document 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;
}
Also used : AaiChannelItem(io.apicurio.datamodels.asyncapi.models.AaiChannelItem) Aai20Document(io.apicurio.datamodels.asyncapi.v2.models.Aai20Document) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 5 with Aai20Document

use of io.apicurio.datamodels.asyncapi.v2.models.Aai20Document in project carbon-apimgt by wso2.

the class AsyncApiParser method getScopes.

@Override
public Set<Scope> getScopes(String resourceConfigsJSON) throws APIManagementException {
    Set<Scope> scopeSet = new LinkedHashSet<>();
    Aai20Document document = (Aai20Document) Library.readDocumentFromJSONString(resourceConfigsJSON);
    if (document.components != null && document.components.securitySchemes != null) {
        Aai20SecurityScheme oauth2 = (Aai20SecurityScheme) document.components.securitySchemes.get("oauth2");
        if (oauth2 != null && oauth2.flows != null && oauth2.flows.implicit != null) {
            Map<String, String> scopes = oauth2.flows.implicit.scopes;
            Extension xScopesBindings = oauth2.flows.implicit.getExtension(APIConstants.SWAGGER_X_SCOPES_BINDINGS);
            Map<String, String> scopeBindings = new HashMap<>();
            if (xScopesBindings != null) {
                scopeBindings = (Map<String, String>) xScopesBindings.value;
            }
            if (scopes != null) {
                for (Map.Entry<String, String> entry : scopes.entrySet()) {
                    Scope scope = new Scope();
                    scope.setKey(entry.getKey());
                    scope.setName(entry.getKey());
                    scope.setDescription(entry.getValue());
                    String scopeBinding = scopeBindings.get(scope.getKey());
                    if (scopeBinding != null) {
                        scope.setRoles(scopeBinding);
                    }
                    scopeSet.add(scope);
                }
            }
        }
    }
    return scopeSet;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Extension(io.apicurio.datamodels.core.models.Extension) Aai20SecurityScheme(io.apicurio.datamodels.asyncapi.v2.models.Aai20SecurityScheme) Scope(org.wso2.carbon.apimgt.api.model.Scope) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Aai20Document(io.apicurio.datamodels.asyncapi.v2.models.Aai20Document) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

Aai20Document (io.apicurio.datamodels.asyncapi.v2.models.Aai20Document)10 HashMap (java.util.HashMap)6 AaiChannelItem (io.apicurio.datamodels.asyncapi.models.AaiChannelItem)5 Extension (io.apicurio.datamodels.core.models.Extension)5 LinkedHashMap (java.util.LinkedHashMap)5 Map (java.util.Map)5 Aai20ChannelItem (io.apicurio.datamodels.asyncapi.v2.models.Aai20ChannelItem)3 LinkedHashSet (java.util.LinkedHashSet)3 Scope (org.wso2.carbon.apimgt.api.model.Scope)3 Aai20Operation (io.apicurio.datamodels.asyncapi.v2.models.Aai20Operation)2 Aai20SecurityScheme (io.apicurio.datamodels.asyncapi.v2.models.Aai20SecurityScheme)2 Aai20Server (io.apicurio.datamodels.asyncapi.v2.models.Aai20Server)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 AaiOperation (io.apicurio.datamodels.asyncapi.models.AaiOperation)1 AaiSecurityScheme (io.apicurio.datamodels.asyncapi.models.AaiSecurityScheme)1 Aai20ImplicitOAuthFlow (io.apicurio.datamodels.asyncapi.v2.models.Aai20ImplicitOAuthFlow)1 Aai20OAuthFlows (io.apicurio.datamodels.asyncapi.v2.models.Aai20OAuthFlows)1