1 package com.atlassian.cache.servlet.resolver;
2
3 import com.atlassian.cache.servlet.CombinedCachingServlet;
4
5 import javax.servlet.*;
6 import javax.servlet.http.*;
7 import java.io.*;
8 import java.security.Principal;
9 import java.util.Enumeration;
10 import java.util.Locale;
11 import java.util.Map;
12
13
14
15
16 public class DwrContentResolver implements ContentResolver
17 {
18 private BogusDwrServlet dwrServlet = new BogusDwrServlet();
19
20
21 public String getContent(String path, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
22 {
23 ByteArrayOutputStream os = new ByteArrayOutputStream();
24 dwrServlet.doGet(new EvilHttpRequest(request, path), new OutputCollectingResponse(response, os));
25 return os.toString(CombinedCachingServlet.DEFAULT_ENCODING);
26 }
27
28 public void init(ServletConfig servletConfig) throws ServletException
29 {
30 dwrServlet = new BogusDwrServlet();
31 dwrServlet.init(servletConfig);
32 }
33
34 public static class EvilHttpRequest implements HttpServletRequest
35 {
36 private HttpServletRequest wrappedRequest;
37 private String pathInfo;
38
39 public EvilHttpRequest(HttpServletRequest wrappedRequest, String pathInfo)
40 {
41 this.wrappedRequest = wrappedRequest;
42 this.pathInfo = pathInfo;
43 }
44
45
46 public String getAuthType()
47 {
48 return wrappedRequest.getAuthType();
49 }
50
51 public Cookie[] getCookies()
52 {
53 return wrappedRequest.getCookies();
54 }
55
56 public long getDateHeader(String s)
57 {
58 return wrappedRequest.getDateHeader(s);
59 }
60
61 public String getHeader(String s)
62 {
63 return wrappedRequest.getHeader(s);
64 }
65
66 public Enumeration getHeaders(String s)
67 {
68 return wrappedRequest.getHeaders(s);
69 }
70
71 public Enumeration getHeaderNames()
72 {
73 return wrappedRequest.getHeaderNames();
74 }
75
76 public int getIntHeader(String s)
77 {
78 return wrappedRequest.getIntHeader(s);
79 }
80
81 public String getMethod()
82 {
83 return wrappedRequest.getMethod();
84 }
85
86 public String getPathInfo()
87 {
88 return pathInfo;
89 }
90
91 public String getPathTranslated()
92 {
93 return wrappedRequest.getPathTranslated();
94 }
95
96 public String getContextPath()
97 {
98 return wrappedRequest.getContextPath();
99 }
100
101 public String getQueryString()
102 {
103 return wrappedRequest.getQueryString();
104 }
105
106 public String getRemoteUser()
107 {
108 return wrappedRequest.getRemoteUser();
109 }
110
111 public boolean isUserInRole(String s)
112 {
113 return wrappedRequest.isUserInRole(s);
114 }
115
116 public Principal getUserPrincipal()
117 {
118 return wrappedRequest.getUserPrincipal();
119 }
120
121 public String getRequestedSessionId()
122 {
123 return wrappedRequest.getRequestedSessionId();
124 }
125
126 public String getRequestURI()
127 {
128 return wrappedRequest.getRequestURI();
129 }
130
131 public StringBuffer getRequestURL()
132 {
133 return wrappedRequest.getRequestURL();
134 }
135
136 public String getServletPath()
137 {
138 return "/dwr";
139 }
140
141 public HttpSession getSession(boolean b)
142 {
143 return wrappedRequest.getSession(b);
144 }
145
146 public HttpSession getSession()
147 {
148 return wrappedRequest.getSession();
149 }
150
151 public boolean isRequestedSessionIdValid()
152 {
153 return wrappedRequest.isRequestedSessionIdValid();
154 }
155
156 public boolean isRequestedSessionIdFromCookie()
157 {
158 return wrappedRequest.isRequestedSessionIdFromCookie();
159 }
160
161 public boolean isRequestedSessionIdFromURL()
162 {
163 return wrappedRequest.isRequestedSessionIdFromURL();
164 }
165
166
167
168
169 public boolean isRequestedSessionIdFromUrl()
170 {
171 return wrappedRequest.isRequestedSessionIdFromUrl();
172 }
173
174 public Object getAttribute(String s)
175 {
176 return wrappedRequest.getAttribute(s);
177 }
178
179 public Enumeration getAttributeNames()
180 {
181 return wrappedRequest.getAttributeNames();
182 }
183
184 public String getCharacterEncoding()
185 {
186 return wrappedRequest.getCharacterEncoding();
187 }
188
189 public void setCharacterEncoding(String s)
190 throws UnsupportedEncodingException
191 {
192 wrappedRequest.setCharacterEncoding(s);
193 }
194
195 public int getContentLength()
196 {
197 return wrappedRequest.getContentLength();
198 }
199
200 public String getContentType()
201 {
202 return wrappedRequest.getContentType();
203 }
204
205 public ServletInputStream getInputStream()
206 throws IOException
207 {
208 return wrappedRequest.getInputStream();
209 }
210
211 public String getParameter(String s)
212 {
213 return wrappedRequest.getParameter(s);
214 }
215
216 public Enumeration getParameterNames()
217 {
218 return wrappedRequest.getParameterNames();
219 }
220
221 public String[] getParameterValues(String s)
222 {
223 return wrappedRequest.getParameterValues(s);
224 }
225
226 public Map getParameterMap()
227 {
228 return wrappedRequest.getParameterMap();
229 }
230
231 public String getProtocol()
232 {
233 return wrappedRequest.getProtocol();
234 }
235
236 public String getScheme()
237 {
238 return wrappedRequest.getScheme();
239 }
240
241 public String getServerName()
242 {
243 return wrappedRequest.getServerName();
244 }
245
246 public int getServerPort()
247 {
248 return wrappedRequest.getServerPort();
249 }
250
251 public BufferedReader getReader()
252 throws IOException
253 {
254 return wrappedRequest.getReader();
255 }
256
257 public String getRemoteAddr()
258 {
259 return wrappedRequest.getRemoteAddr();
260 }
261
262 public String getRemoteHost()
263 {
264 return wrappedRequest.getRemoteHost();
265 }
266
267 public void setAttribute(String s, Object o)
268 {
269 wrappedRequest.setAttribute(s, o);
270 }
271
272 public void removeAttribute(String s)
273 {
274 wrappedRequest.removeAttribute(s);
275 }
276
277 public Locale getLocale()
278 {
279 return wrappedRequest.getLocale();
280 }
281
282 public Enumeration getLocales()
283 {
284 return wrappedRequest.getLocales();
285 }
286
287 public boolean isSecure()
288 {
289 return wrappedRequest.isSecure();
290 }
291
292 public RequestDispatcher getRequestDispatcher(String s)
293 {
294 return wrappedRequest.getRequestDispatcher(s);
295 }
296
297
298
299
300 public String getRealPath(String s)
301 {
302 return wrappedRequest.getRealPath(s);
303 }
304 }
305
306 private static class OutputCollectingResponse implements HttpServletResponse
307 {
308 private HttpServletResponse response;
309 private ServletOutputStream os;
310
311 public OutputCollectingResponse(HttpServletResponse response, OutputStream os)
312 {
313 this.response = response;
314 this.os = new ServletOutputStreamAdapter(os);
315 }
316
317 public void addCookie(Cookie cookie)
318 {
319 response.addCookie(cookie);
320 }
321
322 public boolean containsHeader(String s)
323 {
324 return response.containsHeader(s);
325 }
326
327 public String encodeURL(String s)
328 {
329 return response.encodeURL(s);
330 }
331
332 public String encodeRedirectURL(String s)
333 {
334 return response.encodeRedirectURL(s);
335 }
336
337
338
339
340 public String encodeUrl(String s)
341 {
342 return response.encodeUrl(s);
343 }
344
345
346
347
348 public String encodeRedirectUrl(String s)
349 {
350 return response.encodeRedirectUrl(s);
351 }
352
353 public void sendError(int i, String s)
354 throws IOException
355 {
356 response.sendError(i, s);
357 }
358
359 public void sendError(int i)
360 throws IOException
361 {
362 response.sendError(i);
363 }
364
365 public void sendRedirect(String s)
366 throws IOException
367 {
368 response.sendRedirect(s);
369 }
370
371 public void setDateHeader(String s, long l)
372 {
373
374 }
375
376 public void addDateHeader(String s, long l)
377 {
378
379 }
380
381 public void setHeader(String s, String s1)
382 {
383
384 }
385
386 public void addHeader(String s, String s1)
387 {
388
389 }
390
391 public void setIntHeader(String s, int i)
392 {
393
394 }
395
396 public void addIntHeader(String s, int i)
397 {
398
399 }
400
401 public void setStatus(int i)
402 {
403 response.setStatus(i);
404 }
405
406
407
408
409 public void setStatus(int i, String s)
410 {
411 response.setStatus(i, s);
412 }
413
414 public String getCharacterEncoding()
415 {
416 return response.getCharacterEncoding();
417 }
418
419 public ServletOutputStream getOutputStream()
420 throws IOException
421 {
422 return os;
423 }
424
425 public PrintWriter getWriter()
426 throws IOException
427 {
428 return new PrintWriter(getOutputStream());
429 }
430
431 public void setContentLength(int i)
432 {
433 response.setContentLength(i);
434 }
435
436 public void setContentType(String s)
437 {
438 response.setContentType(s);
439 }
440
441 public void setBufferSize(int i)
442 {
443 response.setBufferSize(i);
444 }
445
446 public int getBufferSize()
447 {
448 return response.getBufferSize();
449 }
450
451 public void flushBuffer()
452 throws IOException
453 {
454 response.flushBuffer();
455 }
456
457 public void resetBuffer()
458 {
459 response.resetBuffer();
460 }
461
462 public boolean isCommitted()
463 {
464 return response.isCommitted();
465 }
466
467 public void reset()
468 {
469 response.reset();
470 }
471
472 public void setLocale(Locale locale)
473 {
474 response.setLocale(locale);
475 }
476
477 public Locale getLocale()
478 {
479 return response.getLocale();
480 }
481 }
482
483 private static class ServletOutputStreamAdapter extends ServletOutputStream
484 {
485 private OutputStream os;
486
487 public ServletOutputStreamAdapter(OutputStream os)
488 {
489 this.os = os;
490 }
491
492 public void write(int b) throws IOException
493 {
494 os.write(b);
495 }
496
497 public void flush() throws IOException
498 {
499 os.flush();
500 }
501
502 public void close() throws IOException
503 {
504
505 }
506 }
507
508 }