View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.validator;
21  
22  import org.apache.myfaces.tobago.internal.component.AbstractUIFile;
23  import org.apache.myfaces.tobago.internal.util.ContentType;
24  import org.apache.myfaces.tobago.util.MessageUtils;
25  
26  import javax.faces.application.FacesMessage;
27  import javax.faces.component.StateHolder;
28  import javax.faces.component.UIComponent;
29  import javax.faces.context.FacesContext;
30  import javax.faces.validator.Validator;
31  import javax.faces.validator.ValidatorException;
32  import javax.servlet.http.Part;
33  import java.util.Arrays;
34  
35  /**
36   * <p><strong>FileItemValidator</strong> is a {@link Validator} that checks
37   * the FileItem in the value of the associated component.
38   */
39  @org.apache.myfaces.tobago.apt.annotation.Validator(id = FileItemValidator.VALIDATOR_ID)
40  public class FileItemValidator implements Validator, StateHolder {
41    public static final String VALIDATOR_ID = "org.apache.myfaces.tobago.FileItem";
42    public static final String SIZE_LIMIT_MESSAGE_ID = "org.apache.myfaces.tobago.FileItemValidator.SIZE_LIMIT";
43    public static final String CONTENT_TYPE_MESSAGE_ID = "org.apache.myfaces.tobago.FileItemValidator.CONTENT_TYPE";
44    private Integer maxSize = null;
45    private String[] contentType;
46    private boolean transientValue;
47  
48    /**
49     * No-arg constructor used during restoreState
50     */
51    public FileItemValidator() {
52    }
53  
54    @Override
55    public void validate(final FacesContext facesContext, final UIComponent component, final Object value)
56        throws ValidatorException {
57      if (value != null && component instanceof AbstractUIFile) {
58        final Part file = (Part) value;
59        if (maxSize != null && file.getSize() > maxSize) {
60          final FacesMessage facesMessage = MessageUtils.getMessage(
61              facesContext, facesContext.getViewRoot().getLocale(), FacesMessage.SEVERITY_ERROR,
62              SIZE_LIMIT_MESSAGE_ID, maxSize, component.getId());
63          throw new ValidatorException(facesMessage);
64        }
65        // Check only a valid file
66        if (file.getSize() > 0 && contentType != null && contentType.length > 0) {
67          boolean found = false;
68          for (final String contentTypeStr : contentType) {
69            if (ContentType.valueOf(contentTypeStr).match(ContentType.valueOf(file.getContentType()))) {
70              found = true;
71              break;
72            }
73          }
74          if (!found) {
75            final String message;
76            if (contentType.length == 1) {
77              message = contentType[0];
78            } else {
79              message = Arrays.toString(contentType);
80            }
81            final FacesMessage facesMessage = MessageUtils.getMessage(
82                facesContext, facesContext.getViewRoot().getLocale(), FacesMessage.SEVERITY_ERROR,
83                CONTENT_TYPE_MESSAGE_ID, message, component.getId());
84            throw new ValidatorException(facesMessage);
85          }
86        }
87      }
88    }
89  
90    public int getMaxSize() {
91      return maxSize;
92    }
93  
94    public void setMaxSize(final int maxSize) {
95      if (maxSize > 0) {
96        this.maxSize = maxSize;
97      }
98    }
99  
100   public String[] getContentType() {
101     return contentType;
102   }
103 
104   public void setContentType(final String[] contentType) {
105     this.contentType = contentType;
106   }
107 
108   @Override
109   public Object saveState(final FacesContext context) {
110     final Object[] values = new Object[2];
111     values[0] = maxSize;
112     values[1] = contentType;
113     return values;
114   }
115 
116   @Override
117   public void restoreState(final FacesContext context, final Object state) {
118     final Object[] values = (Object[]) state;
119     maxSize = (Integer) values[0];
120     contentType = (String[]) values[1];
121   }
122 
123   @Override
124   public boolean isTransient() {
125     return transientValue;
126   }
127 
128   @Override
129   public void setTransient(final boolean newTransientValue) {
130     this.transientValue = newTransientValue;
131   }
132 }