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