001// Copyright 2007-2013 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 java.io.IOException;
018
019import org.apache.tapestry5.TrackableComponentEventCallback;
020import org.apache.tapestry5.internal.InternalConstants;
021import org.apache.tapestry5.internal.structure.ComponentPageElement;
022import org.apache.tapestry5.internal.structure.Page;
023import org.apache.tapestry5.internal.util.Holder;
024import org.apache.tapestry5.ioc.internal.util.TapestryException;
025import org.apache.tapestry5.json.JSONObject;
026import org.apache.tapestry5.services.Ajax;
027import org.apache.tapestry5.services.ComponentEventRequestHandler;
028import org.apache.tapestry5.services.ComponentEventRequestParameters;
029import org.apache.tapestry5.services.ComponentEventResultProcessor;
030import org.apache.tapestry5.services.Environment;
031import org.apache.tapestry5.services.Request;
032
033/**
034 * Similar to {@link ComponentEventRequestHandlerImpl}, but built around the Ajax request cycle, where the action
035 * request sends back an immediate JSON response containing the new content.
036 */
037@SuppressWarnings({"unchecked", "rawtypes"})
038public class AjaxComponentEventRequestHandler implements ComponentEventRequestHandler
039{
040    private final RequestPageCache cache;
041
042    private final Request request;
043
044    private final PageRenderQueue queue;
045
046    private final ComponentEventResultProcessor resultProcessor;
047
048    private final Environment environment;
049
050    private final AjaxPartialResponseRenderer partialRenderer;
051
052    private final PageActivator pageActivator;
053
054    public AjaxComponentEventRequestHandler(RequestPageCache cache, Request request, PageRenderQueue queue, @Ajax
055    ComponentEventResultProcessor resultProcessor, PageActivator pageActivator,
056                                            Environment environment,
057                                            AjaxPartialResponseRenderer partialRenderer)
058    {
059        this.cache = cache;
060        this.queue = queue;
061        this.resultProcessor = resultProcessor;
062        this.pageActivator = pageActivator;
063        this.request = request;
064        this.environment = environment;
065        this.partialRenderer = partialRenderer;
066    }
067
068    public void handle(ComponentEventRequestParameters parameters) throws IOException
069    {
070        Page activePage = cache.get(parameters.getActivePageName());
071
072        final Holder<Boolean> resultProcessorInvoked = Holder.create();
073        resultProcessorInvoked.put(false);
074
075        ComponentEventResultProcessor interceptor = new ComponentEventResultProcessor()
076        {
077            public void processResultValue(Object value) throws IOException
078            {
079                resultProcessorInvoked.put(true);
080
081                resultProcessor.processResultValue(value);
082            }
083        };
084
085        // If we end up doing a partial render, the page render queue service needs to know the
086        // page that will be rendered (for logging purposes, if nothing else).
087
088        queue.setRenderingPage(activePage);
089
090        if (pageActivator.activatePage(activePage.getRootElement().getComponentResources(), parameters
091                .getPageActivationContext(), interceptor))
092            return;
093
094        request.setAttribute(InternalConstants.PAGE_NAME_ATTRIBUTE_NAME, parameters.getActivePageName());
095
096        Page containerPage = cache.get(parameters.getContainingPageName());
097
098        ComponentPageElement element = containerPage.getComponentElementByNestedId(parameters.getNestedComponentId());
099
100        // In many cases, the triggered element is a Form that needs to be able to
101        // pass its event handler return values to the correct result processor.
102        // This is certainly the case for forms.
103
104        TrackableComponentEventCallback callback = new ComponentResultProcessorWrapper(interceptor);
105
106        environment.push(ComponentEventResultProcessor.class, interceptor);
107        environment.push(TrackableComponentEventCallback.class, callback);
108
109        boolean handled = element
110                .triggerContextEvent(parameters.getEventType(), parameters.getEventContext(), callback);
111
112        if (!handled)
113            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,
114                    null);
115
116        environment.pop(TrackableComponentEventCallback.class);
117        environment.pop(ComponentEventResultProcessor.class);
118
119
120        // If the result processor was passed a value, then it will already have rendered. Otherwise it was not passed a value,
121        // but it's still possible that we still want to do a partial page render ... if filters were added to the render queue.
122        // In that event, run the partial page render now and return.
123
124        boolean wasInvoked = resultProcessorInvoked.get();
125
126        if ((!wasInvoked) && queue.isPartialRenderInitialized())
127        {
128            partialRenderer.renderPartialPageMarkup();
129            return;
130        }
131
132        // If the result processor was passed a value, then it will already have rendered, and there is nothing more to do.
133
134        if (wasInvoked) { return; }
135
136        // Send an empty JSON reply if no value was returned from the component event handler method.
137        // This is the typical behavior when an Ajax component event handler returns null. It still
138        // will go through a pipeline that will add information related to partial page rendering.
139
140        resultProcessor.processResultValue(new JSONObject());
141    }
142}