Classes in this File | Line Coverage | Branch Coverage | Complexity | |||||||
UploadPart |
|
| 1.5555555555555556;1.556 |
1 | // Copyright 2004, 2005 The Apache Software Foundation |
|
2 | // |
|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
|
4 | // you may not use this file except in compliance with the License. |
|
5 | // You may obtain a copy of the License at |
|
6 | // |
|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
|
8 | // |
|
9 | // Unless required by applicable law or agreed to in writing, software |
|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
12 | // See the License for the specific language governing permissions and |
|
13 | // limitations under the License. |
|
14 | ||
15 | package org.apache.tapestry.multipart; |
|
16 | ||
17 | import org.apache.commons.fileupload.FileItem; |
|
18 | import org.apache.commons.io.FilenameUtils; |
|
19 | import org.apache.hivemind.ApplicationRuntimeException; |
|
20 | import org.apache.hivemind.util.Defense; |
|
21 | import org.apache.tapestry.Tapestry; |
|
22 | import org.apache.tapestry.request.IUploadFile; |
|
23 | ||
24 | import java.io.File; |
|
25 | import java.io.IOException; |
|
26 | import java.io.InputStream; |
|
27 | ||
28 | /** |
|
29 | * Portion of a multi-part request representing an uploaded file. |
|
30 | * |
|
31 | * @author Joe Panico |
|
32 | * @since 2.0.1 |
|
33 | */ |
|
34 | public class UploadPart extends Object implements IUploadFile |
|
35 | { |
|
36 | ||
37 | private FileItem _fileItem; |
|
38 | ||
39 | public UploadPart(FileItem fileItem) |
|
40 | 2 | { |
41 | 2 | Defense.notNull(fileItem, "fileItem"); |
42 | ||
43 | 2 | _fileItem = fileItem; |
44 | 2 | } |
45 | ||
46 | public String getContentType() |
|
47 | { |
|
48 | 0 | return _fileItem.getContentType(); |
49 | } |
|
50 | ||
51 | /** |
|
52 | * Leverages {@link File}to convert the full file path and extract the |
|
53 | * name. |
|
54 | */ |
|
55 | public String getFileName() |
|
56 | { |
|
57 | 1 | return FilenameUtils.getName(getFilePath()); |
58 | } |
|
59 | ||
60 | /** |
|
61 | * @since 2.0.4 |
|
62 | */ |
|
63 | ||
64 | public String getFilePath() |
|
65 | { |
|
66 | 2 | return _fileItem.getName(); |
67 | } |
|
68 | ||
69 | public InputStream getStream() |
|
70 | { |
|
71 | try |
|
72 | { |
|
73 | 0 | return _fileItem.getInputStream(); |
74 | } |
|
75 | 0 | catch (IOException ex) |
76 | { |
|
77 | 0 | throw new ApplicationRuntimeException(MultipartMessages |
78 | .unableToOpenContentFile(this, ex), ex); |
|
79 | } |
|
80 | } |
|
81 | ||
82 | /** |
|
83 | * Deletes the external content file, if one exists. |
|
84 | */ |
|
85 | ||
86 | public void cleanup() |
|
87 | { |
|
88 | 0 | _fileItem.delete(); |
89 | 0 | } |
90 | ||
91 | /** |
|
92 | * Writes the uploaded content to a file. This should be invoked at most |
|
93 | * once (perhaps we should add a check for this). This will often be a |
|
94 | * simple file rename. |
|
95 | * |
|
96 | * @since 3.0 |
|
97 | */ |
|
98 | public void write(File file) |
|
99 | { |
|
100 | try |
|
101 | { |
|
102 | 0 | _fileItem.write(file); |
103 | } |
|
104 | 0 | catch (Exception ex) |
105 | { |
|
106 | 0 | throw new ApplicationRuntimeException(Tapestry.format( |
107 | "UploadPart.write-failure", file, ex.getMessage()), ex); |
|
108 | 0 | } |
109 | 0 | } |
110 | ||
111 | /** |
|
112 | * @since 3.0 |
|
113 | */ |
|
114 | public long getSize() |
|
115 | { |
|
116 | 0 | return _fileItem.getSize(); |
117 | } |
|
118 | ||
119 | /** |
|
120 | * @since 3.0 |
|
121 | */ |
|
122 | public boolean isInMemory() |
|
123 | { |
|
124 | 0 | return _fileItem.isInMemory(); |
125 | } |
|
126 | ||
127 | } |