Search in sources :

Example 21 with ConfigContext

use of org.wso2.carbon.apimgt.impl.template.ConfigContext in project carbon-apimgt by wso2.

the class GatewaySourceGeneratorImpl method getEndpointConfigStringFromTemplate.

@Override
public String getEndpointConfigStringFromTemplate(Endpoint endpoint) throws APITemplateException {
    StringWriter writer = new StringWriter();
    String templatePath = "resources" + File.separator + "template" + File.separator + "endpoint.xml";
    try {
        // build the context for template and apply the necessary decorators
        ConfigContext configcontext = new EndpointContext(endpoint, packageName);
        VelocityContext context = configcontext.getContext();
        VelocityEngine velocityengine = new VelocityEngine();
        velocityengine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        velocityengine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        velocityengine.init();
        Template template = velocityengine.getTemplate(templatePath);
        template.merge(context, writer);
    } catch (ResourceNotFoundException e) {
        log.error("Template " + templatePath + " not Found", e);
        throw new APITemplateException("Template " + templatePath + " not Found", ExceptionCodes.TEMPLATE_EXCEPTION);
    } catch (ParseErrorException e) {
        log.error("Syntax error in " + templatePath, e);
        throw new APITemplateException("Syntax error in " + templatePath, ExceptionCodes.TEMPLATE_EXCEPTION);
    }
    return writer.toString();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) EndpointContext(org.wso2.carbon.apimgt.core.template.EndpointContext) VelocityContext(org.apache.velocity.VelocityContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader) APITemplateException(org.wso2.carbon.apimgt.core.template.APITemplateException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) CompositeAPIConfigContext(org.wso2.carbon.apimgt.core.template.CompositeAPIConfigContext) ConfigContext(org.wso2.carbon.apimgt.core.template.ConfigContext) APIConfigContext(org.wso2.carbon.apimgt.core.template.APIConfigContext) ResourceConfigContext(org.wso2.carbon.apimgt.core.template.ResourceConfigContext) Template(org.apache.velocity.Template)

Example 22 with ConfigContext

use of org.wso2.carbon.apimgt.impl.template.ConfigContext 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 ConfigContext

use of org.wso2.carbon.apimgt.impl.template.ConfigContext 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 ConfigContext

use of org.wso2.carbon.apimgt.impl.template.ConfigContext 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 ConfigContext

use of org.wso2.carbon.apimgt.impl.template.ConfigContext 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

VelocityContext (org.apache.velocity.VelocityContext)18 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 StringWriter (java.io.StringWriter)11 VelocityEngine (org.apache.velocity.app.VelocityEngine)11 Template (org.apache.velocity.Template)9 HashMap (java.util.HashMap)8 Map (java.util.Map)6 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)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 APITemplateException (org.wso2.carbon.apimgt.impl.template.APITemplateException)4 ConfigContext (org.wso2.carbon.apimgt.impl.template.ConfigContext)4 ArrayList (java.util.ArrayList)3 ParseErrorException (org.apache.velocity.exception.ParseErrorException)3 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)3 ClasspathResourceLoader (org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader)3