001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. 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 distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.navigate.spring;
016:
017: import java.util.Locale;
018:
019: import javax.servlet.http.HttpServletRequest;
020:
021: import org.apache.struts.Globals;
022: import org.springframework.web.servlet.DispatcherServlet;
023: import org.springframework.web.servlet.LocaleResolver;
024: import org.springframework.web.servlet.ModelAndView;
025: import org.springframework.web.servlet.View;
026: import org.springframework.web.servlet.ViewResolver;
027: import org.strecks.context.ActionContext;
028: import org.strecks.exceptions.ApplicationConfigurationException;
029: import org.strecks.exceptions.ApplicationRuntimeException;
030: import org.strecks.navigate.NavigationHandler;
031: import org.strecks.navigate.spring.annotation.SpringView;
032: import org.strecks.spring.SpringUtils;
033: import org.strecks.util.StringUtils;
034: import org.strecks.view.ViewAdapter;
035:
036: /**
037: * Implements the Spring view handling navigation functionality
038: * @author Phil Zoio
039: */
040: public class SpringViewNavigationHandler implements
041: NavigationHandler<ModelAndView> {
042:
043: private String name;
044:
045: private boolean useResolver;
046:
047: private LocaleResolver localeResolver;
048:
049: public SpringViewNavigationHandler(String name) {
050: super ();
051:
052: if (StringUtils.notBlankOrNull(name)) {
053: this .name = name;
054: useResolver = true;
055: }
056:
057: // TODO - introduce the capability of parameterizing the LocaleResolver?
058: localeResolver = new StrecksLocaleResolver();
059:
060: }
061:
062: public ViewAdapter getActionForward(ActionContext actionContext,
063: ModelAndView modelAndView) {
064:
065: // update the request with the LocaleResolver
066: HttpServletRequest request = actionContext.getRequest();
067: request.setAttribute(
068: DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,
069: localeResolver);
070:
071: View view = null;
072:
073: if (modelAndView.isReference()) {
074:
075: // non-default resolver supplied: use resolver to get view
076: if (useResolver) {
077:
078: Object bean = SpringUtils.getSpringBean(actionContext
079: .getContext(), name);
080: if (!(bean instanceof ViewResolver)) {
081: throw new ApplicationConfigurationException(
082: "Bean referenced using the 'resolver' attribute of @"
083: + SpringView.class.getSimpleName()
084: + " annotation is not instance of "
085: + ViewResolver.class.getName());
086: }
087:
088: ViewResolver viewResolver = (ViewResolver) bean;
089: String viewName = modelAndView.getViewName();
090:
091: try {
092: Locale locale = (Locale) request
093: .getAttribute(Globals.LOCALE_KEY);
094: view = viewResolver.resolveViewName(viewName,
095: locale);
096: } catch (Exception e) {
097: throw new ApplicationRuntimeException(
098: "Exception thrown during resolution of view name "
099: + viewName + " using ViewResolver "
100: + viewResolver.getClass(), e);
101: }
102:
103: }
104: // no resolver supplied: use name to look up View
105: else {
106: String viewName = modelAndView.getViewName();
107: Object bean = SpringUtils.getSpringBean(actionContext
108: .getContext(), viewName);
109: if (!(bean instanceof View)) {
110: throw new ApplicationConfigurationException(
111: "Bean referenced using the 'viewName' property of the returned ModelAndView of the method containing @"
112: + SpringView.class.getSimpleName()
113: + " is not an instance of "
114: + View.class.getName());
115: }
116:
117: view = (View) bean;
118: }
119:
120: } else {
121: view = modelAndView.getView();
122: }
123: return new SpringRenderingViewAdapter(view, modelAndView
124: .getModel());
125:
126: }
127:
128: LocaleResolver getLocaleResolver() {
129: return localeResolver;
130: }
131:
132: String getName() {
133: return name;
134: }
135:
136: boolean isUseResolver() {
137: return useResolver;
138: }
139:
140: }
|