1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
package org.apache.tiles.request.freemarker; |
23 | |
|
24 | |
import java.io.PrintWriter; |
25 | |
import java.io.Writer; |
26 | |
import java.util.ArrayList; |
27 | |
import java.util.Collections; |
28 | |
import java.util.List; |
29 | |
import java.util.Locale; |
30 | |
import java.util.Map; |
31 | |
|
32 | |
import javax.servlet.http.HttpServletRequest; |
33 | |
import javax.servlet.http.HttpServletResponse; |
34 | |
|
35 | |
import org.apache.tiles.request.AbstractViewRequest; |
36 | |
import org.apache.tiles.request.ApplicationContext; |
37 | |
import org.apache.tiles.request.DispatchRequest; |
38 | |
import org.apache.tiles.request.servlet.ServletRequest; |
39 | |
|
40 | |
import freemarker.core.Environment; |
41 | |
import freemarker.ext.servlet.HttpRequestHashModel; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public class FreemarkerRequest extends AbstractViewRequest { |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
private List<String> scopes; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
private Environment env; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
private Map<String, Object> pageScope; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
public static FreemarkerRequest createServletFreemarkerRequest( |
73 | |
ApplicationContext applicationContext, Environment env) { |
74 | 2 | HttpRequestHashModel requestModel = FreemarkerRequestUtil |
75 | |
.getRequestHashModel(env); |
76 | 2 | HttpServletRequest request = requestModel.getRequest(); |
77 | 2 | HttpServletResponse response = requestModel.getResponse(); |
78 | 2 | DispatchRequest enclosedRequest = new ServletRequest( |
79 | |
applicationContext, request, response); |
80 | 2 | return new FreemarkerRequest(enclosedRequest, env); |
81 | |
} |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
public FreemarkerRequest(DispatchRequest enclosedRequest, |
92 | |
Environment env) { |
93 | 11 | super(enclosedRequest); |
94 | 11 | List<String> scopes = new ArrayList<String>(); |
95 | 11 | scopes.addAll(enclosedRequest.getAvailableScopes()); |
96 | 11 | scopes.add("page"); |
97 | 11 | this.scopes = Collections.unmodifiableList(scopes); |
98 | 11 | this.env = env; |
99 | 11 | } |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public Environment getEnvironment() { |
107 | 4 | return env; |
108 | |
} |
109 | |
|
110 | |
|
111 | |
@Override |
112 | |
public Locale getRequestLocale() { |
113 | 1 | return env.getLocale(); |
114 | |
} |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
public Map<String, Object> getPageScope() { |
122 | 1 | if (pageScope == null) { |
123 | 1 | pageScope = new EnvironmentScopeMap(env); |
124 | |
} |
125 | 1 | return pageScope; |
126 | |
} |
127 | |
|
128 | |
@Override |
129 | |
public List<String> getAvailableScopes() { |
130 | 1 | return scopes; |
131 | |
} |
132 | |
|
133 | |
|
134 | |
@Override |
135 | |
public PrintWriter getPrintWriter() { |
136 | 2 | Writer writer = env.getOut(); |
137 | 2 | if (writer instanceof PrintWriter) { |
138 | 1 | return (PrintWriter) writer; |
139 | |
} |
140 | 1 | return new PrintWriter(writer); |
141 | |
} |
142 | |
|
143 | |
|
144 | |
@Override |
145 | |
public Writer getWriter() { |
146 | 1 | return env.getOut(); |
147 | |
} |
148 | |
|
149 | |
@Override |
150 | |
public Map<String, Object> getContext(String scope) { |
151 | 1 | return "page".equals(scope) ? getPageScope() : super.getContext(scope); |
152 | |
} |
153 | |
|
154 | |
} |