This repository has been archived by the owner on Nov 16, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
AbstractFileUpload.java
293 lines (257 loc) · 10 KB
/
AbstractFileUpload.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* JBoss, Home of Professional Open Source
* Copyright ${year}, Red Hat, Inc. and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.richfaces.component;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ComponentSystemEvent;
import javax.faces.event.FacesEvent;
import javax.faces.event.ListenerFor;
import javax.faces.event.PostAddToViewEvent;
import org.richfaces.cdk.annotations.Attribute;
import org.richfaces.cdk.annotations.EventName;
import org.richfaces.cdk.annotations.JsfComponent;
import org.richfaces.cdk.annotations.JsfRenderer;
import org.richfaces.cdk.annotations.Tag;
import org.richfaces.event.FileUploadEvent;
import org.richfaces.event.FileUploadListener;
import org.richfaces.model.UploadedFile;
import org.richfaces.renderkit.RenderKitUtils;
/**
* <p> The <rich:fileUpload> component allows the user to upload files to a server. It features multiple uploads,
* progress bars, restrictions on file types, and restrictions on sizes of the files to be uploaded. </p>
*
* @author Konstantin Mishin
*/
@JsfComponent(tag = @Tag(generate = false, handler = "org.richfaces.view.facelets.FileUploadHandler"), renderer = @JsfRenderer(type = "org.richfaces.FileUploadRenderer"), attributes = {
"events-mouse-props.xml", "events-key-props.xml", "core-props.xml", "ajax-props.xml", "i18n-props.xml", "fileUploadListener-props.xml" })
@ListenerFor(systemEventClass = PostAddToViewEvent.class)
public abstract class AbstractFileUpload extends UIComponentBase {
public static final String COMPONENT_TYPE = "org.richfaces.FileUpload";
public static final String COMPONENT_FAMILY = "org.richfaces.FileUpload";
private int queuedFileUploadEvents = 0;
/**
* Defines comma separated list of file extensions accepted by component.
* The component does not provide any feedback when rejecting file.
* For introducing feedback for rejection, use ontyperejected parameter.
*/
@Attribute
public abstract String getAcceptedTypes();
/**
* Defines maximum number of files allowed to be uploaded. After a number of files in the list equals to the value
* of this attribute, "Add" button disappears and nothing could be uploaded even if you clear the whole list.
* In order to upload files again you should rerender the component. (Negative numbers means no limits; default value -1).
*/
@Attribute(defaultValue = "-1")
public abstract Integer getMaxFilesQuantity();
/**
* If "true", this component is disabled
*/
@Attribute(defaultValue = "false")
public abstract boolean isDisabled();
/**
* <p>If "true" duplicate file uploads are prevented</p>
* <p>Default is "false"</p>
*/
@Attribute(defaultValue = "false")
public abstract boolean isNoDuplicate();
/**
* <p>If "true" upload stats immediately after file selection</p>
* <p>Default is "false"</p>
*/
@Attribute(defaultValue = "false")
public abstract boolean isImmediateUpload();
/**
* Javascript code executed when a file is selected
*/
@Attribute(events = @EventName("fileselect"))
public abstract String getOnfileselect();
/**
* Javascript code executed when a file is submitted
*/
@Attribute(events = @EventName("filesubmit"))
public abstract String getOnfilesubmit();
/**
* Javascript code executed when a file does not meet the conditions stated by acceptedTypes parameter.
*/
@Attribute(events = @EventName("typerejected"))
public abstract String getOntyperejected();
/**
* Javascript code executed when a file upload is complete
*/
@Attribute(events = @EventName("uploadcomplete"))
public abstract String getOnuploadcomplete();
/**
* Javascript code executed when a file is cleared
*/
@Attribute(events = @EventName("clear"))
public abstract String getOnclear();
/**
* The label for the Add button.
*/
@Attribute
public abstract String getAddLabel();
/**
* The label for the Add button.
*/
@Attribute
public abstract String getUploadLabel();
/**
* The label for the Clear button.
*/
@Attribute
public abstract String getClearAllLabel();
/**
* The label displayed when a file is successfully uploaded.
*/
@Attribute
public abstract String getDoneLabel();
/**
* The label displayed when a file exceeds the size limit.
*/
@Attribute
public abstract String getSizeExceededLabel();
/**
* The label displayed when a file upload fails due to a server error.
*/
@Attribute
public abstract String getServerErrorLabel();
/**
* The label displayed for the Clear link
*/
@Attribute
public abstract String getClearLabel();
/**
* The label displayed for the Delete link
*/
@Attribute
public abstract String getDeleteLabel();
/**
* <p>Defines height of file list.</p>
* <p>Default value is "210px".</p>
*/
@Attribute(defaultValue = "210px")
public abstract String getListHeight();
@Override
public void processEvent(ComponentSystemEvent event) throws AbortProcessingException {
super.processEvent(event);
FacesContext context = getFacesContext();
Map<String, UIComponent> facets = getFacets();
UIComponent component = facets.get("progress");
if (component == null) {
try {
component = context.getApplication().createComponent(context, "org.richfaces.ProgressBar",
"org.richfaces.ProgressBarRenderer");
} catch (FacesException e) {
// To work without ProgressBar.
}
if (component != null) {
component.setId(getId() + "_pb");
facets.put("progress", component);
}
}
if (component != null) {
String resourcePath = RenderKitUtils.getResourcePath(context, "org.richfaces", "fileUploadProgress");
component.getAttributes().put("resource", resourcePath);
}
}
/**
* <p>
* Add a new {@link FileUploadListener} to the set of listeners interested in being notified when
* {@link org.richfaces.FileUploadEvent}s occur.
* </p>
*
* @param listener The {@link FileUploadListener} to be added
* @throws NullPointerException if <code>listener</code> is <code>null</code>
*/
public void addFileUploadListener(FileUploadListener listener) {
addFacesListener(listener);
}
/**
* <p>
* Return the set of registered {@link FileUploadListener}s for this {@link AbstractFileUpload} instance. If there are no
* registered listeners, a zero-length array is returned.
* </p>
*/
public FileUploadListener[] getFileUploadListeners() {
return (FileUploadListener[]) getFacesListeners(FileUploadListener.class);
}
/**
* <p>
* Remove an existing {@link FileUploadListener} (if any) from the set of listeners interested in being notified when
* {@link FileUploadListener}s occur.
* </p>
*
* @param listener The {@link FileUploadListener} to be removed
* @throws NullPointerException if <code>listener</code> is <code>null</code>
*/
public void removeFileUploadListener(FileUploadListener listener) {
removeFacesListener(listener);
}
/**
* Get a list of accepted types from {@link #getAcceptedTypes()} attribute.
*/
public List<String> getAcceptedTypesList() {
String acceptedTypes = this.getAcceptedTypes();
if (acceptedTypes != null) {
return Arrays.asList(acceptedTypes.toLowerCase().replaceAll("\\s+", "").split(","));
} else {
return Collections.emptyList();
}
}
/**
* Checks whether this component can accept given {@link UploadedFile}..
*
* First, the number of enqueued {@link FileUploadEvent} events can't exceed {@link #getMaxFilesQuantity()}.
*
* Then, the file extension of uploaded file needs to be acceptable by this component (see {@link #getAcceptedTypes()}).
*/
public boolean acceptsFile(UploadedFile file) {
final String clientId = this.getClientId();
final int maxFilesQuantity = this.getMaxFilesQuantity();
final List<String> acceptedTypes = this.getAcceptedTypesList();
if ((maxFilesQuantity > 0) && (queuedFileUploadEvents >= maxFilesQuantity))
return false;
if (clientId.equals(file.getParameterName())) {
if (acceptedTypes.isEmpty()) {
return true;
}
if (acceptedTypes.contains(file.getFileExtension().toLowerCase())) {
return true;
}
}
return false;
}
@Override
public void queueEvent(FacesEvent event) {
if (event instanceof FileUploadEvent) {
queuedFileUploadEvents += 1;
}
super.queueEvent(event);
}
}