Search in sources :

Example 61 with VelocityContext

use of org.apache.velocity.VelocityContext in project knox by apache.

the class TestUtils method merge.

public static String merge(String resource, Properties properties) {
    ClasspathResourceLoader loader = new ClasspathResourceLoader();
    loader.getResourceStream(resource);
    VelocityEngine engine = new VelocityEngine();
    Properties config = new Properties();
    config.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.NullLogSystem");
    config.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
    config.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
    engine.init(config);
    VelocityContext context = new VelocityContext(properties);
    Template template = engine.getTemplate(resource);
    StringWriter writer = new StringWriter();
    template.merge(context, writer);
    return writer.toString();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader) Properties(java.util.Properties) Template(org.apache.velocity.Template)

Example 62 with VelocityContext

use of org.apache.velocity.VelocityContext in project nutzboot by nutzam.

the class VelocityView method render.

@Override
public void render(HttpServletRequest req, HttpServletResponse resp, Object obj) throws Throwable {
    resp.setCharacterEncoding(charsetEncoding);
    if (resp.getContentType() == null) {
        resp.setContentType("text/html; charset=" + charsetEncoding);
    }
    try {
        String templateUrl = templateClasspath + evalPath(req, obj);
        Template template = engine.getTemplate(templateUrl, charsetEncoding);
        VelocityWebContext webContext = new VelocityWebContext(req, resp);
        VelocityContext context = new VelocityContext(webContext);
        PrintWriter writer = resp.getWriter();
        template.merge(context, writer);
    // writer.flush();
    } catch (IOException e) {
        throw Lang.wrapThrow(e);
    }
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) IOException(java.io.IOException) Template(org.apache.velocity.Template) PrintWriter(java.io.PrintWriter)

Example 63 with VelocityContext

use of org.apache.velocity.VelocityContext in project knox by apache.

the class AmbariServiceDefinitionTest method startGatewayServer.

public static void startGatewayServer() throws Exception {
    services = new DefaultGatewayServices();
    Map<String, String> options = new HashMap<>();
    options.put("persist-master", "false");
    options.put("master", "password");
    try {
        services.init(config, options);
    } catch (ServiceLifecycleException e) {
        // I18N not required.
        e.printStackTrace();
    }
    topos = services.getService(GatewayServices.TOPOLOGY_SERVICE);
    gateway = GatewayServer.startGateway(config, services);
    MatcherAssert.assertThat("Failed to start gateway.", gateway, notNullValue());
    gatewayPort = gateway.getAddresses()[0].getPort();
    gatewayUrl = "http://localhost:" + gatewayPort + "/" + config.getGatewayPath();
    String topologyPath = "/test-topology";
    clusterPath = "/" + config.getGatewayPath() + topologyPath;
    clusterUrl = gatewayUrl + topologyPath;
    LOG.info("Gateway port = " + gateway.getAddresses()[0].getPort());
    params = new Properties();
    params.put("AMBARI_URL", "http://localhost:" + mockAmbari.getPort());
    velocity = new VelocityEngine();
    velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.NullLogSystem");
    velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
    velocity.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
    velocity.init();
    context = new VelocityContext();
    context.put("cluster_url", clusterUrl);
    context.put("cluster_path", clusterPath);
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) HashMap(java.util.HashMap) VelocityContext(org.apache.velocity.VelocityContext) ServiceLifecycleException(org.apache.knox.gateway.services.ServiceLifecycleException) DefaultGatewayServices(org.apache.knox.gateway.services.DefaultGatewayServices) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader) Properties(java.util.Properties)

Example 64 with VelocityContext

use of org.apache.velocity.VelocityContext in project knox by apache.

the class GatewayBasicFuncTest method testYarnRmProxy.

