Skip to content

Instantly share code, notes, and snippets.

@alex4u2nv
Created December 31, 2012 01:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alex4u2nv/4416737 to your computer and use it in GitHub Desktop.
Save alex4u2nv/4416737 to your computer and use it in GitHub Desktop.
package com.swazzy.viewresolver;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mobile.device.Device;
import org.springframework.mobile.device.DeviceUtils;
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.ContentNegotiatingViewResolver;
import com.swazzy.controller.UserAgent;
public class HttpHeaderViewResolver extends ContentNegotiatingViewResolver {
/**
* @return the viewPrefixPattern
*/
static Logger log = Logger.getLogger(HttpHeaderViewResolver.class);
private String headerParam;
private Map<String, String> viewPrefixPattern;
@Autowired
UserAgent userAgent;
@Override
public View resolveViewName(String viewName, Locale locale)
throws Exception {
// Get the HTTP Header param "User-Agent"
String headerParamValue = ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes()).getRequest().getHeader(headerParam);
viewName = setViewName(viewName, headerParamValue);
return super.resolveViewName(viewName, locale);
}
private String setViewName(String viewName, String headerParamValue) {
if (userAgent.isRobot()) {
log.debug("Forwarding to robots/" + viewName);
return "robots/" + viewName;
} else if (userAgent.isMobile()) {
log.debug("Forwarding to mobile/" + viewName);
return "mobile/" + viewName;
}
return viewName;
}
/**
* @return the headerParam
*/
public String getHeaderParam() {
return headerParam;
}
/**
* @param headerParam
* the headerParam to set
*/
public void setHeaderParam(String headerParam) {
this.headerParam = headerParam;
}
/**
* @return the viewPrefixPattern
*/
public Map<String, String> getViewPrefixPattern() {
return viewPrefixPattern;
}
/**
* @param viewPrefixPattern
* the viewPrefixPattern to set
*/
public void setViewPrefixPattern(Map<String, String> viewPrefixPattern) {
this.viewPrefixPattern = viewPrefixPattern;
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<context:component-scan base-package="com.swazzy.mvc" />
<mvc:annotation-driven />
<bean id="tilesViewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
<property name="order" value="1" />
</bean>
<mvc:resources mapping="/static/**" location="/" />
<mvc:interceptors>
<bean
class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" />
</mvc:interceptors>
<mvc:default-servlet-handler />
<!-- freemarker config -->
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/views/" />
</bean>
<bean id="ftlViewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true" />
<property name="prefix" value="" />
<property name="suffix" value=".ftl" />
<property name="order" value="0" />
</bean>
<bean id="HttpHeaderViewResolver" class="com.swazzy.viewresolver.HttpHeaderViewResolver">
<property name="viewResolvers">
<list>
<ref bean="viewResolver" />
<ref bean="ftlViewResolver" />
</list>
</property>
<property name="headerParam" value="User-Agent" />
<property name="viewPrefixPattern" ref="userAgentPatterns" />
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<dependencies>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>2.2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.19</version>
</dependency>
<dependency>
<groupId>org.springframework.mobile</groupId>
<artifactId>spring-mobile-device</artifactId>
<version>1.1.0.M1</version>
</dependency>
</dependencies>
</project>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<util:map id="userAgentPatterns" map-class="java.util.HashMap" key-type="java.lang.String" value-type="java.lang.String">
<entry key="search-robot" value=".*(Googlebot|Yahoo|bingbot|acoon|aibot|AltaVista|amzn_assoc|ASPSeek|augurfind|Bigsearch|BlogSearch|DataparkSearch|search).*" />
</util:map>
</beans>
package com.swazzy.controller;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.http.HttpRequest;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.mobile.device.Device;
import org.springframework.mobile.device.DeviceUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/**
*
* @author "Alexander D. Mahabir"
*
*/
@Component("userAgent")
@Scope("session")
public class UserAgent implements Device{
static Logger log = Logger.getLogger(UserAgent.class);
private String useragent;
private Device device;
private String type;
private static final String USER_AGENT="User-Agent";
@Resource
@Qualifier("userAgentPatterns")
private Map<String, String> userAgentPatterns;
boolean set=false;
boolean robot=false;
/**
*
* @return
*/
public boolean isRobot() {
return robot;
}
/**
*
*/
@Override
public boolean isMobile() {
return device.isMobile();
}
/**
*
*/
@Override
public boolean isTablet() {
return device.isTablet();
}
/**
* @return the useragent
*/
public String getUseragent() {
return useragent;
}
/**
* @param useragent the useragent to set
*/
public void setUseragent(String useragent) {
this.useragent = useragent;
}
/**
* @return the device
*/
public Device getDevice() {
return device;
}
/**
* @param device the device to set
*/
public void setDevice(Device device) {
this.device = device;
}
/**
* @return the type
*/
public String getType() {
return type;
}
/**
* @param type the type to set
*/
public void setType(String type) {
this.type = type;
}
/**
*
* @param useragent
* @param device
* @param type
*/
public UserAgent(String useragent, Device device, String type) {
super();
this.useragent = useragent;
this.device = device;
this.type = type;
this.set=true;
}
/**
*
*/
public UserAgent() {
super();
}
/**
*
*/
@PostConstruct
private void init() {
try {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes()).getRequest();
device = DeviceUtils.getCurrentDevice(request);
this.useragent = request.getHeader(USER_AGENT);
parseTypes();
this.set = true;
} catch (Exception e) {
}
}
/**
*
*/
private void parseTypes() {
Iterator<Entry<String, String>> it = userAgentPatterns.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> pairs = (Map.Entry<String, String>) it
.next();
Pattern pattern = Pattern.compile(pairs.getValue(), Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(useragent);
if (matcher.find()) {
switch (pairs.getKey()) {
case "search-robot":
this.robot = true;
break;
default:
break;
}
this.type = pairs.getKey();
}
}
}
/**
* @return the set
*/
public boolean isSet() {
return set;
}
/**
* @param set the set to set
*/
public void setSet(boolean set) {
this.set = set;
}
@Override
public boolean isNormal() {
return device.isNormal();
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return String
.format("UserAgent [useragent=%s, device=%s, type=%s, userAgentPatterns=%s, set=%s, robot=%s]",
useragent, device, type, userAgentPatterns, set, robot);
}
/**
* @return the userAgentPatterns
*/
public Map<String, String> getUserAgentPatterns() {
return userAgentPatterns;
}
/**
* @param userAgentPatterns the userAgentPatterns to set
*/
public void setUserAgentPatterns(Map<String, String> userAgentPatterns) {
this.userAgentPatterns = userAgentPatterns;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment