Search in sources :

Example 1 with WebSocketTopicMappingConfiguration

use of org.wso2.carbon.apimgt.api.model.WebSocketTopicMappingConfiguration in project carbon-apimgt by wso2.

the class TemplateBuilderUtil method addGqlWebSocketTopicMappings.

/**
 * This method is used to add websocket topic mappings for GraphQL subscription. Here both production and sandbox
 * endpoint urls are added under single wild card resource.
 *
 * @param api GraphQL API
 */
public static void addGqlWebSocketTopicMappings(API api) {
    org.json.JSONObject endpointConfiguration = new org.json.JSONObject(api.getEndpointConfig()).getJSONObject(APIConstants.WS_PROTOCOL);
    String sandboxEndpointUrl = !endpointConfiguration.isNull(APIConstants.API_DATA_SANDBOX_ENDPOINTS) ? endpointConfiguration.getJSONObject(APIConstants.API_DATA_SANDBOX_ENDPOINTS).getString(APIConstants.ENDPOINT_URL) : null;
    String productionEndpointUrl = !endpointConfiguration.isNull(APIConstants.API_DATA_PRODUCTION_ENDPOINTS) ? endpointConfiguration.getJSONObject(APIConstants.API_DATA_PRODUCTION_ENDPOINTS).getString(APIConstants.ENDPOINT_URL) : null;
    Map<String, String> endpoints = new HashMap<>();
    if (sandboxEndpointUrl != null) {
        endpoints.put(APIConstants.GATEWAY_ENV_TYPE_SANDBOX, sandboxEndpointUrl);
    }
    if (productionEndpointUrl != null) {
        endpoints.put(APIConstants.GATEWAY_ENV_TYPE_PRODUCTION, productionEndpointUrl);
    }
    Map<String, Map<String, String>> perTopicMappings = new HashMap<>();
    perTopicMappings.put("/*", endpoints);
    api.setWebSocketTopicMappingConfiguration(new WebSocketTopicMappingConfiguration(perTopicMappings));
    addWebsocketTopicResourceKeys(api);
}
Also used : HashMap(java.util.HashMap) WebSocketTopicMappingConfiguration(org.wso2.carbon.apimgt.api.model.WebSocketTopicMappingConfiguration) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with WebSocketTopicMappingConfiguration

use of org.wso2.carbon.apimgt.api.model.WebSocketTopicMappingConfiguration in project carbon-apimgt by wso2.

the class TemplateBuilderUtilTest method testAddGqlWebSocketTopicMappings.

@Test
public void testAddGqlWebSocketTopicMappings() {
    API api = new API(new APIIdentifier("admin", "GraphQLAPI", "1.0"));
    String endpointConfig = "{\"endpoint_type\":\"graphql\", \n" + "\"http\":{\"endpoint_type\":\"http\",\n" + "\"sandbox_endpoints\":{\"url\":\"https://sandbox.com\"},\n" + "\"production_endpoints\":{\"url\":\"https://production.com\"}},\n" + "\"ws\":{\"endpoint_type\":\"ws\",\n" + "\"sandbox_endpoints\":{\"url\":\"" + wsSandEndpoint + "\"},\n" + "\"production_endpoints\":{\"url\":\"" + wsProdEndpoint + "\"}}}";
    api.setEndpointConfig(endpointConfig);
    TemplateBuilderUtil.addGqlWebSocketTopicMappings(api);
    WebSocketTopicMappingConfiguration topicMappingConfiguration = api.getWebSocketTopicMappingConfiguration();
    Assert.assertNotNull(topicMappingConfiguration);
    Map<String, String> mappings = topicMappingConfiguration.getMappings().get(wildCardResource);
    Assert.assertNotNull(mappings);
    Assert.assertEquals(mappings.get(APIConstants.GATEWAY_ENV_TYPE_SANDBOX), wsSandEndpoint);
    Assert.assertEquals(mappings.get(APIConstants.GATEWAY_ENV_TYPE_PRODUCTION), wsProdEndpoint);
    Assert.assertEquals(mappings.get("resourceKey"), mappingWildCard);
    // test endpoint config wit only production type endpoints
    api = new API(new APIIdentifier("admin", "GraphQLAPI", "1.0"));
    api.setEndpointConfig("{\"endpoint_type\":\"graphql\", \n" + "\"http\":{\"endpoint_type\":\"http\",\n" + "\"production_endpoints\":{\"url\":\"https://production.com\"}},\n" + "\"ws\":{\"endpoint_type\":\"ws\",\n" + "\"production_endpoints\":{\"url\":\"" + wsProdEndpoint + "\"}}}");
    TemplateBuilderUtil.addGqlWebSocketTopicMappings(api);
    topicMappingConfiguration = api.getWebSocketTopicMappingConfiguration();
    mappings = topicMappingConfiguration.getMappings().get(wildCardResource);
    Assert.assertNull(mappings.get(APIConstants.GATEWAY_ENV_TYPE_SANDBOX));
    Assert.assertEquals(mappings.get(APIConstants.GATEWAY_ENV_TYPE_PRODUCTION), wsProdEndpoint);
}
Also used : WebSocketTopicMappingConfiguration(org.wso2.carbon.apimgt.api.model.WebSocketTopicMappingConfiguration) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Test(org.junit.Test)

