Search in sources :

Example 26 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 testSecurityConfigContextPerEndpointSandbox.

@Test
public void testSecurityConfigContextPerEndpointSandbox() throws Exception {
    String json = "{\"endpoint_security\":{\n" + "  \"sandbox\":{\n" + "    \"enabled\":true,\n" + "    \"type\":\"DIGEST\",\n" + "    \"username\":\"admin\",\n" + "    \"password\":\"admin123#QA\"\n" + "  }\n" + "  }\n" + "}";
    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    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 sandbox = endpointSecurityModelMap.get("sandbox");
    Assert.assertTrue("Property enabled cannot be false.", sandbox.isEnabled());
    Assert.assertTrue("Property type cannot be other.", sandbox.getType().equalsIgnoreCase("digest"));
    Assert.assertTrue("Property username does not match.", "admin".equals(sandbox.getUsername()));
    Assert.assertTrue("Property base64value does not match. ", new String(Base64.encodeBase64("admin:admin123#QA".getBytes())).equalsIgnoreCase(sandbox.getBase64EncodedPassword()));
    Assert.assertTrue("Property securevault_alias does not match.", "TestAPI--v1.0.0--sandbox".equalsIgnoreCase(sandbox.getAlias()));
    Assert.assertTrue("Property isSecureVaultEnabled cannot be false. ", velocityContext.get("isSecureVaultEnabled").equals(true));
    EndpointSecurityModel production = endpointSecurityModelMap.get("production");
    Assert.assertFalse("Property enabled cannot be true.", production.isEnabled());
}
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)

Example 27 with ConfigContext

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

the class TransportConfigContextTest method testTransportConfigContext.

@Test
public void testTransportConfigContext() throws Exception {
    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    api.setTransports(Constants.TRANSPORT_HTTP);
    ConfigContext configcontext = new APIConfigContext(api);
    TransportConfigContext transportConfigContext = new TransportConfigContext(configcontext, api);
    transportConfigContext.validate();
    Assert.assertTrue(Constants.TRANSPORT_HTTP.equalsIgnoreCase(transportConfigContext.getContext().get("transport").toString()));
    api.setTransports(Constants.TRANSPORT_HTTP + "," + Constants.TRANSPORT_HTTPS);
    configcontext = new APIConfigContext(api);
    transportConfigContext = new TransportConfigContext(configcontext, api);
    Assert.assertTrue(StringUtils.EMPTY.equalsIgnoreCase(transportConfigContext.getContext().get("transport").toString()));
}
Also used : API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) TransportConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.TransportConfigContext) APIConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext) TransportConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.TransportConfigContext) 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 28 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 getConfigStringForWebSocketEndpointTemplate.

@Override
public String getConfigStringForWebSocketEndpointTemplate(String endpointType, String resourceKey, String endpointUrl) throws APITemplateException {
    StringWriter writer = new StringWriter();
    try {
        ConfigContext configcontext = new APIConfigContext(this.api);
        configcontext = new EndpointBckConfigContext(configcontext, api);
        configcontext = new EndpointConfigContext(configcontext, 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 + "_endpoints");
        context.put("websocketResourceKey", resourceKey);
        context.put("endpointUrl", endpointUrl);
        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 29 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 getMappingOutSequence.

/**
 * gets out sequence for the soap operation
 *
 * @return out sequence for the converted soap operation
 */
public String getMappingOutSequence() {
    ConfigContext configcontext = new SOAPToRESTConfigContext();
    StringWriter writer = new StringWriter();
    try {
        VelocityContext context = configcontext.getContext();
        context.internalGetKeys();
        VelocityEngine velocityengine = new VelocityEngine();
        APIUtil.initializeVelocityContext(velocityengine);
        velocityengine.init();
        org.apache.velocity.Template template = velocityengine.getTemplate(this.getOutSeqTemplatePath());
        template.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 30 with ConfigContext

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

the class TemplateUtilContextTest method testTemplateUtilContext.

@Test
public void testTemplateUtilContext() throws Exception {
    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    ConfigContext configcontext = new APIConfigContext(api);
    TemplateUtilContext templateUtilContext = new TemplateUtilContext(configcontext);
    String xmlSampleText = "<data>TemplateUtilContextTest Class</data>";
    String xmlEscapedText = "&lt;data&gt;TemplateUtilContextTest Class&lt;/data&gt;";
    String result = templateUtilContext.escapeXml(xmlSampleText);
    Assert.assertTrue("Failed to escape XML tags in the provided string : " + xmlSampleText, xmlEscapedText.equalsIgnoreCase(result));
    Assert.assertNotNull(templateUtilContext.getContext().get("util"));
}
Also used : TemplateUtilContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.TemplateUtilContext) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) 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)

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