1 // Copyright 2006, 2007, 2008 The Apache Software Foundation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package org.apache.tapestry5.internal.services;
16
17 import org.apache.tapestry5.EventConstants;
18 import org.apache.tapestry5.internal.structure.ComponentPageElement;
19 import org.apache.tapestry5.internal.structure.Page;
20 import org.apache.tapestry5.ioc.internal.util.TapestryException;
21 import org.apache.tapestry5.services;
22
23 import java.io.IOException;
24
25 public class ComponentEventRequestHandlerImpl implements ComponentEventRequestHandler
26 {
27 private final ComponentEventResultProcessor resultProcessor;
28
29 private final RequestPageCache cache;
30
31 private final Response response;
32
33 private final ActionRenderResponseGenerator generator;
34
35 private final Environment environment;
36
37 public ComponentEventRequestHandlerImpl(@Traditional ComponentEventResultProcessor resultProcessor,
38 RequestPageCache cache, Response response,
39 ActionRenderResponseGenerator generator, Environment environment)
40 {
41 this.resultProcessor = resultProcessor;
42 this.cache = cache;
43 this.response = response;
44 this.generator = generator;
45 this.environment = environment;
46 }
47
48 public void handle(ComponentEventRequestParameters parameters) throws IOException
49 {
50 Page activePage = cache.get(parameters.getActivePageName());
51
52 ComponentResultProcessorWrapper callback = new ComponentResultProcessorWrapper(resultProcessor);
53
54 // If activating the page returns a "navigational result", then don't trigger the action
55 // on the component.
56
57 activePage.getRootElement().triggerContextEvent(EventConstants.ACTIVATE,
58 parameters.getPageActivationContext(), callback);
59
60 if (callback.isAborted()) return;
61
62 Page containerPage = cache.get(parameters.getContainingPageName());
63
64 ComponentPageElement element = containerPage.getComponentElementByNestedId(parameters.getNestedComponentId());
65
66 environment.push(ComponentEventResultProcessor.class, resultProcessor);
67
68 boolean handled = element.triggerContextEvent(parameters.getEventType(), parameters.getEventContext(),
69 callback);
70
71 if (!handled)
72 throw new TapestryException(ServicesMessages.eventNotHandled(element, parameters.getEventType()), element,
73 null);
74
75 environment.pop(ComponentEventResultProcessor.class);
76
77 if (callback.isAborted()) return;
78
79 if (!response.isCommitted()) generator.generateResponse(activePage);
80 }
81 }