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);
}
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();
}
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"));
}
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();
}
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();
}
Aggregations