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.myfaces.tobago.internal.component.AbstractUIFile;
23 import org.apache.myfaces.tobago.internal.util.ContentType;
24 import org.apache.myfaces.tobago.util.MessageUtils;
25
26 import javax.faces.application.FacesMessage;
27 import javax.faces.component.StateHolder;
28 import javax.faces.component.UIComponent;
29 import javax.faces.context.FacesContext;
30 import javax.faces.validator.Validator;
31 import javax.faces.validator.ValidatorException;
32 import javax.servlet.http.Part;
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 @Override
55 public void validate(final FacesContext facesContext, final UIComponent component, final Object value)
56 throws ValidatorException {
57 if (value != null && component instanceof AbstractUIFile) {
58 final Part file = (Part) value;
59 if (maxSize != null && file.getSize() > maxSize) {
60 throw new ValidatorException(MessageUtils.getMessage(
61 facesContext, FacesMessage.SEVERITY_ERROR, SIZE_LIMIT_MESSAGE_ID, maxSize, component.getId()));
62 }
63
64 if (file.getSize() > 0 && contentType != null && contentType.length > 0) {
65 boolean found = false;
66 for (final String contentTypeStr : contentType) {
67 if (ContentType.valueOf(contentTypeStr).match(ContentType.valueOf(file.getContentType()))) {
68 found = true;
69 break;
70 }
71 }
72 if (!found) {
73 final String message;
74 if (contentType.length == 1) {
75 message = contentType[0];
76 } else {
77 message = Arrays.toString(contentType);
78 }
79 throw new ValidatorException(MessageUtils.getMessage(
80 facesContext, FacesMessage.SEVERITY_ERROR, CONTENT_TYPE_MESSAGE_ID, message, component.getId()));
81 }
82 }
83 }
84 }
85
86 public int getMaxSize() {
87 return maxSize;
88 }
89
90 public void setMaxSize(final int maxSize) {
91 if (maxSize > 0) {
92 this.maxSize = maxSize;
93 }
94 }
95
96 public String[] getContentType() {
97 return contentType;
98 }
99
100 public void setContentType(final String[] contentType) {
101 this.contentType = contentType;
102 }
103
104 @Override
105 public Object saveState(final FacesContext context) {
106 final Object[] values = new Object[2];
107 values[0] = maxSize;
108 values[1] = contentType;
109 return values;
110 }
111
112 @Override
113 public void restoreState(final FacesContext context, final Object state) {
114 final Object[] values = (Object[]) state;
115 maxSize = (Integer) values[0];
116 contentType = (String[]) values[1];
117 }
118
119 @Override
120 public boolean isTransient() {
121 return transientValue;
122 }
123
124 @Override
125 public void setTransient(final boolean newTransientValue) {
126 this.transientValue = newTransientValue;
127 }
128 }