1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.validator;
21
22 import org.apache.commons.fileupload.FileItem;
23 import org.apache.myfaces.tobago.internal.component.AbstractUIFile;
24 import org.apache.myfaces.tobago.internal.util.ContentType;
25 import org.apache.myfaces.tobago.util.MessageUtils;
26
27 import javax.faces.application.FacesMessage;
28 import javax.faces.component.StateHolder;
29 import javax.faces.component.UIComponent;
30 import javax.faces.context.FacesContext;
31 import javax.faces.validator.Validator;
32 import javax.faces.validator.ValidatorException;
33 import java.util.Arrays;
34
35
36
37
38
39 @org.apache.myfaces.tobago.apt.annotation.Validator(id = FileItemValidator.VALIDATOR_ID)
40 public class FileItemValidator implements Validator, StateHolder {
41 public static final String VALIDATOR_ID = "org.apache.myfaces.tobago.FileItem";
42 public static final String SIZE_LIMIT_MESSAGE_ID = "org.apache.myfaces.tobago.FileItemValidator.SIZE_LIMIT";
43 public static final String CONTENT_TYPE_MESSAGE_ID = "org.apache.myfaces.tobago.FileItemValidator.CONTENT_TYPE";
44 private Integer maxSize = null;
45 private String[] contentType;
46 private boolean transientValue;
47
48
49
50
51 public FileItemValidator() {
52 }
53
54 public void validate(FacesContext facesContext, UIComponent component, Object value) throws ValidatorException {
55 if (value != null && component instanceof AbstractUIFile) {
56 FileItem file = (FileItem) value;
57 if (maxSize != null && file.getSize() > maxSize) {
58 FacesMessage facesMessage = MessageUtils.getMessage(
59 facesContext, facesContext.getViewRoot().getLocale(), FacesMessage.SEVERITY_ERROR,
60 SIZE_LIMIT_MESSAGE_ID, maxSize, component.getId());
61 throw new ValidatorException(facesMessage);
62 }
63
64 if (file.getSize() > 0 && contentType != null && contentType.length > 0) {
65 boolean found = false;
66 for (String contentTypeStr : contentType) {
67 if (ContentType.valueOf(contentTypeStr).match(ContentType.valueOf(file.getContentType()))) {
68 found = true;
69 break;
70 }
71 }
72 if (!found) {
73 String message;
74 if (contentType.length == 1) {
75 message = contentType[0];
76 } else {
77 message = Arrays.toString(contentType);
78 }
79 FacesMessage facesMessage = MessageUtils.getMessage(
80 facesContext, facesContext.getViewRoot().getLocale(), FacesMessage.SEVERITY_ERROR,
81 CONTENT_TYPE_MESSAGE_ID, message, component.getId());
82 throw new ValidatorException(facesMessage);
83 }
84 }
85 }
86 }
87
88 public int getMaxSize() {
89 return maxSize;
90 }
91
92 public void setMaxSize(int maxSize) {
93 if (maxSize > 0) {
94 this.maxSize = maxSize;
95 }
96 }
97
98 public String[] getContentType() {
99 return contentType;
100 }
101
102 public void setContentType(String[] contentType) {
103 this.contentType = contentType;
104 }
105
106 public Object saveState(FacesContext context) {
107 Object[] values = new Object[2];
108 values[0] = maxSize;
109 values[1] = contentType;
110 return values;
111 }
112
113 public void restoreState(FacesContext context, Object state) {
114 Object[] values = (Object[]) state;
115 maxSize = (Integer) values[0];
116 contentType = (String[]) values[1];
117 }
118
119 public boolean isTransient() {
120 return transientValue;
121 }
122
123 public void setTransient(boolean newTransientValue) {
124 this.transientValue = newTransientValue;
125 }
126 }