Search in sources :

Example 11 with ConfigContext

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext in project carbon-apimgt by wso2.

the class APITemplateBuilderImpl method getConfigStringForPrototypeScriptAPI.

@Override
public String getConfigStringForPrototypeScriptAPI(Environment environment) throws APITemplateException {
    StringWriter writer = new StringWriter();
    try {
        // build the context for template and apply the necessary decorators
        ConfigContext configcontext = new APIConfigContext(this.api);
        configcontext = new TransportConfigContext(configcontext, api);
        configcontext = new ResourceConfigContext(configcontext, api);
        configcontext = new EndpointBckConfigContext(configcontext, api);
        configcontext = new EndpointConfigContext(configcontext, api);
        configcontext = new SecurityConfigContext(configcontext, api);
        configcontext = new JwtConfigContext(configcontext);
        configcontext = new ResponseCacheConfigContext(configcontext, api);
        configcontext = new HandlerConfigContex(configcontext, handlers);
        configcontext = new EnvironmentConfigContext(configcontext, environment);
        configcontext = new TemplateUtilContext(configcontext);
        // @todo: this validation might be better to do when the builder is initialized.
        configcontext.validate();
        VelocityContext context = configcontext.getContext();
        context.internalGetKeys();
        /*  first, initialize velocity engine  */
        VelocityEngine velocityengine = new VelocityEngine();
        APIUtil.initializeVelocityContext(velocityengine);
        velocityengine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CarbonUtils.getCarbonHome());
        initVelocityEngine(velocityengine);
        Template t = velocityengine.getTemplate(this.getPrototypeTemplatePath());
        t.merge(context, writer);
    } catch (Exception e) {
        log.error("Velocity Error", e);
        throw new APITemplateException("Velocity Error", e);
    }
    return writer.toString();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) VelocityContext(org.apache.velocity.VelocityContext) UserStoreException(org.wso2.carbon.user.api.UserStoreException) RegistryException(org.wso2.carbon.registry.api.RegistryException) APITemplateException(org.wso2.carbon.apimgt.impl.template.APITemplateException) Template(org.apache.velocity.Template) StringWriter(java.io.StringWriter) APITemplateException(org.wso2.carbon.apimgt.impl.template.APITemplateException)

Example 12 with ConfigContext

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext in project carbon-apimgt by wso2.

the class APITemplateBuilderImpl method getConfigStringForEndpointTemplate.

/**
 * Sets the necessary variables to velocity context.
 *
 * @param endpointType Type of the endpoint : production or sandbox
 * @return The string of endpoint file content
 * @throws APITemplateException Thrown if an error occurred
 */
@Override
public String getConfigStringForEndpointTemplate(String endpointType) throws APITemplateException {
    StringWriter writer = new StringWriter();
    try {
        ConfigContext configcontext = new APIConfigContext(this.api);
        configcontext = new EndpointConfigContext(configcontext, this.apiProduct, api);
        configcontext = new TemplateUtilContext(configcontext);
        configcontext.validate();
        VelocityContext context = configcontext.getContext();
        context.internalGetKeys();
        VelocityEngine velocityengine = new VelocityEngine();
        APIUtil.initializeVelocityContext(velocityengine);
        velocityengine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CarbonUtils.getCarbonHome());
        initVelocityEngine(velocityengine);
        context.put("type", endpointType);
        Template template = velocityengine.getTemplate(this.getEndpointTemplatePath());
        template.merge(context, writer);
    } catch (Exception e) {
        log.error("Velocity Error");
        throw new APITemplateException("Velocity Error", e);
    }
    return writer.toString();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) APITemplateException(org.wso2.carbon.apimgt.impl.template.APITemplateException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) RegistryException(org.wso2.carbon.registry.api.RegistryException) APITemplateException(org.wso2.carbon.apimgt.impl.template.APITemplateException) Template(org.apache.velocity.Template)

Example 13 with ConfigContext

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext in project carbon-apimgt by wso2.

the class RESTToSOAPMsgTemplate method getMappingInSequence.

/**
 * gets in sequence for the soap operation
 *
 * @param mapping       soap to rest mapping json
 * @param method        http method for the resource
 * @param soapAction    soap action for the operation
 * @param namespace     soap namespace for the operation
 * @param soapNamespace soap namespace for the soap version
 * @return in sequence string
 */
public String getMappingInSequence(Map<String, String> mapping, String method, String soapAction, String namespace, String soapNamespace, JSONArray array) {
    ConfigContext configcontext = new SOAPToRESTConfigContext(mapping, method, soapAction, namespace, soapNamespace, array);
    StringWriter writer = new StringWriter();
    try {
        VelocityContext context = configcontext.getContext();
        context.internalGetKeys();
        VelocityEngine velocityengine = new VelocityEngine();
        APIUtil.initializeVelocityContext(velocityengine);
        velocityengine.init();
        org.apache.velocity.Template t = velocityengine.getTemplate(this.getInSeqTemplatePath());
        t.merge(context, writer);
    } catch (Exception e) {
        log.error("Velocity Error", e);
    }
    return writer.toString();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) ConfigContext(org.wso2.carbon.apimgt.impl.template.ConfigContext)

