use of org.apache.velocity.exception.VelocityException in project xwiki-platform by xwiki.
the class WebJarsResourceReferenceHandlerTest method failingResourceEvaluation.
@Test
public void failingResourceEvaluation() throws Exception {
WebJarsResourceReference reference = new WebJarsResourceReference("wiki:wiki", Arrays.asList("angular", "2.1.11", "angular.js"));
reference.addParameter("evaluate", "true");
ByteArrayInputStream resourceStream = new ByteArrayInputStream("content".getBytes());
when(this.classLoader.getResourceAsStream("META-INF/resources/webjars/angular/2.1.11/angular.js")).thenReturn(resourceStream);
VelocityManager velocityManager = this.componentManager.getInstance(VelocityManager.class);
VelocityEngine velocityEngine = mock(VelocityEngine.class);
when(velocityManager.getVelocityEngine()).thenReturn(velocityEngine);
when(velocityEngine.evaluate(any(), any(), eq("angular/2.1.11/angular.js"), any(Reader.class))).thenThrow(new VelocityException("Bad code!"));
this.handler.handle(reference, this.chain);
// Verify the exception is logged.
verify(this.componentManager.getMockedLogger()).error(eq("Failed to evaluate the Velocity code from WebJar resource [angular/2.1.11/angular.js]"), any(ResourceReferenceHandlerException.class));
// Verify that the client is properly notified about the failure.
verify(this.response.getHttpServletResponse()).sendError(500, "Failed to evaluate the Velocity code from WebJar resource [angular/2.1.11/angular.js]");
// The next handlers are still called.
verify(this.chain).handleNext(reference);
}
use of org.apache.velocity.exception.VelocityException in project carbon-apimgt by wso2.
the class ThrottlePolicyTemplateBuilder method getThrottlePolicyForAppLevel.
/**
* Generate application level policy
*
* @param policy policy with level 'app'. Multiple pipelines are not allowed. Can define more than one condition
* as set of conditions. all these conditions should be passed as a single pipeline
* @return the generated execution plan for the policy
* @throws APITemplateException if failed to generate policy
*/
public String getThrottlePolicyForAppLevel(ApplicationPolicy policy) throws APITemplateException {
StringWriter writer = new StringWriter();
if (log.isDebugEnabled()) {
log.debug("Generating policy for app level :" + policy.toString());
}
try {
VelocityEngine velocityengine = new VelocityEngine();
APIUtil.initializeVelocityContext(velocityengine);
velocityengine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CarbonUtils.getCarbonHome());
velocityengine.init();
Template template = velocityengine.getTemplate(getTemplatePathForApplication());
VelocityContext context = new VelocityContext();
setConstantContext(context);
context.put("policy", policy);
context.put("quotaPolicy", policy.getDefaultLimit());
template.merge(context, writer);
if (log.isDebugEnabled()) {
log.debug("Policy : " + writer.toString());
}
} catch (VelocityException e) {
log.error("Velocity Error", e);
throw new APITemplateException("Velocity Error", e);
}
return writer.toString();
}
use of org.apache.velocity.exception.VelocityException in project carbon-apimgt by wso2.
the class ThrottlePolicyTemplateBuilder method getThrottlePolicyForSubscriptionLevel.
/**
* Generate policy for subscription level
*
* @param policy policy with level 'sub'. Multiple pipelines are not allowed. Can define more than one condition
* as set of conditions. all these conditions should be passed as a single pipeline
* @return the generated execution plan for the policy
* @throws APITemplateException if failed to generate policy
*/
public String getThrottlePolicyForSubscriptionLevel(SubscriptionPolicy policy) throws APITemplateException {
StringWriter writer = new StringWriter();
if (log.isDebugEnabled()) {
log.debug("Generating policy for subscription Level :" + policy.toString());
}
try {
VelocityEngine velocityengine = new VelocityEngine();
velocityengine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CarbonUtils.getCarbonHome());
APIUtil.initializeVelocityContext(velocityengine);
velocityengine.init();
Template template;
if (PolicyConstants.EVENT_COUNT_TYPE.equals(policy.getDefaultLimit().getQuotaType())) {
template = velocityengine.getTemplate(getTemplatePathForAsyncSubscription());
} else {
template = velocityengine.getTemplate(getTemplatePathForSubscription());
}
VelocityContext context = new VelocityContext();
setConstantContext(context);
context.put("policy", policy);
context.put("quotaPolicy", policy.getDefaultLimit());
template.merge(context, writer);
if (log.isDebugEnabled()) {
log.debug("Policy : " + writer.toString());
}
} catch (VelocityException e) {
log.error("Velocity Error", e);
throw new APITemplateException("Velocity Error", e);
}
return writer.toString();
}
use of org.apache.velocity.exception.VelocityException in project maven-plugins by apache.
the class VelocityTemplate method generate.
/**
* Using a specified Velocity Template and provided context, create the outputFilename.
*
* @param outputFilename the file to be generated.
* @param template the velocity template to use.
* @param context the velocity context map.
* @throws VelocityException if the template was not found or any other Velocity exception.
* @throws MojoExecutionException if merging the velocity template failed.
* @throws IOException if there was an error when writing to the output file.
*/
public void generate(String outputFilename, String template, Context context) throws VelocityException, MojoExecutionException, IOException {
Writer writer = null;
try {
File f = new File(outputFilename);
if (!f.getParentFile().exists()) {
f.getParentFile().mkdirs();
}
writer = new FileWriter(f);
getVelocity().getEngine().mergeTemplate(templateDirectory + "/" + template, context, writer);
} catch (ResourceNotFoundException e) {
throw new ResourceNotFoundException("Template not found: " + templateDirectory + "/" + template, e);
} catch (VelocityException | IOException e) {
// to get past generic catch for Exception.
throw e;
} catch (Exception e) {
throw new MojoExecutionException(e.getMessage(), e);
} finally {
if (writer != null) {
writer.flush();
writer.close();
getLog().debug("File " + outputFilename + " created...");
}
}
}
Aggregations