2
0

AddPermissionValidator.java 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* Copyright 2004, 2005, 2006 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. package sample.contact;
  16. import org.acegisecurity.acl.basic.SimpleAclEntry;
  17. import org.springframework.validation.Errors;
  18. import org.springframework.validation.ValidationUtils;
  19. import org.springframework.validation.Validator;
  20. /**
  21. * Validates {@link AddPermission}.
  22. *
  23. * @author Ben Alex
  24. * @version $Id$
  25. */
  26. public class AddPermissionValidator implements Validator {
  27. //~ Methods ========================================================================================================
  28. public boolean supports(Class clazz) {
  29. return clazz.equals(AddPermission.class);
  30. }
  31. public void validate(Object obj, Errors errors) {
  32. AddPermission addPermission = (AddPermission) obj;
  33. ValidationUtils.rejectIfEmptyOrWhitespace(errors, "permission", "err.permission", "Permission is required. *");
  34. ValidationUtils.rejectIfEmptyOrWhitespace(errors, "recipient", "err.recipient", "Recipient is required. *");
  35. if (addPermission.getPermission() != null) {
  36. int permission = addPermission.getPermission().intValue();
  37. if ((permission != SimpleAclEntry.NOTHING) && (permission != SimpleAclEntry.ADMINISTRATION)
  38. && (permission != SimpleAclEntry.READ) && (permission != SimpleAclEntry.DELETE)
  39. && (permission != SimpleAclEntry.READ_WRITE_DELETE)) {
  40. errors.rejectValue("permission", "err.permission.invalid", "The indicated permission is invalid. *");
  41. }
  42. }
  43. if (addPermission.getRecipient() != null) {
  44. if (addPermission.getRecipient().length() > 100) {
  45. errors.rejectValue("recipient", "err.recipient.length",
  46. "The recipient is too long (maximum 100 characters). *");
  47. }
  48. }
  49. }
  50. }