View Javadoc
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$
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          super(field, group);
54      }
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          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       * 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          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       * A suitable validator.
100      *
101      * @return A suitable validator
102      */
103     @Override
104 	protected String getDefaultValidator()
105     {
106         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         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      * 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         ParameterParser pp = (ParameterParser) super.parser;
149         if (isMultiValued())
150         {
151             Part[] ss = pp.getParts(getKey());
152             // this definition of not set might need refined.  But
153             // not sure the situation will arise.
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      * Sets the value of the field from data in the parser.
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 }