    001// Copyright 2014 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.TapestryConstants;
    018import org.apache.tapestry5.beanmodel.services.*;
    019import org.apache.tapestry5.http.services.Request;
    020import org.apache.tapestry5.ioc.IOOperation;
    021import org.apache.tapestry5.ioc.OperationTracker;
    022import org.apache.tapestry5.services.ComponentEventRequestParameters;
    023import org.apache.tapestry5.services.ComponentRequestFilter;
    024import org.apache.tapestry5.services.ComponentRequestHandler;
    025import org.apache.tapestry5.services.PageRenderRequestParameters;
    026
    027import java.io.IOException;
    028
    029/**
    030 * After processing the component event request (including Ajax requests), or the page render request,
    031 * checks for the {@link org.apache.tapestry5.TapestryConstants#RESPONSE_RENDERER} request attribute,
    032 * and invokes it to render the deferred response.
    033 *
    034 * @since 5.4
    035 */
    036public class DeferredResponseRenderer implements ComponentRequestFilter
    037{
    038    private final Request request;
    039
    040    private final OperationTracker tracker;
    041
    042    public DeferredResponseRenderer(Request request, OperationTracker tracker)
    043    {
    044        this.request = request;
    045        this.tracker = tracker;
    046    }
    047
    048    public void handleComponentEvent(ComponentEventRequestParameters parameters, ComponentRequestHandler handler) throws IOException
    049    {
    050        handler.handleComponentEvent(parameters);
    051
    052        invokeQueuedRenderer();
    053    }
    054
    055    public void handlePageRender(PageRenderRequestParameters parameters, ComponentRequestHandler handler) throws IOException
    056    {
    057        handler.handlePageRender(parameters);
    058
    059        invokeQueuedRenderer();
    060    }
    061
    062    private void invokeQueuedRenderer() throws IOException
    063    {
    064        while (true)
    065        {
    066
    067            IOOperation responseRenderer = (IOOperation) request.getAttribute(TapestryConstants.RESPONSE_RENDERER);
    068
    069            if (responseRenderer == null)
    070            {
    071                break;
    072            }
    073
    074            // There's a particular case where an operation puts a different operation into the attribute;
    075            // we'll handle that on the next pass.
    076            request.setAttribute(TapestryConstants.RESPONSE_RENDERER, null);
    077
    078            tracker.perform("Executing deferred response renderer.", responseRenderer);
    079        }
    080    }
    081}