1 package org.apache.fulcrum.intake.model;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import javax.servlet.http.Part;
23
24 import org.apache.fulcrum.intake.IntakeException;
25 import org.apache.fulcrum.intake.IntakeRuntimeException;
26 import org.apache.fulcrum.intake.validator.FileValidator;
27 import org.apache.fulcrum.intake.validator.ValidationException;
28 import org.apache.fulcrum.parser.ParameterParser;
29 import org.apache.fulcrum.parser.ValueParser;
30
31
32
33
34
35
36
37 public class UploadPartField
38 extends Field<Part>
39 {
40
41 private static final long serialVersionUID = -963692413506822188L;
42
43
44
45
46
47
48
49
50 public UploadPartField(XmlField field, Group group)
51 throws IntakeException
52 {
53 super(field, group);
54 }
55
56
57
58
59
60
61
62
63
64 @Override
65 public void setDefaultValue(String prop)
66 {
67 if (prop != null)
68 {
69 throw new IntakeRuntimeException(
70 "Default values are not valid for "
71 + this.getClass().getName());
72 }
73
74 defaultValue = null;
75 }
76
77
78
79
80
81
82
83
84
85 @Override
86 public void setEmptyValue(String prop)
87 {
88 if (prop != null)
89 {
90 throw new IntakeRuntimeException(
91 "Empty values are not valid for "
92 + this.getClass().getName());
93 }
94
95 emptyValue = null;
96 }
97
98
99
100
101
102
103 @Override
104 protected String getDefaultValidator()
105 {
106 return FileValidator.class.getName();
107 }
108
109
110
111
112
113
114
115
116
117
118
119 @Override
120 public Field<Part> init(ValueParser vp)
121 throws IntakeException
122 {
123 if (!(vp instanceof ParameterParser))
124 {
125 throw new IntakeException(
126 "UploadPartFields can only be used with ParameterParser");
127 }
128
129 super.init(vp);
130
131 if (parser.containsKey(getKey()))
132 {
133 setSet(true);
134 validate();
135 }
136
137 return this;
138 }
139
140
141
142
143
144
145 @Override
146 public boolean validate()
147 {
148 ParameterParser pp = (ParameterParser) super.parser;
149 if (isMultiValued())
150 {
151 Part[] ss = pp.getParts(getKey());
152
153
154 if (ss.length == 0)
155 {
156 setSet(false);
157 }
158
159 if (getValidator() != null)
160 {
161 for (int i = 0; i < ss.length; i++)
162 {
163 try
164 {
165 ((FileValidator) getValidator()).assertValidity(ss[i]);
166 }
167 catch (ValidationException ve)
168 {
169 setMessage(ve.getMessage());
170 }
171 }
172 }
173
174 if (isSet() && isValid())
175 {
176 doSetValue();
177 }
178 }
179 else
180 {
181 Part s = pp.getPart(getKey());
182 if (s == null || s.getSize() == 0)
183 {
184 setSet(false);
185 }
186
187 if (getValidator() != null)
188 {
189 try
190 {
191 ((FileValidator) getValidator()).assertValidity(s);
192
193 if (isSet())
194 {
195 doSetValue();
196 }
197 }
198 catch (ValidationException ve)
199 {
200 setMessage(ve.getMessage());
201 }
202 }
203 else if (isSet())
204 {
205 doSetValue();
206 }
207 }
208
209 return isValid();
210 }
211
212
213
214
215 @Override
216 protected void doSetValue()
217 {
218 ParameterParser pp = (ParameterParser) super.parser;
219 if (isMultiValued())
220 {
221 setTestValue(pp.getParts(getKey()));
222 }
223 else
224 {
225 setTestValue(pp.getPart(getKey()));
226 }
227 }
228 }