Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
UploadPartField |
|
| 4.0;4 |
1 | package org.apache.fulcrum.intake.model; | |
2 | ||
3 | /* | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
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 | * @author <a href="mailto:jmcnally@collab.net">John McNally</a> | |
33 | * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> | |
34 | * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a> | |
35 | * @version $Id: UploadPartField.java 1837232 2018-08-01 12:30:05Z tv $ | |
36 | */ | |
37 | public class UploadPartField | |
38 | extends Field<Part> | |
39 | { | |
40 | /** Serial version */ | |
41 | private static final long serialVersionUID = -963692413506822188L; | |
42 | ||
43 | /** | |
44 | * Constructor. | |
45 | * | |
46 | * @param field xml field definition object | |
47 | * @param group xml group definition object | |
48 | * @throws IntakeException thrown by superclass | |
49 | */ | |
50 | public UploadPartField(XmlField field, Group group) | |
51 | throws IntakeException | |
52 | { | |
53 | 1 | super(field, group); |
54 | 1 | } |
55 | ||
56 | /** | |
57 | * It is not possible to set the default value for this field type. | |
58 | * Calling this method with a non-null parameter will result in a | |
59 | * IntakeRuntimeException | |
60 | * | |
61 | * @param prop Parameter for the default values | |
62 | * @throws IntakeRuntimeException if the parameter is not null | |
63 | */ | |
64 | @Override | |
65 | public void setDefaultValue(String prop) | |
66 | { | |
67 | 1 | if (prop != null) |
68 | { | |
69 | 0 | throw new IntakeRuntimeException( |
70 | "Default values are not valid for " | |
71 | 0 | + this.getClass().getName()); |
72 | } | |
73 | ||
74 | 1 | defaultValue = null; |
75 | 1 | } |
76 | ||
77 | /** | |
78 | * It is not possible to set the empty value for this field type. | |
79 | * Calling this method with a non-null parameter will result in a | |
80 | * IntakeRuntimeException | |
81 | * | |
82 | * @param prop Parameter for the empty values | |
83 | * @throws IntakeRuntimeException if the parameter is not null | |
84 | */ | |
85 | @Override | |
86 | public void setEmptyValue(String prop) | |
87 | { | |
88 | 1 | if (prop != null) |
89 | { | |
90 | 0 | throw new IntakeRuntimeException( |
91 | "Empty values are not valid for " | |
92 | 0 | + this.getClass().getName()); |
93 | } | |
94 | ||
95 | 1 | emptyValue = null; |
96 | 1 | } |
97 | ||
98 | /** | |
99 | * A suitable validator. | |
100 | * | |
101 | * @return A suitable validator | |
102 | */ | |
103 | @Override | |
104 | protected String getDefaultValidator() | |
105 | { | |
106 | 1 | return FileValidator.class.getName(); |
107 | } | |
108 | ||
109 | /** | |
110 | * Method called when this field (the group it belongs to) is | |
111 | * pulled from the pool. The request data is searched to determine | |
112 | * if a value has been supplied for this field. if so, the value | |
113 | * is validated. | |
114 | * | |
115 | * @param vp a <code>ValueParser</code> value | |
116 | * @return a <code>Field</code> value | |
117 | * @throws IntakeException if an error occurs | |
118 | */ | |
119 | @Override | |
120 | public Field<Part> init(ValueParser vp) | |
121 | throws IntakeException | |
122 | { | |
123 | 2 | if (!(vp instanceof ParameterParser)) |
124 | { | |
125 | 0 | throw new IntakeException( |
126 | "UploadPartFields can only be used with ParameterParser"); | |
127 | } | |
128 | ||
129 | 2 | super.init(vp); |
130 | ||
131 | 2 | if (parser.containsKey(getKey())) |
132 | { | |
133 | 0 | setSet(true); |
134 | 0 | validate(); |
135 | } | |
136 | ||
137 | 2 | return this; |
138 | } | |
139 | ||
140 | /** | |
141 | * Compares request data with constraints and sets the valid flag. | |
142 | * | |
143 | * @return the valid flag | |
144 | */ | |
145 | @Override | |
146 | public boolean validate() | |
147 | { | |
148 | 0 | ParameterParser pp = (ParameterParser) super.parser; |
149 | 0 | if (isMultiValued()) |
150 | { | |
151 | 0 | Part[] ss = pp.getParts(getKey()); |
152 | // this definition of not set might need refined. But | |
153 | // not sure the situation will arise. | |
154 | 0 | if (ss.length == 0) |
155 | { | |
156 | 0 | setSet(false); |
157 | } | |
158 | ||
159 | 0 | if (getValidator() != null) |
160 | { | |
161 | 0 | for (int i = 0; i < ss.length; i++) |
162 | { | |
163 | try | |
164 | { | |
165 | 0 | ((FileValidator) getValidator()).assertValidity(ss[i]); |
166 | } | |
167 | 0 | catch (ValidationException ve) |
168 | { | |
169 | 0 | setMessage(ve.getMessage()); |
170 | 0 | } |
171 | } | |
172 | } | |
173 | ||
174 | 0 | if (isSet() && isValid()) |
175 | { | |
176 | 0 | doSetValue(); |
177 | } | |
178 | 0 | } |
179 | else | |
180 | { | |
181 | 0 | Part s = pp.getPart(getKey()); |
182 | 0 | if (s == null || s.getSize() == 0) |
183 | { | |
184 | 0 | setSet(false); |
185 | } | |
186 | ||
187 | 0 | if (getValidator() != null) |
188 | { | |
189 | try | |
190 | { | |
191 | 0 | ((FileValidator) getValidator()).assertValidity(s); |
192 | ||
193 | 0 | if (isSet()) |
194 | { | |
195 | 0 | doSetValue(); |
196 | } | |
197 | } | |
198 | 0 | catch (ValidationException ve) |
199 | { | |
200 | 0 | setMessage(ve.getMessage()); |
201 | 0 | } |
202 | } | |
203 | 0 | else if (isSet()) |
204 | { | |
205 | 0 | doSetValue(); |
206 | } | |
207 | } | |
208 | ||
209 | 0 | return isValid(); |
210 | } | |
211 | ||
212 | /** | |
213 | * Sets the value of the field from data in the parser. | |
214 | */ | |
215 | @Override | |
216 | protected void doSetValue() | |
217 | { | |
218 | 0 | ParameterParser pp = (ParameterParser) super.parser; |
219 | 0 | if (isMultiValued()) |
220 | { | |
221 | 0 | setTestValue(pp.getParts(getKey())); |
222 | } | |
223 | else | |
224 | { | |
225 | 0 | setTestValue(pp.getPart(getKey())); |
226 | } | |
227 | 0 | } |
228 | } |