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.velocity; |
23 | |
|
24 | |
import java.io.IOException; |
25 | |
import java.io.PrintWriter; |
26 | |
import java.io.Writer; |
27 | |
import java.util.ArrayList; |
28 | |
import java.util.Collections; |
29 | |
import java.util.List; |
30 | |
import java.util.Map; |
31 | |
|
32 | |
import javax.servlet.RequestDispatcher; |
33 | |
import javax.servlet.ServletException; |
34 | |
import javax.servlet.http.HttpServletRequest; |
35 | |
import javax.servlet.http.HttpServletResponse; |
36 | |
|
37 | |
import org.apache.tiles.request.AbstractViewRequest; |
38 | |
import org.apache.tiles.request.ApplicationContext; |
39 | |
import org.apache.tiles.request.DispatchRequest; |
40 | |
import org.apache.tiles.request.servlet.ExternalWriterHttpServletResponse; |
41 | |
import org.apache.tiles.request.servlet.ServletRequest; |
42 | |
import org.apache.tiles.request.servlet.ServletUtil; |
43 | |
import org.apache.velocity.context.Context; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public class VelocityRequest extends AbstractViewRequest { |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
private final List<String> scopes; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
private final Context ctx; |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
private Writer writer; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
private Map<String, Object> pageScope; |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public static VelocityRequest createVelocityRequest( |
83 | |
ApplicationContext applicationContext, HttpServletRequest request, |
84 | |
HttpServletResponse response, Context velocityContext, Writer writer) { |
85 | 2 | DispatchRequest servletRequest = new ServletRequest( |
86 | |
applicationContext, request, response); |
87 | 2 | VelocityRequest velocityRequest = new VelocityRequest( |
88 | |
servletRequest, velocityContext, writer); |
89 | 2 | return velocityRequest; |
90 | |
} |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
public VelocityRequest( |
100 | |
DispatchRequest enclosedRequest, Context ctx, Writer writer) { |
101 | 12 | super(enclosedRequest); |
102 | 12 | List<String> scopes = new ArrayList<String>(); |
103 | 12 | scopes.addAll(enclosedRequest.getAvailableScopes()); |
104 | 12 | scopes.add("page"); |
105 | 12 | this.scopes = Collections.unmodifiableList(scopes); |
106 | 12 | this.ctx = ctx; |
107 | 12 | this.writer = writer; |
108 | 12 | } |
109 | |
|
110 | |
@Override |
111 | |
public List<String> getAvailableScopes() { |
112 | 1 | return scopes; |
113 | |
} |
114 | |
|
115 | |
|
116 | |
@Override |
117 | |
protected void doInclude(String path) throws IOException { |
118 | 3 | ServletRequest servletRequest = org.apache.tiles.request.servlet.ServletUtil.getServletRequest(this); |
119 | 3 | HttpServletRequest request = servletRequest.getRequest(); |
120 | 3 | HttpServletResponse response = servletRequest.getResponse(); |
121 | 3 | RequestDispatcher rd = request.getRequestDispatcher(path); |
122 | |
|
123 | 3 | if (rd == null) { |
124 | 1 | throw new IOException("No request dispatcher returned for path '" |
125 | |
+ path + "'"); |
126 | |
} |
127 | |
|
128 | 2 | PrintWriter printWriter = getPrintWriter(); |
129 | |
try { |
130 | 2 | rd.include(request, new ExternalWriterHttpServletResponse(response, |
131 | |
printWriter)); |
132 | 1 | } catch (ServletException ex) { |
133 | 1 | throw ServletUtil.wrapServletException(ex, "ServletException including path '" |
134 | |
+ path + "'."); |
135 | 1 | } |
136 | 1 | } |
137 | |
|
138 | |
|
139 | |
@Override |
140 | |
public PrintWriter getPrintWriter() { |
141 | 5 | if (writer == null) { |
142 | 1 | throw new IllegalStateException( |
143 | |
"A writer-less Tiles request has been created, cannot return a PrintWriter"); |
144 | |
} |
145 | 4 | if (writer instanceof PrintWriter) { |
146 | 1 | return (PrintWriter) writer; |
147 | |
} |
148 | 3 | return new PrintWriter(writer); |
149 | |
} |
150 | |
|
151 | |
|
152 | |
@Override |
153 | |
public Writer getWriter() { |
154 | 2 | if (writer == null) { |
155 | 1 | throw new IllegalStateException( |
156 | |
"A writer-less Tiles request has been created, cannot return a PrintWriter"); |
157 | |
} |
158 | 1 | return writer; |
159 | |
} |
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
public Map<String, Object> getPageScope() { |
167 | 1 | if (pageScope == null) { |
168 | 1 | pageScope = new VelocityScopeMap(ctx); |
169 | |
} |
170 | 1 | return pageScope; |
171 | |
} |
172 | |
|
173 | |
@Override |
174 | |
public Map<String, Object> getContext(String scope) { |
175 | 0 | return "page".equals(scope) ? getPageScope() : super.getContext(scope); |
176 | |
} |
177 | |
|
178 | |
} |