View Javadoc

1   /* Copyright 2004 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package sample.contact;
17  
18  import org.acegisecurity.acl.basic.SimpleAclEntry;
19  
20  import org.springframework.validation.Errors;
21  import org.springframework.validation.ValidationUtils;
22  import org.springframework.validation.Validator;
23  
24  
25  /***
26   * Validates {@link AddPermission}.
27   *
28   * @author Ben Alex
29   * @version $Id: AddPermissionValidator.java,v 1.2 2005/11/17 00:56:08 benalex Exp $
30   */
31  public class AddPermissionValidator implements Validator {
32      //~ Methods ================================================================
33  
34      public boolean supports(Class clazz) {
35          return clazz.equals(AddPermission.class);
36      }
37  
38      public void validate(Object obj, Errors errors) {
39          AddPermission addPermission = (AddPermission) obj;
40  
41          ValidationUtils.rejectIfEmptyOrWhitespace(errors, "permission",
42              "err.permission", "Permission is required");
43          ValidationUtils.rejectIfEmptyOrWhitespace(errors, "recipient",
44              "err.recipient", "Recipient is required");
45  
46          if (addPermission.getPermission() != null) {
47              int permission = addPermission.getPermission().intValue();
48  
49              if ((permission != SimpleAclEntry.NOTHING)
50                  && (permission != SimpleAclEntry.ADMINISTRATION)
51                  && (permission != SimpleAclEntry.READ)
52                  && (permission != SimpleAclEntry.DELETE)
53                  && (permission != SimpleAclEntry.READ_WRITE_DELETE)) {
54                  errors.rejectValue("permission", "err.permission.invalid",
55                      "The indicated permission is invalid.");
56              }
57          }
58  
59          if (addPermission.getRecipient() != null) {
60              if (addPermission.getRecipient().length() > 100) {
61                  errors.rejectValue("recipient", "err.recipient.length",
62                      "The recipient is too long (maximum 100 characters).");
63              }
64          }
65      }
66  }