All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.skyway.spring.util.viewresolution.UserAgentViewResolver Maven / Gradle / Ivy

The newest version!
/**
* Copyright 2007 - 2011 Skyway Software, Inc.
*/
package org.skyway.spring.util.viewresolution;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

/**
 * This View Resolver will resolve views based on the user-agent header from the HttpServletRequest.
 * To configure the view resolver, in the web-context.xml file of your project, add a bean definition that specifies
 * the substring to match in the user-agent, a prefix and/or a suffix to append to the logical view names.  
 *
 * @author CConway
 *
 */
public class UserAgentViewResolver extends UrlBasedViewResolver {

	private String agentSubstring;

	/**
	 * Resolve a symbolic view name to a physical page.  This method inspects the 
	 * user-agent header for a substring matching the given agentSubstring.  
	 * If a match is found, this ViewResolver is used to resolve the view.  
	 * If the agentSubstring does not appear in the header null is returned so another
	 * resolver can attempt to resolve the view.
	 */
	public View resolveViewName(String viewName, Locale locale) throws Exception {
		
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
		String agent = request.getHeader("user-agent");
		
		if (agent.indexOf(agentSubstring) >=0 ) {
			return super.resolveViewName(viewName, locale);
		}
		
		return null;		
	}

	/**
	 * The substring to match in the user-agent header.
	 * 
	 * @return
	 */
	public String getAgentSubstring() {
		return agentSubstring;
	}

	public void setAgentSubstring(String agentSubstring) {
		this.agentSubstring = agentSubstring;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy