Search in sources :

Example 21 with APIConfigContext

use of org.wso2.carbon.apimgt.core.template.APIConfigContext in project carbon-apimgt by wso2.

the class APIStoreImpl method setGatewayDefinitionSource.

private void setGatewayDefinitionSource(CompositeAPI.Builder apiBuilder) throws APIManagementException {
    List<UriTemplate> list = new ArrayList<>(apiBuilder.getUriTemplates().values());
    List<TemplateBuilderDTO> resourceList = new ArrayList<>();
    String appId = null;
    List<CompositeAPIEndpointDTO> endpointDTOs = new ArrayList<CompositeAPIEndpointDTO>();
    try {
        appId = apiBuilder.getApplicationId();
        List<Subscription> subscriptions = getApiSubscriptionDAO().getAPISubscriptionsByApplication(apiBuilder.getApplicationId(), ApiType.STANDARD);
        for (Subscription subscription : subscriptions) {
            CompositeAPIEndpointDTO endpointDTO = new CompositeAPIEndpointDTO();
            API api = subscription.getApi();
            endpointDTO.setEndpointName(api.getName());
            // TODO: currently only HTTPS endpoint considered. Websocket APIs and http transport should considered
            endpointDTO.setTransportType(APIMgtConstants.HTTPS);
            // TODO: replace host with gateway domain host
            String endpointUrl = APIMgtConstants.HTTPS + APIMgtConstants.WEB_PROTOCOL_SUFFIX + config.getHostname() + "/" + api.getContext() + "/" + api.getVersion();
            endpointDTO.setEndpointUrl(endpointUrl);
            endpointDTOs.add(endpointDTO);
        }
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error while getting subscriptions of the application " + appId;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    }
    for (UriTemplate uriTemplate : list) {
        TemplateBuilderDTO dto = new TemplateBuilderDTO();
        dto.setTemplateId(uriTemplate.getTemplateId());
        dto.setUriTemplate(uriTemplate.getUriTemplate());
        dto.setHttpVerb(uriTemplate.getHttpVerb());
        resourceList.add(dto);
    }
    GatewaySourceGenerator gatewaySourceGenerator = getGatewaySourceGenerator();
    APIConfigContext apiConfigContext = new APIConfigContext(apiBuilder.build(), config.getGatewayPackageName());
    gatewaySourceGenerator.setApiConfigContext(apiConfigContext);
    String gatewayConfig = gatewaySourceGenerator.getCompositeAPIConfigStringFromTemplate(resourceList, endpointDTOs);
    if (log.isDebugEnabled()) {
        log.debug("API " + apiBuilder.getName() + "gateway config: " + gatewayConfig);
    }
    apiBuilder.gatewayConfig(gatewayConfig);
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) ArrayList(java.util.ArrayList) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) CompositeAPIEndpointDTO(org.wso2.carbon.apimgt.core.template.dto.CompositeAPIEndpointDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) TemplateBuilderDTO(org.wso2.carbon.apimgt.core.template.dto.TemplateBuilderDTO) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) APIConfigContext(org.wso2.carbon.apimgt.core.template.APIConfigContext)

Example 22 with APIConfigContext

use of org.wso2.carbon.apimgt.core.template.APIConfigContext in project carbon-apimgt by wso2.

the class EndpointBckConfigContextTest method testEndpointBckConfigContext.

@Test
public void testEndpointBckConfigContext() throws Exception {
    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    String url = "http://maps.googleapis.com/maps/api/geocode/json?address=Colombo";
    api.setUrl(url);
    api.setSandboxUrl(url);
    ConfigContext configcontext = new APIConfigContext(api);
    EndpointBckConfigContext endpointBckConfigContext = new EndpointBckConfigContext(configcontext, api);
    Assert.assertTrue(api.getEndpointConfig().contains(url));
    // setting an empty string as the endpoint config and checking the value which is returned
    api.setEndpointConfig("");
    String endpointConfig = "{\"production_endpoints\":{\"url\":\"" + api.getUrl() + "\", \"config\":null}," + "\"sandbox_endpoint\":{\"url\":\"" + api.getSandboxUrl() + "\"," + "\"config\":null},\"endpoint_type\":\"http\"}";
    EndpointBckConfigContext secondEndpointBckConfigContext = new EndpointBckConfigContext(configcontext, api);
    Assert.assertTrue(api.getEndpointConfig().contains(endpointConfig));
    // setting null as the endpoint config and checking the value which is returned
    api.setEndpointConfig(null);
    EndpointBckConfigContext thirdEndpointBckConfigContext = new EndpointBckConfigContext(configcontext, api);
    Assert.assertTrue(api.getEndpointConfig().contains(endpointConfig));
}
Also used : API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) EndpointBckConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.EndpointBckConfigContext) APIConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext) ConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext) EndpointBckConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.EndpointBckConfigContext) APIConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext) Test(org.junit.Test)

