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