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.beanmodel.services.*;
018import org.apache.tapestry5.http.services.Request;
019import org.apache.tapestry5.internal.InternalConstants;
020import org.apache.tapestry5.internal.structure.Page;
021import org.apache.tapestry5.ioc.annotations.Primary;
022import org.apache.tapestry5.services.ComponentEventResultProcessor;
023import org.apache.tapestry5.services.PageRenderRequestHandler;
024import org.apache.tapestry5.services.PageRenderRequestParameters;
025import org.apache.tapestry5.services.Traditional;
026
027import java.io.IOException;
028
029/**
030 * Handles a page render request by activating and then rendering the page.
031 *
032 * @see org.apache.tapestry5.internal.services.PageRenderDispatcher
033 */
034@SuppressWarnings("unchecked")
035public class PageRenderRequestHandlerImpl implements PageRenderRequestHandler
036{
037    private final RequestPageCache cache;
038
039    private final ComponentEventResultProcessor resultProcessor;
040
041    private final PageResponseRenderer pageResponseRenderer;
042
043    private final PageActivator pageActivator;
044
045    private final Request request;
046
047    public PageRenderRequestHandlerImpl(RequestPageCache cache, @Traditional
048    @Primary
049    ComponentEventResultProcessor resultProcessor, PageResponseRenderer pageResponseRenderer,
050                                        PageActivator pageActivator, Request request)
051    {
052        this.cache = cache;
053        this.resultProcessor = resultProcessor;
054        this.pageResponseRenderer = pageResponseRenderer;
055        this.pageActivator = pageActivator;
056        this.request = request;
057    }
058
059    public void handle(PageRenderRequestParameters parameters) throws IOException
060    {
061        Page page = cache.get(parameters.getLogicalPageName());
062
063        if (request.getAttribute(InternalConstants.BYPASS_ACTIVATION) == null)
064        {
065            if (pageActivator.activatePage(page.getRootElement().getComponentResources(),
066                    parameters.getActivationContext(), resultProcessor))
067            {
068                return;
069            }
070
071            if (!parameters.isLoopback())
072            {
073                page.pageReset();
074            }
075        }
076
077        pageResponseRenderer.renderPageResponse(page);
078    }
079}