View Javadoc

1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.formengine.service.validator;
35  
36  import fr.paris.lutece.plugins.formengine.business.jaxb.formdefinition.Field;
37  import fr.paris.lutece.plugins.formengine.business.jaxb.formdefinition.FileName;
38  import fr.paris.lutece.plugins.formengine.web.FormErrorsList;
39  
40  import java.util.HashSet;
41  import java.util.Iterator;
42  import java.util.Set;
43  import java.util.StringTokenizer;
44  
45  
46  /**
47   * This class checks the file types for an upload field.
48   */
49  public class ValidatorFileTypes extends FieldValidator
50  {
51      private static final String PROPERTY_VALIDATION_MESSAGE = "formengine.validator.message.fileTypes";
52  
53      // The separators that can be used in the provided list of extensions
54      private static final String SEPARATORS = ",; ";
55  
56      /**
57       * The validate implementation.
58       * Check that the field does not contain files with other extensions than
59       * those specified
60       * @param field The field to validate
61       * @param errors A FormErrorsList to add errors into
62       * @return True if validate, false otherwise
63       */
64      public boolean validate( Field field, FormErrorsList errors )
65      {
66          // The parameter contains the list of allowed file types
67          String strFileTypes = this.getRuleParameter(  );
68  
69          // Parse the parameter and build a Set containing the extensions
70          Set<String> fileTypes = new HashSet<String>(  );
71          StringTokenizer parser = new StringTokenizer( strFileTypes, SEPARATORS );
72  
73          while ( parser.hasMoreTokens(  ) )
74          {
75              fileTypes.add( parser.nextToken(  ).toUpperCase(  ) );
76          }
77  
78          // Check if all files have a valid extension
79          boolean bValid = true;
80  
81          if ( field.getFileNames(  ) != null )
82          {
83              Iterator iterFileNames = field.getFileNames(  ).getFileName(  ).iterator(  );
84  
85              while ( bValid && iterFileNames.hasNext(  ) )
86              {
87                  String strFileName = ( (FileName) iterFileNames.next(  ) ).getValue(  );
88                  String strFileType = strFileName.substring( strFileName.lastIndexOf( "." ) + 1 ).toUpperCase(  );
89                  bValid = fileTypes.contains( strFileType );
90              }
91          }
92  
93          if ( !bValid )
94          {
95              String[] messageParams = new String[3];
96              messageParams[0] = field.getName(  );
97              messageParams[1] = field.getLabel(  );
98              messageParams[2] = strFileTypes;
99  
100             if ( this.getErrorMessage(  ) != null )
101             {
102                 errors.addErrorMessage( this.getErrorMessage(  ) );
103             }
104             else
105             {
106                 errors.addError( PROPERTY_VALIDATION_MESSAGE, messageParams );
107             }
108         }
109 
110         return bValid;
111     }
112 }