1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| package org.w3c.tidy.servlet; |
56 |
| |
57 |
| import java.io.IOException; |
58 |
| import java.io.InputStream; |
59 |
| import java.io.OutputStream; |
60 |
| import java.io.PrintWriter; |
61 |
| |
62 |
| import javax.servlet.ServletConfig; |
63 |
| import javax.servlet.ServletException; |
64 |
| import javax.servlet.http.HttpServlet; |
65 |
| import javax.servlet.http.HttpServletRequest; |
66 |
| import javax.servlet.http.HttpServletResponse; |
67 |
| |
68 |
| import org.apache.commons.logging.Log; |
69 |
| import org.apache.commons.logging.LogFactory; |
70 |
| import org.w3c.tidy.servlet.properties.JTidyServletProperties; |
71 |
| import org.w3c.tidy.servlet.reports.Report; |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
| public class TidyServlet extends HttpServlet |
80 |
| { |
81 |
| |
82 |
| |
83 |
| |
84 |
| |
85 |
| private static final long serialVersionUID = 29137L; |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| public static final String CONFIG_PROPERTIES_FILE_NAME = Consts.CONFIG_PROPERTIES_FILE_NAME; |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| public static final String PARAM_REQUEST_ID = "requestID"; |
96 |
| |
97 |
| public static final String PARAM_ACTION = "action"; |
98 |
| |
99 |
| public static final String ACTION_IMAGE = "image"; |
100 |
| |
101 |
| public static final String ACTION_IMAGE_PARAM_SRC_ONLY = "srcOnly"; |
102 |
| |
103 |
| public static final String ACTION_VIEW = "view"; |
104 |
| |
105 |
| public static final String ACTION_REPORT = "report"; |
106 |
| |
107 |
| public static final String ACTION_REPORT_PARAM_SRC_ORG = "src"; |
108 |
| |
109 |
| public static final String ACTION_REPORT_PARAM_SRC_RESULT = "result"; |
110 |
| |
111 |
| private static final String RESOURCE_PREFIX = ""; |
112 |
| |
113 |
| |
114 |
| |
115 |
| |
116 |
| private Log log = LogFactory.getLog(TidyServlet.class); |
117 |
| |
118 |
| private JTidyServletProperties properties = JTidyServletProperties.getInstance(); |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
16
| public final void init(ServletConfig servletConfig) throws ServletException
|
127 |
| { |
128 |
| |
129 |
16
| super.init(servletConfig);
|
130 |
| |
131 |
| |
132 |
16
| properties.loadFile(getInitParameter(CONFIG_PROPERTIES_FILE_NAME));
|
133 |
| } |
134 |
| |
135 |
28
| public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
|
136 |
| { |
137 |
28
| selectAction(request, response);
|
138 |
| } |
139 |
| |
140 |
0
| public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
|
141 |
| { |
142 |
0
| selectAction(request, response);
|
143 |
| } |
144 |
| |
145 |
28
| void selectAction(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
|
146 |
| { |
147 |
28
| String action = request.getParameter(PARAM_ACTION);
|
148 |
28
| String id = request.getParameter(PARAM_REQUEST_ID);
|
149 |
28
| if (action == null)
|
150 |
| { |
151 |
16
| return;
|
152 |
| } |
153 |
| |
154 |
12
| if (action.equalsIgnoreCase(ACTION_IMAGE))
|
155 |
| { |
156 |
7
| redirect2Image(request, response, id);
|
157 |
| } |
158 |
5
| else if (action.equalsIgnoreCase(ACTION_REPORT))
|
159 |
| { |
160 |
5
| printReport(request, response, id, false);
|
161 |
| } |
162 |
0
| else if (action.equalsIgnoreCase(ACTION_VIEW))
|
163 |
| { |
164 |
0
| printReport(request, response, id, true);
|
165 |
| } |
166 |
| } |
167 |
| |
168 |
7
| void redirect2Image(HttpServletRequest request, HttpServletResponse response, String id) throws IOException,
|
169 |
| ServletException |
170 |
| { |
171 |
| |
172 |
7
| String imageNamePrefix = properties.getProperty(
|
173 |
| JTidyServletProperties.PROPERTY_STRING_IMAGENAMEPREFIX, |
174 |
| Consts.DEFAULT_IMAGE_NAME_PREFIX); |
175 |
7
| String imageNameExtension = properties.getProperty(
|
176 |
| JTidyServletProperties.PROPERTY_STRING_IMAGENAMEEXTENSION, |
177 |
| ".png"); |
178 |
| |
179 |
7
| ResponseRecordRepository rrr = JTidyServletProperties.getInstance().getRepositoryInstance(request.getSession());
|
180 |
7
| if (rrr == null)
|
181 |
| { |
182 |
0
| log.info("No ResponseRecordRepository");
|
183 |
| |
184 |
0
| response.setContentType("image/png");
|
185 |
0
| response.setContentLength(0);
|
186 |
0
| return;
|
187 |
| } |
188 |
7
| Object key = rrr.getResponseID(id);
|
189 |
| |
190 |
7
| ResponseRecord record = rrr.getRecord(key, properties.getIntProperty(
|
191 |
| JTidyServletProperties.PROPERTY_INT_IMAGEGETTIMEOUT, |
192 |
| 2000)); |
193 |
| |
194 |
7
| String imageName = "unknown";
|
195 |
7
| if (record != null)
|
196 |
| { |
197 |
7
| imageName = record.getImageName();
|
198 |
| } |
199 |
| else |
200 |
| { |
201 |
0
| log.debug("ResponseRecord not found for ID " + id);
|
202 |
| } |
203 |
| |
204 |
7
| StringBuffer imageURL = new StringBuffer(40);
|
205 |
7
| imageURL.append(imageNamePrefix).append(imageName).append(imageNameExtension);
|
206 |
7
| if (log.isDebugEnabled())
|
207 |
| { |
208 |
0
| log.debug("ResultsDO for ID " + id + " -> " + imageURL);
|
209 |
| } |
210 |
| |
211 |
7
| String src = request.getParameter(ACTION_IMAGE_PARAM_SRC_ONLY);
|
212 |
7
| if (src != null)
|
213 |
| { |
214 |
3
| PrintWriter out = response.getWriter();
|
215 |
3
| out.print(imageURL);
|
216 |
| } |
217 |
| else |
218 |
| { |
219 |
4
| if (!streamResource(imageURL.toString(), response))
|
220 |
| { |
221 |
0
| getServletContext().getRequestDispatcher(imageURL.toString()).forward(request, response);
|
222 |
| } |
223 |
| } |
224 |
| } |
225 |
| |
226 |
4
| private boolean streamResource(String resource, HttpServletResponse response) throws IOException
|
227 |
| { |
228 |
4
| InputStream in = this.getClass().getClassLoader().getResourceAsStream(RESOURCE_PREFIX + resource);
|
229 |
| |
230 |
4
| if (in == null)
|
231 |
| { |
232 |
| |
233 |
0
| in = getServletContext().getResourceAsStream("/" + RESOURCE_PREFIX + resource);
|
234 |
| } |
235 |
| |
236 |
4
| if (in == null)
|
237 |
| { |
238 |
0
| log.warn("resource not found:" + resource);
|
239 |
0
| return false;
|
240 |
| } |
241 |
4
| if (resource.endsWith(".png"))
|
242 |
| { |
243 |
4
| response.setContentType("image/png");
|
244 |
| } |
245 |
0
| else if (resource.endsWith(".gif"))
|
246 |
| { |
247 |
0
| response.setContentType("image/gif");
|
248 |
| } |
249 |
| |
250 |
0
| else if (resource.endsWith(".jpg") || resource.endsWith(".jpeg"))
|
251 |
| { |
252 |
0
| response.setContentType("image/jpeg");
|
253 |
| } |
254 |
| else |
255 |
| { |
256 |
0
| in.close();
|
257 |
0
| return false;
|
258 |
| } |
259 |
| |
260 |
4
| final int BUFFER_SIZE = 2048;
|
261 |
4
| byte[] buffer = new byte[BUFFER_SIZE];
|
262 |
4
| int r = 0;
|
263 |
4
| OutputStream out = response.getOutputStream();
|
264 |
| |
265 |
?
| while ((r = in.read(buffer)) != -1)
|
266 |
| { |
267 |
4
| out.write(buffer, 0, r);
|
268 |
| } |
269 |
4
| out.flush();
|
270 |
4
| in.close();
|
271 |
4
| return true;
|
272 |
| } |
273 |
| |
274 |
5
| void printReport(HttpServletRequest request, HttpServletResponse response, String id, boolean view)
|
275 |
| throws IOException |
276 |
| { |
277 |
5
| response.setContentType("text/html");
|
278 |
| |
279 |
5
| Report report = new Report(request.getSession());
|
280 |
| |
281 |
5
| report.setCompletePage(true);
|
282 |
| |
283 |
5
| report.setView(view);
|
284 |
| |
285 |
5
| if (request.getParameter(ACTION_REPORT_PARAM_SRC_ORG) != null)
|
286 |
| { |
287 |
5
| report.setPrintSource(true);
|
288 |
| } |
289 |
5
| if (request.getParameter(ACTION_REPORT_PARAM_SRC_RESULT) != null)
|
290 |
| { |
291 |
0
| report.setPrintHtmlResult(true);
|
292 |
| } |
293 |
| |
294 |
5
| report.print(response.getWriter(), id);
|
295 |
| } |
296 |
| } |