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 |
86
| public ILink getLink(IRequestCycle cycle, boolean post, Object parameter)
|
55 |
| { |
56 |
86
| Defense.isAssignable(parameter, DirectServiceParameter.class, "parameter");
|
57 |
| |
58 |
86
| DirectServiceParameter dsp = (DirectServiceParameter) parameter;
|
59 |
| |
60 |
86
| IComponent component = dsp.getDirect();
|
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
86
| IPage activePage = cycle.getPage();
|
71 |
86
| IPage componentPage = component.getPage();
|
72 |
| |
73 |
86
| Map parameters = new HashMap();
|
74 |
| |
75 |
86
| boolean stateful = _request.getSession(false) != null;
|
76 |
| |
77 |
86
| parameters.put(ServiceConstants.SERVICE, getName());
|
78 |
86
| parameters.put(ServiceConstants.PAGE, activePage.getPageName());
|
79 |
86
| parameters.put(ServiceConstants.COMPONENT, component.getIdPath());
|
80 |
86
| parameters.put(ServiceConstants.CONTAINER, componentPage == activePage ? null
|
81 |
| : componentPage.getPageName()); |
82 |
86
| parameters.put(ServiceConstants.SESSION, stateful ? "T" : null);
|
83 |
86
| parameters.put(ServiceConstants.PARAMETER, dsp.getServiceParameters());
|
84 |
| |
85 |
86
| return _linkFactory.constructLink(cycle, post, parameters, true);
|
86 |
| } |
87 |
| |
88 |
54
| public void service(IRequestCycle cycle) throws IOException
|
89 |
| { |
90 |
54
| String componentId = cycle.getParameter(ServiceConstants.COMPONENT);
|
91 |
54
| String componentPageName = cycle.getParameter(ServiceConstants.CONTAINER);
|
92 |
54
| String activePageName = cycle.getParameter(ServiceConstants.PAGE);
|
93 |
54
| boolean activeSession = cycle.getParameter(ServiceConstants.SESSION) != null;
|
94 |
| |
95 |
54
| IPage page = cycle.getPage(activePageName);
|
96 |
| |
97 |
54
| cycle.activate(page);
|
98 |
| |
99 |
54
| IPage componentPage = componentPageName == null ? page : cycle.getPage(componentPageName);
|
100 |
| |
101 |
54
| IComponent component = componentPage.getNestedComponent(componentId);
|
102 |
| |
103 |
54
| IDirect direct = null;
|
104 |
| |
105 |
54
| try
|
106 |
| { |
107 |
54
| direct = (IDirect) component;
|
108 |
| } |
109 |
| catch (ClassCastException ex) |
110 |
| { |
111 |
1
| throw new ApplicationRuntimeException(EngineMessages.wrongComponentType(
|
112 |
| component, |
113 |
| IDirect.class), component, null, ex); |
114 |
| } |
115 |
| |
116 |
| |
117 |
| |
118 |
| |
119 |
53
| if (activeSession && direct.isStateful())
|
120 |
| { |
121 |
12
| WebSession session = _request.getSession(false);
|
122 |
| |
123 |
12
| if (session == null || session.isNew())
|
124 |
3
| throw new StaleSessionException(EngineMessages.requestStateSession(direct),
|
125 |
| componentPage); |
126 |
| } |
127 |
| |
128 |
50
| Object[] parameters = _linkFactory.extractListenerParameters(cycle);
|
129 |
| |
130 |
50
| triggerComponent(cycle, direct, parameters);
|
131 |
| |
132 |
| |
133 |
| |
134 |
| |
135 |
44
| _responseRenderer.renderResponse(cycle);
|
136 |
| } |
137 |
| |
138 |
| |
139 |
| |
140 |
50
| protected void triggerComponent(IRequestCycle cycle, IDirect direct, Object[] parameters)
|
141 |
| { |
142 |
50
| cycle.setListenerParameters(parameters);
|
143 |
| |
144 |
50
| direct.trigger(cycle);
|
145 |
| } |
146 |
| |
147 |
105
| public String getName()
|
148 |
| { |
149 |
105
| return Tapestry.DIRECT_SERVICE;
|
150 |
| } |
151 |
| |
152 |
| |
153 |
22
| public void setResponseRenderer(ResponseRenderer responseRenderer)
|
154 |
| { |
155 |
22
| _responseRenderer = responseRenderer;
|
156 |
| } |
157 |
| |
158 |
| |
159 |
26
| public void setLinkFactory(LinkFactory linkFactory)
|
160 |
| { |
161 |
26
| _linkFactory = linkFactory;
|
162 |
| } |
163 |
| |
164 |
| |
165 |
25
| public void setRequest(WebRequest request)
|
166 |
| { |
167 |
25
| _request = request;
|
168 |
| } |
169 |
| } |