Example 23 with APIConfigContext

use of org.wso2.carbon.apimgt.core.template.APIConfigContext in project carbon-apimgt by wso2.

the class EnvironmentConfigContextTest method testEnvironmentConfigContext.

@Test
public void testEnvironmentConfigContext() throws Exception {
    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    String url = "http://maps.googleapis.com/maps/api/geocode/json?address=Colombo";
    String endpointConfig = "{\"production_endpoints\":{\"url\":\"" + url + "\", \"config\":null}," + "\"sandbox_endpoint\":{\"url\":\"" + url + "\",\"config\":null},\"endpoint_type\":\"http\"}";
    api.setEndpointConfig(endpointConfig);
    api.setUrl(url);
    api.setSandboxUrl(url);
    ConfigContext configcontext = new APIConfigContext(api);
    Environment environment = new Environment();
    environment.setType("production");
    EnvironmentConfigContext environmentConfigContext = new EnvironmentConfigContext(configcontext, environment);
    Assert.assertNotNull(environmentConfigContext.getContext().get("environment"));
    Assert.assertNotNull(environmentConfigContext.getContext().get("environmentType"));
}
Also used : Environment(org.wso2.carbon.apimgt.api.model.Environment) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) EnvironmentConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.EnvironmentConfigContext) EnvironmentConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.EnvironmentConfigContext) APIConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext) ConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext) APIConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext) Test(org.junit.Test)

Example 24 with APIConfigContext

use of org.wso2.carbon.apimgt.core.template.APIConfigContext in project carbon-apimgt by wso2.

the class ResourceConfigContextTest method testResourceConfigContextForAPI.

@Test
public void testResourceConfigContextForAPI() throws Exception {
    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    api.setUriTemplates(setAPIUriTemplates());
    ConfigContext configcontext = new APIConfigContext(api);
    ResourceConfigContext resourceConfigContext = new ResourceConfigContext(configcontext, api);
    resourceConfigContext.validate();
    Assert.assertNotNull(resourceConfigContext.getContext().get("resources"));
    // assign an empty URITemplate set and check the result
    Set<URITemplate> uriTemplates = new LinkedHashSet<URITemplate>();
    api.setUriTemplates(uriTemplates);
    configcontext = new APIConfigContext(api);
    resourceConfigContext = new ResourceConfigContext(configcontext, api);
    String errorClass = "org.wso2.carbon.apimgt.api.APIManagementException";
    String expectedErrorMessage = "At least one resource is required";
    try {
        resourceConfigContext.validate();
    } catch (APIManagementException e) {
        Assert.assertTrue(errorClass.equalsIgnoreCase(e.getClass().getName()));
        Assert.assertTrue(expectedErrorMessage.equalsIgnoreCase(e.getMessage()));
    }
    // set a null value for URITemplate and check the result
    api.setUriTemplates(null);
    configcontext = new APIConfigContext(api);
    resourceConfigContext = new ResourceConfigContext(configcontext, api);
    try {
        resourceConfigContext.validate();
    } catch (APIManagementException e) {
        Assert.assertTrue(errorClass.equalsIgnoreCase(e.getClass().getName()));
        Assert.assertTrue(expectedErrorMessage.equalsIgnoreCase(e.getMessage()));
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ResourceConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ResourceConfigContext) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) ResourceConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ResourceConfigContext) APIConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext) ConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext) APIConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext) Test(org.junit.Test)

