Search in sources :

Example 11 with VelocityException

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);
}
Also used : ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) VelocityEngine(org.xwiki.velocity.VelocityEngine) WebJarsResourceReference(org.xwiki.webjars.internal.WebJarsResourceReference) ByteArrayInputStream(java.io.ByteArrayInputStream) VelocityManager(org.xwiki.velocity.VelocityManager) VelocityException(org.apache.velocity.exception.VelocityException) Reader(java.io.Reader) Test(org.junit.Test)

Example 12 with VelocityException

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();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) VelocityException(org.apache.velocity.exception.VelocityException) APITemplateException(org.wso2.carbon.apimgt.impl.template.APITemplateException) Template(org.apache.velocity.Template)

Example 13 with VelocityException

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();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) VelocityException(org.apache.velocity.exception.VelocityException) APITemplateException(org.wso2.carbon.apimgt.impl.template.APITemplateException) Template(org.apache.velocity.Template)

Example 14 with VelocityException

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...");
        }
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileWriter(java.io.FileWriter) VelocityException(org.apache.velocity.exception.VelocityException) IOException(java.io.IOException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) File(java.io.File) FileWriter(java.io.FileWriter) Writer(java.io.Writer) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) VelocityException(org.apache.velocity.exception.VelocityException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException)

Aggregations

VelocityException (org.apache.velocity.exception.VelocityException)14 VelocityContext (org.apache.velocity.VelocityContext)8 Template (org.apache.velocity.Template)7 VelocityEngine (org.apache.velocity.app.VelocityEngine)7 IOException (java.io.IOException)6 StringWriter (java.io.StringWriter)6 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)5 APITemplateException (org.wso2.carbon.apimgt.impl.template.APITemplateException)5 File (java.io.File)4 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)3 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 Logger (com.intellij.openapi.diagnostic.Logger)2 FileType (com.intellij.openapi.fileTypes.FileType)2 FileTypeManager (com.intellij.openapi.fileTypes.FileTypeManager)2 Project (com.intellij.openapi.project.Project)2 ProjectManagerEx (com.intellij.openapi.project.ex.ProjectManagerEx)2 Messages (com.intellij.openapi.ui.Messages)2 FileWriter (java.io.FileWriter)2 Reader (java.io.Reader)2 Writer (java.io.Writer)2