Coverage Report

##### Coverage Report - org.melati.servlet.MultipartFormField

Classes in this File | Line Coverage | Branch Coverage | Complexity
MultipartFormField |
43% | 25/57
30% | 6/20
| 1.85;1.85

1 |  |
     /*
2 |  |
      * $Source: /usr/cvsroot/melati/melati/src/site/resources/withWebmacro/org.melati.servlet.MultipartFormField.html,v $
3 |  |
      * $Revision: 1.1 $
4 |  |
      *
5 |  |
      * Copyright (C) 2000 Vasily Pozhidaev
6 |  |
      *
7 |  |
      * Part of Melati (http://melati.org), a framework for the rapid
8 |  |
      * development of clean, maintainable web applications.
9 |  |
      *
10 |  |
      * Melati is free software; Permission is granted to copy, distribute
11 |  |
      * and/or modify this software under the terms either:
12 |  |
      *
13 |  |
      * a) the GNU General Public License as published by the Free Software
14 |  |
      *    Foundation; either version 2 of the License, or (at your option)
15 |  |
      *    any later version,
16 |  |
      *
17 |  |
      *    or
18 |  |
      *
19 |  |
      * b) any version of the Melati Software License, as published
20 |  |
      *    at http://melati.org
21 |  |
      *
22 |  |
      * You should have received a copy of the GNU General Public License and
23 |  |
      * the Melati Software License along with this program;
24 |  |
      * if not, write to the Free Software Foundation, Inc.,
25 |  |
      * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
26 |  |
      * GNU General Public License and visit http://melati.org to obtain the
27 |  |
      * Melati Software License.
28 |  |
      *
29 |  |
      * Feel free to contact the Developers of Melati (http://melati.org),
30 |  |
      * if you would like to work out a different arrangement than the options
31 |  |
      * outlined here.  It is our intention to allow Melati to be used by as
32 |  |
      * wide an audience as possible.
33 |  |
      *
34 |  |
      * This program is distributed in the hope that it will be useful,
35 |  |
      * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 |  |
      * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37 |  |
      * GNU General Public License for more details.
38 |  |
      *
39 |  |
      * Contact details for copyright holder:
40 |  |
      *
41 |  |
      *     Vasily Pozhidaev <voodoo At knastu.ru; vpozhidaev At mail.ru>
42 |  |
      *     http://paneris.org/~vasilyp
43 |  |
      */
44 |  |
45 |  |
     package org.melati.servlet;
46 |  |
47 |  |
     import java.io.File;
48 |  |
     import java.io.UnsupportedEncodingException;
49 |  |
50 |  |
     /**
51 |  |
      * Holds information parsed from a multipart/form-data file upload.
52 |  |
      *
53 |  |
      * Based on RFC 1867 which describes the format
54 |  |
      * for uploading files in multipart/form-data.
55 |  |
      * Tested on IE 5.0, HotJava 3.0, Netscape Navigator 4.x
56 |  |
      *
57 |  |
      * @see <a href="http://www.ietf.org/rfc/rfc1867.txt">rfc1867</a>
58 |  |
      * @author Vasily Pozhidaev (vasilyp At paneris.org)
59 |  |
      */
