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