-
Notifications
You must be signed in to change notification settings - Fork 195
/
RsVelocity.java
227 lines (215 loc) · 6.85 KB
/
RsVelocity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2018 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.takes.rs;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.net.URL;
import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.takes.Response;
import org.takes.Scalar;
import org.takes.misc.Utf8InputStreamReader;
import org.takes.misc.Utf8OutputStreamWriter;
import org.takes.misc.Utf8String;
/**
* Response that converts Velocity template to text.
*
* <p>This response implementation is rendering a page from
* Apache Velocity template. Here is how you can use it:
*
* <pre>public final class TkHelp implements Take {
* @Override
* public Response act(final Request req) {
* return new RsHTML(
* new RsVelocity(
* this.getClass().getResource("help.html.vm"),
* new RsVelocity.Pair("name", "Jeffrey")
* )
* );
* }
* }</pre>
*
* <p>The class is immutable and thread-safe.
*
* @since 0.1
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public final class RsVelocity extends RsWrap {
/**
* Ctor.
* @param template Template
* @param params List of params
* @since 0.11
*/
public RsVelocity(final CharSequence template,
final RsVelocity.Pair... params) {
this(
new ByteArrayInputStream(
new Utf8String(template.toString()).bytes()
), params
);
}
/**
* Ctor.
* @param template Template
* @param params List of params
* @throws IOException If fails
* @since 0.11
*/
public RsVelocity(final URL template,
final RsVelocity.Pair... params) throws IOException {
this(template.openStream(), params);
}
/**
* Ctor.
* @param template Template
* @param params Entries
*/
public RsVelocity(final InputStream template,
final RsVelocity.Pair... params) {
this(template, RsVelocity.asMap(params));
}
/**
* Ctor.
* @param template Template
* @param params Map of params
*/
public RsVelocity(final InputStream template, final Map<CharSequence,
Object> params) {
this(".", template, params);
}
/**
* Ctor.
* @param folder Template folder
* @param template Template
* @param params Map of params
*/
public RsVelocity(final String folder,
final InputStream template, final Map<CharSequence, Object> params) {
this(folder, template, () -> RsVelocity.convert(params));
}
/**
* Ctor.
* @param folder Template folder
* @param template Template
* @param params Map of params
*/
public RsVelocity(final String folder,
final InputStream template, final Scalar<Map<String, Object>> params) {
super(
new Response() {
@Override
public Iterable<String> head() {
return new RsEmpty().head();
}
@Override
public InputStream body() throws IOException {
return RsVelocity.render(folder, template, params.get());
}
}
);
}
/**
* Render it.
* @param folder Template folder
* @param template Page template
* @param params Params for velocity
* @return Page body
* @throws IOException If fails
*/
private static InputStream render(final String folder,
final InputStream template,
final Map<String, Object> params) throws IOException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final Writer writer = new Utf8OutputStreamWriter(baos);
final VelocityEngine engine = new VelocityEngine();
engine.setProperty(
"file.resource.loader.path",
folder
);
engine.evaluate(
new VelocityContext(params),
writer,
"",
new Utf8InputStreamReader(template)
);
writer.close();
return new ByteArrayInputStream(baos.toByteArray());
}
/**
* Convert entries to map.
* @param entries Entries
* @return Map
*/
@SafeVarargs
private static Map<CharSequence, Object> asMap(
final Map.Entry<CharSequence, Object>... entries) {
final Map<CharSequence, Object> map = new HashMap<>(entries.length);
for (final Map.Entry<CharSequence, Object> ent : entries) {
map.put(ent.getKey(), ent.getValue());
}
return map;
}
/**
* Converts Map of CharSequence, Object to Map of String, Object.
* @param params Parameters in Map of CharSequence, Object
* @return Map of String, Object.
*/
private static Map<String, Object> convert(
final Map<CharSequence, Object> params) {
final Map<String, Object> map = new HashMap<>(params.size());
for (final Map.Entry<CharSequence, Object> ent : params.entrySet()) {
map.put(ent.getKey().toString(), ent.getValue());
}
return map;
}
/**
* Pair of values.
*/
public static final class Pair
extends AbstractMap.SimpleEntry<CharSequence, Object> {
/**
* Serialization marker.
*/
private static final long serialVersionUID = 7362489770169963015L;
/**
* Ctor.
* @param key Key
* @param obj Pass
*/
public Pair(final CharSequence key, final Object obj) {
super(key, obj);
}
}
}