Guest User

MobileInterceptor

a guest
Jan 15th, 2012
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1. /**
  2.  * User: mcucchiara
  3.  */
  4. public class MobileInterceptor extends AbstractInterceptor {
  5.  
  6.     public static final Pattern MOBILE_PATTERN = Pattern.compile(".*(iphone|ipod|android.*mobile|webos|netfront|opera mini|semc-browser|playstation portable|nintendo wii|blackberry).*");
  7.  
  8.     @Override
  9.     public String intercept(ActionInvocation invocation) throws Exception {
  10.  
  11.         String viewType = getViewTypeFromCookie();
  12.         if (viewType == null) {
  13.             viewType = getViewTypeFromRequest();
  14.         }
  15.         if (viewType == null) {
  16.             viewType = getViewTypeFromUserAgent();
  17.         }
  18.  
  19.         //put the value inside the valueStack
  20.         ValueStack stack = invocation.getStack();
  21.         stack.getContext().put(AppConstants.VIEW_TYPE, viewType);
  22.         Object action = invocation.getAction();
  23.         if (action instanceof ViewFolderAware) {
  24.             ((ViewFolderAware) action).setViewFolder(viewType);
  25.         }
  26.         return invocation.invoke();
  27.  
  28.     }
  29.  
  30.     private String getViewTypeFromUserAgent() {
  31.         String userAgent = ServletActionContext.getRequest().getHeader("User-Agent");
  32.         if (userAgent != null) {
  33.             Matcher m = MOBILE_PATTERN.matcher(userAgent.toLowerCase());
  34.             if (m.matches()) {
  35.                 return AppConstants.MOBILE_VIEW;
  36.             }
  37.         }
  38.         return AppConstants.DEFAULT_VIEW;
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment