Commit | Line | Data |
---|---|---|
c2da4d40 JL |
1 | //{{SERVLET_IMPORT_STMTS\r\r |
2 | package FAQ_Factory.TaggingDriver;\r\r | |
3 | import versata.vfc.html.*;\r\r | |
4 | import versata.vfc.html.servlet.*;\r\r | |
5 | import versata.vfc.html.common.*;\r\r | |
6 | import versata.common.*;\r\r | |
7 | import versata.vls.*;\r\r | |
8 | import java.util.*;\r\r | |
9 | import java.math.BigDecimal;\r\r | |
10 | import java.io.*;\r\r | |
11 | import javax.servlet.*;\r\r | |
12 | import javax.servlet.http.*;\r\r | |
13 | import java.rmi.RemoteException;\r\r | |
14 | \r\r | |
15 | //END_SERVLET_IMPORT_STMTS}}\r\r | |
16 | import support.*;\r | |
17 | \r\r | |
18 | //{{SERVLET_CLASS_DECL\r\r | |
19 | public class TaggingDriverServlet extends TaggingDriverBaseServlet\r\r | |
20 | //END_SERVLET_CLASS_DECL}}\r\r | |
21 | {\r\r | |
22 | JustAskConstants constants = new JustAskConstants();\r | |
23 | //private static final String[] legalImageExtensions = \r | |
24 | // new String[] { ".bmp", ".emf", ".gif", ".jpg", ".pcx", ".psp", ".tif", ".tiff", ".wmf", ".wpg" };\r | |
25 | //private static final String[] legalFileExtensions = \r | |
26 | // new String[] { ".doc", ".htm", ".pdf", ".txt" };\r | |
27 | // TaggingDriverServlet Class Constructor\r\r | |
28 | //{{SERVLET_CLASS_CTOR\r\r | |
29 | public TaggingDriverServlet ()\r\r | |
30 | //END_SERVLET_CLASS_CTOR}}\r\r | |
31 | {\r\r | |
32 | super();\r\r | |
33 | }\r\r | |
34 | \r\r | |
35 | public void loadLoginPage(boolean failed, HttpServletResponse res, HttpServletRequest req, String msg)\r\r | |
36 | throws IOException \r\r | |
37 | {\r\r | |
38 | //{{SERVLET_LOADLOGINPAGE\r\r | |
39 | // Load login page. If previous attempt failed, load page with failed message.\r\r | |
40 | // This method will be generated in gen'd servlet and user can\r\r | |
41 | // add code for custom login there, or it can be gen'd to supress login\r\r | |
42 | super.loadLoginPage(failed, res, req,msg);\r\r | |
43 | PrintWriter toClient = res.getWriter();\r\r | |
44 | String params = req.getQueryString();\r\r | |
45 | \r\r | |
46 | //END_SERVLET_LOADLOGINPAGE}}\r\r | |
47 | }\r\r | |
48 | \r\r | |
49 | \r\r | |
50 | public String getFileLocation()\r\r | |
51 | {\r\r | |
52 | return getDefaultFileLocation();\r\r | |
53 | \r\r | |
54 | }\r\r | |
55 | \r\r | |
56 | public String getBaseURL(HttpServletRequest req, String packageName, String clientAppName)\r\r | |
57 | {\r\r | |
58 | return super.getBaseURL(req, packageName, clientAppName);\r\r | |
59 | }\r\r | |
60 | public String getServletURL(HttpServletRequest req)\r\r | |
61 | {\r\r | |
62 | return HttpUtils.getRequestURL(req).toString();\r\r | |
63 | \r\r | |
64 | }\r\r | |
65 | \r\r | |
66 | public PLSORBSession doLogin(HttpServletRequest req)\r\r | |
67 | throws TierSessionLimitException, RemoteException\r\r | |
68 | {\r\r | |
69 | return doDefaultLogin(req);\r\r | |
70 | }\r\r | |
71 | \r\r | |
72 | public void loadStartPage()\r\r | |
73 | {\r\r | |
74 | // Right now, this is done by calling session.start, which calls app.start.\r\r | |
75 | // We may want this here for the case when developer wants to customize.\r\r | |
76 | }\r | |
77 | \r | |
78 | ///////////////////////////////////////////////////////////////////\r\r | |
79 | // This method handles the POST'ing of form data.\r\r | |
80 | // This is for the purpose of allowing users to have INPUT controls\r\r | |
81 | // of type FILE. When a form contains such a control, the data must be\r\r | |
82 | // posted in multipart format, which Jade does not handle by default.\r\r | |
83 | // The strategy is to catch the POST. If it is a regular post, just pass\r\r | |
84 | // it to super. \r\r | |
85 | // If it is a multipart post, we parse the binary stream and build a new\r\r | |
86 | // HttpServletRequest containing all the parameters except of course\r\r | |
87 | // the contents of the files, and submit that request to super.\r\r | |
88 | //\r\r | |
89 | // Remember that this method is invoked for every post, i.e. anytime the\r\r | |
90 | // user clicks any button. Since we typically want special action only\r\r | |
91 | // for a save, we look at the query_string parameter to determine whether\r\r | |
92 | // the query is a save query. If it's not, we ignore all the file-related\r\r | |
93 | // fields.\r\r | |
94 | //\r\r | |
95 | // The form can have any number of FILE inputs. For each such input, this\r\r | |
96 | // will read the file into a temporary file. The name of that temporary file\r\r | |
97 | // will be put in a field whose is that of the FILE input with "_ServerFileName"\r\r | |
98 | // appended. The name of the file on the client will be put in a field whose\r\r | |
99 | // name is that of the FILE input with "_ClientFileName" appended.\r\r | |
100 | //\r\r | |
101 | // Note that there is no way (apparently) to set the value of a FILE input,\r\r | |
102 | // which is why the client-side file name is returned in a separate field.\r\r | |
103 | public void doPost(javax.servlet.http.HttpServletRequest req,\r\r | |
104 | javax.servlet.http.HttpServletResponse res)\r\r | |
105 | throws javax.servlet.ServletException,\r\r | |
106 | java.io.IOException\r\r | |
107 | {\r | |
108 | String contentType = req.getContentType();\r\r | |
109 | //String postedOrgID = "didntgetit";\r\r | |
110 | //boolean gotStartOfOrg = false;\r\r | |
111 | // If this is a regular POST, let super handle it\r\r | |
112 | if(contentType.startsWith("application/x-www-form-urlencoded"))\r\r | |
113 | {\r\r | |
114 | super.doPost(req, res);\r\r | |
115 | return;\r\r | |
116 | }\r\r | |
117 | // If it's not a regular post, it better be multipart\r\r | |
118 | if( ! contentType.startsWith("multipart/form-data"))\r\r | |
119 | {\r\r | |
120 | throw new ServletException("POST content type was neither "\r\r | |
121 | + "application/x-www-form-urlencoded nor "\r\r | |
122 | + "multipart/form-data, which is impossible");\r\r | |
123 | }\r\r | |
124 | // Figure out the directory where the images are placed\r | |
125 | String tmpDir = constants.getImageDir();\r | |
126 | // String tmpDir = "C:\\Program Files\\Vision\\Jade4.1\\JWS\\public_html\\FAQ_Factory\\FAQ\\images\\";\r\r | |
127 | System.err.println("**Temporary dir is : " + tmpDir);\r\r | |
128 | // First read the separator from the contentType, which looks\r\r | |
129 | // like "multipart/form-data; boundary=---------------------------7cf3065520250"\r\r | |
130 | // Be careful : the number of dashes is not exactly as shown in the\r\r | |
131 | // contentType, it actually has two more\r\r | |
132 | int sepIndex = contentType.lastIndexOf('=');\r\r | |
133 | if(sepIndex == -1)\r\r | |
134 | throw new ServletException("Multipart POST does not have " +\r\r | |
135 | "a valid contentType");\r\r | |
136 | String separator = contentType.substring(sepIndex + 1);\r\r | |
137 | if( ! separator.startsWith("----------"))\r\r | |
138 | throw new ServletException("Multipart POST contentType "\r\r | |
139 | + "does not have a valid separator");\r\r | |
140 | separator = "--" + separator;\r\r | |
141 | System.out.println("***Separator is : " + separator);\r\r | |
142 | // We store the name and value of all the parameters in this\r\r | |
143 | // hashtable, which is then given to the FileServletRequest.\r\r | |
144 | Hashtable reqHash = new Hashtable();\r\r | |
145 | // Read the total length of the binary stream, we'll use that\r\r | |
146 | // to make sure we're reading the right amount.\r\r | |
147 | // Note : this can be -1 in some cases, in which case\r\r | |
148 | // you just have to read 'til you can't read no more\r\r | |
149 | int contentLength = req.getContentLength();\r\r | |
150 | System.out.println("***getContentLength: " + contentLength);\r\r | |
151 | // Now read the parameters. Each parameter has the format :\r\r | |
152 | // Separator : -----123456\r\r | |
153 | // Parameter name\r\r | |
154 | // Blank line\r\r | |
155 | // Parameter value (can be multiple lines, ends with next separator)\r\r | |
156 | // The last parameter ends with a separator.\r\r | |
157 | System.out.println("***InputStream :");\r\r | |
158 | ServletInputStream inStr = req.getInputStream();\r\r | |
159 | byte buf[] = new byte[1000];\r\r | |
160 | int byteCount = 0;\r\r | |
161 | int numRead;\r\r | |
162 | String currentAttribName;\r\r | |
163 | boolean isSaveQuery = false;\r | |
164 | boolean isUnique = true;\r | |
165 | boolean isDocManage = false;\r\r | |
166 | // Read the first separator\r\r | |
167 | numRead = inStr.readLine(buf, 0, 1000);\r\r | |
168 | byteCount += numRead;\r\r | |
169 | String strLine = new String(buf, 0, numRead);\r\r | |
170 | if( ! strLine.startsWith(separator))\r\r | |
171 | {\r\r | |
172 | throw new ServletException("POST does not "\r\r | |
173 | + "start with separator line, byte " + byteCount);\r\r | |
174 | }\r\r | |
175 | // Now go over the parameters\r\r | |
176 | while (numRead > 0)\r\r | |
177 | {\r\r | |
178 | // Last one ? ContentLength could be -1\r\r | |
179 | if(byteCount == contentLength)\r\r | |
180 | break;\r\r | |
181 | // Read the raw parameter name\r\r | |
182 | numRead = inStr.readLine(buf, 0, 1000);\r\r | |
183 | if(numRead == -1)\r\r | |
184 | break;\r\r | |
185 | byteCount += numRead;\r\r | |
186 | strLine = new String(buf, 0, numRead);\r\r | |
187 | System.out.println("***Raw parameter name : " + strLine);\r\r | |
188 | // Figure out if the parameter is a FILE input. The name of\r\r | |
189 | // the parameter looks like :\r\r | |
190 | // Content-Disposition: form-data; name="txtT1DocName"\r\r | |
191 | // or in the case of a file parameter :\r\r | |
192 | // Content-Disposition: form-data; name="fileContents"; filename="c:\temp\foo.txt"\r\r | |
193 | boolean isFileAttrib = false;\r\r | |
194 | int semiColIndex = strLine.lastIndexOf(';');\r\r | |
195 | if(semiColIndex == -1)\r\r | |
196 | throw new ServletException("POST - bad parameter name");\r\r | |
197 | String localFileName = "";\r\r | |
198 | String newLocalFileName = "";\r | |
199 | \r\r | |
200 | if(strLine.substring(semiColIndex, semiColIndex + 6).compareTo("; file") == 0)\r\r | |
201 | {\r\r | |
202 | isFileAttrib = true;\r\r | |
203 | int filenameIndex = strLine.lastIndexOf("; filename=\"");\r\r | |
204 | //if(filenameIndex == -1)\r\r | |
205 | // throw new ServletException("POST : no local file name");\r\r | |
206 | localFileName = strLine.substring(filenameIndex + 12,\r\r | |
207 | strLine.length() - 3);\r\r | |
208 | System.out.println("**Param is a file, local file : " + localFileName);\r\r | |
209 | if(localFileName.length() == 0)\r\r | |
210 | {\r\r | |
211 | System.out.println("***There is no file name");\r\r | |
212 | // localFileName = "xyz";\r\r | |
213 | // isFileAttrib = false;\r | |
214 | // break;\r\r | |
215 | }\r\r | |
216 | // JGW\r\r | |
217 | // after we have the local file name. Just pull out the filename and append a rnd\r\r | |
218 | // value to the file\r\r | |
219 | int lastdash = 0;\r\r | |
220 | int lfl = localFileName.length();\r\r | |
221 | for (int ctr=1; ctr < (lfl - 3); ctr++)\r\r | |
222 | {\r\r | |
223 | if(localFileName.substring(ctr,ctr+1).compareTo("\\") == 0)\r\r | |
224 | {\r\r | |
225 | System.out.println("++++lastdash = "+ctr); \r\r | |
226 | lastdash = ctr;\r\r | |
227 | }\r\r | |
228 | System.out.println(ctr + " " +lfl);\r\r | |
229 | }\r | |
230 | int subval = 0;\r | |
231 | if(lastdash == 0)\r\r | |
232 | subval = localFileName.length();\r | |
233 | else\r | |
234 | subval = (localFileName.length() - lastdash) - 1;\r\r | |
235 | \r\r | |
236 | if(localFileName.length() > 0)\r\r | |
237 | {\r\r | |
238 | File fp = null; \r | |
239 | newLocalFileName = localFileName.substring(lfl - subval, lfl);\r\r | |
240 | // end JGW\r | |
241 | fp = new File(tmpDir + newLocalFileName);\r | |
242 | if(fp.exists() && !isDocManage)\r | |
243 | { \r | |
244 | res.setContentType("text/html");\r\r | |
245 | \r\r | |
246 | PrintWriter toClient = res.getWriter();\r\r | |
247 | \r\r | |
248 | String myJscript ="<h3><center><script language=Javascript>";\r\r | |
249 | myJscript = myJscript + " alert('This file already exists.');"; \r\r | |
250 | myJscript = myJscript + " </script>";\r\r | |
251 | myJscript = myJscript + " </center></h3>";\r\r | |
252 | \r\r | |
253 | toClient.println(myJscript);\r\r | |
254 | \r | |
255 | isUnique = false;\r | |
256 | }\r | |
257 | } \r\r | |
258 | } \r\r | |
259 | // Extract the name of the parameter from the line\r\r | |
260 | int openQuoteIndex = strLine.indexOf('=') + 2;\r\r | |
261 | if(openQuoteIndex == -1)\r\r | |
262 | throw new ServletException("POST : could not find "\r\r | |
263 | + "beginning of name of parameter");\r\r | |
264 | int closeQuoteIndex = strLine.indexOf('"', openQuoteIndex);\r\r | |
265 | if(closeQuoteIndex == -1)\r\r | |
266 | throw new ServletException("POST : could not find "\r\r | |
267 | + "end of name of parameter");\r\r | |
268 | currentAttribName = strLine.substring(openQuoteIndex, closeQuoteIndex);\r\r | |
269 | System.out.println("***New attrib name : " + currentAttribName);\r\r | |
270 | // Different behavior between Netscape and IE : IE will\r\r | |
271 | // still send a content type descriptor if there is no file,\r\r | |
272 | // but Netscape won't.\r\r | |
273 | // Our strategy to cope with this is, if the file name is empty,\r\r | |
274 | // we just skip over the contents of the file parameter\r\r | |
275 | if(isFileAttrib && (localFileName.length() == 0))\r\r | |
276 | {\r\r | |
277 | while (true)\r\r | |
278 | {\r\r | |
279 | // Ignore everything until the market\r\r | |
280 | byte buf2[] = new byte[1000];\r\r | |
281 | int numRead2 = inStr.readLine(buf2, 0, 1000);\r\r | |
282 | byteCount += numRead2;\r\r | |
283 | String attVal2 = new String(buf2, 0, numRead2);\r\r | |
284 | if(attVal2.startsWith(separator))\r\r | |
285 | break;\r\r | |
286 | }\r\r | |
287 | }\r\r | |
288 | // Read the value. If it's a file, there could be a\r\r | |
289 | // massive amount of data\r\r | |
290 | else if(isFileAttrib && (localFileName.length() > 0))\r\r | |
291 | {\r\r | |
292 | // Read the content type descriptor\r\r | |
293 | numRead = inStr.readLine(buf, 0, 1000);\r\r | |
294 | byteCount += numRead;\r\r | |
295 | strLine = new String(buf, 0, numRead);\r\r | |
296 | if( ! strLine.startsWith("Content-Type:"))\r\r | |
297 | {\r\r | |
298 | throw new ServletException("POST : expected content type "\r\r | |
299 | + "descriptor");\r\r | |
300 | }\r\r | |
301 | // Read the blank line\r\r | |
302 | numRead = inStr.readLine(buf, 0, 1000);\r\r | |
303 | byteCount += numRead;\r\r | |
304 | if(numRead > 2)\r\r | |
305 | {\r\r | |
306 | throw new ServletException("POST : unexpected data in "\r\r | |
307 | + "file parameter line (not blank)");\r\r | |
308 | }\r\r | |
309 | \r | |
310 | String fullFileName = tmpDir + newLocalFileName;\r\r | |
311 | System.out.println("**Reading file attribute into : " + fullFileName); \r\r | |
312 | BufferedOutputStream outFile = null;\r\r | |
313 | try {\r\r | |
314 | if(isSaveQuery && isUnique)\r\r | |
315 | {\r\r | |
316 | FileOutputStream outStr = new FileOutputStream(fullFileName);\r\r | |
317 | outFile = new BufferedOutputStream(outStr);\r\r | |
318 | }\r\r | |
319 | }\r\r | |
320 | catch (IOException e)\r\r | |
321 | {\r\r | |
322 | throw new ServletException("POST : error while opening data file : " + fullFileName);\r\r | |
323 | }\r\r | |
324 | numRead = inStr.readLine(buf, 0, 1000);\r\r | |
325 | while (true)\r\r | |
326 | {\r\r | |
327 | //System.out.println("**Reading file value");\r\r | |
328 | byteCount += numRead;\r\r | |
329 | if(numRead < 0)\r\r | |
330 | throw new ServletException("POST : data for file "\r\r | |
331 | + "was incomplete");\r\r | |
332 | byte buf2[] = new byte[1000];\r\r | |
333 | int numRead2 = inStr.readLine(buf2, 0, 1000);\r\r | |
334 | String attVal2 = new String(buf2, 0, numRead2);\r\r | |
335 | // If this is the last line (because it's followed\r\r | |
336 | // by a separator), trim off the last CR/LF\r\r | |
337 | if(attVal2.startsWith(separator))\r\r | |
338 | {\r\r | |
339 | if(isSaveQuery && isUnique)\r\r | |
340 | {\r\r | |
341 | outFile.write(buf, 0, numRead - 2);\r\r | |
342 | outFile.flush();\r\r | |
343 | }\r\r | |
344 | break;\r\r | |
345 | }\r\r | |
346 | else\r\r | |
347 | {\r\r | |
348 | if(isSaveQuery && isUnique)\r\r | |
349 | {\r\r | |
350 | outFile.write(buf, 0, numRead);\r\r | |
351 | outFile.flush();\r\r | |
352 | }\r\r | |
353 | }\r\r | |
354 | buf = buf2; // Thank goodness for garbage collection !\r\r | |
355 | numRead = numRead2;\r\r | |
356 | }\r\r | |
357 | // Save the name of the file into the parameter\r\r | |
358 | if(isSaveQuery)\r\r | |
359 | {\r\r | |
360 | // The value doesn't make it back into the file input\r\r | |
361 | reqHash.put(currentAttribName, localFileName);\r\r | |
362 | reqHash.put(currentAttribName + "_ClientFileName", localFileName);\r\r | |
363 | reqHash.put(currentAttribName + "_ServerFileName", fullFileName);\r\r | |
364 | }\r\r | |
365 | }\r\r | |
366 | else // Not a file : just a regular parameter\r\r | |
367 | {\r\r | |
368 | System.out.println("***Reading value of parameter");\r\r | |
369 | // Read the blank line\r\r | |
370 | numRead = inStr.readLine(buf, 0, 1000);\r\r | |
371 | byteCount += numRead;\r\r | |
372 | String s = new String(buf, 0, numRead);\r\r | |
373 | System.out.println("**First param value line : " + s);\r\r | |
374 | if(numRead > 2)\r\r | |
375 | {\r\r | |
376 | throw new ServletException("POST : unexpected data in "\r\r | |
377 | + "parameter line (not blank)");\r\r | |
378 | }\r\r | |
379 | numRead = inStr.readLine(buf, 0, 1000);\r\r | |
380 | String attVal = "";\r\r | |
381 | // Is this a Save query ? Remember that for processing of files\r\r | |
382 | String tmpVal = new String(buf, 0, numRead);\r\r | |
383 | if(tmpVal.indexOf("UploadFile=") != -1)\r\r | |
384 | {\r\r | |
385 | System.out.println("***********************************This is a save query");\r\r | |
386 | isSaveQuery = true;\r\r | |
387 | }\r\r | |
388 | while (true)\r\r | |
389 | {\r\r | |
390 | System.out.println("**Reading parameter value");\r\r | |
391 | byteCount += numRead;\r\r | |
392 | if(numRead < 0)\r\r | |
393 | throw new ServletException("POST : data for parameter "\r\r | |
394 | + "was incomplete");\r\r | |
395 | byte buf2[] = new byte[1000];\r\r | |
396 | int numRead2 = inStr.readLine(buf2, 0, 1000);\r\r | |
397 | String attVal2 = new String(buf2, 0, numRead2);\r\r | |
398 | // If this is the last line (because it's followed\r\r | |
399 | // by a separator), trim off the last CR/LF\r\r | |
400 | if(attVal2.startsWith(separator))\r\r | |
401 | {\r\r | |
402 | attVal += new String(buf, 0, numRead - 2);\r\r | |
403 | break;\r\r | |
404 | }\r\r | |
405 | else\r\r | |
406 | {\r\r | |
407 | attVal += new String(buf, 0, numRead);\r\r | |
408 | }\r\r | |
409 | buf = buf2; // Thank goodness for garbage collection !\r\r | |
410 | numRead = numRead2;\r\r | |
411 | }\r\r | |
412 | // Skip the pseudo-fields that are filled with the\r\r | |
413 | // names of the file\r\r | |
414 | if(isSaveQuery)\r\r | |
415 | {\r\r | |
416 | if( ! ((currentAttribName.endsWith("_ClientFileName")) ||\r\r | |
417 | (currentAttribName.endsWith("_ServerFileName"))))\r\r | |
418 | {\r\r | |
419 | reqHash.put(currentAttribName, attVal);\r\r | |
420 | }\r\r | |
421 | }\r\r | |
422 | else\r | |
423 | {\r\r | |
424 | reqHash.put(currentAttribName, attVal);\r | |
425 | }\r\r | |
426 | System.out.println("**New attrib val : " + attVal);\r | |
427 | if(attVal.length() > 5 && attVal.substring(0,6).equals("sysdir"))\r | |
428 | {\r | |
429 | int endIndex = attVal.indexOf('&');\r | |
430 | System.out.println("sysdir " + attVal.substring(7,endIndex) + "\\");\r | |
431 | tmpDir = tmpDir + attVal.substring(7,endIndex) + "\\";\r | |
432 | \r | |
433 | if(attVal.substring(7,endIndex).indexOf("/") != -1)\r | |
434 | isDocManage = true;\r | |
435 | System.out.println("dir " + tmpDir);\r | |
436 | System.out.println("doc management " + isDocManage);\r | |
437 | File fp = new File(tmpDir);\r | |
438 | if(!fp.exists())\r | |
439 | {\r | |
440 | System.out.println("doesn't exist");\r | |
441 | fp.mkdirs();\r | |
442 | System.out.println("created");\r | |
443 | }\r | |
444 | } \r\r | |
445 | }\r\r | |
446 | }\r | |
447 | if(!isUnique)\r\r | |
448 | {\r\r | |
449 | reqHash.put("txtT1ImageUpload", "");\r\r | |
450 | }\r\r | |
451 | System.out.println("***End of InputStream : bytes " + byteCount);\r\r | |
452 | System.out.println("***req " + req.toString());\r | |
453 | System.out.println("***hash " + reqHash.toString());\r\r | |
454 | FileServletRequest newRequest = new FileServletRequest(req, reqHash);\r\r | |
455 | super.doPost(newRequest, res);\r\r | |
456 | }\r | |
457 | \r | |
458 | public void doGet(javax.servlet.http.HttpServletRequest req,\r\r | |
459 | javax.servlet.http.HttpServletResponse res)\r | |
460 | throws javax.servlet.ServletException,\r\r | |
461 | java.io.IOException\r\r | |
462 | {\r | |
463 | if(req.getParameter("GetImages") != null &&\r | |
464 | req.getParameter("ImageExtensions") != null)\r | |
465 | {\r | |
466 | // Get the image extensions from the request\r | |
467 | Vector legalImageExtensions = new Vector();\r | |
468 | StringTokenizer tok = new StringTokenizer(req.getParameter("ImageExtensions"), ":");\r\r | |
469 | while (tok.hasMoreTokens())\r | |
470 | {\r | |
471 | legalImageExtensions.addElement(tok.nextToken());\r | |
472 | } \r | |
473 | \r\r | |
474 | // Return the directory listing\r | |
475 | String currentSystem = req.getParameter("GetImages");\r | |
476 | ObjectOutputStream out = new ObjectOutputStream(res.getOutputStream());\r | |
477 | File dir = new File(constants.getImageDir() + currentSystem);\r | |
478 | // For some reason implementing a FileNameFilter throws an IO exception\r | |
479 | // even if it only returns true. So we implement one ourselves.\r\r | |
480 | String[] validImageFiles = getValidImageFiles(dir.list(), legalImageExtensions);\r | |
481 | if(validImageFiles != null)\r | |
482 | {\r\r | |
483 | out.writeObject(validImageFiles);\r | |
484 | }\r\r | |
485 | out.flush();\r\r | |
486 | out.close();\r\r | |
487 | }\r | |
488 | else if(req.getParameter("GetFiles") != null &&\r | |
489 | req.getParameter("FileExtensions") != null)\r | |
490 | {\r | |
491 | // Get the file extensions from the request\r | |
492 | Vector legalFileExtensions = new Vector();\r | |
493 | StringTokenizer tok = new StringTokenizer(req.getParameter("FileExtensions"), ":");\r\r | |
494 | while (tok.hasMoreTokens())\r | |
495 | {\r | |
496 | legalFileExtensions.addElement(tok.nextToken());\r | |
497 | }\r | |
498 | \r | |
499 | // Return the directory listing\r | |
500 | String currentSystem = req.getParameter("GetFiles");\r | |
501 | ObjectOutputStream out = new ObjectOutputStream(res.getOutputStream());\r | |
502 | File dir = new File(constants.getImageDir() + currentSystem);\r | |
503 | // For some reason implementing a FileNameFilter throws an IO exception\r | |
504 | // even if it only returns true. So we implement one ourselves.\r\r | |
505 | String[] validFiles = getValidFiles(dir.list(), legalFileExtensions);\r | |
506 | if(validFiles != null)\r | |
507 | {\r\r | |
508 | out.writeObject(validFiles);\r | |
509 | }\r\r | |
510 | out.flush();\r\r | |
511 | out.close();\r\r | |
512 | }\r | |
513 | else\r | |
514 | {\r | |
515 | super.doGet(req, res);\r | |
516 | }\r | |
517 | }\r | |
518 | \r | |
519 | private String[] getValidImageFiles(String[] files, Vector legalImageExtensions)\r | |
520 | {\r | |
521 | \r | |
522 | Vector validImageVector = new Vector();\r | |
523 | for ( int fileIndex=0; fileIndex < files.length; fileIndex++)\r | |
524 | {\r | |
525 | for ( int i=0; i < legalImageExtensions.size(); i++)\r | |
526 | {\r | |
527 | if(files[fileIndex].endsWith((String)legalImageExtensions.elementAt(i)))\r | |
528 | {\r | |
529 | validImageVector.addElement(files[fileIndex]);\r | |
530 | break;\r | |
531 | }\r | |
532 | }\r | |
533 | }\r | |
534 | if(validImageVector.isEmpty())\r | |
535 | {\r | |
536 | return null;\r | |
537 | }\r | |
538 | else\r | |
539 | {\r | |
540 | String[] result = new String[validImageVector.size()];\r | |
541 | int i2 = 0;\r | |
542 | Enumeration theFiles = validImageVector.elements();\r | |
543 | while (theFiles.hasMoreElements())\r | |
544 | {\r | |
545 | result[i2++] = ((String)theFiles.nextElement());\r | |
546 | }\r | |
547 | return result;\r | |
548 | }\r | |
549 | }\r\r | |
550 | \r\r | |
551 | private String[] getValidFiles(String[] files, Vector legalFileExtensions)\r | |
552 | {\r | |
553 | \r | |
554 | Vector validFileVector = new Vector();\r | |
555 | for ( int fileIndex=0; fileIndex < files.length; fileIndex++)\r | |
556 | {\r | |
557 | for ( int i=0; i < legalFileExtensions.size(); i++)\r | |
558 | {\r | |
559 | if(files[fileIndex].endsWith((String)legalFileExtensions.elementAt(i)))\r | |
560 | {\r | |
561 | validFileVector.addElement(files[fileIndex]);\r | |
562 | break;\r | |
563 | }\r | |
564 | }\r | |
565 | }\r | |
566 | if(validFileVector.isEmpty())\r | |
567 | {\r | |
568 | return null;\r | |
569 | }\r | |
570 | else\r | |
571 | {\r | |
572 | String[] result = new String[validFileVector.size()];\r | |
573 | int i2 = 0;\r | |
574 | Enumeration theFiles = validFileVector.elements();\r | |
575 | while (theFiles.hasMoreElements())\r | |
576 | {\r | |
577 | result[i2++] = ((String)theFiles.nextElement());\r | |
578 | }\r | |
579 | return result;\r | |
580 | }\r | |
581 | }\r\r | |
582 | \r\r | |
583 | \r\r | |
584 | }\r | |
585 | \r |