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.IAction; |
24 |
| import org.apache.tapestry.IComponent; |
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 |
| |
44 |
| |
45 |
| public class ActionService implements IEngineService |
46 |
| { |
47 |
| |
48 |
| private ResponseRenderer _responseRenderer; |
49 |
| |
50 |
| |
51 |
| private LinkFactory _linkFactory; |
52 |
| |
53 |
| |
54 |
| private static final String ACTION = "action"; |
55 |
| |
56 |
| |
57 |
| private WebRequest _request; |
58 |
| |
59 |
| |
60 |
| private IRequestCycle _requestCycle; |
61 |
| |
62 |
30
| public ILink getLink(boolean post, Object parameter)
|
63 |
| { |
64 |
30
| Defense.isAssignable(parameter, ActionServiceParameter.class, "parameter");
|
65 |
| |
66 |
30
| ActionServiceParameter asp = (ActionServiceParameter) parameter;
|
67 |
| |
68 |
30
| IComponent component = asp.getComponent();
|
69 |
30
| IPage activePage = _requestCycle.getPage();
|
70 |
30
| IPage componentPage = component.getPage();
|
71 |
| |
72 |
30
| Map parameters = new HashMap();
|
73 |
| |
74 |
30
| boolean stateful = _request.getSession(false) != null;
|
75 |
| |
76 |
30
| parameters.put(ServiceConstants.COMPONENT, component.getIdPath());
|
77 |
30
| parameters.put(ServiceConstants.PAGE, activePage.getPageName());
|
78 |
30
| parameters.put(ServiceConstants.CONTAINER, activePage == componentPage ? null
|
79 |
| : componentPage.getPageName()); |
80 |
30
| parameters.put(ACTION, asp.getActionId());
|
81 |
30
| parameters.put(ServiceConstants.SESSION, stateful ? "T" : null);
|
82 |
| |
83 |
30
| return _linkFactory.constructLink(this, post, parameters, true);
|
84 |
| } |
85 |
| |
86 |
22
| public void service(IRequestCycle cycle) throws IOException
|
87 |
| { |
88 |
22
| String componentId = cycle.getParameter(ServiceConstants.COMPONENT);
|
89 |
22
| String componentPageName = cycle.getParameter(ServiceConstants.CONTAINER);
|
90 |
22
| String activePageName = cycle.getParameter(ServiceConstants.PAGE);
|
91 |
22
| String actionId = cycle.getParameter(ACTION);
|
92 |
22
| boolean activeSession = cycle.getParameter(ServiceConstants.SESSION) != null;
|
93 |
| |
94 |
22
| IPage page = cycle.getPage(activePageName);
|
95 |
| |
96 |
| |
97 |
| |
98 |
22
| cycle.activate(page);
|
99 |
| |
100 |
22
| IPage componentPage = componentPageName == null ? page : cycle.getPage(componentPageName);
|
101 |
| |
102 |
22
| IComponent component = componentPage.getNestedComponent(componentId);
|
103 |
| |
104 |
22
| IAction action = null;
|
105 |
| |
106 |
22
| try
|
107 |
| { |
108 |
22
| action = (IAction) component;
|
109 |
| } |
110 |
| catch (ClassCastException ex) |
111 |
| { |
112 |
2
| throw new ApplicationRuntimeException(EngineMessages.wrongComponentType(
|
113 |
| component, |
114 |
| IAction.class), component, null, ex); |
115 |
| } |
116 |
| |
117 |
| |
118 |
| |
119 |
| |
120 |
20
| if (activeSession && action.getRequiresSession())
|
121 |
| { |
122 |
8
| WebSession session = _request.getSession(false);
|
123 |
| |
124 |
8
| if (session == null || session.isNew())
|
125 |
4
| throw new StaleSessionException(EngineMessages.requestStateSession(component),
|
126 |
| componentPage); |
127 |
| |
128 |
| } |
129 |
| |
130 |
16
| cycle.rewindPage(actionId, action);
|
131 |
| |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
| |
137 |
16
| _responseRenderer.renderResponse(cycle);
|
138 |
| } |
139 |
| |
140 |
36
| public String getName()
|
141 |
| { |
142 |
36
| return Tapestry.ACTION_SERVICE;
|
143 |
| } |
144 |
| |
145 |
| |
146 |
18
| public void setResponseRenderer(ResponseRenderer responseRenderer)
|
147 |
| { |
148 |
18
| _responseRenderer = responseRenderer;
|
149 |
| } |
150 |
| |
151 |
| |
152 |
18
| public void setLinkFactory(LinkFactory linkFactory)
|
153 |
| { |
154 |
18
| _linkFactory = linkFactory;
|
155 |
| } |
156 |
| |
157 |
| |
158 |
22
| public void setRequest(WebRequest request)
|
159 |
| { |
160 |
22
| _request = request;
|
161 |
| } |
162 |
| |
163 |
| |
164 |
18
| public void setRequestCycle(IRequestCycle requestCycle)
|
165 |
| { |
166 |
18
| _requestCycle = requestCycle;
|
167 |
| } |
168 |
| } |