1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
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 org.apache.hivemind.ApplicationRuntimeException; |
22 |
| import org.apache.hivemind.util.Defense; |
23 |
| import org.apache.tapestry.IComponent; |
24 |
| import org.apache.tapestry.IDirect; |
25 |
| import org.apache.tapestry.IPage; |
26 |
| import org.apache.tapestry.IRequestCycle; |
27 |
| import org.apache.tapestry.StaleSessionException; |
28 |
| import org.apache.tapestry.Tapestry; |
29 |
| import org.apache.tapestry.services.LinkFactory; |
30 |
| import org.apache.tapestry.services.ResponseRenderer; |
31 |
| import org.apache.tapestry.services.ServiceConstants; |
32 |
| import org.apache.tapestry.web.WebRequest; |
33 |
| import org.apache.tapestry.web.WebSession; |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| public class DirectService implements IEngineService |
44 |
| { |
45 |
| |
46 |
| protected ResponseRenderer _responseRenderer; |
47 |
| |
48 |
| |
49 |
| protected LinkFactory _linkFactory; |
50 |
| |
51 |
| |
52 |
| protected WebRequest _request; |
53 |
| |
54 |
| |
55 |
| private IRequestCycle _requestCycle; |
56 |
| |
57 |
65
| public ILink getLink(boolean post, Object parameter)
|
58 |
| { |
59 |
65
| Defense.isAssignable(parameter, DirectServiceParameter.class, "parameter");
|
60 |
| |
61 |
65
| DirectServiceParameter dsp = (DirectServiceParameter) parameter;
|
62 |
| |
63 |
65
| IComponent component = dsp.getDirect();
|
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
65
| IPage activePage = _requestCycle.getPage();
|
74 |
65
| IPage componentPage = component.getPage();
|
75 |
| |
76 |
65
| Map parameters = new HashMap();
|
77 |
| |
78 |
65
| boolean stateful = _request.getSession(false) != null;
|
79 |
| |
80 |
65
| parameters.put(ServiceConstants.PAGE, activePage.getPageName());
|
81 |
65
| parameters.put(ServiceConstants.COMPONENT, component.getIdPath());
|
82 |
65
| parameters.put(ServiceConstants.CONTAINER, componentPage == activePage ? null
|
83 |
| : componentPage.getPageName()); |
84 |
65
| parameters.put(ServiceConstants.SESSION, stateful ? "T" : null);
|
85 |
65
| parameters.put(ServiceConstants.PARAMETER, dsp.getServiceParameters());
|
86 |
| |
87 |
65
| return _linkFactory.constructLink(this, post, parameters, true);
|
88 |
| } |
89 |
| |
90 |
50
| public void service(IRequestCycle cycle) throws IOException
|
91 |
| { |
92 |
50
| String componentId = cycle.getParameter(ServiceConstants.COMPONENT);
|
93 |
50
| String componentPageName = cycle.getParameter(ServiceConstants.CONTAINER);
|
94 |
50
| String activePageName = cycle.getParameter(ServiceConstants.PAGE);
|
95 |
50
| boolean activeSession = cycle.getParameter(ServiceConstants.SESSION) != null;
|
96 |
| |
97 |
50
| IPage page = cycle.getPage(activePageName);
|
98 |
| |
99 |
50
| cycle.activate(page);
|
100 |
| |
101 |
50
| IPage componentPage = componentPageName == null ? page : cycle.getPage(componentPageName);
|
102 |
| |
103 |
50
| IComponent component = componentPage.getNestedComponent(componentId);
|
104 |
| |
105 |
50
| IDirect direct = null;
|
106 |
| |
107 |
50
| try
|
108 |
| { |
109 |
50
| direct = (IDirect) component;
|
110 |
| } |
111 |
| catch (ClassCastException ex) |
112 |
| { |
113 |
1
| throw new ApplicationRuntimeException(EngineMessages.wrongComponentType(
|
114 |
| component, |
115 |
| IDirect.class), component, null, ex); |
116 |
| } |
117 |
| |
118 |
| |
119 |
| |
120 |
| |
121 |
49
| if (activeSession && direct.isStateful())
|
122 |
| { |
123 |
12
| WebSession session = _request.getSession(false);
|
124 |
| |
125 |
12
| if (session == null || session.isNew())
|
126 |
3
| throw new StaleSessionException(EngineMessages.requestStateSession(direct),
|
127 |
| componentPage); |
128 |
| } |
129 |
| |
130 |
46
| Object[] parameters = _linkFactory.extractListenerParameters(cycle);
|
131 |
| |
132 |
46
| triggerComponent(cycle, direct, parameters);
|
133 |
| |
134 |
| |
135 |
| |
136 |
| |
137 |
42
| _responseRenderer.renderResponse(cycle);
|
138 |
| } |
139 |
| |
140 |
| |
141 |
| |
142 |
46
| protected void triggerComponent(IRequestCycle cycle, IDirect direct, Object[] parameters)
|
143 |
| { |
144 |
46
| cycle.setListenerParameters(parameters);
|
145 |
| |
146 |
46
| direct.trigger(cycle);
|
147 |
| } |
148 |
| |
149 |
78
| public String getName()
|
150 |
| { |
151 |
78
| return Tapestry.DIRECT_SERVICE;
|
152 |
| } |
153 |
| |
154 |
| |
155 |
20
| public void setResponseRenderer(ResponseRenderer responseRenderer)
|
156 |
| { |
157 |
20
| _responseRenderer = responseRenderer;
|
158 |
| } |
159 |
| |
160 |
| |
161 |
24
| public void setLinkFactory(LinkFactory linkFactory)
|
162 |
| { |
163 |
24
| _linkFactory = linkFactory;
|
164 |
| } |
165 |
| |
166 |
| |
167 |
23
| public void setRequest(WebRequest request)
|
168 |
| { |
169 |
23
| _request = request;
|
170 |
| } |
171 |
| |
172 |
| |
173 |
21
| public void setRequestCycle(IRequestCycle requestCycle)
|
174 |
| { |
175 |
21
| _requestCycle = requestCycle;
|
176 |
| } |
177 |
| } |