01: /*
02: * Copyright 2007 Outerthought bvba and Schaubroeck nv
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.outerj.daisy.frontend;
17:
18: import org.apache.cocoon.forms.validation.WidgetValidator;
19: import org.apache.cocoon.forms.validation.ValidationErrorAware;
20: import org.apache.cocoon.forms.validation.ValidationError;
21: import org.apache.cocoon.forms.formmodel.Widget;
22: import org.apache.cocoon.components.ContextHelper;
23: import org.apache.avalon.framework.context.Contextualizable;
24: import org.apache.avalon.framework.context.Context;
25: import org.apache.avalon.framework.context.ContextException;
26: import org.outerj.daisy.repository.user.UserNotFoundException;
27: import org.outerj.daisy.repository.Repository;
28:
29: public class UserLoginValidator implements WidgetValidator,
30: Contextualizable {
31: private Context context;
32:
33: public void contextualize(Context context) throws ContextException {
34: this .context = context;
35: }
36:
37: public boolean validate(Widget widget) {
38: String login = (String) widget.getValue();
39: if (login == null)
40: return true;
41:
42: boolean valid = true;
43: try {
44: FrontEndContext frontEndContext = FrontEndContext
45: .get(ContextHelper.getRequest(context));
46: Repository repository = frontEndContext.getRepository();
47: repository.getUserManager().getPublicUserInfo(login);
48: } catch (UserNotFoundException e) {
49: valid = false;
50: ((ValidationErrorAware) widget)
51: .setValidationError(new ValidationError(
52: "Non-existing user login: " + login, false));
53: } catch (Throwable e) {
54: throw new RuntimeException(
55: "Error trying to check existence of user \""
56: + login + "\".", e);
57: }
58:
59: return valid;
60: }
61: }
|