]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
ee1d43b7138d09b64cff6779d745ad87b35932ec
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  */
24
25 package org.collectionspace.services.authorization.storage;
26
27 import org.collectionspace.services.authorization.perms.Permission;
28 import org.collectionspace.services.common.ServiceMessages;
29 import org.collectionspace.services.common.context.ServiceContext;
30 import org.collectionspace.services.common.document.DocumentHandler.Action;
31 import org.collectionspace.services.common.document.InvalidDocumentException;
32 import org.collectionspace.services.common.document.ValidatorHandler;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * PermissionValidatorHandler executes validation rules for permission
38  * @author 
39  */
40 public class PermissionValidatorHandler implements ValidatorHandler {
41
42     final Logger logger = LoggerFactory.getLogger(PermissionValidatorHandler.class);
43
44     @Override
45     public void validate(Action action, ServiceContext ctx)
46             throws InvalidDocumentException {
47         if (logger.isDebugEnabled()) {
48             logger.debug("validate() action=" + action.name());
49         }
50         try {
51             Permission permission = (Permission) ctx.getInput();
52             StringBuilder msgBldr = new StringBuilder(ServiceMessages.VALIDATION_FAILURE);
53             boolean invalid = false;
54
55             if (action.equals(Action.CREATE)) {
56
57                 //create specific validation here
58                 if (permission.getResourceName() == null || permission.getResourceName().isEmpty()) {
59                     invalid = true;
60                     msgBldr.append("\nresourceName : missing or empty");
61                 }
62             } else if (action.equals(Action.UPDATE)) {
63                 //update specific validation here
64                 if (permission.getResourceName() == null || permission.getResourceName().isEmpty()) {
65                     invalid = true;
66                     msgBldr.append("\nresourceName : cannot be missing or empty");
67                 }
68             }
69             if (invalid) {
70                 String msg = msgBldr.toString();
71                 logger.error(msg);
72                 throw new InvalidDocumentException(msg);
73             }
74         } catch (InvalidDocumentException ide) {
75             throw ide;
76         } catch (Exception e) {
77             throw new InvalidDocumentException(e);
78         }
79     }
80
81 }