60 |  |
     public class MultipartFormField {
61 |  |
62 | 128 |
       private String contentDisposition = "";
63 | 128 |
       private String fieldName = "";
64 | 128 |
       private String filePath = "";
65 | 128 |
       private String contentType = "";
66 | 128 |
       private FormDataAdaptor adaptor = null;
67 |  |
68 |  |
       /** Number of bytes in a kilobyte. */
69 |  |
       public static final long KILOBYTE = 1024;
70 |  |
       /** Number of bytes in a megabyte. */
71 |  |
       public static final long MEGABYTE = 1024 * 1024;
72 |  |
       /** Number of bytes in a gigabyte. */
73 |  |
       public static final long GIGABYTE = 1024 * 1024 * 1024;
74 |  |
75 |  |
       /** The path to a local file */
76 |  |
       //private File localFile = null;
77 |  |
78 |  |
       /** The url to this file */
79 | 128 |
       String url = null;
80 |  |
81 |  |
       /** Constructor. */
82 | 256 |
       public MultipartFormField() {}
83 |  |
84 |  |
       /**
85 |  |
        * Mime information
86 |  |
        */
87 |  |
88 |  |
       /**
89 |  |
        * Set the <code>ContentType</code>.
90 |  |
        *
91 |  |
        * @param contentType  the Content Type to set it to
92 |  |
        */
93 |  |
       public void setContentType(String contentType) {
94 | 128 |
         this.contentType = contentType;
95 | 128 |
       }
96 |  |
97 |  |
       /**
98 |  |
        * Get the <code>ContentType</code>.
99 |  |
        *
100 |  |
        * @return the <code>ContentType</code>
101 |  |
        */
102 |  |
       public String getContentType() {
103 | 24 |
         return contentType;
104 |  |
       }
105 |  |
106 |  |
       /** Set the <code>ContentDisposition</code>.
107 |  |
        *
108 |  |
        * @param contentDisposition  the Content Disposition to set it to
109 |  |
        */
110 |  |
       public void setContentDisposition(String contentDisposition) {
111 | 128 |
         this.contentDisposition = contentDisposition;
112 | 128 |
       }
113 |  |
114 |  |
       /**
115 |  |
        * Get the <code>ContentDisposition</code>.
116 |  |
        *
117 |  |
        * @return the <code>ContentDisposition</code>
118 |  |
        */
119 |  |
       public String getContentDisposition() {
120 | 0 |
         return contentDisposition;
121 |  |
       }
122 |  |
123 |  |
       /**
124 |  |
        * Set the <code>FieldName</code>.
125 |  |
        *
126 |  |
        * @param fieldName  the name to set <code>fieldName</code> to
127 |  |
        */
128 |  |
       public void setFieldName(String fieldName) {
129 | 128 |
         this.fieldName = fieldName;
130 | 128 |
       }
131 |  |
132 |  |
       /**
133 |  |
        * Get the <code>FieldName</code>.
134 |  |
        *
135 |  |
        * @return the <code>FieldName</code>
136 |  |
        */
137 |  |
       public String getFieldName() {
138 | 128 |
         return fieldName;
139 |  |
       }
140 |  |
141 |  |
       /**
142 |  |
        * Set the <code>UploadedFilePath</code>.
143 |  |
        *
144 |  |
        * @param filePath  the name to set <code>filePath</code> to
145 |  |
        */
146 |  |
       public void setUploadedFilePath(String filePath) {
147 | 42 |
         this.filePath = filePath;
148 | 42 |
       }
149 |  |
150 |  |
       /**
151 |  |
        * Get the <code>UploadedFilePath</code>.
152 |  |
        * @return the <code>UploadedFilePath</code>
153 |  |
        */
154 |  |
       public String getUploadedFilePath() {
155 | 0 |
         return filePath;
156 |  |
       }
157 |  |
158 |  |
       /**
159 |  |
        * Get the <code>UploadedFileName</code>.
160 |  |
        *
161 |  |
        * @return the <code>UploadedFileName</code>
162 |  |
        */
163 |  |
       public String getUploadedFileName() {
164 |  |
         try {
165 | 142 |
           return filePath.substring(((filePath.lastIndexOf("\\") != -1)
166 |  |
                                      ? filePath.lastIndexOf("\\")
167 |  |
                                      : filePath.lastIndexOf("/")) + 1);
168 |  |
         }
169 | 0 |
         catch (Exception e) {
170 | 0 |
           return "";
171 |  |
         }
172 |  |
       }
173 |  |
174 |  |
       /**
175 |  |
        * @return the adaptor
176 |  |
        */
177 |  |
       public FormDataAdaptor getFormDataAdaptor() {
178 | 0 |
         return adaptor;
179 |  |
       }
180 |  |
       /**
181 |  |
        * Work with an uploaded file/stored value.
182 |  |
        *
183 |  |
        * We can store uploaded files or values in different ways depending
184 |  |
        * on which adaptor we use.
185 |  |
        *
186 |  |
        * @param adaptor  a {@link FormDataAdaptor} to set <code>adaptor</code> to
187 |  |
        */
188 |  |
       public void setFormDataAdaptor(FormDataAdaptor adaptor) {
189 | 128 |
         this.adaptor = adaptor;
190 | 128 |
       }
191 |  |
192 |  |
       /**
193 |  |
        * @return the saved data as a byte array
194 |  |
        */
195 |  |
       public byte[] getData() {
196 | 54 |
         return (adaptor != null) ? adaptor.getData() : new byte[0];
197 |  |
       }
198 |  |
199 |  |
       /**
200 |  |
        * @return the saved data as a string
201 |  |
        */
202 |  |
       public String getDataString() {
203 | 0 |
         return new String(getData());
204 |  |
       }
205 |  |
206 |  |
       /**
207 |  |
        * Get the data using the specified encoding.
208 |  |
        *
209 |  |
        * @param enc an encoding which may be null or empty
210 |  |
        * @return the saved data as a string using the encoding supplied
211 |  |
        */
212 |  |
       public String getDataString(String enc) {
213 | 22 |
         if (enc == null || enc.equals(""))
214 | 0 |
           return getDataString();
215 |  |
         try {
216 | 22 |
           return new String(getData(), enc);
217 |  |
         }
218 | 0 |
         catch (UnsupportedEncodingException e) {
219 | 0 |
           e.printStackTrace();
220 | 0 |
           return getDataString();
221 |  |
         }
222 |  |
       }
223 |  |
224 |  |
       /**
225 |  |
        * Return the length of the data.
226 |  |
        * @return the length of the data
227 |  |
        */
228 |  |
       public long getDataSize() {
229 | 0 |
         return (adaptor != null) ? adaptor.getSize() : 0;
230 |  |
       }
231 |  |
232 |  |
       /**
233 |  |
        * Return the data saved as a file (or null if it is not saved).
234 |  |
        * @return The data saved as a file (or null if it is not saved)
235 |  |
        */
236 |  |
       public File getDataFile() {
237 | 8 |
         return (adaptor != null) ? adaptor.getFile() : null;
238 |  |
       }
239 |  |
240 |  |
       /**
241 |  |
        * Return a URL to the saved data (or null if no such URL exists).
242 |  |
        * @return a URL to the saved data (or null if no such URL exists)
243 |  |
        */
244 |  |
       public String getDataURL() {
245 | 2 |
         return (adaptor != null) ? adaptor.getURL() : null;
246 |  |
       }
247 |  |
248 |  |
       /**
249 |  |
        * The size of the file as a formatted string.
250 |  |
        *
251 |  |
        * @return the size <code>String</code>.
252 |  |
        */
253 |  |
       public String getPrettyDataSize() {
254 | 0 |
         long size = 0;
255 |  |
         try {
256 | 0 |
           size = getDataSize();
257 |  |
         }
258 | 0 |
         catch (Exception e) {
259 | 0 |
           return "Unknown";
260 | 0 |
         }
261 | 0 |
         String sizeString = null;
262 | 0 |
         if ((size / KILOBYTE) >= 1) {
263 | 0 |
           if ((size / MEGABYTE) >= 1) {
264 | 0 |
             if ((size / GIGABYTE) >= 1)
265 | 0 |
               sizeString = (size / GIGABYTE) + " Gb";
266 |  |
             else
267 | 0 |
               sizeString = (size / MEGABYTE) + " Mb";
268 |  |
           } else {
269 | 0 |
             sizeString = (size / KILOBYTE) + " Kb";
270 |  |
           }
271 |  |
         } else {
272 | 0 |
           sizeString = size + " bytes";
273 |  |
         }
274 | 0 |
         return sizeString;
275 |  |
       }
276 |  |
277 |  |
       /**
278 |  |
        * {@inheritDoc}
279 |  |
        * @see java.lang.Object#toString()
280 |  |
        */
281 |  |
       public String toString() {
282 | 0 |
         String returnString =  "contentDisposition=" + contentDisposition;
283 | 0 |
         returnString += "; fieldName=" + fieldName;
284 | 0 |
         returnString += "; filePath=" + filePath;
285 | 0 |
         returnString += "; contentType=" + contentType;
286 | 0 |
         returnString += "; adaptor=" + adaptor;
287 | 0 |
         returnString += "; url=" + url;
288 | 0 |
         return returnString;
289 |  |
       }
290 |  |
291 |  |
292 |  |
     }