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