KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > vlib > Protected


1 // Copyright 2004 The Apache Software Foundation
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 org.apache.tapestry.vlib;
16
17 import org.apache.tapestry.PageRedirectException;
18 import org.apache.tapestry.callback.PageCallback;
19 import org.apache.tapestry.event.PageEvent;
20 import org.apache.tapestry.event.PageValidateListener;
21 import org.apache.tapestry.form.IFormComponent;
22 import org.apache.tapestry.html.BasePage;
23 import org.apache.tapestry.valid.IValidationDelegate;
24 import org.apache.tapestry.vlib.pages.Login;
25
26 /**
27  * Base page used for pages that should be protected by the {@link Login} page.
28  * If the user is not logged in, they are redirected to the Login page first.
29  * Also, implements an error property and a validationDelegate.
30  *
31  * @author Howard Lewis Ship
32  * @version $Id: Protected.java,v 1.8 2004/02/19 17:37:49 hlship Exp $
33  *
34  **/

35
36 public abstract class Protected extends BasePage implements IErrorProperty, PageValidateListener
37 {
38     private IValidationDelegate _validationDelegate;
39
40     public void initialize()
41     {
42         _validationDelegate = null;
43     }
44
45     public IValidationDelegate getValidationDelegate()
46     {
47         if (_validationDelegate == null)
48             _validationDelegate = new VirtualLibraryDelegate();
49
50         return _validationDelegate;
51     }
52
53     protected void setErrorField(String JavaDoc componentId, String JavaDoc message)
54     {
55         IFormComponent component = (IFormComponent) getComponent(componentId);
56
57         IValidationDelegate delegate = getValidationDelegate();
58
59         delegate.setFormComponent(component);
60         delegate.record(message, null);
61     }
62
63     /**
64      * Returns true if the delegate indicates an error, or the error property is not null.
65      *
66      **/

67
68     protected boolean isInError()
69     {
70         return getError() != null || getValidationDelegate().getHasErrors();
71     }
72
73     /**
74      * Checks if the user is logged in. If not, they are sent
75      * to the {@link Login} page before coming back to whatever this
76      * page is.
77      *
78      **/

79
80     public void pageValidate(PageEvent event)
81     {
82         Visit visit = (Visit) getVisit();
83
84         if (visit != null && visit.isUserLoggedIn())
85             return;
86
87         // User not logged in ... redirect through the Login page.
88

89         Login login = (Login) getRequestCycle().getPage("Login");
90
91         login.setCallback(new PageCallback(this));
92
93         throw new PageRedirectException(login);
94     }
95 }
Popular Tags