Skip to content

Commit 621e587

Browse files
committed
Add rocker template engine fix #565
1 parent 0353071 commit 621e587

12 files changed

Lines changed: 786 additions & 5 deletions

File tree

coverage-report/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<source>${project.parent.basedir}/jooby-filewatcher/src/main/java</source>
8989
<source>${project.parent.basedir}/jooby-lang-js/src/main/java</source>
9090
<source>${project.parent.basedir}/jooby-requery/src/main/java</source>
91+
<source>${project.parent.basedir}/jooby-rocker/src/main/java</source>
9192
</sources>
9293
</configuration>
9394
</execution>
@@ -151,6 +152,7 @@
151152
<source>${project.parent.basedir}/jooby-filewatcher/src/test/java</source>
152153
<source>${project.parent.basedir}/jooby-lang-js/src/test/java</source>
153154
<source>${project.parent.basedir}/jooby-requery/src/test/java</source>
155+
<source>${project.parent.basedir}/jooby-rocker/src/test/java</source>
154156
</sources>
155157
</configuration>
156158
</execution>
@@ -563,6 +565,12 @@
563565
<version>${project.version}</version>
564566
</dependency>
565567

568+
<dependency>
569+
<groupId>org.jooby</groupId>
570+
<artifactId>jooby-rocker</artifactId>
571+
<version>${project.version}</version>
572+
</dependency>
573+
566574
<dependency>
567575
<groupId>org.jooby</groupId>
568576
<artifactId>jooby-ebean</artifactId>

jooby-requery/src/main/java/org/jooby/requery/Requery.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@
9393
* <h3>maven</h3>
9494
* <p>
9595
* We do provide code generation via Maven profile. All you have to do is to write a
96-
* <code>requery.activator</code> file inside the <code>src/etc</code> folder. File present triggers
97-
* Requery annotation processor and generated contents.
96+
* <code>requery.activator</code> file inside the <code>src/etc</code> folder. File presence
97+
* triggers Requery annotation processor and generated contents.
9898
* </p>
9999
*
100100
* <p>

jooby-rocker/pom.xml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
3+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.jooby</groupId>
7+
<artifactId>jooby-project</artifactId>
8+
<version>1.0.4-SNAPSHOT</version>
9+
</parent>
10+
<artifactId>jooby-rocker</artifactId>
11+
<name>jooby-rocker</name>
12+
13+
<dependencies>
14+
<dependency>
15+
<groupId>org.jooby</groupId>
16+
<artifactId>jooby-filewatcher</artifactId>
17+
</dependency>
18+
19+
<dependency>
20+
<groupId>com.fizzed</groupId>
21+
<artifactId>rocker-runtime</artifactId>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>com.fizzed</groupId>
26+
<artifactId>rocker-compiler</artifactId>
27+
<scope>provided</scope>
28+
</dependency>
29+
30+
<!-- Test -->
31+
<dependency>
32+
<groupId>org.jooby</groupId>
33+
<artifactId>jooby</artifactId>
34+
<version>${project.version}</version>
35+
<scope>test</scope>
36+
<classifier>tests</classifier>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>junit</groupId>
41+
<artifactId>junit</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>org.easymock</groupId>
47+
<artifactId>easymock</artifactId>
48+
<scope>test</scope>
49+
</dependency>
50+
51+
<dependency>
52+
<groupId>org.powermock</groupId>
53+
<artifactId>powermock-api-easymock</artifactId>
54+
<scope>test</scope>
55+
</dependency>
56+
57+
<dependency>
58+
<groupId>org.powermock</groupId>
59+
<artifactId>powermock-module-junit4</artifactId>
60+
<scope>test</scope>
61+
</dependency>
62+
63+
<dependency>
64+
<groupId>org.jacoco</groupId>
65+
<artifactId>org.jacoco.agent</artifactId>
66+
<classifier>runtime</classifier>
67+
<scope>test</scope>
68+
</dependency>
69+
70+
</dependencies>
71+
</project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.jooby.rocker;
20+
21+
import static com.fizzed.rocker.RockerUtils.requireTemplateClass;
22+
23+
import java.util.Map;
24+
25+
import com.fizzed.rocker.RockerModel;
26+
import com.fizzed.rocker.RockerTemplate;
27+
import com.fizzed.rocker.runtime.DefaultRockerTemplate;
28+
29+
public abstract class RequestRockerTemplate extends DefaultRockerTemplate {
30+
31+
public Map<String, Object> locals;
32+
33+
public RequestRockerTemplate(final RockerModel model) {
34+
super(model);
35+
}
36+
37+
@Override
38+
protected void __associate(final RockerTemplate context) {
39+
this.locals = requireTemplateClass(context, RequestRockerTemplate.class).locals;
40+
}
41+
42+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.jooby.rocker;
20+
21+
import java.nio.channels.Channels;
22+
import java.util.Map;
23+
24+
import org.jooby.MediaType;
25+
import org.jooby.Renderer;
26+
import org.jooby.Route;
27+
import org.jooby.View;
28+
29+
import com.fizzed.rocker.Rocker;
30+
import com.fizzed.rocker.RockerModel;
31+
import com.fizzed.rocker.runtime.ArrayOfByteArraysOutput;
32+
33+
class RockerRenderer implements Renderer {
34+
35+
private String prefix;
36+
37+
private String suffix;
38+
39+
public RockerRenderer(final String prefix, final String suffix) {
40+
this.prefix = prefix;
41+
this.suffix = suffix;
42+
}
43+
44+
@SuppressWarnings({"rawtypes", "unchecked" })
45+
@Override
46+
public void render(final Object value, final Context ctx) throws Exception {
47+
Object model = value;
48+
/** View? */
49+
if (value instanceof View) {
50+
View view = (View) value;
51+
String path = path(Route.normalize(prefix + "/" + view.name() + suffix));
52+
53+
Map data = view.model();
54+
model = Rocker.template(path).bind(data);
55+
}
56+
/** RockerModel: */
57+
if (model instanceof RockerModel) {
58+
ArrayOfByteArraysOutput output = ((RockerModel) model).render(ArrayOfByteArraysOutput.FACTORY,
59+
template -> {
60+
if (template instanceof RequestRockerTemplate) {
61+
RequestRockerTemplate rrt = (RequestRockerTemplate) template;
62+
rrt.locals = ctx.locals();
63+
}
64+
});
65+
ctx.type(MediaType.html)
66+
.length(output.getByteLength())
67+
// FIXME: make more efficient. Context should provide a way to send partial results
68+
.send(Channels.newInputStream(output.asReadableByteChannel()));
69+
}
70+
}
71+
72+
private String path(final String path) {
73+
if (path.startsWith("/")) {
74+
return path.substring(1);
75+
}
76+
return path;
77+
}
78+
79+
}

0 commit comments

Comments
 (0)