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