001// Copyright 2010-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.EventContext;
018import org.apache.tapestry5.TapestryConstants;
019import org.apache.tapestry5.internal.EmptyEventContext;
020import org.apache.tapestry5.internal.InternalConstants;
021import org.apache.tapestry5.ioc.IOOperation;
022import org.apache.tapestry5.ioc.services.TypeCoercer;
023import org.apache.tapestry5.services.*;
024
025import java.io.IOException;
026
027/**
028 * Used to trigger the rendering of a particular page without causing a redirect to that page.
029 * The content of the page is just streamed to the client.
030 *
031 * @since 5.2.0
032 */
033public class StreamPageContentResultProcessor implements ComponentEventResultProcessor<StreamPageContent>
034{
035    private final PageRenderRequestHandler handler;
036
037    private final ComponentClassResolver resolver;
038
039    private final TypeCoercer typeCoercer;
040
041    private final RequestGlobals requestGlobals;
042
043    private final Request request;
044
045    public StreamPageContentResultProcessor(PageRenderRequestHandler handler, ComponentClassResolver resolver, TypeCoercer typeCoercer, RequestGlobals requestGlobals, Request request)
046    {
047        this.handler = handler;
048        this.resolver = resolver;
049        this.typeCoercer = typeCoercer;
050        this.requestGlobals = requestGlobals;
051        this.request = request;
052    }
053
054    public void processResultValue(StreamPageContent value) throws IOException
055    {
056        Class<?> pageClass = value.getPageClass();
057        Object[] activationContext = value.getPageActivationContext();
058
059        final String pageName = pageClass == null
060                ? requestGlobals.getActivePageName()
061                : resolver.resolvePageClassNameToPageName(pageClass.getName());
062
063        final EventContext context = activationContext == null
064                ? new EmptyEventContext()
065                : new ArrayEventContext(typeCoercer, activationContext);
066
067        if (value.isBypassActivation())
068        {
069            request.setAttribute(InternalConstants.BYPASS_ACTIVATION, true);
070        }
071
072        request.setAttribute(TapestryConstants.RESPONSE_RENDERER, new IOOperation<Void>()
073        {
074            public Void perform() throws IOException
075            {
076                handler.handle(new PageRenderRequestParameters(pageName, context, false));
077
078                return null;
079            }
080        });
081    }
082}