|
|||||||||||||||||||
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 | |||||||||||||||
DirectService.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.engine;
|
|
16 |
|
|
17 |
import java.io.IOException;
|
|
18 |
import java.util.HashMap;
|
|
19 |
import java.util.Map;
|
|
20 |
|
|
21 |
import javax.servlet.ServletException;
|
|
22 |
import javax.servlet.http.HttpServletRequest;
|
|
23 |
import javax.servlet.http.HttpSession;
|
|
24 |
|
|
25 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
26 |
import org.apache.hivemind.util.Defense;
|
|
27 |
import org.apache.tapestry.IComponent;
|
|
28 |
import org.apache.tapestry.IDirect;
|
|
29 |
import org.apache.tapestry.IPage;
|
|
30 |
import org.apache.tapestry.IRequestCycle;
|
|
31 |
import org.apache.tapestry.StaleSessionException;
|
|
32 |
import org.apache.tapestry.Tapestry;
|
|
33 |
import org.apache.tapestry.request.ResponseOutputStream;
|
|
34 |
import org.apache.tapestry.services.LinkFactory;
|
|
35 |
import org.apache.tapestry.services.ResponseRenderer;
|
|
36 |
import org.apache.tapestry.services.ServiceConstants;
|
|
37 |
|
|
38 |
/**
|
|
39 |
* Implementation of the direct service, which encodes the page and component id in the service
|
|
40 |
* context, and passes application-defined parameters as well.
|
|
41 |
*
|
|
42 |
* @author Howard Lewis Ship
|
|
43 |
* @since 1.0.9
|
|
44 |
*/
|
|
45 |
|
|
46 |
public class DirectService implements IEngineService |
|
47 |
{ |
|
48 |
/** @since 3.1 */
|
|
49 |
private ResponseRenderer _responseRenderer;
|
|
50 |
|
|
51 |
/** @since 3.1 */
|
|
52 |
private LinkFactory _linkFactory;
|
|
53 |
|
|
54 |
/** @since 3.1 */
|
|
55 |
private HttpServletRequest _request;
|
|
56 |
|
|
57 | 104 |
public ILink getLink(IRequestCycle cycle, Object parameter)
|
58 |
{ |
|
59 | 104 |
Defense.isAssignable(parameter, DirectServiceParameter.class, "parameter"); |
60 |
|
|
61 | 104 |
DirectServiceParameter dsp = (DirectServiceParameter) parameter; |
62 |
|
|
63 | 104 |
IComponent component = dsp.getDirect(); |
64 |
|
|
65 |
// New since 1.0.1, we use the component to determine
|
|
66 |
// the page, not the cycle. Through the use of tricky
|
|
67 |
// things such as Block/InsertBlock, it is possible
|
|
68 |
// that a component from a page different than
|
|
69 |
// the response page will render.
|
|
70 |
// In 1.0.6, we start to record *both* the render page
|
|
71 |
// and the component page (if different).
|
|
72 |
|
|
73 | 104 |
IPage activePage = cycle.getPage(); |
74 | 104 |
IPage componentPage = component.getPage(); |
75 |
|
|
76 | 104 |
Map parameters = new HashMap();
|
77 |
|
|
78 | 104 |
boolean stateful = _request.getSession(false) != null; |
79 |
|
|
80 | 104 |
parameters.put(ServiceConstants.SERVICE, Tapestry.DIRECT_SERVICE); |
81 | 104 |
parameters.put(ServiceConstants.PAGE, activePage.getPageName()); |
82 | 104 |
parameters.put(ServiceConstants.COMPONENT, component.getIdPath()); |
83 | 104 |
parameters.put(ServiceConstants.CONTAINER, componentPage == activePage ? null
|
84 |
: componentPage.getPageName()); |
|
85 | 104 |
parameters.put(ServiceConstants.SESSION, stateful ? "T" : null); |
86 | 104 |
parameters.put(ServiceConstants.PARAMETER, dsp.getServiceParameters()); |
87 |
|
|
88 | 104 |
return _linkFactory.constructLink(cycle, parameters, true); |
89 |
} |
|
90 |
|
|
91 | 76 |
public void service(IRequestCycle cycle, ResponseOutputStream output) throws ServletException, |
92 |
IOException |
|
93 |
{ |
|
94 | 76 |
String componentId = cycle.getParameter(ServiceConstants.COMPONENT); |
95 | 76 |
String componentPageName = cycle.getParameter(ServiceConstants.CONTAINER); |
96 | 76 |
String activePageName = cycle.getParameter(ServiceConstants.PAGE); |
97 | 76 |
boolean activeSession = cycle.getParameter(ServiceConstants.SESSION) != null; |
98 |
|
|
99 | 76 |
IPage page = cycle.getPage(activePageName); |
100 |
|
|
101 | 76 |
cycle.activate(page); |
102 |
|
|
103 | 76 |
IPage componentPage = componentPageName == null ? page : cycle.getPage(componentPageName);
|
104 |
|
|
105 | 76 |
IComponent component = componentPage.getNestedComponent(componentId); |
106 |
|
|
107 | 76 |
IDirect direct = null;
|
108 |
|
|
109 | 76 |
try
|
110 |
{ |
|
111 | 76 |
direct = (IDirect) component; |
112 |
} |
|
113 |
catch (ClassCastException ex)
|
|
114 |
{ |
|
115 | 1 |
throw new ApplicationRuntimeException(EngineMessages.wrongComponentType( |
116 |
component, |
|
117 |
IDirect.class), component, null, ex); |
|
118 |
} |
|
119 |
|
|
120 |
// Check for a StaleSession only when the session was stateful when
|
|
121 |
// the link was created.
|
|
122 |
|
|
123 | 75 |
if (activeSession && direct.isStateful())
|
124 |
{ |
|
125 | 19 |
HttpSession session = _request.getSession(); |
126 |
|
|
127 | 19 |
if (session == null || session.isNew()) |
128 | 5 |
throw new StaleSessionException(EngineMessages.requestStateSession(direct), |
129 |
componentPage); |
|
130 |
} |
|
131 |
|
|
132 | 70 |
Object[] parameters = _linkFactory.extractServiceParameters(cycle); |
133 |
|
|
134 | 70 |
cycle.setServiceParameters(parameters); |
135 |
|
|
136 | 70 |
direct.trigger(cycle); |
137 |
|
|
138 |
// Render the response. This will be the active page
|
|
139 |
// unless the direct component (or its delegate) changes it.
|
|
140 |
|
|
141 | 53 |
_responseRenderer.renderResponse(cycle, output); |
142 |
} |
|
143 |
|
|
144 | 194 |
public String getName()
|
145 |
{ |
|
146 | 194 |
return Tapestry.DIRECT_SERVICE;
|
147 |
} |
|
148 |
|
|
149 |
/** @since 3.1 */
|
|
150 | 55 |
public void setResponseRenderer(ResponseRenderer responseRenderer) |
151 |
{ |
|
152 | 55 |
_responseRenderer = responseRenderer; |
153 |
} |
|
154 |
|
|
155 |
/** @since 3.1 */
|
|
156 | 58 |
public void setLinkFactory(LinkFactory linkFactory) |
157 |
{ |
|
158 | 58 |
_linkFactory = linkFactory; |
159 |
} |
|
160 |
|
|
161 |
/** @since 3.1 */
|
|
162 | 57 |
public void setRequest(HttpServletRequest request) |
163 |
{ |
|
164 | 57 |
_request = request; |
165 |
} |
|
166 |
} |
|