Example 14 with ConfigContext

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext in project carbon-apimgt by wso2.

the class ResourceConfigContextTest method testResourceConfigContextForAPIProduct.

@Test
public void testResourceConfigContextForAPIProduct() throws Exception {
    APIProductIdentifier apiProductIdentifier = new APIProductIdentifier("admin", "TestAPIProduct", "1.0.0", UUID.randomUUID().toString());
    APIProduct apiProduct = new APIProduct(apiProductIdentifier);
    apiProduct.setProductResources(getAPIProductResources(apiProductIdentifier));
    apiProduct.setType(APIConstants.API_PRODUCT);
    ConfigContext configcontext = new APIConfigContext(apiProduct);
    ResourceConfigContext resourceConfigContext = new ResourceConfigContext(configcontext, apiProduct);
    resourceConfigContext.validate();
    VelocityContext context = resourceConfigContext.getContext();
    Assert.assertNotNull(context.get("apiType"));
    Assert.assertEquals(context.get("apiType"), APIConstants.API_PRODUCT);
    Object aggregates = context.get("aggregates");
    Assert.assertNotNull(aggregates);
    Assert.assertTrue(aggregates instanceof List);
    List<APIProductResource> apiProductResources = (List<APIProductResource>) aggregates;
    Assert.assertTrue(assertAPIProductResourceList(apiProduct.getProductResources(), apiProductResources));
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) VelocityContext(org.apache.velocity.VelocityContext) ResourceConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ResourceConfigContext) List(java.util.List) 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 15 with ConfigContext

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext in project carbon-apimgt by wso2.

the class SecurityConfigContextTest method testSecurityConfigContextForAPIProduct.

@Test
public void testSecurityConfigContextForAPIProduct() throws Exception {
    APIProduct apiProduct = new APIProduct(new APIProductIdentifier("admin", "TestProduct", "1.0.0"));
    apiProduct.setUuid(UUID.randomUUID().toString());
    String apiid = UUID.randomUUID().toString();
    List<APIProductResource> apiProductResourceList = new ArrayList<>();
    APIProductResource apiProductResource = new APIProductResource();
    apiProductResource.setApiIdentifier(new APIIdentifier("admin_api1_v1"));
    apiProductResource.setApiId(apiid);
    Map<String, EndpointSecurity> endpointSecurityMap = new HashMap<>();
    EndpointSecurity endpointSecurity = new EndpointSecurity();
    endpointSecurity.setType("BASIC");
    endpointSecurity.setUsername("admin");
    endpointSecurity.setPassword("admin123");
    endpointSecurity.setEnabled(true);
    endpointSecurityMap.put("production", endpointSecurity);
    apiProductResource.setApiId(apiid);
    apiProductResource.setEndpointSecurityMap(endpointSecurityMap);
    apiProductResourceList.add(apiProductResource);
    apiProduct.setProductResources(apiProductResourceList);
    ConfigContext configcontext = new APIConfigContext(apiProduct);
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_SECUREVAULT_ENABLE)).thenReturn("true");
    Map<String, APIDTO> apidtoMap = new HashMap<>();
    apidtoMap.put(apiid, new APIDTO().name("api1").version("v1").provider("admin"));
    SecurityConfigContext securityConfigContext = new SecurityConfigContextWrapper(configcontext, apiProduct, apiManagerConfiguration, apidtoMap);
    securityConfigContext.validate();
    VelocityContext velocityContext = securityConfigContext.getContext();
    Assert.assertNotNull(velocityContext.get("endpoint_security"));
    Map<String, Map<String, EndpointSecurityModel>> endpointSecurityModelMap = (Map<String, Map<String, EndpointSecurityModel>>) velocityContext.get("endpoint_security");
    Map<String, EndpointSecurityModel> endpointSecurityModelMap1 = endpointSecurityModelMap.get(apiProductResource.getApiId());
    EndpointSecurityModel production = endpointSecurityModelMap1.get("production");
    Assert.assertTrue("Property enabled cannot be false.", production.isEnabled());
    Assert.assertTrue("Property type cannot be other.", production.getType().equalsIgnoreCase("basic"));
    Assert.assertTrue("Property username does not match.", "admin".equals(production.getUsername()));
    Assert.assertTrue("Property base64value does not match. ", new String(Base64.encodeBase64("admin:admin123".getBytes())).equalsIgnoreCase(production.getBase64EncodedPassword()));
    Assert.assertTrue("Property securevault_alias does not match.", "TestProduct--v1.0.0--api1--vv1--production".equalsIgnoreCase(production.getAlias()));
    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) HashMap(java.util.HashMap) VelocityContext(org.apache.velocity.VelocityContext) ArrayList(java.util.ArrayList) EndpointSecurity(org.wso2.carbon.apimgt.api.model.EndpointSecurity) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) EndpointSecurityModel(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.EndpointSecurityModel) 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