|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| package org.apache.tapestry.web; |
|
16 |
| |
|
17 |
| import java.io.IOException; |
|
18 |
| import java.security.Principal; |
|
19 |
| import java.util.List; |
|
20 |
| import java.util.Locale; |
|
21 |
| |
|
22 |
| import javax.servlet.RequestDispatcher; |
|
23 |
| import javax.servlet.ServletException; |
|
24 |
| import javax.servlet.http.HttpServletRequest; |
|
25 |
| import javax.servlet.http.HttpServletResponse; |
|
26 |
| import javax.servlet.http.HttpSession; |
|
27 |
| |
|
28 |
| import org.apache.hivemind.ApplicationRuntimeException; |
|
29 |
| import org.apache.hivemind.HiveMind; |
|
30 |
| import org.apache.hivemind.util.Defense; |
|
31 |
| import org.apache.tapestry.describe.DescriptionReceiver; |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| public class ServletWebRequest implements WebRequest |
|
41 |
| { |
|
42 |
| private final HttpServletRequest _servletRequest; |
|
43 |
| |
|
44 |
| private final HttpServletResponse _servletResponse; |
|
45 |
| |
|
46 |
| private WebSession _webSession; |
|
47 |
| |
|
48 |
432
| public ServletWebRequest(HttpServletRequest request, HttpServletResponse response)
|
|
49 |
| { |
|
50 |
432
| Defense.notNull(request, "request");
|
|
51 |
432
| Defense.notNull(response, "response");
|
|
52 |
| |
|
53 |
432
| _servletRequest = request;
|
|
54 |
432
| _servletResponse = response;
|
|
55 |
| } |
|
56 |
| |
|
57 |
1104
| public List getParameterNames()
|
|
58 |
| { |
|
59 |
1104
| return WebUtils.toSortedList(_servletRequest.getParameterNames());
|
|
60 |
| } |
|
61 |
| |
|
62 |
3
| public String getParameterValue(String parameterName)
|
|
63 |
| { |
|
64 |
3
| return _servletRequest.getParameter(parameterName);
|
|
65 |
| } |
|
66 |
| |
|
67 |
1068
| public String[] getParameterValues(String parameterName)
|
|
68 |
| { |
|
69 |
1068
| return _servletRequest.getParameterValues(parameterName);
|
|
70 |
| } |
|
71 |
| |
|
72 |
438
| public String getContextPath()
|
|
73 |
| { |
|
74 |
438
| return _servletRequest.getContextPath();
|
|
75 |
| } |
|
76 |
| |
|
77 |
933
| public WebSession getSession(boolean create)
|
|
78 |
| { |
|
79 |
933
| if (_webSession != null)
|
|
80 |
237
| return _webSession;
|
|
81 |
| |
|
82 |
696
| HttpSession session = _servletRequest.getSession(create);
|
|
83 |
| |
|
84 |
696
| if (session != null)
|
|
85 |
111
| _webSession = new ServletWebSession(session);
|
|
86 |
| |
|
87 |
696
| return _webSession;
|
|
88 |
| } |
|
89 |
| |
|
90 |
3
| public List getAttributeNames()
|
|
91 |
| { |
|
92 |
3
| return WebUtils.toSortedList(_servletRequest.getAttributeNames());
|
|
93 |
| } |
|
94 |
| |
|
95 |
144
| public Object getAttribute(String name)
|
|
96 |
| { |
|
97 |
144
| return _servletRequest.getAttribute(name);
|
|
98 |
| } |
|
99 |
| |
|
100 |
375
| public void setAttribute(String name, Object attribute)
|
|
101 |
| { |
|
102 |
375
| if (attribute == null)
|
|
103 |
3
| _servletRequest.removeAttribute(name);
|
|
104 |
| else |
|
105 |
372
| _servletRequest.setAttribute(name, attribute);
|
|
106 |
| } |
|
107 |
| |
|
108 |
321
| public String getScheme()
|
|
109 |
| { |
|
110 |
321
| return _servletRequest.getScheme();
|
|
111 |
| } |
|
112 |
| |
|
113 |
321
| public String getServerName()
|
|
114 |
| { |
|
115 |
321
| return _servletRequest.getServerName();
|
|
116 |
| } |
|
117 |
| |
|
118 |
321
| public int getServerPort()
|
|
119 |
| { |
|
120 |
321
| return _servletRequest.getServerPort();
|
|
121 |
| } |
|
122 |
| |
|
123 |
372
| public String getRequestURI()
|
|
124 |
| { |
|
125 |
372
| return _servletRequest.getRequestURI();
|
|
126 |
| } |
|
127 |
| |
|
128 |
21
| public void forward(String URL)
|
|
129 |
| { |
|
130 |
21
| if (HiveMind.isBlank(URL))
|
|
131 |
| { |
|
132 |
3
| performForward("/");
|
|
133 |
3
| return;
|
|
134 |
| } |
|
135 |
| |
|
136 |
18
| boolean internal = !(URL.startsWith("/") || URL.indexOf("://") > 0);
|
|
137 |
| |
|
138 |
18
| if (internal)
|
|
139 |
12
| performForward("/" + URL);
|
|
140 |
| else |
|
141 |
6
| sendRedirect(URL);
|
|
142 |
| } |
|
143 |
| |
|
144 |
6
| private void sendRedirect(String URL)
|
|
145 |
| { |
|
146 |
6
| String finalURL = _servletResponse.encodeRedirectURL(URL);
|
|
147 |
| |
|
148 |
6
| try
|
|
149 |
| { |
|
150 |
6
| _servletResponse.sendRedirect(finalURL);
|
|
151 |
| } |
|
152 |
| catch (IOException ex) |
|
153 |
| { |
|
154 |
3
| throw new ApplicationRuntimeException(WebMessages.unableToRedirect(URL, ex), ex);
|
|
155 |
| } |
|
156 |
| |
|
157 |
| } |
|
158 |
| |
|
159 |
15
| private void performForward(String URL)
|
|
160 |
| { |
|
161 |
15
| RequestDispatcher dispatcher = _servletRequest.getRequestDispatcher(URL);
|
|
162 |
| |
|
163 |
15
| if (dispatcher == null)
|
|
164 |
3
| throw new ApplicationRuntimeException(WebMessages.unableToFindDispatcher(URL));
|
|
165 |
| |
|
166 |
12
| try
|
|
167 |
| { |
|
168 |
12
| dispatcher.forward(_servletRequest, _servletResponse);
|
|
169 |
| } |
|
170 |
| catch (ServletException ex) |
|
171 |
| { |
|
172 |
3
| throw new ApplicationRuntimeException(WebMessages.unableToForward(URL, ex), ex);
|
|
173 |
| } |
|
174 |
| catch (IOException ex) |
|
175 |
| { |
|
176 |
3
| throw new ApplicationRuntimeException(WebMessages.unableToForward(URL, ex), ex);
|
|
177 |
| } |
|
178 |
| } |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
372
| public String getActivationPath()
|
|
184 |
| { |
|
185 |
372
| return _servletRequest.getServletPath();
|
|
186 |
| } |
|
187 |
| |
|
188 |
372
| public String getPathInfo()
|
|
189 |
| { |
|
190 |
372
| return _servletRequest.getPathInfo();
|
|
191 |
| } |
|
192 |
| |
|
193 |
342
| public Locale getLocale()
|
|
194 |
| { |
|
195 |
342
| return _servletRequest.getLocale();
|
|
196 |
| } |
|
197 |
| |
|
198 |
51
| public void describeTo(DescriptionReceiver receiver)
|
|
199 |
| { |
|
200 |
51
| receiver.describeAlternate(_servletRequest);
|
|
201 |
| } |
|
202 |
| |
|
203 |
3
| public String getHeader(String name)
|
|
204 |
| { |
|
205 |
3
| return _servletRequest.getHeader(name);
|
|
206 |
| } |
|
207 |
| |
|
208 |
0
| public String getRemoteUser()
|
|
209 |
| { |
|
210 |
0
| return _servletRequest.getRemoteUser();
|
|
211 |
| } |
|
212 |
| |
|
213 |
0
| public Principal getUserPrincipal()
|
|
214 |
| { |
|
215 |
0
| return _servletRequest.getUserPrincipal();
|
|
216 |
| } |
|
217 |
| |
|
218 |
0
| public boolean isUserInRole(String role)
|
|
219 |
| { |
|
220 |
0
| return _servletRequest.isUserInRole(role);
|
|
221 |
| } |
|
222 |
| } |