Skip to content
This repository was archived by the owner on Dec 15, 2018. It is now read-only.

Commit 9cb2441

Browse files
Daniel-Doschkal
authored andcommitted
Groovy ViewEngine (#176)
1 parent fb00783 commit 9cb2441

File tree

14 files changed

+482
-0
lines changed

14 files changed

+482
-0
lines changed

ext/groovy/pom.xml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright © 2017 Ivar Grimstad ([email protected])
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
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, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<project
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
21+
xmlns="http://maven.apache.org/POM/4.0.0"
22+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
23+
<modelVersion>4.0.0</modelVersion>
24+
<parent>
25+
<groupId>org.mvc-spec.ozark.ext</groupId>
26+
<artifactId>ozark-parent-ext</artifactId>
27+
<version>1.0.0-m04-SNAPSHOT</version>
28+
</parent>
29+
<artifactId>ozark-groovy</artifactId>
30+
<packaging>jar</packaging>
31+
<name>Ozark Groovy Extension</name>
32+
33+
<dependencies>
34+
<dependency>
35+
<groupId>org.codehaus.groovy</groupId>
36+
<artifactId>groovy-templates</artifactId>
37+
<version>2.5.0</version>
38+
</dependency>
39+
</dependencies>
40+
</project>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright © 2017 Ivar Grimstad ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mvcspec.ozark.ext.groovy;
17+
18+
import java.io.BufferedReader;
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.io.InputStreamReader;
22+
import java.io.OutputStreamWriter;
23+
import java.io.Writer;
24+
import java.nio.charset.Charset;
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
import java.util.stream.Collectors;
28+
29+
import javax.enterprise.context.ApplicationScoped;
30+
import javax.inject.Inject;
31+
import javax.mvc.engine.ViewEngineContext;
32+
import javax.mvc.engine.ViewEngineException;
33+
import javax.servlet.ServletContext;
34+
import javax.servlet.http.HttpServletRequest;
35+
36+
import org.codehaus.groovy.control.CompilationFailedException;
37+
import org.mvcspec.ozark.engine.ViewEngineBase;
38+
import org.mvcspec.ozark.engine.ViewEngineConfig;
39+
40+
import groovy.lang.Writable;
41+
import groovy.text.Template;
42+
import groovy.text.markup.MarkupTemplateEngine;
43+
44+
/**
45+
* @author Daniel Dias
46+
*/
47+
@ApplicationScoped
48+
public class GroovyViewEngine extends ViewEngineBase {
49+
50+
@Inject
51+
@ViewEngineConfig
52+
private MarkupTemplateEngine markupTemplateEngine;
53+
54+
@Inject
55+
private ServletContext servletContext;
56+
57+
public boolean supports(String view) {
58+
return view.endsWith(".tpl");
59+
}
60+
61+
public void processView(ViewEngineContext context) throws ViewEngineException {
62+
63+
Map<String, Object> model = new HashMap<>(context.getModels());
64+
model.put("request", context.getRequest(HttpServletRequest.class));
65+
Charset charset = resolveCharsetAndSetContentType(context);
66+
try (Writer writer = new OutputStreamWriter(context.getOutputStream(), charset);
67+
InputStream resourceAsStream = servletContext.getResourceAsStream(resolveView(context));
68+
InputStreamReader in = new InputStreamReader(resourceAsStream, "UTF-8");) {
69+
Template template = markupTemplateEngine.createTemplate(in);
70+
Writable output = template.make(model);
71+
output.writeTo(writer);
72+
} catch (IOException | CompilationFailedException | ClassNotFoundException e) {
73+
throw new ViewEngineException(e);
74+
}
75+
}
76+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright © 2017 Ivar Grimstad ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mvcspec.ozark.ext.groovy;
17+
18+
import javax.enterprise.inject.Produces;
19+
20+
import org.mvcspec.ozark.engine.ViewEngineConfig;
21+
22+
import groovy.text.markup.MarkupTemplateEngine;
23+
24+
/**
25+
* @author Daniel Dias
26+
*/
27+
public class MarkupTemplateEngineProducer {
28+
29+
@Produces
30+
@ViewEngineConfig
31+
public MarkupTemplateEngine getMarkupTemplateEngine() {
32+
return new MarkupTemplateEngine();
33+
}
34+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright © 2017 Ivar Grimstad ([email protected])
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
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, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
20+
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
21+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
23+
bean-discovery-mode="all">
24+
</beans>

ext/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<module>thymeleaf</module>
4343
<module>velocity</module>
4444
<module>pebble</module>
45+
<module>groovy</module>
4546
</modules>
4647

4748
<dependencies>

test/groovy/pom.xml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright © 2017 Ivar Grimstad ([email protected])
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
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, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
20+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
21+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
22+
<modelVersion>4.0.0</modelVersion>
23+
<parent>
24+
<groupId>org.mvc-spec.ozark.test</groupId>
25+
<artifactId>ozark-parent-test</artifactId>
26+
<version>1.0.0-m04-SNAPSHOT</version>
27+
</parent>
28+
<artifactId>groovy</artifactId>
29+
<packaging>war</packaging>
30+
<name>Ozark Groovy Tests</name>
31+
<build>
32+
<finalName>test-groovy</finalName>
33+
</build>
34+
<dependencies>
35+
<dependency>
36+
<groupId>org.mvc-spec.ozark.ext</groupId>
37+
<artifactId>ozark-groovy</artifactId>
38+
<version>${project.version}</version>
39+
<scope>compile</scope>
40+
</dependency>
41+
</dependencies>
42+
</project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright © 2017 Ivar Grimstad ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mvcspec.ozark.test.groovy;
17+
18+
import javax.inject.Inject;
19+
import javax.mvc.Controller;
20+
import javax.mvc.Models;
21+
import javax.mvc.View;
22+
import javax.ws.rs.GET;
23+
import javax.ws.rs.Path;
24+
import javax.ws.rs.Produces;
25+
import javax.ws.rs.QueryParam;
26+
27+
/**
28+
* HelloController test.
29+
*
30+
* @author Daniel Dias
31+
*/
32+
@Path("hello")
33+
public class HelloController {
34+
35+
@Inject
36+
private Models models;
37+
38+
@GET
39+
@Controller
40+
@View("hello.tpl")
41+
public void hello(@QueryParam("user") String user) {
42+
models.put("user", user);
43+
}
44+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright © 2017 Ivar Grimstad ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mvcspec.ozark.test.groovy;
17+
18+
import javax.ws.rs.ApplicationPath;
19+
import javax.ws.rs.core.Application;
20+
import java.util.Collections;
21+
import java.util.Set;
22+
23+
/**
24+
* Class MyApplication.
25+
*
26+
* @author Daniel Dias
27+
*/
28+
@ApplicationPath("resources")
29+
public class MyApplication extends Application {
30+
31+
@Override
32+
public Set<Class<?>> getClasses() {
33+
return Collections.singleton(HelloController.class);
34+
}
35+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright © 2017 Ivar Grimstad ([email protected])
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
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, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
20+
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
21+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
23+
bean-discovery-mode="all">
24+
</beans>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
yieldUnescaped '<!DOCTYPE html>'
2+
html(lang:'en') {
3+
head {
4+
meta('http-equiv':'"Content-Type" content="text/html; charset=utf-8"')
5+
link(rel: "stylesheet", href: "${request.contextPath}/ozark.css")
6+
title('Hello')
7+
}
8+
body {
9+
h1("Hello $user")
10+
}
11+
}

0 commit comments

Comments
 (0)