1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.dbforms.event;
25
26 import org.dbforms.config.DbFormsConfig;
27 import org.dbforms.config.FieldValue;
28 import org.dbforms.config.FieldValues;
29 import org.dbforms.config.Table;
30 import org.dbforms.config.ValidationException;
31
32 import org.dbforms.util.FileHolder;
33 import java.sql.Connection;
34
35 import java.util.HashMap;
36 import java.util.Iterator;
37 import java.util.Map;
38
39 import javax.servlet.http.HttpServletRequest;
40
41
42
43 /***
44 * This Interceptor can be used to automatically store the filenames, content
45 * types and file sizes of BLOBS in some table column specified, so that it is
46 * not lost. Currently, the interceptor must be configured manually in
47 * dbforms-config.xml inside the adequate table definition (see user guide)
48 * The interceptor MUST be initialized with the following parameters:
49 *
50 * <ul>
51 * <li>
52 * <b>blob-column </b>- the name of the BLOB field
53 * </li>
54 * <li>
55 * <b>name-column </b>- a character field to store the FILENAME of the uploaded
56 * file
57 * </li>
58 * </ul>
59 *
60 * Optionaly may be specified:
61 *
62 * <ul>
63 * <li>
64 * <b>mime-column </b>- a character field to store the content typpe of the
65 * uploaded file
66 * </li>
67 * <li>
68 * <b>size-column </b>- an integer field to store the file size of the uploaded
69 * file
70 * </li>
71 * </ul>
72 *
73 * if the table contains multiple BLOBs, then a unique integer n has to be
74 * appended to blob-column and blob-name to associate the correct pairs of
75 * BLOB, NAME, MIME and SIZE fields. Usage Examples:
76 *
77 * <p>
78 * if one BLOB:
79 * </p>
80 *
81 * <p>
82 * <interceptor className="org.dbforms.event.BlobInterceptor"> <br>
83 * <param name="blob-column" value="file"/> <br>
84 * <param name="name-column" value="filename"/> <br>
85 * <param name="mime-column" value="mime_type"/> <br>
86 * <param name="size-column" value="file_size"/> <br>
87 * </interceptor> <br>
88 * </p>
89 *
90 * <p>
91 * if mulitple BLOBs:
92 * </p>
93 *
94 * <p>
95 * <interceptor className="org.dbforms.event.BlobInterceptor"> <br>
96 * <param name="blob-column1" value="file"/> <br>
97 * <param name="name-column1" value="filename"/> <br>
98 * <param name="mime-column1" value="mime_type"/> <br>
99 * <param name="size-column1" value="file_size"/> <br>
100 * <param name="blob-column2" value="otherfile"/> <br>
101 * <param name="name-column2" value="otherfilename"/> <br>
102 * <param name="mime-column2" value="other_mime_type"/> <br>
103 * <param name="size-column2" value="other_file_size"/> <br>
104 * <param name="blob-column3" value="foofile"/> <br>
105 * <param name="name-column3" value="foofilename"/> <br>
106 * <param name="mime-column3" value="foo_mime_type"/> <br>
107 * <param name="size-column3" value="foo_file_size"/> <br>
108 * </interceptor> <br>
109 * </p>
110 *
111 * @author Joe Peer
112 */
113 public class BlobInterceptor extends DbEventInterceptorSupport {
114 private static final int BLOB_COL = 0;
115 private static final int NAME_COL = 1;
116 private static final int MIME_COL = 2;
117 private static final int SIZE_COL = 3;
118 private HashMap blobFieldData;
119
120 /***
121 * takes params, sorts/groups and stores for later use
122 *
123 * @param params DOCUMENT ME!
124 */
125 public void setParameterMap(Map params) {
126 super.setParameterMap(params);
127 blobFieldData = new HashMap();
128
129 for (Iterator iter = params.entrySet()
130 .iterator(); iter.hasNext();) {
131 Map.Entry me = (Map.Entry) iter.next();
132 String key = (String) me.getKey();
133 String value = (String) me.getValue();
134
135 if (key.startsWith("blob-column")) {
136 Integer ii = getSuffixAsInteger(key, "blob-column");
137 String[] s = (String[]) blobFieldData.get(ii);
138
139 if (s == null) {
140 s = new String[4];
141 blobFieldData.put(ii, s);
142 }
143
144 s[BLOB_COL] = value;
145 } else if (key.startsWith("name-column")) {
146 Integer ii = getSuffixAsInteger(key, "name-column");
147 String[] s = (String[]) blobFieldData.get(ii);
148
149 if (s == null) {
150 s = new String[4];
151 blobFieldData.put(ii, s);
152 }
153
154 s[NAME_COL] = value;
155 } else if (key.startsWith("mime-column")) {
156 Integer ii = getSuffixAsInteger(key, "mime-column");
157 String[] s = (String[]) blobFieldData.get(ii);
158
159 if (s == null) {
160 s = new String[4];
161 blobFieldData.put(ii, s);
162 }
163
164 s[MIME_COL] = value;
165 } else if (key.startsWith("size-column")) {
166 Integer ii = getSuffixAsInteger(key, "size-column");
167 String[] s = (String[]) blobFieldData.get(ii);
168
169 if (s == null) {
170 s = new String[4];
171 blobFieldData.put(ii, s);
172 }
173
174 s[SIZE_COL] = value;
175 }
176 }
177 }
178
179
180 /***
181 * stores filenames, content types and file sizes of all blobs in the
182 * NAME_COLUMNs, MIME_COLUMNs and SIZE_COLUMNs specified
183 *
184 * @param request DOCUMENT ME!
185 * @param table DOCUMENT ME!
186 * @param fieldValues DOCUMENT ME!
187 * @param config DOCUMENT ME!
188 * @param con DOCUMENT ME!
189 *
190 * @return DOCUMENT ME!
191 */
192 public int preInsert(HttpServletRequest request,
193 Table table,
194 FieldValues fieldValues,
195 DbFormsConfig config,
196 Connection con) throws ValidationException {
197 assignBlobData(table, fieldValues);
198
199 return GRANT_OPERATION;
200 }
201
202
203 /***
204 * stores filenames, content types and file sizes of all blobs in the
205 * NAME_COLUMNs, MIME_COLUMNs and SIZE_COLUMNs specified
206 *
207 * @param request DOCUMENT ME!
208 * @param table DOCUMENT ME!
209 * @param fieldValues DOCUMENT ME!
210 * @param config DOCUMENT ME!
211 * @param con DOCUMENT ME!
212 *
213 * @return DOCUMENT ME!
214 */
215 public int preUpdate(HttpServletRequest request,
216 Table table,
217 FieldValues fieldValues,
218 DbFormsConfig config,
219 Connection con) throws ValidationException {
220 assignBlobData(table, fieldValues);
221
222 return GRANT_OPERATION;
223 }
224
225
226 /***
227 * utility function
228 *
229 * @param value DOCUMENT ME!
230 * @param stub DOCUMENT ME!
231 *
232 * @return DOCUMENT ME!
233 */
234 private Integer getSuffixAsInteger(String value,
235 String stub) {
236 String suffix = value.substring(stub.length());
237
238 if (suffix.length() == 0) {
239 return new Integer(Integer.MIN_VALUE);
240 } else {
241 return new Integer(suffix);
242 }
243 }
244
245
246 /***
247 * goes through params and makes sure that the fileholder's file name info
248 * associated to the blob field BLOB_COLUMN is stored in the NAME_COLUMN
249 * field. The same is with MIME_COLUMN and SIZE_COLUMN fields.
250 *
251 * @param table DOCUMENT ME!
252 * @param fieldValues DOCUMENT ME!
253 */
254 private void assignBlobData(Table table,
255 FieldValues fieldValues) {
256 for (Iterator iter = blobFieldData.values()
257 .iterator(); iter.hasNext();) {
258 String[] s = (String[]) iter.next();
259 FieldValue fv = fieldValues.get(s[BLOB_COL]);
260
261 if (fv != null) {
262 Object o = fv.getFieldValueAsObject();
263
264 if ((o != null) && o instanceof FileHolder) {
265 String fileName = ((FileHolder) o).getFileName();
266 String contentType = ((FileHolder) o).getContentType();
267 int fileLength = ((FileHolder) o).getFileLength();
268 setValue(table, fieldValues, s[NAME_COL], fileName);
269
270 if (s[MIME_COL] != null) {
271 setValue(table, fieldValues, s[MIME_COL], contentType);
272 }
273
274 if (s[SIZE_COL] != null) {
275 setValue(table, fieldValues, s[SIZE_COL],
276 String.valueOf(fileLength));
277 }
278 }
279 }
280 }
281 }
282 }