1 /*
2 * $Source$
3 * $Revision$
4 *
5 * Copyright (C) 2000 Myles Chippendale
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 * Myles Chippendale <mylesc At paneris.org>
42 * http://paneris.org/
43 * 29 Stanley Road, Oxford, OX4 1QY, UK
44 */
45
46
47 package org.melati.template;
48
49 import java.io.InputStream;
50 import java.io.IOException;
51 import java.util.Hashtable;
52 import java.util.Enumeration;
53 import java.util.Map;
54
55 import javax.servlet.http.HttpSession;
56 import org.melati.Melati;
57 import org.melati.servlet.MultipartFormField;
58 import org.melati.servlet.MultipartFormDataDecoder;
59
60
61 /**
62 * A {@link ServletTemplateContext} which allows access to the filename and
63 * body of any file which is uploaded from a HTML form field.
64 *
65 * (by setting its ENCTYPE to ``multipart/form-data'' and
66 * setting the field's type to * ``file'').
67 * <p>
68 * You can retrieve the value of any field variable as usual by
69 * using getForm(s).
70 * <p>
71 * Note that you need to pass in a {@link ServletTemplateContext} to the constructor.
72 */
73 public class MultipartTemplateContext implements ServletTemplateContext {
74 ServletTemplateContext peer;
75 Hashtable<String,MultipartFormField> fields;
76 Melati melati;
77
78 /**
79 * Constructor.
80 * @param melati our Melati
81 * @param context the ServletTemplateContext
82 */
83 public MultipartTemplateContext(Melati melati, ServletTemplateContext context)
84 throws IOException {
85 peer = context;
86 this.melati = melati;
87 try {
88 InputStream in = melati.getRequest().getInputStream();
89 MultipartFormDataDecoder decoder=
90 new MultipartFormDataDecoder(
91 melati,
92 in,
93 melati.getRequest().getContentType(),
94 melati.getConfig().getFormDataAdaptorFactory());
95 fields = decoder.parseData();
96 }
97 catch (IOException e) {
98 fields = new Hashtable<String,MultipartFormField>();
99 throw e;
100 }
101 }
102
103 /**
104 * Constructor.
105 * @param melati our Melati
106 * @param context the ServletTemplateContext
107 * @param maxSize maximum allowed size
108 */
109 public MultipartTemplateContext(Melati melati, ServletTemplateContext context,
110 int maxSize)
111 throws IOException {
112 peer = context;
113 this.melati = melati;
114 try {
115 InputStream in = melati.getRequest().getInputStream();
116 MultipartFormDataDecoder decoder=
117 new MultipartFormDataDecoder(melati,
118 in,
119 melati.getRequest().getContentType(),
120 melati.getConfig().getFormDataAdaptorFactory(),
121 maxSize);
122 fields = decoder.parseData();
123 }
124 catch (IOException e) {
125 fields = new Hashtable<String,MultipartFormField>();
126 throw e;
127 }
128 }
129
130 /**
131 * {@inheritDoc}
132 * @see org.melati.template.TemplateContext#put(java.lang.String, java.lang.Object)
133 */
134 public Object put(String s, Object o) {
135 return peer.put(s,o);
136 }
137
138 /**
139 * {@inheritDoc}
140 * @see org.melati.template.ServletTemplateContext#getFormField(java.lang.String)
141 */
142 public String getFormField(String s) {
143 MultipartFormField field = (MultipartFormField)fields.get(s);
144 if (field == null)
145 return peer.getFormField(s);
146 return field.getDataString(melati.getResponse().getCharacterEncoding());
147 }
148
149 /**
150 * {@inheritDoc}
151 * @see org.melati.template.ServletTemplateContext#getMultipartFormField(java.lang.String)
152 */
153 public MultipartFormField getMultipartFormField(String s) {
154 return (MultipartFormField)fields.get(s);
155 }
156
157 /**
158 * @return an Enumeration of Field names
159 */
160 public Enumeration<String> getFieldNames() {
161 return fields.keys();
162 }
163
164 /**
165 * {@inheritDoc}
166 * @see org.melati.template.TemplateContext#get(java.lang.String)
167 */
168 public Object get(String s) {
169 return peer.get(s);
170 }
171
172 /**
173 * {@inheritDoc}
174 * @see org.melati.template.ServletTemplateContext#getSession()
175 */
176 public HttpSession getSession() {
177 return peer.getSession();
178 }
179
180 /**
181 * {@inheritDoc}
182 * @see org.melati.template.TemplateContext#getContext()
183 */
184 public Object getContext() {
185 return peer.getContext();
186 }
187
188 /**
189 * {@inheritDoc}
190 * @see org.melati.template.TemplateContext#setPassbackExceptionHandling()
191 */
192 public void setPassbackExceptionHandling() {
193 peer.setPassbackExceptionHandling();
194 }
195
196 /**
197 * {@inheritDoc}
198 * @see org.melati.template.TemplateContext#setPropagateExceptionHandling()
199 */
200 public void setPropagateExceptionHandling() {
201 peer.setPropagateExceptionHandling();
202 }
203
204 @Override
205 public void putAll(Map<String, ?> m) {
206 peer.putAll(m);
207 }
208 }
209
210
211
212
213
214