@Test(timeout = TestUtils.MEDIUM_TIMEOUT)
public void testYarnRmProxy() throws Exception {
    LOG_ENTER();
    String username = "hdfs";
    String password = "hdfs-password";
    String path = "/v1/cluster/apps/application_1399541193872_0033/";
    String gatewayPath = driver.getUrl("RESOURCEMANAGER") + path;
    Map<String, Matcher<?>> matchers = new HashMap<>();
    VelocityEngine velocity = new VelocityEngine();
    velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.NullLogSystem");
    velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
    velocity.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
    velocity.init();
    VelocityContext context = new VelocityContext();
    context.put("proxy_address", driver.getRealUrl("RESOURCEMANAGER"));
    String name = TestUtils.getResourceName(this.getClass(), "yarn/app_running.json");
    Template template = velocity.getTemplate(name);
    StringWriter sw = new StringWriter();
    template.merge(context, sw);
    String request = sw.toString();
    driver.getMock("RESOURCEMANAGER").expect().method("GET").pathInfo(path).queryParam("user.name", username).respond().status(HttpStatus.SC_OK).content(request.getBytes()).contentType(ContentType.JSON.toString());
    String encryptedTrackingUrl = given().auth().preemptive().basic(username, password).header("X-XSRF-Header", "jksdhfkhdsf").then().statusCode(HttpStatus.SC_OK).contentType(ContentType.JSON).when().get(gatewayPath + (driver.isUseGateway() ? "" : "?user.name=" + username)).getBody().path("app.trackingUrl");
    String encryptedQuery = new URI(encryptedTrackingUrl).getQuery();
    driver.assertComplete();
    // Test that root address of MapReduce Application Master REST API is not accessible through Knox
    // For example, https://{gateway_host}:{gateway_port}/gateway/{cluster}/resourcemanager/proxy/{app_id}/?_={encrypted_application_proxy_location} should return Not Found response
    // https://{gateway_host}:{gateway_port}/gateway/{cluster}/resourcemanager/proxy/{app_id}/ws/v1/mapreduce/?_={encrypted_application_proxy_location} returns OK
    given().auth().preemptive().basic(username, password).header("X-XSRF-Header", "jksdhfkhdsf").then().statusCode(HttpStatus.SC_NOT_FOUND).when().get(encryptedTrackingUrl);
    String resource = null;
    path = "/proxy/application_1399541193872_0033/ws/v1/mapreduce/info";
    resource = "yarn/proxy-mapreduce-info";
    getYarnRmProxyData(encryptedQuery, path, resource, ContentType.JSON);
    getYarnRmProxyData(encryptedQuery, path, resource, ContentType.XML);
    path = "/proxy/application_1399541193872_0033/ws/v1/mapreduce";
    getYarnRmProxyData(encryptedQuery, path, resource, ContentType.JSON);
    getYarnRmProxyData(encryptedQuery, path, resource, ContentType.XML);
    path = "/proxy/application_1399541193872_0033/ws/v1/mapreduce/jobs";
    resource = "yarn/proxy-mapreduce-jobs";
    getYarnRmProxyData(encryptedQuery, path, resource, ContentType.JSON);
    getYarnRmProxyData(encryptedQuery, path, resource, ContentType.XML);
    path = "/proxy/application_1399541193872_0033/ws/v1/mapreduce/jobs/job_1399541193872_0035";
    resource = "yarn/proxy-mapreduce-job";
    getYarnRmProxyData(encryptedQuery, path, resource, ContentType.JSON);
    getYarnRmProxyData(encryptedQuery, path, resource, ContentType.XML);
    path = "/proxy/application_1399541193872_0033/ws/v1/mapreduce/jobs/job_1399541193872_0035/counters";
    resource = "yarn/proxy-mapreduce-job-counters";
    getYarnRmProxyData(encryptedQuery, path, resource, ContentType.JSON);
    getYarnRmProxyData(encryptedQuery, path, resource, ContentType.XML);
    // TODO: Need to understand what we should do with following properties
    // hadoop.proxyuser.HTTP.hosts
    // dfs.namenode.secondary.http-address
    // dfs.namenode.http-address
    // mapreduce.jobhistory.webapp.address
    // mapreduce.jobhistory.webapp.https.address
    // dfs.namenode.https-address
    // mapreduce.job.submithostname
    // yarn.resourcemanager.webapp.address
    // yarn.resourcemanager.hostname
    // mapreduce.jobhistory.address
    // yarn.resourcemanager.webapp.https.address
    // hadoop.proxyuser.oozie.hosts
    // hadoop.proxyuser.hive.hosts
    // dfs.namenode.secondary.https-address
    // hadoop.proxyuser.hcat.hosts
    // hadoop.proxyuser.HTTP.hosts
    // TODO: resolve java.util.regex.PatternSyntaxException: Unmatched closing ')' near index 17   m@\..*EXAMPLE\.COM)s
    path = "/proxy/application_1399541193872_0035/ws/v1/mapreduce/jobs/job_1399541193872_0035/conf";
    resource = "yarn/proxy-mapreduce-job-conf";
    // getYarnRmProxyJobConf( encryptedQuery, path, resource, ContentType.JSON );
    // getYarnRmProxyJobConf( encryptedQuery, path, resource, ContentType.XML );
    path = "/proxy/application_1399541193872_0036/ws/v1/mapreduce/jobs/job_1399541193872_0036/jobattempts";
    resource = "yarn/proxy-mapreduce-job-attempts";
    matchers.clear();
    matchers.put("jobAttempts.jobAttempt[0].nodeHttpAddress", isEmptyString());
    matchers.put("jobAttempts.jobAttempt[0].nodeId", not(containsString("host.yarn.com:45454")));
    matchers.put("jobAttempts.jobAttempt[0].logsLink", isEmptyString());
    getYarnRmProxyData(encryptedQuery, path, resource, ContentType.JSON, matchers);
    getYarnRmProxyData(encryptedQuery, path, resource, ContentType.XML, matchers);
    path = "/proxy/application_1399541193872_0036/ws/v1/mapreduce/jobs/job_1399541193872_0036/tasks";
    resource = "yarn/proxy-mapreduce-tasks";
    getYarnRmProxyData(encryptedQuery, path, resource, ContentType.JSON);
    getYarnRmProxyData(encryptedQuery, path, resource, ContentType.XML);
    path = "/proxy/application_1399541193872_0036/ws/v1/mapreduce/jobs/job_1399541193872_0036/tasks/task_1399541193872_0036_r_00";
    resource = "yarn/proxy-mapreduce-task";
    getYarnRmProxyData(encryptedQuery, path, resource, ContentType.JSON);
    getYarnRmProxyData(encryptedQuery, path, resource, ContentType.XML);
    path = "/proxy/application_1399541193872_0036/ws/v1/mapreduce/jobs/job_1399541193872_0036/tasks/task_1399541193872_0036_r_00/counters";
    resource = "yarn/proxy-mapreduce-task-counters";
    getYarnRmProxyData(encryptedQuery, path, resource, ContentType.JSON);
    getYarnRmProxyData(encryptedQuery, path, resource, ContentType.XML);
    path = "/proxy/application_1399541193872_0036/ws/v1/mapreduce/jobs/job_1399541193872_0036/tasks/task_1399541193872_0036_r_00/attempts";
    resource = "yarn/proxy-mapreduce-task-attempts";
    matchers.clear();
    matchers.put("taskAttempts.taskAttempt[0].nodeHttpAddress", isEmptyString());
    getYarnRmProxyData(encryptedQuery, path, resource, ContentType.JSON, matchers);
    getYarnRmProxyData(encryptedQuery, path, resource, ContentType.XML, matchers);
    path = "/proxy/application_1399541193872_0036/ws/v1/mapreduce/jobs/job_1399541193872_0036/tasks/task_1399541193872_0036_r_00/attempts/attempt_1399541193872_0036_r_000000_0";
    resource = "yarn/proxy-mapreduce-task-attempt";
    matchers.clear();
    matchers.put("taskAttempt.nodeHttpAddress", isEmptyString());
    getYarnRmProxyData(encryptedQuery, path, resource, ContentType.JSON, matchers);
    getYarnRmProxyData(encryptedQuery, path, resource, ContentType.XML, matchers);
    path = "/proxy/application_1399541193872_0036/ws/v1/mapreduce/jobs/job_1399541193872_0036/tasks/task_1399541193872_0036_r_00/attempts/attempt_1399541193872_0036_r_000000_0/counters";
    resource = "yarn/proxy-mapreduce-task-attempt-counters";
    getYarnRmProxyData(encryptedQuery, path, resource, ContentType.JSON);
    getYarnRmProxyData(encryptedQuery, path, resource, ContentType.XML);
    LOG_EXIT();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) MockRequestMatcher(org.apache.knox.test.mock.MockRequestMatcher) Matcher(org.hamcrest.Matcher) HashMap(java.util.HashMap) VelocityContext(org.apache.velocity.VelocityContext) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader) IsEmptyString.isEmptyString(org.hamcrest.text.IsEmptyString.isEmptyString) Matchers.containsString(org.hamcrest.Matchers.containsString) URI(java.net.URI) Template(org.apache.velocity.Template) VerifyTest(org.apache.knox.test.category.VerifyTest) Test(org.junit.Test)

