Search in sources :

Example 1 with EndpointConfigContext

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

the class TemplateBuilderUtil method addEndpoints.

private static void addEndpoints(API api, APITemplateBuilder builder, GatewayAPIDTO gatewayAPIDTO) throws APITemplateException, XMLStreamException {
    ArrayList<String> arrayListToAdd = getEndpointType(api);
    for (String type : arrayListToAdd) {
        String endpointConfigContext = builder.getConfigStringForEndpointTemplate(type);
        GatewayContentDTO endpoint = new GatewayContentDTO();
        endpoint.setName(getEndpointName(endpointConfigContext));
        endpoint.setContent(endpointConfigContext);
        gatewayAPIDTO.setEndpointEntriesToBeAdd(addGatewayContentToList(endpoint, gatewayAPIDTO.getEndpointEntriesToBeAdd()));
    }
}
Also used : GatewayContentDTO(org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO)

Example 2 with EndpointConfigContext

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.EndpointConfigContext 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 3 with EndpointConfigContext

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.EndpointConfigContext 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 4 with EndpointConfigContext

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.EndpointConfigContext 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 5 with EndpointConfigContext

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.EndpointConfigContext 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)

Aggregations

StringWriter (java.io.StringWriter)3 Template (org.apache.velocity.Template)3 VelocityContext (org.apache.velocity.VelocityContext)3 VelocityEngine (org.apache.velocity.app.VelocityEngine)3 APITemplateException (org.wso2.carbon.apimgt.impl.template.APITemplateException)3 RegistryException (org.wso2.carbon.registry.api.RegistryException)3 UserStoreException (org.wso2.carbon.user.api.UserStoreException)3 GatewayContentDTO (org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.junit.Test)1 API (org.wso2.carbon.apimgt.api.model.API)1 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)1 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)1 APIConfigContext (org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext)1 ConfigContext (org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext)1 EndpointConfigContext (org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.EndpointConfigContext)1