|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ExternalCallback.java | 100% | 100% | 100% | 100% |
|
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.ApplicationRuntimeException; | |
18 | import org.apache.hivemind.util.Defense; | |
19 | import org.apache.tapestry.IExternalPage; | |
20 | import org.apache.tapestry.IRequestCycle; | |
21 | import org.apache.tapestry.Tapestry; | |
22 | ||
23 | /** | |
24 | * A callback for returning to an {@link org.apache.tapestry.IExternalPage}. | |
25 | * <p> | |
26 | * Example usage of <tt>ExternalCallback</tt>: | |
27 | * <p> | |
28 | * The External page ensure a user is authenticated in the | |
29 | * {@link org.apache.tapestry.IPage#validate(IRequestCycle)} method. If the user is not | |
30 | * authenticated, they are redirected to the Login page, after setting a callback in the Login page. | |
31 | * <p> | |
32 | * The Login page <tt>formSubmit()</tt> {@link org.apache.tapestry.IActionListener} authenticates | |
33 | * the user and then invokes {@link ICallback#performCallback(IRequestCycle)} to the External page. | |
34 | * | |
35 | * <pre> | |
36 | * | |
37 | * | |
38 | * | |
39 | * | |
40 | * | |
41 | * | |
42 | * | |
43 | * | |
44 | * | |
45 | * | |
46 | * | |
47 | * public class External extends BasePage implements IExternalPage { | |
48 | * | |
49 | * private Integer _itemId; | |
50 | * | |
51 | * public void validate(IRequestCycle cycle) throws RequestCycleException { | |
52 | * Visit visit = (Visit) getVisit(); | |
53 | * | |
54 | * if (!visit.isAuthenticated()) { | |
55 | * Login login = (Login) cycle.getPage("Login"); | |
56 | * | |
57 | * login.setCallback | |
58 | * (new ExternalCallback(this, cycle.getServiceParameters())); | |
59 | * | |
60 | * throw new PageRedirectException(login); | |
61 | * } | |
62 | * } | |
63 | * | |
64 | * public void activateExternalPage(Object[] params, IRequestCycle cycle) | |
65 | * throws RequestCycleException { | |
66 | * _itemId = (Integer) params[0]; | |
67 | * } | |
68 | * } | |
69 | * | |
70 | * public Login extends BasePage { | |
71 | * | |
72 | * private ICallback _callback; | |
73 | * | |
74 | * public void setCallback(ICallback _callback) { | |
75 | * _callback = callback; | |
76 | * } | |
77 | * | |
78 | * public void formSubmit(IRequestCycle cycle) { | |
79 | * // Authentication code | |
80 | * .. | |
81 | * | |
82 | * Visit visit = (Visit) getVisit(); | |
83 | * | |
84 | * visit.setAuthenticated(true); | |
85 | * | |
86 | * if (_callback != null) { | |
87 | * _callback.performCallback(cycle); | |
88 | * } | |
89 | * } | |
90 | * } | |
91 | * | |
92 | * | |
93 | * | |
94 | * | |
95 | * | |
96 | * | |
97 | * | |
98 | * | |
99 | * | |
100 | * | |
101 | * | |
102 | * </pre> | |
103 | * | |
104 | * @see org.apache.tapestry.IExternalPage | |
105 | * @see org.apache.tapestry.engine.ExternalService | |
106 | * @author Malcolm Edgar | |
107 | * @since 2.3 | |
108 | */ | |
109 | ||
110 | public class ExternalCallback implements ICallback | |
111 | { | |
112 | private static final long serialVersionUID = -6783421589702643930L; | |
113 | ||
114 | private String _pageName; | |
115 | ||
116 | private Object[] _parameters; | |
117 | ||
118 | /** | |
119 | * Creates a new ExternalCallback for the named <tt>IExternalPage</tt>. The parameters (which | |
120 | * may be null) is retained, not copied. | |
121 | */ | |
122 | ||
123 | 3 | public ExternalCallback(String pageName, Object[] parameters) |
124 | { | |
125 | 3 | Defense.notNull(pageName, "pageName"); |
126 | ||
127 | 3 | _pageName = pageName; |
128 | 3 | _parameters = parameters; |
129 | } | |
130 | ||
131 | /** | |
132 | * Creates a new ExternalCallback for the page. The parameters (which may be null) is retained, | |
133 | * not copied. | |
134 | */ | |
135 | ||
136 | 1 | public ExternalCallback(IExternalPage page, Object[] parameters) |
137 | { | |
138 | 1 | Defense.notNull(page, "page"); |
139 | ||
140 | 1 | _pageName = page.getPageName(); |
141 | 1 | _parameters = parameters; |
142 | } | |
143 | ||
144 | /** | |
145 | * Invokes {@link IRequestCycle#setPage(String)} to select the previously identified | |
146 | * <tt>IExternalPage</tt> as the response page and activates the page by invoking | |
147 | * <tt>activateExternalPage()</tt> with the callback parameters and request cycle. | |
148 | */ | |
149 | ||
150 | 4 | public void performCallback(IRequestCycle cycle) |
151 | { | |
152 | 4 | Defense.notNull(cycle, "cycle"); |
153 | ||
154 | 4 | try |
155 | { | |
156 | 4 | IExternalPage page = (IExternalPage) cycle.getPage(_pageName); |
157 | ||
158 | 3 | cycle.activate(page); |
159 | ||
160 | 3 | page.activateExternalPage(_parameters, cycle); |
161 | } | |
162 | catch (ClassCastException ex) | |
163 | { | |
164 | 1 | throw new ApplicationRuntimeException(CallbackMessages.pageNotExternal(_pageName), ex); |
165 | } | |
166 | } | |
167 | ||
168 | 3 | public String toString() |
169 | { | |
170 | 3 | StringBuffer buffer = new StringBuffer("ExternalCallback["); |
171 | ||
172 | 3 | buffer.append(_pageName); |
173 | ||
174 | 3 | if (_parameters != null) |
175 | { | |
176 | 2 | for (int i = 0; i < _parameters.length; i++) |
177 | { | |
178 | 4 | if (i == 0) |
179 | 2 | buffer.append('/'); |
180 | else | |
181 | 2 | buffer.append(", "); |
182 | ||
183 | 4 | buffer.append(_parameters[i]); |
184 | } | |
185 | } | |
186 | ||
187 | 3 | buffer.append(']'); |
188 | ||
189 | 3 | return buffer.toString(); |
190 | } | |
191 | } |
|