|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ExternalCallback.java | 0% | 0% | 0% | 0% |
|
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.tapestry.IExternalPage;
|
|
19 |
import org.apache.tapestry.IRequestCycle;
|
|
20 |
import org.apache.tapestry.Tapestry;
|
|
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.
|
|
29 |
* If the user is not authenticated, they are redirected to the Login page, after
|
|
30 |
* setting a callback in the Login page.
|
|
31 |
* <p>
|
|
32 |
* The Login page <tt>formSubmit()</tt> {@link org.apache.tapestry.IActionListener}
|
|
33 |
* authenticates the user and then invokes {@link ICallback#performCallback(IRequestCycle)}
|
|
34 |
* to the External page.
|
|
35 |
* <pre>
|
|
36 |
* public class External extends BasePage implements IExternalPage {
|
|
37 |
*
|
|
38 |
* private Integer _itemId;
|
|
39 |
*
|
|
40 |
* public void validate(IRequestCycle cycle) throws RequestCycleException {
|
|
41 |
* Visit visit = (Visit) getVisit();
|
|
42 |
*
|
|
43 |
* if (!visit.isAuthenticated()) {
|
|
44 |
* Login login = (Login) cycle.getPage("Login");
|
|
45 |
*
|
|
46 |
* login.setCallback
|
|
47 |
* (new ExternalCallback(this, cycle.getServiceParameters()));
|
|
48 |
*
|
|
49 |
* throw new PageRedirectException(login);
|
|
50 |
* }
|
|
51 |
* }
|
|
52 |
*
|
|
53 |
* public void activateExternalPage(Object[] params, IRequestCycle cycle)
|
|
54 |
* throws RequestCycleException {
|
|
55 |
* _itemId = (Integer) params[0];
|
|
56 |
* }
|
|
57 |
* }
|
|
58 |
*
|
|
59 |
* public Login extends BasePage {
|
|
60 |
*
|
|
61 |
* private ICallback _callback;
|
|
62 |
*
|
|
63 |
* public void setCallback(ICallback _callback) {
|
|
64 |
* _callback = callback;
|
|
65 |
* }
|
|
66 |
*
|
|
67 |
* public void formSubmit(IRequestCycle cycle) {
|
|
68 |
* // Authentication code
|
|
69 |
* ..
|
|
70 |
*
|
|
71 |
* Visit visit = (Visit) getVisit();
|
|
72 |
*
|
|
73 |
* visit.setAuthenticated(true);
|
|
74 |
*
|
|
75 |
* if (_callback != null) {
|
|
76 |
* _callback.performCallback(cycle);
|
|
77 |
* }
|
|
78 |
* }
|
|
79 |
* }
|
|
80 |
* </pre>
|
|
81 |
*
|
|
82 |
* @see org.apache.tapestry.IExternalPage
|
|
83 |
* @see org.apache.tapestry.engine.ExternalService
|
|
84 |
*
|
|
85 |
* @author Malcolm Edgar
|
|
86 |
* @since 2.3
|
|
87 |
*
|
|
88 |
**/
|
|
89 |
|
|
90 |
public class ExternalCallback implements ICallback |
|
91 |
{ |
|
92 |
private String _pageName;
|
|
93 |
private Object[] _parameters;
|
|
94 |
|
|
95 |
/**
|
|
96 |
* Creates a new ExternalCallback for the named <tt>IExternalPage</tt>.
|
|
97 |
* The parameters (which may be null) is retained, not copied.
|
|
98 |
*
|
|
99 |
**/
|
|
100 |
|
|
101 | 0 |
public ExternalCallback(String pageName, Object[] parameters)
|
102 |
{ |
|
103 | 0 |
_pageName = pageName; |
104 | 0 |
_parameters = parameters; |
105 |
} |
|
106 |
|
|
107 |
/**
|
|
108 |
* Creates a new ExternalCallback for the page. The parameters
|
|
109 |
* (which may be null) is retained, not copied.
|
|
110 |
*
|
|
111 |
**/
|
|
112 |
|
|
113 | 0 |
public ExternalCallback(IExternalPage page, Object[] parameters)
|
114 |
{ |
|
115 | 0 |
_pageName = page.getPageName(); |
116 | 0 |
_parameters = parameters; |
117 |
} |
|
118 |
|
|
119 |
/**
|
|
120 |
* Invokes {@link IRequestCycle#setPage(String)} to select the previously
|
|
121 |
* identified <tt>IExternalPage</tt> as the response page and activates
|
|
122 |
* the page by invoking <tt>activateExternalPage()</tt> with the callback
|
|
123 |
* parameters and request cycle.
|
|
124 |
*
|
|
125 |
**/
|
|
126 |
|
|
127 | 0 |
public void performCallback(IRequestCycle cycle) |
128 |
{ |
|
129 | 0 |
try
|
130 |
{ |
|
131 | 0 |
IExternalPage page = (IExternalPage) cycle.getPage(_pageName); |
132 |
|
|
133 | 0 |
cycle.activate(page); |
134 |
|
|
135 | 0 |
page.activateExternalPage(_parameters, cycle); |
136 |
} |
|
137 |
catch (ClassCastException ex)
|
|
138 |
{ |
|
139 | 0 |
throw new ApplicationRuntimeException( |
140 |
Tapestry.format("ExternalCallback.page-not-compatible", _pageName),
|
|
141 |
ex); |
|
142 |
} |
|
143 |
} |
|
144 |
|
|
145 | 0 |
public String toString()
|
146 |
{ |
|
147 | 0 |
StringBuffer buffer = new StringBuffer("ExternalCallback["); |
148 |
|
|
149 | 0 |
buffer.append(_pageName); |
150 | 0 |
buffer.append('/'); |
151 |
|
|
152 | 0 |
if (_parameters != null) |
153 |
{ |
|
154 | 0 |
String sep = " ";
|
155 |
|
|
156 | 0 |
for (int i = 0; i < _parameters.length; i++) |
157 |
{ |
|
158 | 0 |
buffer.append(sep); |
159 | 0 |
buffer.append(_parameters[i]); |
160 |
|
|
161 | 0 |
sep = ", ";
|
162 |
} |
|
163 |
} |
|
164 |
|
|
165 | 0 |
buffer.append(']'); |
166 |
|
|
167 | 0 |
return buffer.toString();
|
168 |
} |
|
169 |
} |
|