Davide
Davide

Reputation: 103

jersey-freemarker

I'm developing a small tool based on jersey and freemarker, which will enable designers to test there freemarker templates, locally, using some mok-objects.

I'm sorry to write here, but I cant find any documentation about it except some code and javadocs.

To do that I did the following:

1 Dependencies:

<dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-freemarker</artifactId>
    <version>1.9</version>
</dependency>

2 Starting grizzly, telling where to find freemarker templates:

protected static HttpServer startServer() throws IOException {
    System.out.println("Starting grizzly...");

    Map<String, Object> params = new HashMap<String, Object>();
    params.put("com.sun.jersey.freemarker.templateBasePath", "/");      
    ResourceConfig rc = new PackagesResourceConfig("resource.package");
    rc.setPropertiesAndFeatures(params);

    HttpServer server = GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
    server.getServerConfiguration().addHttpHandler(
            new StaticHttpHandler("/libs"), "/libs"); 

    return server;
}

3 Creates the root resource and binds freemarker files:

@Context ResourceConfig resourceConfig;
@Path("{path: ([^\\s]+(\\.(?i)(ftl))$)}")
public Viewable renderFtl (@PathParam("path") String path) throws IOException {
    Viewable view = new Viewable("/"+path);
    return view;
}

Everything works fine, except that freemarker files are not rendered. I have an empty white page, but file exists and debugger enter inside renderFtl method right.

Do you know how can I do that?

I read a lot of articles here and around the web, but old posts only or articles talking about spring integration and I don't want to integrate it because I don't need it.

I really like Jersey, I think is one of the most complete and power framework on java world, but anytime I try to find documentation on specific features or contribs libraries, I'm lost... There no escape from groups forums :)

Where can I find a complete documentation about it?

Tanks a lot David

Updates:

Trying to solve I understood I cannot use built-in jersey support, because it needs to use files placed in resources tree. So What I did is to build freemarker configuration, in test for now, directly @runtime and returns a StreamingOutput object:

@Path("{path: ([^\\s]+(\\.(?i)(ftl))$)}")
public StreamingOutput renderFtl (@PathParam("path") String path) throws Exception {
    Configuration cfg = new Configuration();
    // Specify the data source where the template files come from.
    // Here I set a file directory for it:
    cfg.setDirectoryForTemplateLoading(new File("."));

    // Create the root hash
    Map<String, Object> root = new HashMap<String, Object>();
    Template temp = cfg.getTemplate(path);
    return new FTLOutput(root, temp);
}

FTLOutput is here:

This is not a good code, but is for test only...

class FTLOutput implements StreamingOutput {

    private Object root; 
    private Template t;

    public FTLOutput(Object root, Template t) {
        this.root = root;
        this.t = t;
    }

    @Override
    public void write(OutputStream output) throws IOException {
        Writer writer = new OutputStreamWriter(output);
        try {
        t.process(root, writer);
        writer.flush();
    } catch (TemplateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

I have no errors evidence on debug and freemarker tells me that template is found and rendered, but jersey still no give me a result...

I really don't know why!

Upvotes: 4

Views: 2800

Answers (1)

Pavel Bucek
Pavel Bucek

Reputation: 5324

  1. Why are you using Jersey 1.9? 1.11 is already out, you should update if you can

  2. Have you seen "freemarker" sample from Jersey? It demonstrates simple usecase of using freemarker with jersey.

  3. Where are your resources? Templates are being found by calling [LastMatchedResourceClass].getResources(...), so if your templates are not accessible as resources, they can't be rendered correctly. you can checkout Jersey source and place some breakpoints into FreemarkerViewProcessor, it should tell you where exactly the problem is..

Upvotes: 1

Related Questions