/** * Merge the template with the context. Can be overridden to customize the behavior. * * @param template the template to merge * @param context the Velocity context to use for rendering * @param response servlet response (use this to get the OutputStream or Writer) * @throws Exception if thrown by Velocity * @see org.apache.velocity.Template#merge */ protected void mergeTemplate( Template template, Context context, HttpServletResponse response) throws Exception { try { response.setCharacterEncoding(Config.DEFAULT_ENCODING); template.merge(context, response.getWriter()); } catch (MethodInvocationException ex) { Throwable cause = ex.getCause(); throw new NestedServletException( "Method invocation failed during rendering of Velocity view with name '" + getBeanName() + "': " + ex.getMessage() + "; reference [" + ex.getReferenceName() + "], method '" + ex.getMethodName() + "'", cause == null ? ex : cause); } } }
/** * Merge the template with the context. * Can be overridden to customize the behavior. * @param template the template to merge * @param context the Velocity context to use for rendering * @param response servlet response (use this to get the OutputStream or Writer) * @throws Exception if thrown by Velocity * @see Template#merge */ protected void mergeTemplate( Template template, Context context, HttpServletResponse response) throws Exception { try { template.merge(context, response.getWriter()); } catch (MethodInvocationException ex) { Throwable cause = ex.getWrappedThrowable(); throw new NestedServletException( "Method invocation failed during rendering of Velocity view with name '" + getBeanName() + "': " + ex.getMessage() + "; reference [" + ex.getReferenceName() + "], method '" + ex.getMethodName() + "'", cause==null ? ex : cause); } }
logger.error(e.toString()); } catch (MethodInvocationException e) { logger.error(e.getMessage()); } catch (FileNotFoundException e) { logger.error(e.toString());
/** * Merge the template with the context. * Can be overridden to customize the behavior. * @param template the template to merge * @param context the Velocity context to use for rendering * @param response servlet response (use this to get the OutputStream or Writer) * @throws Exception if thrown by Velocity * @see Template#merge */ protected void mergeTemplate( Template template, Context context, HttpServletResponse response) throws Exception { try { template.merge(context, response.getWriter()); } catch (MethodInvocationException ex) { Throwable cause = ex.getWrappedThrowable(); throw new NestedServletException( "Method invocation failed during rendering of Velocity view with name '" + getBeanName() + "': " + ex.getMessage() + "; reference [" + ex.getReferenceName() + "], method '" + ex.getMethodName() + "'", cause==null ? ex : cause); } }
/** * Merge the template with the context. * Can be overridden to customize the behavior. * @param template the template to merge * @param context the Velocity context to use for rendering * @param response servlet response (use this to get the OutputStream or Writer) * @throws Exception if thrown by Velocity * @see org.apache.velocity.Template#merge */ protected void mergeTemplate( Template template, Context context, HttpServletResponse response) throws Exception { try { template.merge(context, response.getWriter()); } catch (MethodInvocationException ex) { Throwable cause = ex.getWrappedThrowable(); throw new NestedServletException( "Method invocation failed during rendering of Velocity view with name '" + getBeanName() + "': " + ex.getMessage() + "; reference [" + ex.getReferenceName() + "], method '" + ex.getMethodName() + "'", cause==null ? ex : cause); } }
public String generateMail( String templateName, AuthenticationKey authkey, String baseUrl ) { VelocityContext context = createVelocityContext( authkey, baseUrl ); String packageName = getClass().getPackage().getName().replace( '.', '/' ); String templateFile = packageName + "/template/" + templateName + ".vm"; StringWriter writer = new StringWriter(); try { velocity.getEngine().mergeTemplate( templateFile, context, writer ); } catch ( ResourceNotFoundException e ) { getLogger().error( "No such template: '" + templateFile + "'." ); } catch ( ParseErrorException e ) { getLogger().error( "Unable to generate email for template '" + templateFile + "': " + e.getMessage(), e ); } catch ( MethodInvocationException e ) { getLogger().error( "Unable to generate email for template '" + templateFile + "': " + e.getMessage(), e ); } catch ( Exception e ) { getLogger().error( "Unable to generate email for template '" + templateFile + "': " + e.getMessage(), e ); } return writer.getBuffer().toString(); }
@Test public void testEvaluateStrict() { String template = "${baz}"; Map<String, Object> variables = new HashMap<>(); variables.put("foo", "bar"); String logTag = "test"; try { velocityNonStrictHelper.evaluate(template, variables, logTag); fail(); } catch (MethodInvocationException methodInvocationException) { Assert.assertEquals("Exception message not equal.", "Variable $baz has not been set at test[line 1, column 1]", methodInvocationException.getMessage()); } }
@Test public void testGetS3KeyPrefixUndefinedVelocityVariable() { // Create database entities required for testing. businessObjectDataServiceTestHelper.createDatabaseEntitiesForGetS3KeyPrefixTesting(true); // Get the test partition columns. List<SchemaColumn> testPartitionColumns = schemaColumnDaoTestHelper.getTestPartitionColumns(); String testPartitionKey = testPartitionColumns.get(0).getName(); // Create an undefined velocity variable. String undefinedVelocityVariable = "$UNDEFINED_VARIABLE"; // Create an S3 storage entity with the S3 key prefix template containing an undefined variable. storageDaoTestHelper.createStorageEntity(STORAGE_NAME_2, StoragePlatformEntity.S3, Arrays.asList( new Attribute(configurationHelper.getProperty(ConfigurationValue.S3_ATTRIBUTE_NAME_KEY_PREFIX_VELOCITY_TEMPLATE), undefinedVelocityVariable))); // Try to get an S3 key prefix when the S3 key prefix velocity template contains an undefined variable. try { storageUnitService.getS3KeyPrefix( new BusinessObjectDataKey(NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, PARTITION_VALUE, SUBPARTITION_VALUES, DATA_VERSION), testPartitionKey, STORAGE_NAME_2, false); fail("Should throw an MethodInvocationException when the S3 key prefix velocity template contains an undefined variable."); } catch (MethodInvocationException e) { assertEquals(String.format("Variable %s has not been set at %s[line 1, column 1]", undefinedVelocityVariable, configurationHelper.getProperty(ConfigurationValue.S3_ATTRIBUTE_NAME_KEY_PREFIX_VELOCITY_TEMPLATE)), e.getMessage()); } }