KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > multipart > cos > CosMultipartHttpServletRequest


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.multipart.cos;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30
31 import com.oreilly.servlet.MultipartRequest;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 import org.springframework.util.FileCopyUtils;
36 import org.springframework.web.multipart.MultipartFile;
37 import org.springframework.web.multipart.support.AbstractMultipartHttpServletRequest;
38
39 /**
40  * MultipartHttpServletRequest implementation for Jason Hunter's COS.
41  * Wraps a COS MultipartRequest with Spring MultipartFile instances.
42  *
43  * <p>Not intended for direct application usage. Application code can cast
44  * to this implementation to access the underlying COS MultipartRequest,
45  * if it ever needs to.
46  *
47  * @author Juergen Hoeller
48  * @since 06.10.2003
49  * @see CosMultipartResolver
50  * @see com.oreilly.servlet.MultipartRequest
51  */

52 public class CosMultipartHttpServletRequest extends AbstractMultipartHttpServletRequest {
53
54     protected static final Log logger = LogFactory.getLog(CosMultipartHttpServletRequest.class);
55
56     private final MultipartRequest multipartRequest;
57
58
59     /**
60      * Wrap the given HttpServletRequest in a MultipartHttpServletRequest.
61      * @param request the servlet request to wrap
62      * @param multipartRequest the COS multipart request to wrap
63      */

64     protected CosMultipartHttpServletRequest(HttpServletRequest JavaDoc request, MultipartRequest multipartRequest) {
65         super(request);
66         this.multipartRequest = multipartRequest;
67         setMultipartFiles(initFileMap(multipartRequest));
68     }
69
70     /**
71      * Return the underlying <code>com.oreilly.servlet.MultipartRequest</code>
72      * instance. There is hardly any need to access this.
73      */

74     public MultipartRequest getMultipartRequest() {
75         return multipartRequest;
76     }
77
78     /**
79      * Initialize a Map with file name Strings as keys and
80      * CosMultipartFile instances as values.
81      * @param multipartRequest the COS multipart request
82      * to get the multipart file data from
83      * @return the Map with CosMultipartFile values
84      */

85     private Map JavaDoc initFileMap(MultipartRequest multipartRequest) {
86         Map JavaDoc files = new HashMap JavaDoc();
87         Enumeration JavaDoc fileNames = multipartRequest.getFileNames();
88         while (fileNames.hasMoreElements()) {
89             String JavaDoc fileName = (String JavaDoc) fileNames.nextElement();
90             files.put(fileName, new CosMultipartFile(fileName));
91         }
92         return files;
93     }
94
95
96     public Enumeration JavaDoc getParameterNames() {
97         return this.multipartRequest.getParameterNames();
98     }
99
100     public String JavaDoc getParameter(String JavaDoc name) {
101         return this.multipartRequest.getParameter(name);
102     }
103
104     public String JavaDoc[] getParameterValues(String JavaDoc name) {
105         return this.multipartRequest.getParameterValues(name);
106     }
107
108     public Map JavaDoc getParameterMap() {
109         Map JavaDoc params = new HashMap JavaDoc();
110         Enumeration JavaDoc names = getParameterNames();
111         while (names.hasMoreElements()) {
112             String JavaDoc name = (String JavaDoc) names.nextElement();
113             params.put(name, getParameterValues(name));
114         }
115         return Collections.unmodifiableMap(params);
116     }
117
118
119     /**
120      * Implementation of Spring's MultipartFile interface on top
121      * of a COS MultipartRequest object.
122      */

123     private class CosMultipartFile implements MultipartFile {
124
125         private final String JavaDoc name;
126
127         private final File JavaDoc file;
128
129         private final long size;
130
131         public CosMultipartFile(String JavaDoc name) {
132             this.name = name;
133             this.file = multipartRequest.getFile(this.name);
134             this.size = (this.file != null ? this.file.length() : 0);
135         }
136
137         public String JavaDoc getName() {
138             return name;
139         }
140
141         public String JavaDoc getOriginalFilename() {
142             String JavaDoc filename = multipartRequest.getOriginalFileName(this.name);
143             return (filename != null ? filename : "");
144         }
145
146         public String JavaDoc getContentType() {
147             return multipartRequest.getContentType(this.name);
148         }
149
150         public boolean isEmpty() {
151             return (this.size == 0);
152         }
153
154         public long getSize() {
155             return this.size;
156         }
157
158         public byte[] getBytes() throws IOException JavaDoc {
159             if (this.file != null && !this.file.exists()) {
160                 throw new IllegalStateException JavaDoc("File has been moved - cannot be read again");
161             }
162             return (this.size > 0 ? FileCopyUtils.copyToByteArray(this.file) : new byte[0]);
163         }
164
165         public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
166             if (this.file != null && !this.file.exists()) {
167                 throw new IllegalStateException JavaDoc("File has been moved - cannot be read again");
168             }
169             if (this.size != 0) {
170                 return new FileInputStream JavaDoc(this.file);
171             }
172             else {
173                 return new ByteArrayInputStream JavaDoc(new byte[0]);
174             }
175         }
176
177         public void transferTo(File JavaDoc dest) throws IOException JavaDoc, IllegalStateException JavaDoc {
178             if (this.file != null && !this.file.exists()) {
179                 throw new IllegalStateException JavaDoc("File has already been moved - cannot be transferred again");
180             }
181
182             if (dest.exists() && !dest.delete()) {
183                 throw new IOException JavaDoc(
184                         "Destination file [" + dest.getAbsolutePath() + "] already exists and could not be deleted");
185             }
186
187             boolean moved = false;
188             if (this.file != null) {
189                 moved = this.file.renameTo(dest);
190                 if (!moved) {
191                     FileCopyUtils.copy(this.file, dest);
192                 }
193             }
194             else {
195                 dest.createNewFile();
196             }
197
198             if (logger.isDebugEnabled()) {
199                 logger.debug("Multipart file '" + getName() + "' with original filename [" +
200                         getOriginalFilename() + "], stored " +
201                         (this.file != null ? "at [" + this.file.getAbsolutePath() + "]" : "in memory") +
202                         ": " + (moved ? "moved" : "copied") + " to [" + dest.getAbsolutePath() + "]");
203             }
204         }
205     }
206
207 }
208
Popular Tags