KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > session > MultipartRequest


1 /*
2  * $Id: MultipartRequest.java,v 1.6 2005/01/28 14:47:51 blueshift Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.session;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.wings.UploadFilterManager;
19 import org.wings.util.LocaleCharSet;
20
21 import javax.servlet.ServletInputStream JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
24 import java.io.*;
25 import java.net.URLEncoder JavaDoc;
26 import java.util.*;
27
28 /**
29  * A utility class to handle <tt>multipart/form-data</tt> requests,
30  * the kind of requests that support file uploads. This class can
31  * receive arbitrarily large files (up to an artificial limit you can set),
32  * and fairly efficiently too. And it knows and works around several browser
33  * bugs that don't know how to upload files correctly.
34  * <p/>
35  * A client can upload files using an HTML form with the following structure.
36  * Note that not all browsers support file uploads.
37  * <blockquote><pre>
38  * &lt;FORM ACTION="/servlet/Handler" METHOD=POST
39  * ENCTYPE="multipart/form-data"&gt;
40  * What is your name? &lt;INPUT TYPE=TEXT NAME=submitter&gt; &lt;BR&gt;
41  * Which file to upload? &lt;INPUT TYPE=FILE NAME=file&gt; &lt;BR&gt;
42  * &lt;INPUT TYPE=SUBMIT&GT;
43  * &lt;/FORM&gt;
44  * </pre></blockquote>
45  * <p/>
46  * The full file upload specification is contained in experimental RFC 1867,
47  * available at <a HREF="http://ds.internic.net/rfc/rfc1867.txt">
48  * http://ds.internic.net/rfc/rfc1867.txt</a>.
49  *
50  * @author <a HREF="mailto:hengels@mercatis.de">Holger Engels</a>
51  * @version $Revision: 1.6 $
52  */

