]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
6391d067f5dc165ba44b1276464c0f450451be6b
[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  *  This document is a part of the source code and related artifacts
25  *  for CollectionSpace, an open source collections management system
26  *  for museums and related institutions:
27
28  *  http://www.collectionspace.org
29  *  http://wiki.collectionspace.org
30
31  *  Copyright 2009 University of California at Berkeley
32
33  *  Licensed under the Educational Community License (ECL), Version 2.0.
34  *  You may not use this file except in compliance with this License.
35
36  *  You may obtain a copy of the ECL 2.0 License at
37
38  *  https://source.collectionspace.org/collection-space/LICENSE.txt
39
40  *  Unless required by applicable law or agreed to in writing, software
41  *  distributed under the License is distributed on an "AS IS" BASIS,
42  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
43  *  See the License for the specific language governing permissions and
44  *  limitations under the License.
45  */
46 /*
47  * To change this template, choose Tools | Templates
48  * and open the template in the editor.
49  */
50 package org.collectionspace.services.account.storage;
51
52 import java.util.List;
53 import java.util.regex.Matcher;
54 import java.util.regex.Pattern;
55 import org.collectionspace.services.account.AccountsCommon;
56 import org.collectionspace.services.common.context.ServiceContext;
57 import org.collectionspace.services.common.document.DocumentHandler.Action;
58 import org.collectionspace.services.common.document.InvalidDocumentException;
59 import org.collectionspace.services.common.document.ValidatorHandler;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62
63 /**
64  *
65  * @author 
66  */
67 public class AccountValidatorHandler implements ValidatorHandler {
68
69     final Logger logger = LoggerFactory.getLogger(AccountValidatorHandler.class);
70
71     @Override
72     public void validate(Action action, ServiceContext ctx)
73             throws InvalidDocumentException {
74         if (logger.isDebugEnabled()) {
75             logger.debug("validate() action=" + action.name());
76         }
77         try {
78             AccountsCommon account = (AccountsCommon) ctx.getInput();
79             StringBuilder msgBldr = new StringBuilder("validate() ");
80             boolean invalid = false;
81
82             if (action.equals(Action.CREATE)) {
83                 //FIXME tenant would be retrieved from security context once
84                 //authentication is made mandatory, no need for validation
85                 List<AccountsCommon.Tenant> tl = account.getTenant();
86                 if (tl == null || tl.size() == 0) {
87                     msgBldr.append("\ntenant : missing information!");
88                     invalid = true;
89                 }
90                 //create specific validation here
91                 if (account.getScreenName() == null || account.getScreenName().isEmpty()) {
92                     invalid = true;
93                     msgBldr.append("\nscreenName : missing");
94                 }
95                 if (account.getUserId() == null || account.getUserId().isEmpty()) {
96                     invalid = true;
97                     msgBldr.append("\nuserId : missing");
98                 }
99                 if (account.getEmail() == null || account.getEmail().isEmpty()) {
100                     invalid = true;
101                     msgBldr.append("\nemail : missing");
102                 } else {
103                     if (invalidEmail(account.getEmail(), msgBldr)) {
104                         invalid = true;
105                     }
106                 }
107             } else if (action.equals(Action.UPDATE)) {
108                 //update specific validation here
109                 if (account.getScreenName() != null && account.getScreenName().isEmpty()) {
110                     invalid = true;
111                     msgBldr.append("\nscreenName : cannot be changed!");
112                 }
113                 if (account.getPassword() != null
114                         && (account.getUserId() == null || account.getUserId().isEmpty())) {
115                     invalid = true;
116                     msgBldr.append("\npassword : userId is needed");
117                 }
118                 if (account.getEmail() != null) {
119                     if (invalidEmail(account.getEmail(), msgBldr)) {
120                         invalid = true;
121                     }
122                 }
123             }
124             if (invalid) {
125                 String msg = msgBldr.toString();
126                 logger.error(msg);
127                 throw new InvalidDocumentException(msg);
128             }
129         } catch (InvalidDocumentException ide) {
130             throw ide;
131         } catch (Exception e) {
132             throw new InvalidDocumentException(e);
133         }
134     }
135
136     private boolean invalidEmail(String email, StringBuilder msgBldr) {
137         boolean invalid = false;
138         Pattern p = Pattern.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[_A-Za-z0-9-]+)");
139         Matcher m = p.matcher(email);
140         if (!m.find()) {
141             invalid = true;
142             msgBldr.append("\nemail : invalid " + email);
143         }
144         return invalid;
145     }
146 }