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