Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * User: mcucchiara
- */
- public class MobileInterceptor extends AbstractInterceptor {
- public static final Pattern MOBILE_PATTERN = Pattern.compile(".*(iphone|ipod|android.*mobile|webos|netfront|opera mini|semc-browser|playstation portable|nintendo wii|blackberry).*");
- @Override
- public String intercept(ActionInvocation invocation) throws Exception {
- String viewType = getViewTypeFromCookie();
- if (viewType == null) {
- viewType = getViewTypeFromRequest();
- }
- if (viewType == null) {
- viewType = getViewTypeFromUserAgent();
- }
- //put the value inside the valueStack
- ValueStack stack = invocation.getStack();
- stack.getContext().put(AppConstants.VIEW_TYPE, viewType);
- Object action = invocation.getAction();
- if (action instanceof ViewFolderAware) {
- ((ViewFolderAware) action).setViewFolder(viewType);
- }
- return invocation.invoke();
- }
- private String getViewTypeFromUserAgent() {
- String userAgent = ServletActionContext.getRequest().getHeader("User-Agent");
- if (userAgent != null) {
- Matcher m = MOBILE_PATTERN.matcher(userAgent.toLowerCase());
- if (m.matches()) {
- return AppConstants.MOBILE_VIEW;
- }
- }
- return AppConstants.DEFAULT_VIEW;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment