001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.common.web.view;
025:
026: import java.util.ArrayList;
027: import java.util.Collections;
028: import java.util.Iterator;
029: import java.util.Locale;
030: import java.util.Map;
031:
032: import javax.servlet.http.HttpServletRequest;
033:
034: import org.springframework.beans.factory.BeanFactoryUtils;
035: import org.springframework.beans.factory.ListableBeanFactory;
036: import org.springframework.beans.factory.NoSuchBeanDefinitionException;
037: import org.springframework.core.OrderComparator;
038: import org.springframework.web.servlet.DispatcherServlet;
039: import org.springframework.web.servlet.ModelAndView;
040: import org.springframework.web.servlet.RequestToViewNameTranslator;
041: import org.springframework.web.servlet.View;
042: import org.springframework.web.servlet.ViewResolver;
043: import org.springframework.web.servlet.support.RequestContextUtils;
044: import org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator;
045:
046: /**
047: * Helper class that resolves views the same way as a {@link DispatcherServlet}.
048: */
049: public class ViewResolverHelper {
050:
051: private ArrayList viewResolvers;
052:
053: private RequestToViewNameTranslator viewNameTranslator;
054:
055: public ViewResolverHelper(ListableBeanFactory beanFactory) {
056: Map matchingBeans = BeanFactoryUtils
057: .beansOfTypeIncludingAncestors(beanFactory,
058: ViewResolver.class, true, false);
059:
060: if (!matchingBeans.isEmpty()) {
061: this .viewResolvers = new ArrayList(matchingBeans.values());
062: Collections.sort(this .viewResolvers, new OrderComparator());
063: }
064:
065: try {
066: this .viewNameTranslator = (RequestToViewNameTranslator) beanFactory
067: .getBean(
068: DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME,
069: RequestToViewNameTranslator.class);
070: } catch (NoSuchBeanDefinitionException ex) {
071: this .viewNameTranslator = new DefaultRequestToViewNameTranslator();
072: }
073: }
074:
075: public View resolveView(HttpServletRequest request, ModelAndView mv)
076: throws ViewResolutionException {
077:
078: if (mv.hasView() && !mv.isReference()) {
079: return mv.getView();
080: }
081: return resolveView(request, mv.getViewName());
082: }
083:
084: public View resolveView(HttpServletRequest request, String viewName)
085: throws ViewResolutionException {
086:
087: try {
088: if (viewName == null) {
089: viewName = viewNameTranslator.getViewName(request);
090: }
091: Iterator i = viewResolvers.iterator();
092: while (i.hasNext()) {
093: ViewResolver viewResolver = (ViewResolver) i.next();
094: Locale locale = RequestContextUtils.getLocale(request);
095: View view = viewResolver.resolveViewName(viewName,
096: locale);
097: if (view != null) {
098: return view;
099: }
100: }
101: } catch (Exception e) {
102: throw new ViewResolutionException(viewName, e);
103: }
104: throw new ViewResolutionException(viewName);
105: }
106:
107: }
|