Example 25 with APIConfigContext

use of org.wso2.carbon.apimgt.core.template.APIConfigContext in project carbon-apimgt by wso2.

the class SecurityConfigContextTest method testSecurityConfigContextOauth.

@Test
public void testSecurityConfigContextOauth() throws Exception {
    String json = "{\"endpoint_security\":{\n" + "  \"production\":{\n" + "    \"enabled\":true,\n" + "    \"type\":\"oauth\",\n" + "    \"clientId\":\"123-456\",\n" + "    \"clientSecret\":\"admin\",\n" + "    \"grantType\":\"client_credentials\"\n" + "  },\n" + "  \"sandbox\":{\n" + "    \"enabled\":true,\n" + "    \"type\":\"oauth\",\n" + "    \"clientId\":\"123-4567\",\n" + "    \"clientSecret\":\"admin\",\n" + "    \"grantType\":\"client_credentials\"\n" + "  }\n" + "  }\n" + "}";
    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setUuid(UUID.randomUUID().toString());
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    api.setTransports(Constants.TRANSPORT_HTTP);
    api.setEndpointConfig(json);
    ConfigContext configcontext = new APIConfigContext(api);
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_SECUREVAULT_ENABLE)).thenReturn("true");
    SecurityConfigContext securityConfigContext = new SecurityConfigContextWrapper(configcontext, api, apiManagerConfiguration);
    securityConfigContext.validate();
    VelocityContext velocityContext = securityConfigContext.getContext();
    Assert.assertNotNull(velocityContext.get("endpoint_security"));
    Map<String, EndpointSecurityModel> endpointSecurityModelMap = (Map<String, EndpointSecurityModel>) velocityContext.get("endpoint_security");
    EndpointSecurityModel production = endpointSecurityModelMap.get("production");
    Assert.assertTrue("Property enabled cannot be false.", production.isEnabled());
    Assert.assertTrue("Property type cannot be other.", production.getType().equalsIgnoreCase("oauth"));
    Assert.assertTrue("Property clientid does not match.", "123-456".equals(production.getClientId()));
    Assert.assertEquals(production.getClientSecretAlias(), "TestAPI--v1.0.0--oauth--clientSecret--production");
    EndpointSecurityModel sandbox = endpointSecurityModelMap.get("sandbox");
    Assert.assertTrue("Property enabled cannot be false.", sandbox.isEnabled());
    Assert.assertTrue("Property type cannot be other.", sandbox.getType().equalsIgnoreCase("oauth"));
    Assert.assertTrue("Property username does not match.", "123-4567".equals(sandbox.getClientId()));
    Assert.assertEquals(sandbox.getClientSecretAlias(), "TestAPI--v1.0.0--oauth--clientSecret--sandbox");
    Assert.assertTrue("Property isSecureVaultEnabled cannot be false. ", velocityContext.get("isSecureVaultEnabled").equals(true));
}
Also used : SecurityConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.SecurityConfigContext) VelocityContext(org.apache.velocity.VelocityContext) EndpointSecurityModel(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.EndpointSecurityModel) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) HashMap(java.util.HashMap) Map(java.util.Map) APIConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext) ConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext) SecurityConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.SecurityConfigContext) APIConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)17 APIConfigContext (org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext)17 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)16 ConfigContext (org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext)16 API (org.wso2.carbon.apimgt.api.model.API)14 VelocityContext (org.apache.velocity.VelocityContext)12 HashMap (java.util.HashMap)9 API (org.wso2.carbon.apimgt.core.models.API)8 Map (java.util.Map)7 APIConfigContext (org.wso2.carbon.apimgt.core.template.APIConfigContext)7 ArrayList (java.util.ArrayList)6 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)6 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)6 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)6 EndpointSecurityModel (org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.EndpointSecurityModel)6 SecurityConfigContext (org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.SecurityConfigContext)6 StringWriter (java.io.StringWriter)5 Template (org.apache.velocity.Template)5 VelocityEngine (org.apache.velocity.app.VelocityEngine)5 APITemplateException (org.wso2.carbon.apimgt.core.template.APITemplateException)4