Search in sources :

Example 31 with ConfigContext

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

the class APIConfigContextTest method testGetContext.

@Test
public void testGetContext() throws Exception {
    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.BLOCKED);
    api.setContextTemplate("/");
    APIConfigContext configContext = new APIConfigContext(api);
}
Also used : 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) Test(org.junit.Test)

Example 32 with ConfigContext

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

the class EndpointConfigContextTest method testEndpointConfigContext.

@Test(expected = APITemplateException.class)
public void testEndpointConfigContext() 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);
    EndpointConfigContext endpointConfigContext = new EndpointConfigContext(configcontext, api);
    endpointConfigContext.validate();
    Assert.assertNotNull(endpointConfigContext.getContext().get("endpoint_config"));
    // set an empty string and check the validation
    endpointConfig = "";
    api.setEndpointConfig(endpointConfig);
    endpointConfigContext = new EndpointConfigContext(configcontext, api);
    endpointConfigContext.validate();
    // set a null value and check the validation
    endpointConfig = null;
    api.setEndpointConfig(endpointConfig);
    endpointConfigContext = new EndpointConfigContext(configcontext, api);
    endpointConfigContext.validate();
    // set invalid value and check the validation
    String invalidEndpointConfig = "\"production_endpoints\"{\"url\":\"" + url + "\", \"config\":null}," + "\"sandbox_endpoint\":{\"url\":\"" + url + "\",\"config\":null},\"endpoint_type\":\"http\"";
    api.setEndpointConfig(invalidEndpointConfig);
    endpointConfigContext = new EndpointConfigContext(configcontext, api);
    endpointConfigContext.validate();
}
Also used : EndpointConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.EndpointConfigContext) 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) EndpointConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.EndpointConfigContext) 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 33 with ConfigContext

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

the class HandlerConfigContextTest method testHandlerConfigContex.

@Test
public void testHandlerConfigContex() throws Exception {
    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    ConfigContext configcontext = new APIConfigContext(api);
    List<HandlerConfig> handlers = new ArrayList<HandlerConfig>();
    HandlerConfigContex handlerConfigContex = new HandlerConfigContex(configcontext, handlers);
    Assert.assertNotNull(handlerConfigContex.getContext().get("handlers"));
}
Also used : HandlerConfigContex(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.HandlerConfigContex) HandlerConfig(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.HandlerConfig) ArrayList(java.util.ArrayList) 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)

Example 34 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) Template(org.apache.velocity.Template)

Example 35 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 getConfigStringForTemplate.

@Override
public String getConfigStringForTemplate(Environment environment) throws APITemplateException {
    StringWriter writer = new StringWriter();
    try {
        // build the context for template and apply the necessary decorators
        ConfigContext configcontext = null;
        if (api != null) {
            configcontext = createConfigContext(api, environment);
        } else {
            // API Product scenario
            configcontext = createConfigContext(apiProduct, environment);
        }
        // @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 = null;
        if (api != null) {
            t = velocityengine.getTemplate(getTemplatePath());
            if (APIConstants.APITransportType.WS.toString().equals(api.getType())) {
                context.put("topicMappings", this.api.getWebSocketTopicMappingConfiguration().getMappings());
            } else if (APIConstants.APITransportType.WEBSUB.toString().equals(api.getType())) {
                String signingAlgorithm = api.getWebsubSubscriptionConfiguration().getSigningAlgorithm();
                context.put("signingAlgorithm", signingAlgorithm.toLowerCase() + "=");
                context.put("secret", api.getWebsubSubscriptionConfiguration().getSecret());
                context.put("hmacSignatureGenerationAlgorithm", "Hmac" + signingAlgorithm);
                context.put("signatureHeader", api.getWebsubSubscriptionConfiguration().getSignatureHeader());
                context.put("isSecurityEnabled", !StringUtils.isEmpty(api.getWebsubSubscriptionConfiguration().getSecret()));
            } else if (APIConstants.GRAPHQL_API.equals(api.getType())) {
                boolean isSubscriptionAvailable = false;
                if (api.getWebSocketTopicMappingConfiguration() != null) {
                    isSubscriptionAvailable = true;
                    context.put(APIConstants.VELOCITY_API_WEBSOCKET_TOPIC_MAPPINGS, this.api.getWebSocketTopicMappingConfiguration().getMappings());
                }
                context.put(APIConstants.VELOCITY_GRAPHQL_API_SUBSCRIPTION_AVAILABLE, isSubscriptionAvailable);
            }
        } else {
            t = velocityengine.getTemplate(getApiProductTemplatePath());
        }
        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) 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)

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