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