View Javadoc
1   package org.apache.fulcrum.intake.validator;
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 java.io.IOException;
23  import java.io.InputStream;
24  import java.io.UnsupportedEncodingException;
25  import java.nio.charset.Charset;
26  import java.util.regex.Matcher;
27  import java.util.regex.Pattern;
28  
29  import javax.servlet.http.Part;
30  
31  /**
32   * A validator that will compare a Part testValue against the following
33   * constraints in addition to those listed in DefaultValidator.
34   *
35   * This validator can serve as the base class for more specific validators
36   *
37   * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
38   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
39   * @author <a href="mailto:Colin.Chalmers@maxware.nl">Colin Chalmers</a>
40   * @version $Id$
41   */
42  public class FileValidator
43          extends DefaultValidator<Part>
44  {
45      private final static Pattern charsetPattern = Pattern.compile(".+charset\\s*=\\s*(.+)");
46  
47      /**
48       * Default constructor
49       */
50      public FileValidator()
51      {
52          super();
53      }
54  
55      /**
56       * Determine whether a testValue meets the criteria specified
57       * in the constraints defined for this validator
58       *
59       * @param testValue a <code>Part</code> to be tested
60       * @throws ValidationException containing an error message if the
61       * testValue did not pass the validation tests.
62       */
63      public void assertValidity(Part testValue)
64              throws ValidationException
65      {
66          byte[] fileData = new byte[(int) testValue.getSize()];
67          String contentType = testValue.getContentType();
68          String charset = Charset.defaultCharset().name();
69  
70          if (contentType.contains("charset"))
71          {
72              Matcher matcher = charsetPattern.matcher(contentType);
73              if (matcher.matches())
74              {
75                  charset = matcher.group(1);
76              }
77          }
78  
79          try (InputStream fis = testValue.getInputStream())
80          {
81              int byteSize = fis.read(fileData);
82              if ( fileData.length != byteSize )
83              {
84              	throw new ValidationException("Byte length mismatch found");
85              }
86              
87          }
88          catch (IOException e)
89          {
90              fileData = null;
91          }
92  
93          String content = null;
94          try
95          {
96          	if ( fileData != null )
97          	{
98          		content = new String(fileData, charset);
99          	}
100         }
101         catch (UnsupportedEncodingException e)
102         {
103             throw new ValidationException("Invalid charset " + charset);
104         }
105 
106         super.assertValidity(content);
107     }
108 }