Example 65 with VelocityContext

use of org.apache.velocity.VelocityContext in project wicket by apache.

the class VelocityPanel method evaluateVelocityTemplate.

/**
 * Evaluates the template and returns the result.
 *
 * @param templateReader
 *            used to read the template
 * @return the result of evaluating the velocity template
 */
private String evaluateVelocityTemplate(final Reader templateReader) {
    if (evaluatedTemplate == null) {
        // Get model as a map
        @SuppressWarnings("rawtypes") final Map map = (Map) getDefaultModelObject();
        // create a Velocity context object using the model if set
        final VelocityContext ctx = new VelocityContext(map);
        // create a writer for capturing the Velocity output
        StringWriter writer = new StringWriter();
        // string to be used as the template name for log messages in case
        // of error
        final String logTag = getId();
        try {
            // execute the velocity script and capture the output in writer
            Velocity.evaluate(ctx, writer, logTag, templateReader);
            // replace the tag's body the Velocity output
            evaluatedTemplate = writer.toString();
            if (escapeHtml()) {
                // encode the result in order to get valid html output that
                // does not break the rest of the page
                evaluatedTemplate = Strings.escapeMarkup(evaluatedTemplate).toString();
            }
            return evaluatedTemplate;
        } catch (Exception e) {
            onException(e);
        }
        return null;
    }
    return evaluatedTemplate;
}
Also used : StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) Map(java.util.Map) WicketRuntimeException(org.apache.wicket.WicketRuntimeException)

Aggregations

VelocityContext (org.apache.velocity.VelocityContext)492 StringWriter (java.io.StringWriter)156 Template (org.apache.velocity.Template)120 Test (org.junit.Test)72 IOException (java.io.IOException)60 VelocityEngine (org.apache.velocity.app.VelocityEngine)53 File (java.io.File)47 ArrayList (java.util.ArrayList)39 HashMap (java.util.HashMap)36 Map (java.util.Map)36 Identity (org.olat.core.id.Identity)36 Context (org.apache.velocity.context.Context)32 MailTemplate (org.olat.core.util.mail.MailTemplate)28 Writer (java.io.Writer)22 Properties (java.util.Properties)20 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)19 ParseErrorException (org.apache.velocity.exception.ParseErrorException)16 ClasspathResourceLoader (org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader)16 FileWriter (java.io.FileWriter)15 OutputStreamWriter (java.io.OutputStreamWriter)14