Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PageCallback |
|
| 1.0;1 |
1 | // Copyright 2004, 2005 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.callback; | |
16 | ||
17 | import org.apache.hivemind.util.Defense; | |
18 | import org.apache.tapestry.IPage; | |
19 | import org.apache.tapestry.IRequestCycle; | |
20 | ||
21 | /** | |
22 | * Simple callback for returning to a page. | |
23 | * <p> | |
24 | * Example usage of <tt>PageCallback</tt>: | |
25 | * <p> | |
26 | * The Home page ensure a user is authenticated in the | |
27 | * {@link org.apache.tapestry.IPage#validate(IRequestCycle)} method. If the user is not | |
28 | * authenticated, they are redirected to the Login page, after setting a callback in the Login page. | |
29 | * <p> | |
30 | * The Login page <tt>formSubmit()</tt> {@link org.apache.tapestry.IActionListener} authenticates | |
31 | * the user and then invokes {@link ICallback#performCallback(IRequestCycle)} to the Home page. | |
32 | * | |
33 | * <pre> | |
34 | * | |
35 | * | |
36 | * public class Home extends BasePage { | |
37 | * | |
38 | * public void validate(IRequestCycle cycle) { | |
39 | * Visit visit = (Visit) getVisit(); | |
40 | * | |
41 | * if (!visit.isAuthenticated()) { | |
42 | * Login login = (Login) cycle.getPage("Login"); | |
43 | * | |
44 | * login.setCallback(new PageCallback(this)); | |
45 | * | |
46 | * throw new PageRedirectException(login); | |
47 | * } | |
48 | * } | |
49 | * } | |
50 | * | |
51 | * public Login extends BasePage { | |
52 | * | |
53 | * private ICallback _callback; | |
54 | * | |
55 | * public void setCallback(ICallback _callback) { | |
56 | * _callback = callback; | |
57 | * } | |
58 | * | |
59 | * public void formSubmit(IRequestCycle cycle) { | |
60 | * // Authentication code | |
61 | * .. | |
62 | * | |
63 | * Visit visit = (Visit) getVisit(); | |
64 | * | |
65 | * visit.setAuthenticated(true); | |
66 | * | |
67 | * if (_callback != null) { | |
68 | * _callback.performCallback(cycle); | |
69 | * } | |
70 | * } | |
71 | * } | |
72 | * | |
73 | * | |
74 | * </pre> | |
75 | * | |
76 | * @author Howard Lewis Ship | |
77 | * @since 0.2.9 | |
78 | */ | |
79 | ||
80 | public class PageCallback implements ICallback | |
81 | { | |
82 | /** | |
83 | * @since 2.0.4 | |
84 | */ | |
85 | ||
86 | private static final long serialVersionUID = -3286806776105690068L; | |
87 | ||
88 | private String _pageName; | |
89 | ||
90 | public PageCallback(String pageName) | |
91 | 0 | { |
92 | 0 | Defense.notNull(pageName, "pageName"); |
93 | 0 | _pageName = pageName; |
94 | 0 | } |
95 | ||
96 | public PageCallback(IPage page) | |
97 | 0 | { |
98 | 0 | Defense.notNull(page, "page"); |
99 | ||
100 | 0 | _pageName = page.getPageName(); |
101 | 0 | } |
102 | ||
103 | public String toString() | |
104 | { | |
105 | 0 | return "PageCallback[" + _pageName + "]"; |
106 | } | |
107 | ||
108 | /** | |
109 | * Invokes {@link IRequestCycle#activate(String)} to select the previously identified page as | |
110 | * the response page. | |
111 | */ | |
112 | ||
113 | public void performCallback(IRequestCycle cycle) | |
114 | { | |
115 | 0 | Defense.notNull(cycle, "cycle"); |
116 | ||
117 | 0 | cycle.activate(_pageName); |
118 | 0 | } |
119 | } |