1
2
3
4
5
6
7
8
9 package com.atlassian.jira.servlet;
10
11 import java.io.BufferedReader;
12 import java.io.ByteArrayInputStream;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.OutputStream;
16 import java.io.UnsupportedEncodingException;
17 import java.security.Principal;
18 import java.util.Enumeration;
19 import java.util.Locale;
20 import java.util.Map;
21
22 import javax.servlet.RequestDispatcher;
23 import javax.servlet.ServletConfig;
24 import javax.servlet.ServletException;
25 import javax.servlet.ServletInputStream;
26 import javax.servlet.http.Cookie;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import javax.servlet.http.HttpSession;
30
31 import uk.ltd.getahead.dwr.Configuration;
32 import uk.ltd.getahead.dwr.Messages;
33 import uk.ltd.getahead.dwr.util.Logger;
34
35 import com.atlassian.jira.ComponentManager;
36 import com.atlassian.jira.web.servlet.FileServerServlet;
37 import com.atlassian.plugin.PluginAccessor;
38 import com.atlassian.plugin.PluginManager;
39 import com.atlassian.plugin.servlet.PluginResourceDownload;
40
41 public class DWRServlet extends uk.ltd.getahead.dwr.DWRServlet {
42
43 protected String urlPivot = "";
44
45 protected String getPivotParameterKey() {
46 return "urlPivot";
47 }
48
49 public void configure(ServletConfig config, Configuration configuration) throws ServletException {
50 super.configure(config, configuration);
51 urlPivot = config.getInitParameter(getPivotParameterKey());
52 }
53
54 protected String getUrlPivot() {
55 return urlPivot;
56 }
57
58 protected void setUrlPivot(String urlPivot) {
59 this.urlPivot = urlPivot;
60 }
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 private static final long serialVersionUID = 1L;
77
78 private static final Logger log = Logger.getLogger(DWRServlet.class);
79
80
81 protected PluginAccessor pluginAccessor = (PluginAccessor)ComponentManager.getInstance().getComponentInstanceOfType(PluginAccessor.class);
82
83 protected HttpServletRequest getWrappedRequest(HttpServletRequest req) {
84 return new EvilHttpRequest(req, getUrlPivot());
85 }
86
87 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
88 super.doGet(getWrappedRequest(req), resp);
89 }
90
91 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
92 super.doPost(getWrappedRequest(req), resp);
93 }
94
95
96
97
98 protected void readFile(String configFile, Configuration configuration) throws ServletException {
99 try {
100 InputStream in = getConfigInputStream(configFile);
101 if (in == null) {
102 log.error("Missing config file: " + configFile);
103 } else {
104 configuration.addConfig(in);
105 }
106 } catch (Exception ex) {
107 throw new ServletException(Messages.getString("DWRServlet.ConfigError", configFile), ex);
108 }
109 }
110
111 protected InputStream getConfigInputStream(String _configFile) throws IOException {
112
113 FileServerServlet s = new FileServerServlet();
114 MockResponse mockResponse = new MockResponse();
115 CharResponseWrapper charResponseWrapper = new CharResponseWrapper(mockResponse);
116 PluginResourceDownload downloadStrategy = new PluginResourceDownload(pluginAccessor);
117
118
119 MockRequest mockRequest = new MockRequest();
120 mockRequest.setRequestURI(_configFile);
121
122
123 downloadStrategy.serveFile(s, mockRequest, charResponseWrapper);
124 OutputStream out = charResponseWrapper.getOutputStream();
125
126 return new ByteArrayInputStream(out.toString().getBytes());
127 }
128
129
130 class MockResponse extends EmptyResponse implements HttpServletResponse {
131
132 }
133
134
135 class MockRequest extends EmptyRequest implements HttpServletRequest {
136 String requestURI;
137
138 public String getRequestURI() {
139 return requestURI;
140 }
141
142 public void setRequestURI(String _requestURI) {
143 requestURI = _requestURI;
144 }
145 }
146
147 public static class EvilHttpRequest implements HttpServletRequest {
148 private HttpServletRequest wrappedRequest;
149 private String pathInfo;
150 private String servletPath;
151 private String urlPivot;
152
153 public EvilHttpRequest(HttpServletRequest wrappedRequest, String _urlPivot) {
154 this.wrappedRequest = wrappedRequest;
155 this.servletPath = wrappedRequest.getServletPath();
156 this.pathInfo = wrappedRequest.getPathInfo();
157 this.urlPivot = _urlPivot;
158 String fullPath = servletPath + pathInfo;
159 int split = fullPath.indexOf(urlPivot) + urlPivot.length() - 1;
160 this.servletPath = fullPath.substring(0, split );
161 this.pathInfo = fullPath.substring(split);
162 }
163
164 public String getPathInfo() {
165 return pathInfo;
166 }
167
168 public String getServletPath() {
169 return servletPath;
170 }
171
172 public String getAuthType() {
173 return wrappedRequest.getAuthType();
174 }
175
176 public Cookie[] getCookies() {
177 return wrappedRequest.getCookies();
178 }
179
180 public long getDateHeader(String s) {
181 return wrappedRequest.getDateHeader(s);
182 }
183
184 public String getHeader(String s) {
185 return wrappedRequest.getHeader(s);
186 }
187
188 public Enumeration getHeaders(String s) {
189 return wrappedRequest.getHeaders(s);
190 }
191
192 public Enumeration getHeaderNames() {
193 return wrappedRequest.getHeaderNames();
194 }
195
196 public int getIntHeader(String s) {
197 return wrappedRequest.getIntHeader(s);
198 }
199
200 public String getMethod() {
201 return wrappedRequest.getMethod();
202 }
203
204 public String getPathTranslated() {
205 return wrappedRequest.getPathTranslated();
206 }
207
208 public String getContextPath() {
209 return wrappedRequest.getContextPath();
210 }
211
212 public String getQueryString() {
213 return wrappedRequest.getQueryString();
214 }
215
216 public String getRemoteUser() {
217 return wrappedRequest.getRemoteUser();
218 }
219
220 public boolean isUserInRole(String s) {
221 return wrappedRequest.isUserInRole(s);
222 }
223
224 public Principal getUserPrincipal() {
225 return wrappedRequest.getUserPrincipal();
226 }
227
228 public String getRequestedSessionId() {
229 return wrappedRequest.getRequestedSessionId();
230 }
231
232 public String getRequestURI() {
233 return wrappedRequest.getRequestURI();
234 }
235
236 public StringBuffer getRequestURL() {
237 return wrappedRequest.getRequestURL();
238 }
239
240 public HttpSession getSession(boolean b) {
241 return wrappedRequest.getSession(b);
242 }
243
244 public HttpSession getSession() {
245 return wrappedRequest.getSession();
246 }
247
248 public boolean isRequestedSessionIdValid() {
249 return wrappedRequest.isRequestedSessionIdValid();
250 }
251
252 public boolean isRequestedSessionIdFromCookie() {
253 return wrappedRequest.isRequestedSessionIdFromCookie();
254 }
255
256 public boolean isRequestedSessionIdFromURL() {
257 return wrappedRequest.isRequestedSessionIdFromURL();
258 }
259
260
261
262
263 public boolean isRequestedSessionIdFromUrl() {
264 return wrappedRequest.isRequestedSessionIdFromUrl();
265 }
266
267 public Object getAttribute(String s) {
268 return wrappedRequest.getAttribute(s);
269 }
270
271 public Enumeration getAttributeNames() {
272 return wrappedRequest.getAttributeNames();
273 }
274
275 public String getCharacterEncoding() {
276 return wrappedRequest.getCharacterEncoding();
277 }
278
279 public void setCharacterEncoding(String s) throws UnsupportedEncodingException {
280 wrappedRequest.setCharacterEncoding(s);
281 }
282
283 public int getContentLength() {
284 return wrappedRequest.getContentLength();
285 }
286
287 public String getContentType() {
288 return wrappedRequest.getContentType();
289 }
290
291 public ServletInputStream getInputStream() throws IOException {
292 return wrappedRequest.getInputStream();
293 }
294
295 public String getParameter(String s) {
296 return wrappedRequest.getParameter(s);
297 }
298
299 public Enumeration getParameterNames() {
300 return wrappedRequest.getParameterNames();
301 }
302
303 public String[] getParameterValues(String s) {
304 return wrappedRequest.getParameterValues(s);
305 }
306
307 public Map getParameterMap() {
308 return wrappedRequest.getParameterMap();
309 }
310
311 public String getProtocol() {
312 return wrappedRequest.getProtocol();
313 }
314
315 public String getScheme() {
316 return wrappedRequest.getScheme();
317 }
318
319 public String getServerName() {
320 return wrappedRequest.getServerName();
321 }
322
323 public int getServerPort() {
324 return wrappedRequest.getServerPort();
325 }
326
327 public BufferedReader getReader() throws IOException {
328 return wrappedRequest.getReader();
329 }
330
331 public String getRemoteAddr() {
332 return wrappedRequest.getRemoteAddr();
333 }
334
335 public String getRemoteHost() {
336 return wrappedRequest.getRemoteHost();
337 }
338
339 public void setAttribute(String s, Object o) {
340 wrappedRequest.setAttribute(s, o);
341 }
342
343 public void removeAttribute(String s) {
344 wrappedRequest.removeAttribute(s);
345 }
346
347 public Locale getLocale() {
348 return wrappedRequest.getLocale();
349 }
350
351 public Enumeration getLocales() {
352 return wrappedRequest.getLocales();
353 }
354
355 public boolean isSecure() {
356 return wrappedRequest.isSecure();
357 }
358
359 public RequestDispatcher getRequestDispatcher(String s) {
360 return wrappedRequest.getRequestDispatcher(s);
361 }
362
363
364
365
366 public String getRealPath(String s) {
367 return wrappedRequest.getRealPath(s);
368 }
369 }
370
371 }