/*
* Copyright 2005-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.strecks.navigate.spring;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.Globals;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.strecks.context.ActionContext;
import org.strecks.exceptions.ApplicationConfigurationException;
import org.strecks.exceptions.ApplicationRuntimeException;
import org.strecks.navigate.NavigationHandler;
import org.strecks.navigate.spring.annotation.SpringView;
import org.strecks.spring.SpringUtils;
import org.strecks.util.StringUtils;
import org.strecks.view.ViewAdapter;
/**
* Implements the Spring view handling navigation functionality
* @author Phil Zoio
*/
public class SpringViewNavigationHandler implements NavigationHandler<ModelAndView>
{
private String name;
private boolean useResolver;
private LocaleResolver localeResolver;
public SpringViewNavigationHandler(String name)
{
super();
if (StringUtils.notBlankOrNull(name))
{
this.name = name;
useResolver = true;
}
// TODO - introduce the capability of parameterizing the LocaleResolver?
localeResolver = new StrecksLocaleResolver();
}
public ViewAdapter getActionForward(ActionContext actionContext, ModelAndView modelAndView)
{
// update the request with the LocaleResolver
HttpServletRequest request = actionContext.getRequest();
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, localeResolver);
View view = null;
if (modelAndView.isReference())
{
// non-default resolver supplied: use resolver to get view
if (useResolver)
{
Object bean = SpringUtils.getSpringBean(actionContext.getContext(), name);
if (!(bean instanceof ViewResolver))
{
throw new ApplicationConfigurationException("Bean referenced using the 'resolver' attribute of @"
+ SpringView.class.getSimpleName() + " annotation is not instance of "
+ ViewResolver.class.getName());
}
ViewResolver viewResolver = (ViewResolver) bean;
String viewName = modelAndView.getViewName();
try
{
Locale locale = (Locale) request.getAttribute(Globals.LOCALE_KEY);
view = viewResolver.resolveViewName(viewName, locale);
}
catch (Exception e)
{
throw new ApplicationRuntimeException("Exception thrown during resolution of view name " + viewName
+ " using ViewResolver " + viewResolver.getClass(), e);
}
}
// no resolver supplied: use name to look up View
else
{
String viewName = modelAndView.getViewName();
Object bean = SpringUtils.getSpringBean(actionContext.getContext(), viewName);
if (!(bean instanceof View))
{
throw new ApplicationConfigurationException(
"Bean referenced using the 'viewName' property of the returned ModelAndView of the method containing @"
+ SpringView.class.getSimpleName() + " is not an instance of " + View.class.getName());
}
view = (View) bean;
}
}
else
{
view = modelAndView.getView();
}
return new SpringRenderingViewAdapter(view, modelAndView.getModel());
}
LocaleResolver getLocaleResolver()
{
return localeResolver;
}
String getName()
{
return name;
}
boolean isUseResolver()
{
return useResolver;
}
}