001// Copyright 2007, 2008, 2010, 2011, 2012 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.SymbolConstants;
019import org.apache.tapestry5.beanmodel.services.*;
020import org.apache.tapestry5.http.services.Dispatcher;
021import org.apache.tapestry5.http.services.Request;
022import org.apache.tapestry5.http.services.Response;
023import org.apache.tapestry5.internal.EmptyEventContext;
024import org.apache.tapestry5.ioc.annotations.Symbol;
025import org.apache.tapestry5.services.ComponentClassResolver;
026import org.apache.tapestry5.services.ComponentRequestHandler;
027import org.apache.tapestry5.services.LocalizationSetter;
028import org.apache.tapestry5.services.PageRenderRequestParameters;
029
030import java.io.IOException;
031
032/**
033 * Recognizes a request for the application root (i.e., "/") and handles this the same as a render request for the
034 * "Start" page. Support for the Start page is kept for legacy purposes, Index pages are the correct approach.
035 */
036public class RootPathDispatcher implements Dispatcher
037{
038    private static final EventContext EMPTY_CONTEXT = new EmptyEventContext();
039
040    private final ComponentClassResolver componentClassResolver;
041
042    private final ComponentRequestHandler handler;
043
044    private final String startPageName;
045
046    private final PageRenderRequestParameters parameters;
047
048    private final LocalizationSetter localizationSetter;
049
050    public RootPathDispatcher(ComponentClassResolver componentClassResolver,
051
052                              ComponentRequestHandler handler,
053
054                              @Symbol(SymbolConstants.START_PAGE_NAME)
055                              String startPageName, LocalizationSetter localizationSetter)
056    {
057        this.componentClassResolver = componentClassResolver;
058        this.handler = handler;
059        this.startPageName = startPageName;
060        this.localizationSetter = localizationSetter;
061
062        parameters = new PageRenderRequestParameters(this.startPageName, EMPTY_CONTEXT, false);
063    }
064
065    public boolean dispatch(Request request, final Response response) throws IOException
066    {
067        // Only match the root path
068
069        if (request.getPath().equals("/") && componentClassResolver.isPageName(startPageName))
070        {
071            localizationSetter.setNonPersistentLocaleFromLocaleName(request.getLocale().toString());
072
073            handler.handlePageRender(parameters);
074
075            return true;
076        }
077
078        return false;
079    }
080
081}