|
|||||||||||||||||||
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
UploadPart.java | - | 73.3% | 88.9% | 79.2% |
|
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 java.io.File; | |
18 | import java.io.IOException; | |
19 | import java.io.InputStream; | |
20 | ||
21 | import org.apache.commons.fileupload.FileItem; | |
22 | import org.apache.hivemind.ApplicationRuntimeException; | |
23 | import org.apache.hivemind.util.Defense; | |
24 | import org.apache.tapestry.Tapestry; | |
25 | import org.apache.tapestry.request.IUploadFile; | |
26 | ||
27 | /** | |
28 | * Portion of a multi-part request representing an uploaded file. | |
29 | * | |
30 | * @author Joe Panico | |
31 | * @since 2.0.1 | |
32 | */ | |
33 | public class UploadPart extends Object implements IUploadFile | |
34 | { | |
35 | private FileItem _fileItem; | |
36 | ||
37 | 4 | public UploadPart(FileItem fileItem) |
38 | { | |
39 | 4 | Defense.notNull(fileItem, "fileItem"); |
40 | ||
41 | 4 | _fileItem = fileItem; |
42 | } | |
43 | ||
44 | 3 | public String getContentType() |
45 | { | |
46 | 3 | return _fileItem.getContentType(); |
47 | } | |
48 | ||
49 | /** | |
50 | * Leverages {@link File}to convert the full file path and extract the name. | |
51 | */ | |
52 | 3 | public String getFileName() |
53 | { | |
54 | 3 | File file = new File(this.getFilePath()); |
55 | ||
56 | 3 | return file.getName(); |
57 | } | |
58 | ||
59 | /** | |
60 | * @since 2.0.4 | |
61 | */ | |
62 | ||
63 | 12 | public String getFilePath() |
64 | { | |
65 | 12 | return _fileItem.getName(); |
66 | } | |
67 | ||
68 | 3 | public InputStream getStream() |
69 | { | |
70 | 3 | try |
71 | { | |
72 | 3 | return _fileItem.getInputStream(); |
73 | } | |
74 | catch (IOException ex) | |
75 | { | |
76 | 0 | throw new ApplicationRuntimeException(MultipartMessages.unableToOpenContentFile( |
77 | this, | |
78 | ex), ex); | |
79 | } | |
80 | } | |
81 | ||
82 | /** | |
83 | * Deletes the external content file, if one exists. | |
84 | */ | |
85 | ||
86 | 4 | public void cleanup() |
87 | { | |
88 | 4 | _fileItem.delete(); |
89 | } | |
90 | ||
91 | /** | |
92 | * Writes the uploaded content to a file. This should be invoked at most once (perhaps we should | |
93 | * add a check for this). This will often be a simple file rename. | |
94 | * | |
95 | * @since 3.0 | |
96 | */ | |
97 | 0 | public void write(File file) |
98 | { | |
99 | 0 | try |
100 | { | |
101 | 0 | _fileItem.write(file); |
102 | } | |
103 | catch (Exception ex) | |
104 | { | |
105 | 0 | throw new ApplicationRuntimeException(Tapestry.format( |
106 | "UploadPart.write-failure", | |
107 | file, | |
108 | ex.getMessage()), ex); | |
109 | } | |
110 | } | |
111 | ||
112 | /** | |
113 | * @since 3.0 | |
114 | */ | |
115 | 3 | public long getSize() |
116 | { | |
117 | 3 | return _fileItem.getSize(); |
118 | } | |
119 | ||
120 | /** | |
121 | * @since 3.0 | |
122 | */ | |
123 | 3 | public boolean isInMemory() |
124 | { | |
125 | 3 | return _fileItem.isInMemory(); |
126 | } | |
127 | ||
128 | } |
|