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