org.apache.velocity.tools.ToolManager Java Examples

The following examples show how to use org.apache.velocity.tools.ToolManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VelocityTransformer.java    From AuTe-Framework with Apache License 2.0 6 votes vote down vote up
public String transform(String mockGuid, String requestBody, Map<String, Object> context, String stringTemplate) {
    log.debug("transform (requestBody = {}, context = {}, template = {})", requestBody, context, stringTemplate);

    final VelocityEngine velocityEngine = new VelocityEngine();
    velocityEngine.init();
    final ToolManager toolManager = new ToolManager();
    toolManager.setVelocityEngine(velocityEngine);
    velocityContext = toolManager.createContext();
    if (context != null) {
        context.forEach((k, v) -> velocityContext.put(k, v));
    }
    velocityContext.put("staticContext", staticContext.computeIfAbsent(mockGuid, k -> new HashMap<>()));

    try {
        return render(stringTemplate);
    } catch (ParseException e) {
        log.error("exception while parsing, return exception message", e);
        return e.getMessage();
    }
}
 
Example #2
Source File: CustomVelocityResponseTransformer.java    From AuTe-Framework with Apache License 2.0 4 votes vote down vote up
@Override
public ResponseDefinition transform(
    final Request request,
    final ResponseDefinition responseDefinition,
    final FileSource files,
    final Parameters parameters
) throws RuntimeException {
    final VelocityEngine velocityEngine = new VelocityEngine();
    velocityEngine.init();
    final ToolManager toolManager = new ToolManager();
    toolManager.setVelocityEngine(velocityEngine);

    context = toolManager.createContext();
    URL url = getRequestUrl(request);
    addBodyToContext(request.getBodyAsString());
    addHeadersToContext(request.getHeaders());
    context.put("requestAbsoluteUrl", request.getAbsoluteUrl());
    context.put("requestUrl", request.getUrl());
    context.put("requestMethod", request.getMethod());
    context.put("queryParams", urlExtractor.extractQueryParameters(url));
    context.put("pathSegments", urlExtractor.extractPathSegments(url));
    context.put("staticContext", staticContexts.computeIfAbsent(url.getPath(), k -> new HashMap<>()));
    context.put(ExtendedScript.MQ_PROPERTIES, properties);

    String body;
    if (responseDefinition.specifiesBodyFile() && templateDeclared(responseDefinition)) {
        body = getRenderedBody(responseDefinition);
    } else if (responseDefinition.specifiesBodyContent()) {
        try {
            body = getRenderedBodyFromFile(responseDefinition);
        } catch (ParseException e) {
            body = e.getMessage();
            e.printStackTrace();
        }
    } else {
        return responseDefinition;
    }
    if (isPresentConvertCommand(responseDefinition.getHeaders().getHeader(MultipartConstant.CONVERT_BASE64_IN_MULTIPART.getValue()))) {
        log.info(" >>> ");
        log.info(body);
        return buildResponse(parseConvertBody(body), body, responseDefinition, true);
    }
    return buildResponse(null, body, responseDefinition, false);
}
 
Example #3
Source File: EmailSenderImp.java    From swellrt with Apache License 2.0 4 votes vote down vote up
@Inject
public EmailSenderImp(SessionManager sessionManager, AccountStore accountStore, VelocityEngine ve, Config config) {

  this.accountStore = accountStore;
  this.ve = ve;
  this.velocityPath = config.getString("email.template_path");
  this.host = config.getString("email.host");
  this.from = config.getString("email.from_email_address");

  Properties p = new Properties();
  p.put("resource.loader", "file, class");
  p.put("file.resource.loader.class",
      "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
  p.put("file.resource.loader.path", velocityPath);
  p.put("class.resource.loader.class",
      "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");

  ve.init(p);

  Properties properties = new Properties();

  // Get the default Session object.
  mailSession = Session.getDefaultInstance(properties, null);

  // Setup mail server
  properties.setProperty("mail.smtp.host", host);
  properties.setProperty("mail.smtp.from", from);

  manager = new ToolManager(false);

  manager.setVelocityEngine(ve);

  manager.configure("velocity-tools-config.xml");

  try {
    // based on http://stackoverflow.com/a/15654598/4928558
    File file = new File(velocityPath);
    if (!file.exists())
  	  throw new IOException("Folder for email template files not found!");
    URL[] urls = {file.toURI().toURL()};
    propertyClassloader = new URLClassLoader(urls);
    isExternalPropertyClassLoader = true;
  } catch (IOException e) {
    LOG.warning("Loading default email template files"+e.getMessage());
    propertyClassloader = getClass().getClassLoader();
    isExternalPropertyClassLoader = false;
  }

}
 
Example #4
Source File: EmailService.java    From swellrt with Apache License 2.0 4 votes vote down vote up
@Inject
public EmailService(SessionManager sessionManager, AccountStore accountStore, Config config,
    EmailSender emailSender, DecoupledTemplates decTemplates) {
  super(sessionManager);
  this.accountStore = accountStore;
  String velocityPath = config.getString("email.template_path");
  this.host = config.getString("email.host");
  this.from = config.getString("email.from_email_address");
  this.emailSender = emailSender;
  this.decTemplates = decTemplates;

  Properties p = new Properties();
  p.put("resource.loader", "file, class");
  p.put("file.resource.loader.class",
      "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
  p.put("file.resource.loader.path", velocityPath);
  p.put("class.resource.loader.class",
      "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");

  ve = new VelocityEngine();

  ve.init(p);

  this.recoverPasswordTemplateName = ve.resourceExists("RecoverPassword.vm")
      ? RECOVER_PASSWORD_TEMPLATE : CLASSPATH_VELOCITY_PATH + RECOVER_PASSWORD_TEMPLATE;

  this.recoverPasswordMessages =
      new File(velocityPath + RECOVER_PASSWORD_BUNDLE + ".properties").exists()
          ? RECOVER_PASSWORD_BUNDLE
          : CLASSPATH_VELOCITY_PATH.replace("/", ".") + RECOVER_PASSWORD_BUNDLE;


  try {
    // based on http://stackoverflow.com/a/15654598/4928558
    File file = new File(velocityPath);
    URL[] urls = {file.toURI().toURL()};
    loader = new URLClassLoader(urls);
  } catch (MalformedURLException e) {
    LOG.warning("Error constructing classLoader for velocity internationalization resources:"
        + e.getMessage());
  }

  Properties properties = new Properties();


  // Get the default Session object.
  mailSession = Session.getDefaultInstance(properties, null);

  // Setup mail server
  properties.setProperty("mail.smtp.host", host);
  properties.setProperty("mail.smtp.from", from);

  manager = new ToolManager(false);

  manager.setVelocityEngine(ve);

  manager.configure("velocity-tools-config.xml");

}
 
Example #5
Source File: GenericToolsTests.java    From velocity-tools with Apache License 2.0 4 votes vote down vote up
public static @BeforeClass void initGenericToolsTests() throws Exception {
    ToolManager manager = new ToolManager(false);
    manager.configure(TOOLBOX_PATH);
    toolbox = manager.createContext();
}