KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > forms > FileUploadForm


1 /*
2  * $Id: FileUploadForm.java,v 1.3 2005/06/07 12:32:17 bel70 Exp $
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License
8  * at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific language governing rights and
13  * limitations under the License.
14  *
15  * The Original Code is JGossip forum code.
16  *
17  * The Initial Developer of the Original Code is the JResearch, Org.
18  * Portions created by the Initial Developer are Copyright (C) 2004
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Dmitriy Belov <bel@jresearch.org>
23  * .
24  * * ***** END LICENSE BLOCK ***** */

25 /*
26  * Created on 01.07.2004
27  *
28  */

29 package org.jresearch.gossip.forms;
30
31 import java.util.ArrayList JavaDoc;
32 import java.util.List JavaDoc;
33
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35
36 import org.apache.struts.action.ActionError;
37 import org.apache.struts.action.ActionErrors;
38 import org.apache.struts.action.ActionMapping;
39 import org.apache.struts.upload.FormFile;
40 import org.apache.struts.upload.MultipartRequestHandler;
41 import org.jresearch.gossip.IConst;
42 import org.jresearch.gossip.configuration.Configurator;
43 import org.jresearch.gossip.exception.ConfiguratorException;
44
45 /**
46  * @author Dmitry Belov
47  *
48  */

49 public class FileUploadForm extends MessageForm {
50
51     private FormFile[] file;
52
53     private String JavaDoc[] fdesc;
54
55     private int maxFileCount;
56
57     /**
58      *
59      */

60     public FileUploadForm() {
61         Configurator config = Configurator.getInstance();
62         try {
63             this.maxFileCount = config
64                     .getInt(IConst.CONFIG.MAX_ATTACHMENT_COUNT);
65         } catch (ConfiguratorException e) {
66             e.printStackTrace();
67             maxFileCount = 1;
68         }
69         this.file = new FormFile[maxFileCount];
70         this.fdesc = new String JavaDoc[maxFileCount];
71     }
72
73     /**
74      * @return
75      */

76     public List JavaDoc[] getFileData() {
77         List JavaDoc result[] = { new ArrayList JavaDoc(), new ArrayList JavaDoc() };
78         for (int i = 0; i < this.file.length; i++) {
79             if (this.file[i] != null && this.file[i].getFileSize() > 0) {
80                 result[0].add(this.file[i]);
81                 result[1].add(this.fdesc[i]);
82             }
83         }
84         return result;
85     }
86
87     /**
88      * @return Returns the desc.
89      */

90     public String JavaDoc getDesc(int key) {
91         return this.fdesc[key];
92     }
93
94     /**
95      * @param desc
96      * The desc to set.
97      */

98     public void setDesc(int key, String JavaDoc desc) {
99         this.fdesc[key] = desc;
100     }
101
102     /**
103      * @param file
104      * The file to set.
105      */

106     public void setFile(int key, FormFile file) {
107         this.file[key] = file;
108     }
109
110     /**
111      * @return Returns the file.
112      */

113     public FormFile getFile(int key) {
114         return this.file[key];
115     }
116
117     /**
118      *
119      * /** Check to make sure the client hasn't exceeded the maximum allowed
120      * upload size inside of this validate method.
121      */

122     public ActionErrors validate(ActionMapping mapping,
123             HttpServletRequest JavaDoc request) {
124         ActionErrors errors = super.validate(mapping, request);
125         // has the maximum length been exceeded
126
Boolean JavaDoc maxLengthExceeded = (Boolean JavaDoc) request
127                 .getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
128         if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue())) {
129             errors = new ActionErrors();
130             errors.add("maxLengthExceeded", new ActionError(
131                     "error.maxLengthExceeded", "maxLengthExceeded"));
132         }
133         return errors;
134
135     }
136
137 }
Popular Tags