53 public class MultipartRequest
54         extends HttpServletRequestWrapper JavaDoc {
55     private final transient static Log log = LogFactory.getLog(MultipartRequest.class);
56
57     private static final int DEFAULT_MAX_POST_SIZE = 1024 * 1024; // 1 Meg
58

59     private int maxSize;
60     private boolean urlencodedRequest;
61
62     private final HashMap parameters = new HashMap(); // name - value
63
private final HashMap files = new HashMap(); // name - UploadedFile
64
private HashMap map;
65
66     /**
67      * @param request the servlet request
68      * @throws IOException if the uploaded content is larger than 1 Megabyte
69      * or there's a problem reading or parsing the request
70      */

71     public MultipartRequest(HttpServletRequest JavaDoc request) throws IOException {
72         this(request, DEFAULT_MAX_POST_SIZE);
73     }
74
75     /**
76      * @param request the servlet request
77      * @param maxPostSize the maximum size of the POST content
78      * @throws IOException if the uploaded content is larger than
79      * <tt>maxPostSize</tt> or there's a problem reading or parsing the request
80      */

81     public MultipartRequest(HttpServletRequest JavaDoc request,
82                             int maxPostSize) throws IOException {
83         super(request);
84
85         if (request == null)
86             throw new IllegalArgumentException JavaDoc("request cannot be null");
87         if (maxPostSize <= 0)
88             throw new IllegalArgumentException JavaDoc("maxPostSize must be positive");
89
90         maxSize = maxPostSize;
91
92         processRequest(request);
93     }
94
95     /**
96      * Returns the names of all the parameters as an Enumeration of
97      * Strings. It returns an empty Enumeration if there are no parameters.
98      *
99      * @return the names of all the parameters as an Enumeration of Strings
100      */

101     public Enumeration getParameterNames() {
102         if (urlencodedRequest) return super.getParameterNames();
103
104         final Iterator iter = parameters.keySet().iterator();
105         return new Enumeration() {
106             public boolean hasMoreElements() {
107                 return iter.hasNext();
108             }
109
110             public Object JavaDoc nextElement() {
111                 return iter.next();
112             }
113
114         };
115     }
116
117     /**
118      * Returns the names of all the uploaded files as an Enumeration of
119      * Strings. It returns an empty Enumeration if there are no uploaded
120      * files. Each file name is the name specified by the form, not by
121      * the user.
122      *
123      * @return the names of all the uploaded files as an Enumeration of Strings
124      */

125     public Iterator getFileNames() {
126         return files.keySet().iterator();
127     }
128
129     public String JavaDoc[] getParameterValues(String JavaDoc name) {
130         if (urlencodedRequest)
131             return super.getParameterValues(name);
132         ArrayList v = (ArrayList) parameters.get(name);
133         if (v == null) return null;
134         String JavaDoc result[] = new String JavaDoc[v.size()];
135         return (String JavaDoc[]) v.toArray(result);
136     }
137
138     public Map getParameterMap() {
139         if (urlencodedRequest)
140             return super.getParameterMap();
141         if (map == null) {
142             map = new HashMap();
143             for (Iterator iterator = parameters.entrySet().iterator(); iterator.hasNext();) {
144                 Map.Entry entry = (Map.Entry) iterator.next();
145                 List list = (List) entry.getValue();
146                 String JavaDoc[] values = (String JavaDoc[]) list.toArray(new String JavaDoc[list.size()]);
147                 map.put(entry.getKey(), values);
148             }
149         }
150         return map;
151     }
152
153     /**
154      * Returns the filename of the specified file, or null if the
155      * file was not included in the upload. The filename is the name
156      * specified by the user. It is not the name under which the file is
157      * actually saved.
158      *
159      * @param name the file name
160      * @return the filesystem name of the file
161      */

162     public String JavaDoc getFileName(String JavaDoc name) {
163         try {
164             UploadedFile file = (UploadedFile) files.get(name);
165             return file.getFileName(); // may be null
166
} catch (Exception JavaDoc e) {
167             return null;
168         }
169     }
170
171     /**
172      * Returns the fileid of the specified file, or null if the
173      * file was not included in the upload. The fileid is the name
174      * under which the file is saved in the filesystem.
175      *
176      * @param name the file name
177      * @return the filesystem name of the file
178      */

179     public String JavaDoc getFileId(String JavaDoc name) {
180         try {
181             UploadedFile file = (UploadedFile) files.get(name);
182             return file.getId(); // may be null
183
} catch (Exception JavaDoc e) {
184             return null;
185         }
186     }
187
188     /**
189      * Returns the content type of the specified file (as supplied by the
190      * client browser), or null if the file was not included in the upload.
191      *
192      * @param name the file name
193      * @return the content type of the file
194      */

195     public String JavaDoc getContentType(String JavaDoc name) {
196         try {
197             UploadedFile file = (UploadedFile) files.get(name);
198             return file.getContentType(); // may be null
199
} catch (Exception JavaDoc e) {
200             return null;
201         }
202     }
203
204     /**
205      * Returns a File object for the specified file saved on the server's
206      * filesystem, or null if the file was not included in the upload.
207      *
208      * @param name the file name
209      * @return a File object for the named file
210      */

211     public File getFile(String JavaDoc name) {
212         try {
213             UploadedFile file = (UploadedFile) files.get(name);
214             return file.getFile(); // may be null
215
} catch (Exception JavaDoc e) {
216             return null;
217         }
218     }
219
220     /**
221      * Indicates if this class was successfully able to parse request as multipart request.
222      */

223     public final boolean isMultipart() {
224         return !urlencodedRequest;
225     }
226
227     /**
228      * Store exception as request parameter.
229      */

230     protected void setException(String JavaDoc param, Exception JavaDoc ex) {
231         parameters.clear();
232         files.clear();
233
234         putParameter(param, "exception");
235         putParameter(param, ex.getMessage());
236     }
237
238     /**
239      * Parses passed request and stores contained parameters.
240      *
241      * @throws IOException On unrecoverable parsing bugs due to old Tomcat version.
242      */

243     protected void processRequest(HttpServletRequest JavaDoc req)
244             throws IOException {
245         String JavaDoc type = req.getContentType();
246         if (type == null || !type.toLowerCase().startsWith("multipart/form-data")) {
247             urlencodedRequest = true;
248             return;
249         }
250         urlencodedRequest = false;
251
252         for (Iterator iterator = req.getParameterMap().entrySet().iterator(); iterator.hasNext();) {
253             Map.Entry entry = (Map.Entry) iterator.next();
254             parameters.put(entry.getKey(), new ArrayList(Arrays.asList((String JavaDoc[]) entry.getValue())));
255         }
256
257         String JavaDoc boundaryToken = extractBoundaryToken(type);
258         if (boundaryToken == null) {
259             /*
260              * this could happen due to a bug in Tomcat 3.2.2 in combination
261              * with Opera.
262              * Opera sends the boundary on a separate line, which is perfectly
263              * correct regarding the way header may be constructed
264              * (multiline headers). Alas, Tomcat fails to read the header in
265              * the content type line and thus we cannot read it.. haven't
266              * checked later versions of Tomcat, but upgrading is
267              * definitly needed, since we cannot do anything about it here.
268              * (JServ works fine, BTW.) (Henner)
269              */

270             throw new IOException("Separation boundary was not specified (BUG in Tomcat 3.* with Opera?)");
271         }
272
273         MultipartInputStream mimeStream = null;
274
275
276         StringBuffer JavaDoc header = new StringBuffer JavaDoc();
277         StringBuffer JavaDoc content = new StringBuffer JavaDoc();
278         HashMap headers = null;
279         int currentByte = 0;
280         int currentPos = 0;
281         int currentTransformByte = 0;
282         String JavaDoc currentParam = null;
283         File uploadFile = null;
284         OutputStream fileStream = null;
285         boolean done;
286         int last = -1;
287
288         try {
289             mimeStream = new MultipartInputStream(req.getInputStream(), req.getContentLength(), maxSize);
290             while (currentByte != -1) {
291                 // Read MIME part header line
292
done = false;
293                 while ((currentByte = mimeStream.read()) != -1 && !done) {
294                     header.append((char) currentByte); // okay -- let us asume no special characters in the header
295
done = (last == '\n' && currentByte == '\r');
296                     last = currentByte;
297                 }
298                 if (currentByte == -1)
299                     break;
300
301                 headers = parseHeader(header.toString());
302                 header.setLength(0);
303
304                 currentParam = (String JavaDoc) headers.get("name");
305
306                 if (headers.size() == 1) { // .. it's not a file
307
byte[] bytes = new byte[req.getContentLength()];
308                     currentPos = 0;
309                     while ((currentByte = mimeStream.read()) != -1) {
310                         bytes[currentPos] = (byte) currentByte;
311                         currentPos++;
312                         if (currentPos >= boundaryToken.length()) {
313                             int i;
314                             for (i = 0; i < boundaryToken.length(); i++) {
315                                 if (boundaryToken.charAt(boundaryToken.length() - i - 1) != bytes[currentPos - i - 1]) {
316                                     i = 0;
317                                     break;
318                                 }
319                             }
320                             if (i == boundaryToken.length()) { // end of part ..
321
ByteArrayInputStream bais = new ByteArrayInputStream(bytes, 0, currentPos - boundaryToken.length() - 4);
322                                 InputStreamReader ir;
323                                 if (req.getCharacterEncoding() != null)
324                                 // It's common behaviour of browsers to encode their form input in the character
325
// encoding of the page, though they don't declare the used characterset explicetly
326
// for backward compatibility.
327
ir = new InputStreamReader(bais, req.getCharacterEncoding());
328                                 else
329                                     ir = new InputStreamReader(bais);
330                                 content.setLength(0);
331                                 while ((currentTransformByte = ir.read()) != -1) {
332                                     content.append((char) currentTransformByte);
333                                 }
334
335                                 putParameter(currentParam, content.toString());
336                                 break;
337                             }
338                         }
339                     }
340                 } else { // .. it's a file
341
String JavaDoc filename = (String JavaDoc) headers.get("filename");
342                     if (filename != null && filename.length() != 0) {
343                         // The filename may contain a full path. Cut to just the filename.
344
int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
345                         if (slash > -1) {
346                             filename = filename.substring(slash + 1);
347                         }
348                         String JavaDoc name = (String JavaDoc) headers.get("name");
349
350                         String JavaDoc contentType = (String JavaDoc) headers.get("content-type");
351                         try {
352                             uploadFile = File.createTempFile("wings_uploaded",
353                                     "tmp");
354                         } catch (IOException e) {
355                             log.error("couldn't create temp file in '"
356                                     + System.getProperty("java.io.tmpdir")
357                                     + "' (CATALINA_TMPDIR set correctly?)",
358                                     e);
359                             throw e;
360                         }
361
362                         UploadedFile upload = new UploadedFile(filename,
363                                 contentType,
364                                 uploadFile);
365                         fileStream = new FileOutputStream(uploadFile);
366
367                         fileStream = UploadFilterManager.createFilterInstance(name, fileStream);
368
369                         AccessibleByteArrayOutputStream byteArray = new AccessibleByteArrayOutputStream();
370                         byte[] bytes = null;
371
372                         int blength = boundaryToken.length();
373                         int i;
374                         while ((currentByte = mimeStream.read()) != -1) {
375                             byteArray.write(currentByte);
376                             for (i = 0; i < blength; i++) {
377                                 if (boundaryToken.charAt(blength - i - 1) != byteArray.charAt(-i - 1)) {
378                                     i = 0;
379                                     if (byteArray.size() > 512 + blength + 2)
380                                         byteArray.writeTo(fileStream, 512);
381                                     break;
382                                 }
383                             }
384                             if (i == blength) // end of part ..
385
break;
386                         }
387                         bytes = byteArray.toByteArray();
388                         fileStream.write(bytes, 0, bytes.length - blength - 4);
389                         fileStream.close();
390
391                         files.put(name, upload);
392                         putParameter(name, upload.toString());
393                     } else { // workaround for some netscape bug
394
int i;
395                         int blength = boundaryToken.length();
396                         while ((currentByte = mimeStream.read()) != -1) {
397                             content.append((char) currentByte);
398                             if (content.length() >= blength) {
399                                 for (i = 0; i < blength; i++) {
400                                     if (boundaryToken.charAt(blength - i - 1) != content.charAt(content.length() - i - 1)) {
401                                         i = 0;
402                                         break;
403                                     }
404                                 }
405                                 if (i == blength)
406                                     break;
407                             }
408                         }
409                     }
410                 }
411
412                 currentByte = mimeStream.read();
413                 if (currentByte == '\r' && mimeStream.read() != '\n')
414                     log.error("No line return char? " + currentByte);
415                 if (currentByte == '-' && mimeStream.read() != '-')
416                     log.error("?? No clue " + currentByte);
417             }
418         } catch (IOException ex) {
419             // cleanup and store the exception for notification of SFileChooser
420
log.warn("upload", ex);
421             if (uploadFile != null) uploadFile.delete();
422             setException(currentParam, ex);
423         } finally {
424             try { fileStream.close(); } catch (Exception JavaDoc ign) {}
425             try { mimeStream.close(); } catch (Exception JavaDoc ign) {}
426         }
427     }
428
429     private static class AccessibleByteArrayOutputStream extends ByteArrayOutputStream {
430         public byte charAt(int index) {
431             if (count + index < 0) {
432                 log.warn("count: " + count + ", index: " + index + ", buffer: " + new String JavaDoc(buf));
433                 return -1;
434             }
435             if (index < 0)
436                 return buf[count + index];
437             if (index < count)
438                 return buf[index];
439             return -1;
440         }
441
442         public byte[] getBuffer() {
443             return buf;
444         }
445
446         public void writeTo(OutputStream out, int num)
447                 throws IOException {
448             out.write(buf, 0, num);
449             System.arraycopy(buf, num, buf, 0, count - num);
450             count = count - num;
451         }
452     }
453
454     private HashMap parseHeader(String JavaDoc header) {
455         int lastHeader = -1;
456         String JavaDoc[] headerLines;
457         HashMap nameValuePairs = new HashMap();
458
459         StringTokenizer stLines = new StringTokenizer(header, "\r\n", false);
460         headerLines = new String JavaDoc[stLines.countTokens()];
461
462         // Get all the header lines
463
while (stLines.hasMoreTokens()) {
464             String JavaDoc hLine = stLines.nextToken();
465             if (hLine.length() == 0) continue;
466             /* if the first character is a space, then
467              * this line is a header continuation.
468              * (opera sends multiline headers..)
469              */

470             if (lastHeader >= 0 && Character.isWhitespace(hLine.charAt(0)))
471                 headerLines[lastHeader] += hLine;
472             else
473                 headerLines[++lastHeader] = hLine;
474         }
475
476         for (int i = 0; i <= lastHeader; ++i) {
477             String JavaDoc currentHeader = headerLines[i];
478             if (currentHeader.startsWith("Content-Type")) {
479                 String JavaDoc contentType = currentHeader
480                         .substring(currentHeader.indexOf(':') + 1);
481                 int semiColonPos = contentType.indexOf(';');
482                 if (semiColonPos != -1)
483                     contentType = contentType.substring(0, semiColonPos);
484                 nameValuePairs.put("content-type", contentType.trim());
485                 continue;
486             }
487
488             if (!currentHeader.startsWith("Content-Disposition"))
489                 continue;
490
491             StringTokenizer stTokens = new StringTokenizer(currentHeader, ";", false);
492             
493             // Get all the tokens from each line
494
if (stTokens.countTokens() > 1) {
495                 stTokens.nextToken(); // Skip fist Token Content-Disposition: form-data
496
StringTokenizer stnameValue = new StringTokenizer(stTokens.nextToken(), "=", false);
497                 nameValuePairs.put(stnameValue.nextToken().trim(), trim(stnameValue.nextToken(), "\""));
498
499                 // This is a file
500
if (stTokens.hasMoreTokens()) {
501                     stnameValue = new StringTokenizer(stTokens.nextToken(), "=", false);
502
503                     String JavaDoc formType = stnameValue.nextToken().trim(); // String Object default function
504
String JavaDoc filePath = trim(stnameValue.nextToken(), "\""); // Our own trim function.
505
// If is a DOS file get rid of drive letter and colon "e:"
506
if (filePath.indexOf(":") != -1)
507                         filePath = filePath.substring((filePath.indexOf(":") + 1));
508
509                     // get rid of PATH
510
filePath = filePath.substring(filePath.lastIndexOf(File.separator) + 1);
511                     nameValuePairs.put(formType, filePath);
512                 }
513             }
514         }
515         return nameValuePairs;
516     }
517
518     /**
519      * This method gets the substring enclosed in trimChar ; "string" returns string
520      */

521     private String JavaDoc trim(String JavaDoc source, String JavaDoc trimChar) {
522         String JavaDoc target = "";
523         //Blank space from both sides
524
source = source.trim();
525
526         // Make sure a substring is enclosed between specified characters
527
if (source.indexOf(trimChar) != -1 && (source.lastIndexOf(trimChar) >= (source.indexOf(trimChar) + 1)))
528         // Remove double character from both sides
529
target = source.substring(source.indexOf(trimChar) + 1, source.lastIndexOf(trimChar));
530
531         return target;
532     }
533
534     private static class MultipartInputStream extends InputStream JavaDoc {
535         ServletInputStream JavaDoc istream = null;
536         int len, pos, maxLength;
537
538         public MultipartInputStream(ServletInputStream JavaDoc istream, int len, int maxLength) {
539             this.istream = istream;
540             this.len = len;
541             this.pos = 0;
542             this.maxLength = maxLength;
543         }
544
545         /**
546          * @return bytes available in stream.
547          */

548         public int available() throws IOException {
549             return len - pos - 1;
550         }
551
552         /**
553          * @return Next byte in Request.
554          * @throws IOException
555          */

556         public int read() throws IOException {
557             if (pos >= maxLength)
558                 throw new IOException("Size (" + len + ") exceeds maxlength " + maxLength);
559
560             if (pos >= len)
561                 return -1;
562             pos++;
563
564             return istream.read();
565         }
566
567         public int read(byte b[]) throws IOException {
568             return read(b, 0, b.length);
569         }
570
571         public int read(byte b[], int off, int num) throws IOException {
572             if (off > 0)
573                 istream.skip(off);
574
575             if (pos >= len)
576                 return -1;
577
578             if (num > len - pos)
579                 num = len - pos;
580
581             num = istream.read(b, 0, num);
582             pos += num;
583
584             if (pos >= maxLength)
585                 throw new IOException("Size (" + len + ") exceeds maxlength " + maxLength);
586
587             return num;
588         }
589
590         public long skip(long num) throws IOException {
591             if (pos >= len)
592                 return -1;
593
594             if (num > len - pos)
595                 num = len - pos;
596
597             num = istream.skip(num);
598             pos += num;
599
600             if (pos >= maxLength)
601                 throw new IOException("Size (" + len + ") exceeds maxlength " + maxLength);
602
603             return num;
604         }
605
606         public void close() throws IOException {
607             //Ignore closing of the input stream ..
608
}
609     }
610
611     /**
612      * Stores a parameter identified in this request.
613      */

614     protected void putParameter(String JavaDoc name, String JavaDoc value) {
615         ArrayList v = (ArrayList) parameters.get(name);
616         // there is no Parameter yet; create one
617
if (v == null) {
618             v = new ArrayList(2);
619             parameters.put(name, v);
620         }
621         v.add(value);
622     }
623
624     /**
625      * Extracts and returns the boundary token from a line.
626      */

627     private String JavaDoc extractBoundaryToken(String JavaDoc line) {
628         int index = line.indexOf("boundary=");
629         if (index == -1) {
630             return null;
631         }
632         String JavaDoc boundary = line.substring(index + 9); // 9 for "boundary="
633
// The real boundary is always preceeded by an extra "--"
634
//boundary = "--" + boundary;
635

636         return boundary;
637     }
638
639     /**
640      * Extracts and returns the content type from a line, or null if the line was empty.
641      *
642      * @throws IOException if the line is malformatted.
643      */

644     private String JavaDoc extractContentType(String JavaDoc line) throws IOException {
645         String JavaDoc contentType = null;
646
647         // Convert the line to a lowercase string
648
String JavaDoc origline = line;
649         line = origline.toLowerCase();
650
651         // Get the content type, if any
652
if (line.startsWith("content-type")) {
653             int start = line.indexOf(" ");
654             if (start == -1) {
655                 throw new IOException("Content type corrupt: " + origline);
656             }
657             contentType = line.substring(start + 1);
658         } else if (line.length() != 0) { // no content type, so should be empty
659
throw new IOException("Malformed line after disposition: " + origline);
660         }
661
662         return contentType;
663     }
664
665     private static long uniqueId = 0;
666
667     private static final synchronized String JavaDoc uniqueId() {
668         uniqueId++;
669         return System.currentTimeMillis() + "." + uniqueId;
670     }
671
672
673     /**
674      * A class to hold information about an uploaded file.
675      */

676     class UploadedFile {
677         private String JavaDoc fileName;
678         private String JavaDoc type;
679         private File uploadedFile;
680
681         UploadedFile(String JavaDoc fileName, String JavaDoc type, File f) {
682             this.uploadedFile = f;
683             this.fileName = fileName;
684             this.type = type;
685         }
686
687         /**
688          * @return Path of uploaded file
689          */

690         public String JavaDoc getDir() {
691             if (uploadedFile != null)
692                 return uploadedFile.getParentFile().getPath();
693             else
694                 return null;
695         }
696
697         /**
698          * @return Filename passed by browser
699          */

700         public String JavaDoc getFileName() {
701             return fileName;
702         }
703
704         /**
705          * @return MIME type passed by browser
706          */

707         public String JavaDoc getContentType() {
708             return type;
709         }
710
711         /**
712          * @return Uploaded file
713          */

714         public File getFile() {
715             return uploadedFile;
716         }
717
718         /**
719          * @return Uploaded file name
720          */

721         public String JavaDoc getId() {
722             if (uploadedFile != null)
723                 return uploadedFile.getName();
724             else
725                 return null;
726         }
727
728
729         /**
730          * create a URL-encoded form of this uploaded file, that contains
731          * all parameters important for this file. The parameters returned
732          * are 'dir', 'name', 'type' and 'id'
733          * <ul>
734          * <li>'dir' contains the directory in the filesystem, the file
735          * has been stored into.</li>
736          * <li>'name' contains the filename as provided by the user</li>
737          * <li>'type' contains the mime-type of this file.</li>
738          * <li>'id' contains the internal name of the file in the
739          * filesystem.</li>
740          * </ul>
741          */

742         public String JavaDoc toString() {
743             String JavaDoc encoding = getRequest().getCharacterEncoding() != null ? getRequest().getCharacterEncoding() : LocaleCharSet.DEFAULT_ENCODING;
744
745             try {
746                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
747                 buffer.append("dir=");
748                 buffer.append(URLEncoder.encode(getDir(), encoding));
749                 if (getFileName() != null) {
750                     buffer.append("&name=");
751                     buffer.append(URLEncoder.encode(getFileName(), encoding));
752                 }
753                 if (getContentType() != null) {
754                     buffer.append("&type=");
755                     buffer.append(URLEncoder.encode(getContentType(), encoding));
756                 }
757                 buffer.append("&id=");
758                 buffer.append(URLEncoder.encode(getId(), encoding));
759
760                 return buffer.toString();
761             } catch (UnsupportedEncodingException e) {
762                 log.error(getClass().getName(), e);
763                 return null;
764             }
765         }
766     }
767 }
768
769
770
771
Popular Tags