Example 3 with WebSocketTopicMappingConfiguration

use of org.wso2.carbon.apimgt.api.model.WebSocketTopicMappingConfiguration in project carbon-apimgt by wso2.

the class TemplateBuilderUtilTest method addAddGqlWebSocketResourceEndpoints.

@Test
public void addAddGqlWebSocketResourceEndpoints() throws Exception {
    APITemplateBuilder apiTemplateBuilder = Mockito.mock(APITemplateBuilder.class);
    API api = new API(new APIIdentifier("admin", "API", "1.0"));
    Set<URITemplate> uriTemplates = new HashSet<>();
    URITemplate template = new URITemplate();
    template.setAuthType("Any");
    template.setHTTPVerb("POST");
    template.setHttpVerbs("POST");
    template.setUriTemplate(wildCardResource);
    uriTemplates.add(template);
    template = new URITemplate();
    template.setUriTemplate(wildCardResource);
    uriTemplates.add(template);
    api.setUriTemplates(uriTemplates);
    Map<String, Map<String, String>> perTopicMappings = new HashMap<>();
    Map<String, String> endpoints = new HashMap<>();
    endpoints.put(APIConstants.GATEWAY_ENV_TYPE_PRODUCTION, wsProdEndpoint);
    endpoints.put(APIConstants.GATEWAY_ENV_TYPE_SANDBOX, wsSandEndpoint);
    perTopicMappings.put(wildCardResource, endpoints);
    WebSocketTopicMappingConfiguration webSocketTopicMappingConfiguration = new WebSocketTopicMappingConfiguration(perTopicMappings);
    webSocketTopicMappingConfiguration.setResourceKey(wildCardResource, mappingWildCard);
    api.setWebSocketTopicMappingConfiguration(webSocketTopicMappingConfiguration);
    api.setType(APIConstants.GRAPHQL_API);
    GatewayAPIDTO gatewayAPIDTO = new GatewayAPIDTO();
    GatewayContentDTO dummyHttpProdEpContentDTO = new GatewayContentDTO();
    dummyHttpProdEpContentDTO.setName("API--v1.0_APIproductionEndpoint");
    dummyHttpProdEpContentDTO.setContent("dummy content");
    GatewayContentDTO[] gatewayContentDTOS = new GatewayContentDTO[1];
    gatewayContentDTOS[0] = dummyHttpProdEpContentDTO;
    gatewayAPIDTO.setEndpointEntriesToBeAdd(gatewayContentDTOS);
    String dummyProdEndpointConfig = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><endpoint name=\"" + wsProdEpName + "\">dummy prod content</endpoint>";
    String dummySandboxEndpointConfig = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><endpoint name=\"" + wsSandEpName + "\">dummy sandbox content</endpoint>";
    Mockito.when(apiTemplateBuilder.getConfigStringForWebSocketEndpointTemplate(APIConstants.GATEWAY_ENV_TYPE_PRODUCTION, mappingWildCard, wsProdEndpoint)).thenReturn(dummyProdEndpointConfig);
    Mockito.when(apiTemplateBuilder.getConfigStringForWebSocketEndpointTemplate(APIConstants.GATEWAY_ENV_TYPE_SANDBOX, mappingWildCard, wsSandEndpoint)).thenReturn(dummySandboxEndpointConfig);
    TemplateBuilderUtil.addWebSocketResourceEndpoints(api, apiTemplateBuilder, gatewayAPIDTO);
    GatewayContentDTO[] endpointEntries = gatewayAPIDTO.getEndpointEntriesToBeAdd();
    Assert.assertEquals(endpointEntries.length, 3);
    boolean httpEpConfigPresent = false;
    boolean wsProdEpConfigPresent = false;
    boolean wsSandEPConfigPresent = false;
    for (int i = 0; i < 3; i++) {
        if (dummyHttpProdEpContentDTO.getName().equals(endpointEntries[i].getName())) {
            httpEpConfigPresent = true;
            Assert.assertEquals(endpointEntries[i].getContent(), dummyHttpProdEpContentDTO.getContent());
        }
        if (wsProdEpName.equals(endpointEntries[i].getName())) {
            wsProdEpConfigPresent = true;
            Assert.assertEquals(endpointEntries[i].getContent(), dummyProdEndpointConfig);
        }
        if (wsSandEpName.equals(endpointEntries[i].getName())) {
            wsSandEPConfigPresent = true;
            Assert.assertEquals(endpointEntries[i].getContent(), dummySandboxEndpointConfig);
        }
    }
    Assert.assertTrue(wsProdEpConfigPresent);
    Assert.assertTrue(wsSandEPConfigPresent);
    Assert.assertTrue(httpEpConfigPresent);
}
Also used : GatewayAPIDTO(org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO) HashMap(java.util.HashMap) WebSocketTopicMappingConfiguration(org.wso2.carbon.apimgt.api.model.WebSocketTopicMappingConfiguration) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) APITemplateBuilder(org.wso2.carbon.apimgt.impl.template.APITemplateBuilder) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) HashMap(java.util.HashMap) Map(java.util.Map) GatewayContentDTO(org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with WebSocketTopicMappingConfiguration

use of org.wso2.carbon.apimgt.api.model.WebSocketTopicMappingConfiguration in project carbon-apimgt by wso2.

the class TemplateBuilderUtil method addWebsocketTopicMappings.

private static void addWebsocketTopicMappings(API api, APIDTO apidto) {
    org.json.JSONObject endpointConfiguration = new org.json.JSONObject(api.getEndpointConfig());
    String sandboxEndpointUrl = !endpointConfiguration.isNull(APIConstants.API_DATA_SANDBOX_ENDPOINTS) ? endpointConfiguration.getJSONObject(APIConstants.API_DATA_SANDBOX_ENDPOINTS).getString("url") : null;
    String productionEndpointUrl = !endpointConfiguration.isNull(APIConstants.API_DATA_PRODUCTION_ENDPOINTS) ? endpointConfiguration.getJSONObject(APIConstants.API_DATA_PRODUCTION_ENDPOINTS).getString("url") : null;
    Map<String, Map<String, String>> perTopicMappings = new HashMap<>();
    for (APIOperationsDTO operation : apidto.getOperations()) {
        String key = operation.getTarget();
        String mapping = operation.getUriMapping() == null ? "" : Paths.get("/", operation.getUriMapping()).toString();
        Map<String, String> endpoints = new HashMap<>();
        if (sandboxEndpointUrl != null) {
            endpoints.put(APIConstants.GATEWAY_ENV_TYPE_SANDBOX, sandboxEndpointUrl + mapping);
        }
        if (productionEndpointUrl != null) {
            endpoints.put(APIConstants.GATEWAY_ENV_TYPE_PRODUCTION, productionEndpointUrl + mapping);
        }
        perTopicMappings.put(key, endpoints);
    }
    api.setWebSocketTopicMappingConfiguration(new WebSocketTopicMappingConfiguration(perTopicMappings));
    addWebsocketTopicResourceKeys(api);
}
Also used : JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) WebSocketTopicMappingConfiguration(org.wso2.carbon.apimgt.api.model.WebSocketTopicMappingConfiguration) APIOperationsDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIOperationsDTO) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

WebSocketTopicMappingConfiguration (org.wso2.carbon.apimgt.api.model.WebSocketTopicMappingConfiguration)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Test (org.junit.Test)2 API (org.wso2.carbon.apimgt.api.model.API)2 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)2 HashSet (java.util.HashSet)1 JSONObject (org.json.simple.JSONObject)1 GatewayAPIDTO (org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO)1 GatewayContentDTO (org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO)1 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)1 APITemplateBuilder (org.wso2.carbon.apimgt.impl.template.APITemplateBuilder)1 APIOperationsDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIOperationsDTO)1