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.beanmodel.services.*; 017import org.apache.tapestry5.commons.internal.util.TapestryException; 018import org.apache.tapestry5.http.services.Response; 019import org.apache.tapestry5.internal.structure.ComponentPageElement; 020import org.apache.tapestry5.internal.structure.Page; 021import org.apache.tapestry5.ioc.annotations.Primary; 022import org.apache.tapestry5.services.ComponentEventRequestHandler; 023import org.apache.tapestry5.services.ComponentEventRequestParameters; 024import org.apache.tapestry5.services.ComponentEventResultProcessor; 025import org.apache.tapestry5.services.Environment; 026import org.apache.tapestry5.services.Traditional; 027 028import java.io.IOException; 029 030@SuppressWarnings("unchecked") 031public class ComponentEventRequestHandlerImpl implements ComponentEventRequestHandler 032{ 033 private final ComponentEventResultProcessor resultProcessor; 034 035 private final RequestPageCache cache; 036 037 private final Response response; 038 039 private final PageActivator pageActivator; 040 041 private final Environment environment; 042 043 public ComponentEventRequestHandlerImpl(@Traditional 044 @Primary 045 ComponentEventResultProcessor resultProcessor, 046 047 RequestPageCache cache, Response response, 048 049 PageActivator pageActivator, 050 051 Environment environment) 052 { 053 this.resultProcessor = resultProcessor; 054 this.cache = cache; 055 this.response = response; 056 this.pageActivator = pageActivator; 057 this.environment = environment; 058 } 059 060 public void handle(ComponentEventRequestParameters parameters) throws IOException 061 { 062 Page activePage = cache.get(parameters.getActivePageName()); 063 064 if (pageActivator.activatePage(activePage.getRootElement().getComponentResources(), parameters 065 .getPageActivationContext(), resultProcessor)) 066 { 067 return; 068 } 069 070 Page containerPage = cache.get(parameters.getContainingPageName()); 071 072 TrackableComponentEventCallback callback = new ComponentResultProcessorWrapper(resultProcessor); 073 074 environment.push(ComponentEventResultProcessor.class, resultProcessor); 075 environment.push(TrackableComponentEventCallback.class, callback); 076 077 ComponentPageElement element = containerPage.getComponentElementByNestedId(parameters.getNestedComponentId()); 078 079 boolean handled = element.triggerContextEvent(parameters.getEventType(), parameters.getEventContext(), callback); 080 081 if (!handled) 082 { 083 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, 084 null); 085 } 086 087 environment.pop(TrackableComponentEventCallback.class); 088 environment.pop(ComponentEventResultProcessor.class); 089 090 if (callback.isAborted()) 091 { 092 callback.rethrow(); 093 return; 094 } 095 096 // If we get this far without generating a response, the default behavior is to 097 // generate a redirect back to the active page; we can let the ComponentEventResultProcessor handle that. 098 099 if (!response.isCommitted()) 100 { 101 resultProcessor.processResultValue(activePage.getName()); 102 } 103 } 104}