001// Licensed under the Apache License, Version 2.0 (the "License"); 002// you may not use this file except in compliance with the License. 003// You may obtain a copy of the License at 004// 005// http://www.apache.org/licenses/LICENSE-2.0 006// 007// Unless required by applicable law or agreed to in writing, software 008// distributed under the License is distributed on an "AS IS" BASIS, 009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 010// See the License for the specific language governing permissions and 011// limitations under the License. 012 013package org.apache.tapestry5.internal.services; 014 015import org.apache.tapestry5.TrackableComponentEventCallback; 016import org.apache.tapestry5.internal.structure.ComponentPageElement; 017import org.apache.tapestry5.internal.structure.Page; 018import org.apache.tapestry5.ioc.annotations.Primary; 019import org.apache.tapestry5.ioc.internal.util.TapestryException; 020import org.apache.tapestry5.services.*; 021 022import java.io.IOException; 023 024@SuppressWarnings("unchecked") 025public class ComponentEventRequestHandlerImpl implements ComponentEventRequestHandler 026{ 027 private final ComponentEventResultProcessor resultProcessor; 028 029 private final RequestPageCache cache; 030 031 private final Response response; 032 033 private final PageActivator pageActivator; 034 035 private final Environment environment; 036 037 public ComponentEventRequestHandlerImpl(@Traditional 038 @Primary 039 ComponentEventResultProcessor resultProcessor, 040 041 RequestPageCache cache, Response response, 042 043 PageActivator pageActivator, 044 045 Environment environment) 046 { 047 this.resultProcessor = resultProcessor; 048 this.cache = cache; 049 this.response = response; 050 this.pageActivator = pageActivator; 051 this.environment = environment; 052 } 053 054 public void handle(ComponentEventRequestParameters parameters) throws IOException 055 { 056 Page activePage = cache.get(parameters.getActivePageName()); 057 058 if (pageActivator.activatePage(activePage.getRootElement().getComponentResources(), parameters 059 .getPageActivationContext(), resultProcessor)) 060 { 061 return; 062 } 063 064 Page containerPage = cache.get(parameters.getContainingPageName()); 065 066 TrackableComponentEventCallback callback = new ComponentResultProcessorWrapper(resultProcessor); 067 068 environment.push(ComponentEventResultProcessor.class, resultProcessor); 069 environment.push(TrackableComponentEventCallback.class, callback); 070 071 ComponentPageElement element = containerPage.getComponentElementByNestedId(parameters.getNestedComponentId()); 072 073 boolean handled = element.triggerContextEvent(parameters.getEventType(), parameters.getEventContext(), callback); 074 075 if (!handled) 076 { 077 throw new TapestryException(String.format("Request event '%s' (on component %s) was not handled; you must provide a matching event handler method in the component or in one of its containers.", parameters.getEventType(), element.getCompleteId()), element, 078 null); 079 } 080 081 environment.pop(TrackableComponentEventCallback.class); 082 environment.pop(ComponentEventResultProcessor.class); 083 084 if (callback.isAborted()) 085 { 086 callback.rethrow(); 087 return; 088 } 089 090 // If we get this far without generating a response, the default behavior is to 091 // generate a redirect back to the active page; we can let the ComponentEventResultProcessor handle that. 092 093 if (!response.isCommitted()) 094 { 095 resultProcessor.processResultValue(activePage.getName()); 096 } 097 } 098}