Commit 39e359c76d7f083a5b1c3f3af4c6cb4e148278f4
- Diff rendering mode:
- inline
- side by side
spring-mobile-device/src/main/java/org/springframework/mobile/device/mvc/DeviceResolverHandlerInterceptor.java
(100 / 0)
|   | |||
| 1 | /* | ||
| 2 | * Copyright 2010 the original author or authors. | ||
| 3 | * | ||
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | * you may not use this file except in compliance with the License. | ||
| 6 | * You may obtain a copy of the License at | ||
| 7 | * | ||
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | * | ||
| 10 | * Unless required by applicable law or agreed to in writing, software | ||
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | * See the License for the specific language governing permissions and | ||
| 14 | * limitations under the License. | ||
| 15 | */ | ||
| 16 | package org.springframework.mobile.device.mvc; | ||
| 17 | |||
| 18 | import javax.servlet.http.HttpServletRequest; | ||
| 19 | import javax.servlet.http.HttpServletResponse; | ||
| 20 | |||
| 21 | import org.springframework.mobile.device.Device; | ||
| 22 | import org.springframework.mobile.device.DeviceResolver; | ||
| 23 | import org.springframework.mobile.device.lite.LiteDeviceResolver; | ||
| 24 | import org.springframework.web.context.request.RequestAttributes; | ||
| 25 | import org.springframework.web.servlet.HandlerInterceptor; | ||
| 26 | import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; | ||
| 27 | |||
| 28 | /** | ||
| 29 | * A Spring MVC interceptor that resolves the Device that originated the web request <i>before</i> any request handler is invoked. | ||
| 30 | * The resolved Device is exported as a request attribute under the well-known name of {@link #CURRENT_DEVICE_ATTRIBUTE}. | ||
| 31 | * Request handlers such as @Controllers and views may then access the currentDevice to vary their control and rendering logic, respectively. | ||
| 32 | * @author Keith Donald | ||
| 33 | */ | ||
| 34 | public class DeviceResolverHandlerInterceptor extends HandlerInterceptorAdapter { | ||
| 35 | |||
| 36 | /** | ||
| 37 | * The name of the request attribute the current Device is indexed by. | ||
| 38 | * The attribute name is 'currentDevice'. | ||
| 39 | */ | ||
| 40 | public static final String CURRENT_DEVICE_ATTRIBUTE = "currentDevice"; | ||
| 41 | |||
| 42 | private final DeviceResolver deviceResolver; | ||
| 43 | |||
| 44 | /** | ||
| 45 | * Create a device resolving {@link HandlerInterceptor} that defaults to a {@link LiteDeviceResolver} implementation. | ||
| 46 | */ | ||
| 47 | public DeviceResolverHandlerInterceptor() { | ||
| 48 | this(new LiteDeviceResolver()); | ||
| 49 | } | ||
| 50 | |||
| 51 | /** | ||
| 52 | * Create a device resolving {@link HandlerInterceptor}. | ||
| 53 | * @param deviceResolver the device resolver to delegate to in {@link #preHandle(HttpServletRequest, HttpServletResponse, Object)}. | ||
| 54 | */ | ||
| 55 | public DeviceResolverHandlerInterceptor(DeviceResolver deviceResolver) { | ||
| 56 | this.deviceResolver = deviceResolver; | ||
| 57 | } | ||
| 58 | |||
| 59 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | ||
| 60 | Device device = deviceResolver.resolveDevice(request); | ||
| 61 | request.setAttribute(CURRENT_DEVICE_ATTRIBUTE, device); | ||
| 62 | return true; | ||
| 63 | } | ||
| 64 | |||
| 65 | /** | ||
| 66 | * Static utility method that extracts the current device from the web request. | ||
| 67 | * Encapsulates the {@link HttpServletRequest#getAttribute(String)} lookup. | ||
| 68 | * @param request the servlet request | ||
| 69 | * @return the current device, or null if no device has been resolved for the request | ||
| 70 | */ | ||
| 71 | public static Device getCurrentDevice(HttpServletRequest request) { | ||
| 72 | return (Device) request.getAttribute(DeviceResolverHandlerInterceptor.CURRENT_DEVICE_ATTRIBUTE); | ||
| 73 | } | ||
| 74 | |||
| 75 | /** | ||
| 76 | * Static utility method that extracts the current device from the web request. | ||
| 77 | * Encapsulates the {@link HttpServletRequest#getAttribute(String)} lookup. | ||
| 78 | * Throws a runtime exception if the current device has not been resolved. | ||
| 79 | * @param request the servlet request | ||
| 80 | * @return the current device | ||
| 81 | */ | ||
| 82 | public static Device getRequiredCurrentDevice(HttpServletRequest request) { | ||
| 83 | Device device = getCurrentDevice(request); | ||
| 84 | if (device == null) { | ||
| 85 | throw new IllegalStateException("No currenet device is set in this request and one is required - have you configured a DeviceResolvingHandlerInterceptor?"); | ||
| 86 | } | ||
| 87 | return device; | ||
| 88 | } | ||
| 89 | |||
| 90 | /** | ||
| 91 | * Static utility method that extracts the current device from the request attributes map. | ||
| 92 | * Encapsulates the {@link HttpServletRequest#getAttribute(String)} lookup. | ||
| 93 | * @param request the request attributes | ||
| 94 | * @return the current device, or null if no device has been resolved for the request | ||
| 95 | */ | ||
| 96 | public static Device getCurrentDevice(RequestAttributes attributes) { | ||
| 97 | return (Device) attributes.getAttribute(DeviceResolverHandlerInterceptor.CURRENT_DEVICE_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST); | ||
| 98 | } | ||
| 99 | |||
| 100 | } |
spring-mobile-device/src/main/java/org/springframework/mobile/device/mvc/DeviceResolvingHandlerInterceptor.java
(0 / 100)
|   | |||
| 1 | /* | ||
| 2 | * Copyright 2010 the original author or authors. | ||
| 3 | * | ||
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | * you may not use this file except in compliance with the License. | ||
| 6 | * You may obtain a copy of the License at | ||
| 7 | * | ||
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | * | ||
| 10 | * Unless required by applicable law or agreed to in writing, software | ||
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | * See the License for the specific language governing permissions and | ||
| 14 | * limitations under the License. | ||
| 15 | */ | ||
| 16 | package org.springframework.mobile.device.mvc; | ||
| 17 | |||
| 18 | import javax.servlet.http.HttpServletRequest; | ||
| 19 | import javax.servlet.http.HttpServletResponse; | ||
| 20 | |||
| 21 | import org.springframework.mobile.device.Device; | ||
| 22 | import org.springframework.mobile.device.DeviceResolver; | ||
| 23 | import org.springframework.mobile.device.lite.LiteDeviceResolver; | ||
| 24 | import org.springframework.web.context.request.RequestAttributes; | ||
| 25 | import org.springframework.web.servlet.HandlerInterceptor; | ||
| 26 | import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; | ||
| 27 | |||
| 28 | /** | ||
| 29 | * A Spring MVC interceptor that resolves the Device that originated the web request <i>before</i> any request handler is invoked. | ||
| 30 | * The resolved Device is exported as a request attribute under the well-known name of {@link #CURRENT_DEVICE_ATTRIBUTE}. | ||
| 31 | * Request handlers such as @Controllers and views may then access the currentDevice to vary their control and rendering logic, respectively. | ||
| 32 | * @author Keith Donald | ||
| 33 | */ | ||
| 34 | public class DeviceResolvingHandlerInterceptor extends HandlerInterceptorAdapter { | ||
| 35 | |||
| 36 | /** | ||
| 37 | * The name of the request attribute the current Device is indexed by. | ||
| 38 | * The attribute name is 'currentDevice'. | ||
| 39 | */ | ||
| 40 | public static final String CURRENT_DEVICE_ATTRIBUTE = "currentDevice"; | ||
| 41 | |||
| 42 | private final DeviceResolver deviceResolver; | ||
| 43 | |||
| 44 | /** | ||
| 45 | * Create a device resolving {@link HandlerInterceptor} that defaults to a {@link LiteDeviceResolver} implementation. | ||
| 46 | */ | ||
| 47 | public DeviceResolvingHandlerInterceptor() { | ||
| 48 | this(new LiteDeviceResolver()); | ||
| 49 | } | ||
| 50 | |||
| 51 | /** | ||
| 52 | * Create a device resolving {@link HandlerInterceptor}. | ||
| 53 | * @param deviceResolver the device resolver to delegate to in {@link #preHandle(HttpServletRequest, HttpServletResponse, Object)}. | ||
| 54 | */ | ||
| 55 | public DeviceResolvingHandlerInterceptor(DeviceResolver deviceResolver) { | ||
| 56 | this.deviceResolver = deviceResolver; | ||
| 57 | } | ||
| 58 | |||
| 59 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | ||
| 60 | Device device = deviceResolver.resolveDevice(request); | ||
| 61 | request.setAttribute(CURRENT_DEVICE_ATTRIBUTE, device); | ||
| 62 | return true; | ||
| 63 | } | ||
| 64 | |||
| 65 | /** | ||
| 66 | * Static utility method that extracts the current device from the web request. | ||
| 67 | * Encapsulates the {@link HttpServletRequest#getAttribute(String)} lookup. | ||
| 68 | * @param request the servlet request | ||
| 69 | * @return the current device, or null if no device has been resolved for the request | ||
| 70 | */ | ||
| 71 | public static Device getCurrentDevice(HttpServletRequest request) { | ||
| 72 | return (Device) request.getAttribute(DeviceResolvingHandlerInterceptor.CURRENT_DEVICE_ATTRIBUTE); | ||
| 73 | } | ||
| 74 | |||
| 75 | /** | ||
| 76 | * Static utility method that extracts the current device from the web request. | ||
| 77 | * Encapsulates the {@link HttpServletRequest#getAttribute(String)} lookup. | ||
| 78 | * Throws a runtime exception if the current device has not been resolved. | ||
| 79 | * @param request the servlet request | ||
| 80 | * @return the current device | ||
| 81 | */ | ||
| 82 | public static Device getRequiredCurrentDevice(HttpServletRequest request) { | ||
| 83 | Device device = getCurrentDevice(request); | ||
| 84 | if (device == null) { | ||
| 85 | throw new IllegalStateException("No currenet device is set in this request and one is required - have you configured a DeviceResolvingHandlerInterceptor?"); | ||
| 86 | } | ||
| 87 | return device; | ||
| 88 | } | ||
| 89 | |||
| 90 | /** | ||
| 91 | * Static utility method that extracts the current device from the request attributes map. | ||
| 92 | * Encapsulates the {@link HttpServletRequest#getAttribute(String)} lookup. | ||
| 93 | * @param request the request attributes | ||
| 94 | * @return the current device, or null if no device has been resolved for the request | ||
| 95 | */ | ||
| 96 | public static Device getCurrentDevice(RequestAttributes attributes) { | ||
| 97 | return (Device) attributes.getAttribute(DeviceResolvingHandlerInterceptor.CURRENT_DEVICE_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST); | ||
| 98 | } | ||
| 99 | |||
| 100 | } |
spring-mobile-device/src/main/java/org/springframework/mobile/device/mvc/DeviceWebArgumentResolver.java
(2 / 2)
|   | |||
| 22 | 22 | ||
| 23 | 23 | /** | |
| 24 | 24 | * Spring MVC {@link WebArgumentResolver} that resolves @Controller MethodParameters of type {@link Device} | |
| 25 | * to the value of the web request's {@link DeviceResolvingHandlerInterceptor#CURRENT_DEVICE_ATTRIBUTE current device} attribute. | ||
| 25 | * to the value of the web request's {@link DeviceResolverHandlerInterceptor#CURRENT_DEVICE_ATTRIBUTE current device} attribute. | ||
| 26 | 26 | * @author Keith Donald | |
| 27 | 27 | */ | |
| 28 | 28 | public class DeviceWebArgumentResolver implements WebArgumentResolver { | |
| 29 | 29 | ||
| 30 | 30 | public Object resolveArgument(MethodParameter param, NativeWebRequest request) throws Exception { | |
| 31 | 31 | if (Device.class.isAssignableFrom(param.getParameterType())) { | |
| 32 | return DeviceResolvingHandlerInterceptor.getCurrentDevice(request); | ||
| 32 | return DeviceResolverHandlerInterceptor.getCurrentDevice(request); | ||
| 33 | 33 | } else { | |
| 34 | 34 | return WebArgumentResolver.UNRESOLVED; | |
| 35 | 35 | } |
spring-mobile-device/src/main/java/org/springframework/mobile/device/site/CookieSitePreferenceRepository.java
(5 / 0)
|   | |||
| 34 | 34 | setCookieName(DEFAULT_COOKIE_NAME); | |
| 35 | 35 | } | |
| 36 | 36 | ||
| 37 | public CookieSitePreferenceRepository(String cookieDomain) { | ||
| 38 | setCookieName(DEFAULT_COOKIE_NAME); | ||
| 39 | setCookieDomain(cookieDomain); | ||
| 40 | } | ||
| 41 | |||
| 37 | 42 | public void saveSitePreference(SitePreference preference, HttpServletRequest request, HttpServletResponse response) { | |
| 38 | 43 | addCookie(response, preference.name()); | |
| 39 | 44 | } |
spring-mobile-device/src/main/java/org/springframework/mobile/device/site/SitePreferenceResolver.java
(97 / 0)
|   | |||
| 1 | /* | ||
| 2 | * Copyright 2010 the original author or authors. | ||
| 3 | * | ||
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | * you may not use this file except in compliance with the License. | ||
| 6 | * You may obtain a copy of the License at | ||
| 7 | * | ||
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | * | ||
| 10 | * Unless required by applicable law or agreed to in writing, software | ||
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | * See the License for the specific language governing permissions and | ||
| 14 | * limitations under the License. | ||
| 15 | */ | ||
| 16 | package org.springframework.mobile.device.site; | ||
| 17 | |||
| 18 | import javax.servlet.http.HttpServletRequest; | ||
| 19 | import javax.servlet.http.HttpServletResponse; | ||
| 20 | |||
| 21 | import org.springframework.mobile.device.Device; | ||
| 22 | import org.springframework.mobile.device.mvc.DeviceResolverHandlerInterceptor; | ||
| 23 | import org.springframework.web.context.request.RequestAttributes; | ||
| 24 | |||
| 25 | /** | ||
| 26 | * A helper that resolves the user's site preference and makes it available as a request attribute. | ||
| 27 | * Primarily used in support of the case where a user on a mobile device prefers to use the normal site. | ||
| 28 | * The site preference may be changed on behalf of a user by submitting the 'site_preference' query parameter. | ||
| 29 | * The preference value is saved in a repository so it can be remembered until the user decides to change it again. | ||
| 30 | * If no site preference is specified, preference to the mobile site will be given if the current device is a mobile device. | ||
| 31 | * The current user SitePreference is exported as a request attribute with the name {@link #CURRENT_SITE_PREFERENCE_ATTRIBUTE}. | ||
| 32 | * This allows handler mappings and view resolvers further down the line to vary their logic by site preference. | ||
| 33 | * @author Keith Donald | ||
| 34 | */ | ||
| 35 | public class SitePreferenceResolver { | ||
| 36 | |||
| 37 | public static final String CURRENT_SITE_PREFERENCE_ATTRIBUTE = "currentSitePreference"; | ||
| 38 | |||
| 39 | private final SitePreferenceRepository sitePreferenceRepository; | ||
| 40 | |||
| 41 | /** | ||
| 42 | * Creates a new site preference interceptor. | ||
| 43 | * @param sitePreferenceRepository the store for recording user site preference | ||
| 44 | */ | ||
| 45 | public SitePreferenceResolver(SitePreferenceRepository sitePreferenceRepository) { | ||
| 46 | this.sitePreferenceRepository = sitePreferenceRepository; | ||
| 47 | } | ||
| 48 | |||
| 49 | public SitePreference resolveSitePreference(HttpServletRequest request, HttpServletResponse response) { | ||
| 50 | SitePreference preference = getSitePreferenceQueryParameter(request); | ||
| 51 | if (preference != null) { | ||
| 52 | sitePreferenceRepository.saveSitePreference(preference, request, response); | ||
| 53 | } else { | ||
| 54 | preference = sitePreferenceRepository.loadSitePreference(request); | ||
| 55 | } | ||
| 56 | if (preference == null) { | ||
| 57 | preference = getDefaultSitePreferenceForDevice(DeviceResolverHandlerInterceptor.getCurrentDevice(request)); | ||
| 58 | } | ||
| 59 | if (preference != null) { | ||
| 60 | request.setAttribute(CURRENT_SITE_PREFERENCE_ATTRIBUTE, preference); | ||
| 61 | } | ||
| 62 | return preference; | ||
| 63 | } | ||
| 64 | |||
| 65 | // static factory methods | ||
| 66 | |||
| 67 | /** | ||
| 68 | * Get the current site preference for the user that originated this web request. | ||
| 69 | */ | ||
| 70 | public static SitePreference getCurrentSitePreference(HttpServletRequest request) { | ||
| 71 | return (SitePreference) request.getAttribute(CURRENT_SITE_PREFERENCE_ATTRIBUTE); | ||
| 72 | } | ||
| 73 | |||
| 74 | /** | ||
| 75 | * Get the current site preference for the user from the request attributes map. | ||
| 76 | */ | ||
| 77 | public static SitePreference getCurrentSitePreference(RequestAttributes attributes) { | ||
| 78 | return (SitePreference) attributes.getAttribute(CURRENT_SITE_PREFERENCE_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST); | ||
| 79 | } | ||
| 80 | |||
| 81 | // internal helpers | ||
| 82 | |||
| 83 | private SitePreference getSitePreferenceQueryParameter(HttpServletRequest request) { | ||
| 84 | String string = request.getParameter(SITE_PREFERENCE_PARAMETER); | ||
| 85 | return string != null && string.length() > 0 ? SitePreference.valueOf(string.toUpperCase()) : null; | ||
| 86 | } | ||
| 87 | |||
| 88 | private SitePreference getDefaultSitePreferenceForDevice(Device device) { | ||
| 89 | if (device == null) { | ||
| 90 | return null; | ||
| 91 | } | ||
| 92 | return device.isMobile() ? SitePreference.MOBILE : SitePreference.NORMAL; | ||
| 93 | } | ||
| 94 | |||
| 95 | private static final String SITE_PREFERENCE_PARAMETER = "site_preference"; | ||
| 96 | |||
| 97 | } |
spring-mobile-device/src/main/java/org/springframework/mobile/device/site/SitePreferenceResolvingHandlerInterceptor.java
(0 / 107)
|   | |||
| 1 | /* | ||
| 2 | * Copyright 2010 the original author or authors. | ||
| 3 | * | ||
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | * you may not use this file except in compliance with the License. | ||
| 6 | * You may obtain a copy of the License at | ||
| 7 | * | ||
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | * | ||
| 10 | * Unless required by applicable law or agreed to in writing, software | ||
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | * See the License for the specific language governing permissions and | ||
| 14 | * limitations under the License. | ||
| 15 | */ | ||
| 16 | package org.springframework.mobile.device.site; | ||
| 17 | |||
| 18 | import javax.servlet.http.HttpServletRequest; | ||
| 19 | import javax.servlet.http.HttpServletResponse; | ||
| 20 | |||
| 21 | import org.springframework.mobile.device.Device; | ||
| 22 | import org.springframework.mobile.device.mvc.DeviceResolvingHandlerInterceptor; | ||
| 23 | import org.springframework.web.context.request.RequestAttributes; | ||
| 24 | import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; | ||
| 25 | |||
| 26 | /** | ||
| 27 | * A Spring MVC interceptor that applies the user's site preference. | ||
| 28 | * Used to support the case where a user on a mobile device prefers to use the normal site. | ||
| 29 | * A site preference may be changed on behalf of a user by submitting the 'site_preference' query parameter. | ||
| 30 | * The preference value is saved in a repository so it can be remembered until the user decides to change it again. | ||
| 31 | * If no site preference is specified, preference to the mobile site will be given if the current device is a mobile device. | ||
| 32 | * The current user SitePreference is exported as a request attribute with the name {@link #CURRENT_SITE_PREFERENCE_ATTRIBUTE}. | ||
| 33 | * This allows handler mappings and view resolvers further down the line to vary their logic by site preference. | ||
| 34 | * @author Keith Donald | ||
| 35 | */ | ||
| 36 | public class SitePreferenceResolvingHandlerInterceptor extends HandlerInterceptorAdapter { | ||
| 37 | |||
| 38 | public static final String CURRENT_SITE_PREFERENCE_ATTRIBUTE = "currentSitePreference"; | ||
| 39 | |||
| 40 | private final SitePreferenceRepository sitePreferenceRepository; | ||
| 41 | |||
| 42 | /** | ||
| 43 | * Creates a new site preference interceptor that saves the user's site preference value in a cookie. | ||
| 44 | * @see CookieSitePreferenceRepository | ||
| 45 | */ | ||
| 46 | public SitePreferenceResolvingHandlerInterceptor() { | ||
| 47 | this(new CookieSitePreferenceRepository()); | ||
| 48 | } | ||
| 49 | |||
| 50 | /** | ||
| 51 | * Creates a new site preference interceptor. | ||
| 52 | * @param sitePreferenceRepository the store for recording user site preference | ||
| 53 | */ | ||
| 54 | public SitePreferenceResolvingHandlerInterceptor(SitePreferenceRepository sitePreferenceRepository) { | ||
| 55 | this.sitePreferenceRepository = sitePreferenceRepository; | ||
| 56 | } | ||
| 57 | |||
| 58 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | ||
| 59 | SitePreference preference = sitePreference(request, response); | ||
| 60 | if (preference == null) { | ||
| 61 | Device device = DeviceResolvingHandlerInterceptor.getCurrentDevice(request); | ||
| 62 | if (device != null) { | ||
| 63 | preference = device.isMobile() ? SitePreference.MOBILE : SitePreference.NORMAL; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | if (preference != null) { | ||
| 67 | request.setAttribute(CURRENT_SITE_PREFERENCE_ATTRIBUTE, preference); | ||
| 68 | } | ||
| 69 | return true; | ||
| 70 | } | ||
| 71 | |||
| 72 | // static factory methods | ||
| 73 | |||
| 74 | /** | ||
| 75 | * Get the current site preference for the user that originated this web request. | ||
| 76 | */ | ||
| 77 | public static SitePreference getCurrentSitePreference(HttpServletRequest request) { | ||
| 78 | return (SitePreference) request.getAttribute(CURRENT_SITE_PREFERENCE_ATTRIBUTE); | ||
| 79 | } | ||
| 80 | |||
| 81 | /** | ||
| 82 | * Get the current site preference for the user from the request attributes map. | ||
| 83 | */ | ||
| 84 | public static SitePreference getCurrentSitePreference(RequestAttributes attributes) { | ||
| 85 | return (SitePreference) attributes.getAttribute(CURRENT_SITE_PREFERENCE_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST); | ||
| 86 | } | ||
| 87 | |||
| 88 | // internal helpers | ||
| 89 | |||
| 90 | private SitePreference sitePreference(HttpServletRequest request, HttpServletResponse response) { | ||
| 91 | SitePreference site = getSitePreferenceParameter(request); | ||
| 92 | if (site != null) { | ||
| 93 | sitePreferenceRepository.saveSitePreference(site, request, response); | ||
| 94 | return site; | ||
| 95 | } else { | ||
| 96 | return sitePreferenceRepository.loadSitePreference(request); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | private SitePreference getSitePreferenceParameter(HttpServletRequest request) { | ||
| 101 | String string = request.getParameter(SITE_PREFERENCE_PARAMETER); | ||
| 102 | return string != null && string.length() > 0 ? SitePreference.valueOf(string.toUpperCase()) : null; | ||
| 103 | } | ||
| 104 | |||
| 105 | private static final String SITE_PREFERENCE_PARAMETER = "site_preference"; | ||
| 106 | |||
| 107 | } |
spring-mobile-device/src/main/java/org/springframework/mobile/device/site/SitePreferenceWebArgumentResolver.java
(1 / 1)
|   | |||
| 29 | 29 | ||
| 30 | 30 | public Object resolveArgument(MethodParameter param, NativeWebRequest request) throws Exception { | |
| 31 | 31 | if (SitePreference.class.isAssignableFrom(param.getParameterType())) { | |
| 32 | return SitePreferenceResolvingHandlerInterceptor.getCurrentSitePreference(request); | ||
| 32 | return SitePreferenceResolver.getCurrentSitePreference(request); | ||
| 33 | 33 | } else { | |
| 34 | 34 | return WebArgumentResolver.UNRESOLVED; | |
| 35 | 35 | } |
spring-mobile-device/src/main/java/org/springframework/mobile/device/switcher/ServerNameSiteUrlFactory.java
(0 / 66)
|   | |||
| 1 | /* | ||
| 2 | * Copyright 2010 the original author or authors. | ||
| 3 | * | ||
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | * you may not use this file except in compliance with the License. | ||
| 6 | * You may obtain a copy of the License at | ||
| 7 | * | ||
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | * | ||
| 10 | * Unless required by applicable law or agreed to in writing, software | ||
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | * See the License for the specific language governing permissions and | ||
| 14 | * limitations under the License. | ||
| 15 | */ | ||
| 16 | package org.springframework.mobile.device.switcher; | ||
| 17 | |||
| 18 | import javax.servlet.http.HttpServletRequest; | ||
| 19 | |||
| 20 | /** | ||
| 21 | * Site URL factory implementation that differentiates each site by the value of the server name field. | ||
| 22 | * For example, your 'normal' site might be bound to 'myapp.com', while ypur mobile site might be bound to 'm.myapp.com'. | ||
| 23 | * @author Keith Donald | ||
| 24 | */ | ||
| 25 | public class ServerNameSiteUrlFactory implements SiteUrlFactory { | ||
| 26 | |||
| 27 | private final String serverName; | ||
| 28 | |||
| 29 | /** | ||
| 30 | * Creates a new {@link ServerNameSiteUrlFactory}. | ||
| 31 | * @param serverName the server name | ||
| 32 | */ | ||
| 33 | public ServerNameSiteUrlFactory(String serverName) { | ||
| 34 | this.serverName = serverName; | ||
| 35 | } | ||
| 36 | |||
| 37 | public boolean isRequestForSite(HttpServletRequest request) { | ||
| 38 | return serverName.equals(request.getServerName()); | ||
| 39 | } | ||
| 40 | |||
| 41 | public String createSiteUrl(HttpServletRequest request) { | ||
| 42 | StringBuilder builder = new StringBuilder(); | ||
| 43 | builder.append(request.getScheme()).append("://").append(serverName); | ||
| 44 | String optionalPort = optionalPort(request); | ||
| 45 | if (optionalPort != null) { | ||
| 46 | builder.append(optionalPort); | ||
| 47 | } | ||
| 48 | builder.append(request.getRequestURI()); | ||
| 49 | String queryString = request.getQueryString(); | ||
| 50 | if (queryString != null) { | ||
| 51 | builder.append("?").append(queryString); | ||
| 52 | } | ||
| 53 | return builder.toString(); | ||
| 54 | } | ||
| 55 | |||
| 56 | // internal helpers | ||
| 57 | |||
| 58 | private String optionalPort(HttpServletRequest request) { | ||
| 59 | if ("http".equals(request.getScheme()) && request.getServerPort() != 80 || "https".equals(request.getScheme()) && request.getServerPort() != 443) { | ||
| 60 | return ":" + request.getServerPort(); | ||
| 61 | } else { | ||
| 62 | return null; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | } |
spring-mobile-device/src/main/java/org/springframework/mobile/device/switcher/SiteSwitcherHandlerInterceptor.java
(24 / 15)
|   | |||
| 19 | 19 | import javax.servlet.http.HttpServletResponse; | |
| 20 | 20 | ||
| 21 | 21 | import org.springframework.mobile.device.Device; | |
| 22 | import org.springframework.mobile.device.mvc.DeviceResolvingHandlerInterceptor; | ||
| 22 | import org.springframework.mobile.device.mvc.DeviceResolverHandlerInterceptor; | ||
| 23 | 23 | import org.springframework.mobile.device.site.CookieSitePreferenceRepository; | |
| 24 | 24 | import org.springframework.mobile.device.site.SitePreference; | |
| 25 | import org.springframework.mobile.device.site.SitePreferenceResolvingHandlerInterceptor; | ||
| 25 | import org.springframework.mobile.device.site.SitePreferenceRepository; | ||
| 26 | import org.springframework.mobile.device.site.SitePreferenceResolver; | ||
| 26 | 27 | import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; | |
| 27 | 28 | ||
| 28 | 29 | /** | |
| 29 | 30 | * A Spring MVC interceptor that redirects the user to a dedicated mobile web-site if appropriate. | |
| 30 | * Requires that the current device already be resolved by {@link DeviceResolvingHandlerInterceptor}. | ||
| 31 | * Also respects the current user's site preference value if resolved by a {@link SitePreferenceResolvingHandlerInterceptor}. | ||
| 32 | 31 | * @author Keith Donald | |
| 33 | 32 | */ | |
| 34 | 33 | public class SiteSwitcherHandlerInterceptor extends HandlerInterceptorAdapter { | |
| … | … | ||
| 38 | 38 | ||
| 39 | 39 | private final SiteUrlFactory mobileSiteUrlFactory; | |
| 40 | 40 | ||
| 41 | private final SitePreferenceResolver sitePreferenceResolver; | ||
| 42 | |||
| 41 | 43 | /** | |
| 42 | 44 | * Creates a new site switcher. | |
| 43 | 45 | * @param normalSiteUrlFactory the factory for a "normal" site URL e.g. http://app.com | |
| 44 | 46 | * @param mobileSiteUrlFactory the factory for a "mobile" site URL e.g. http://m.app.com | |
| 45 | * @param sitePreferenceRepository the store for recording user site preference | ||
| 47 | * @param sitePreferenceRepository the store for resolving user site preference | ||
| 46 | 48 | */ | |
| 47 | public SiteSwitcherHandlerInterceptor(SiteUrlFactory normalSiteUrlFactory, SiteUrlFactory mobileSiteUrlFactory) { | ||
| 49 | public SiteSwitcherHandlerInterceptor(SiteUrlFactory normalSiteUrlFactory, SiteUrlFactory mobileSiteUrlFactory, | ||
| 50 | SitePreferenceRepository sitePreferenceRepository) { | ||
| 48 | 51 | this.normalSiteUrlFactory = normalSiteUrlFactory; | |
| 49 | 52 | this.mobileSiteUrlFactory = mobileSiteUrlFactory; | |
| 53 | this.sitePreferenceResolver = new SitePreferenceResolver(sitePreferenceRepository); | ||
| 50 | 54 | } | |
| 51 | 55 | ||
| 52 | 56 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | |
| 53 | SitePreference sitePreference = SitePreferenceResolvingHandlerInterceptor.getCurrentSitePreference(request); | ||
| 57 | SitePreference sitePreference = sitePreferenceResolver.resolveSitePreference(request, response); | ||
| 54 | 58 | if (mobileSiteUrlFactory.isRequestForSite(request)) { | |
| 55 | 59 | if (sitePreference == SitePreference.NORMAL) { | |
| 56 | 60 | response.sendRedirect(response.encodeRedirectURL(normalSiteUrlFactory.createSiteUrl(request))); | |
| 57 | 61 | return false; | |
| 58 | 62 | } | |
| 59 | 63 | } else { | |
| 60 | Device device = DeviceResolvingHandlerInterceptor.getRequiredCurrentDevice(request); | ||
| 64 | Device device = DeviceResolverHandlerInterceptor.getRequiredCurrentDevice(request); | ||
| 61 | 65 | if (sitePreference == SitePreference.MOBILE || device.isMobile() && sitePreference == null) { | |
| 62 | 66 | response.sendRedirect(response.encodeRedirectURL(mobileSiteUrlFactory.createSiteUrl(request))); | |
| 63 | 67 | return false; | |
| … | … | ||
| 73 | 73 | // static factory methods | |
| 74 | 74 | ||
| 75 | 75 | /** | |
| 76 | * Creates a site switcher that redirects to a <code>m.</code> host for requests originated by mobile devices (or when there is a mobile site preference). | ||
| 77 | * Uses {@link CookieSitePreferenceRepository}. | ||
| 76 | * Creates a site switcher that redirects to a <code>m.</code> domain for normal site requests that either | ||
| 77 | * originate from a mobile device or indicate a mobile site preference. | ||
| 78 | * Uses a {@link CookieSitePreferenceRepository} that saves a cookie that is shared between the two domains. | ||
| 78 | 79 | */ | |
| 79 | 80 | public static SiteSwitcherHandlerInterceptor mDot(String serverName) { | |
| 80 | return new SiteSwitcherHandlerInterceptor(new ServerNameSiteUrlFactory(serverName), new ServerNameSiteUrlFactory("m." + serverName)); | ||
| 81 | return new SiteSwitcherHandlerInterceptor(new StandardSiteUrlFactory(serverName), | ||
| 82 | new StandardSiteUrlFactory("m." + serverName), new CookieSitePreferenceRepository("." + serverName)); | ||
| 81 | 83 | } | |
| 82 | 84 | ||
| 83 | 85 | /** | |
| 84 | * Creates a site switcher that redirects to a <code>.mobi</code> domain for requests originated by mobile devices (or when there is a mobile site preference). | ||
| 85 | * Will strip off the trailing domain name when building the mobile domain e.g. "app.com" will become "app.mobi", as the .com will be stripped. | ||
| 86 | * Uses {@link CookieSitePreferenceRepository}. | ||
| 86 | * Creates a site switcher that redirects to a <code>.mobi</code> domain for normal site requests that either | ||
| 87 | * originate from a mobile device or indicate a mobile site preference. | ||
| 88 | * Will strip off the trailing domain name when building the mobile domain | ||
| 89 | * e.g. "app.com" will become "app.mobi" (the .com will be stripped). | ||
| 90 | * Uses a {@link CookieSitePreferenceRepository} that saves a cookie that is shared between the two domains. | ||
| 87 | 91 | */ | |
| 88 | 92 | public static SiteSwitcherHandlerInterceptor dotMobi(String serverName) { | |
| 89 | 93 | int lastDot = serverName.lastIndexOf('.'); | |
| 90 | return new SiteSwitcherHandlerInterceptor(new ServerNameSiteUrlFactory(serverName), new ServerNameSiteUrlFactory(serverName.substring(0, lastDot) + ".mobi")); | ||
| 94 | return new SiteSwitcherHandlerInterceptor(new StandardSiteUrlFactory(serverName), | ||
| 95 | new StandardSiteUrlFactory(serverName.substring(0, lastDot) + ".mobi"), | ||
| 96 | new CookieSitePreferenceRepository("." + serverName)); | ||
| 91 | 97 | } | |
| 92 | 98 | ||
| 93 | 99 | } |
spring-mobile-device/src/main/java/org/springframework/mobile/device/switcher/StandardSiteUrlFactory.java
(62 / 0)
|   | |||
| 1 | /* | ||
| 2 | * Copyright 2010 the original author or authors. | ||
| 3 | * | ||
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | * you may not use this file except in compliance with the License. | ||
| 6 | * You may obtain a copy of the License at | ||
| 7 | * | ||
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | * | ||
| 10 | * Unless required by applicable law or agreed to in writing, software | ||
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | * See the License for the specific language governing permissions and | ||
| 14 | * limitations under the License. | ||
| 15 | */ | ||
| 16 | package org.springframework.mobile.device.switcher; | ||
| 17 | |||
| 18 | import javax.servlet.http.HttpServletRequest; | ||
| 19 | |||
| 20 | /** | ||
| 21 | * Site URL factory implementation that differentiates each site by the value of the server name field. | ||
| 22 | * For example, your 'normal' site might be bound to 'myapp.com', while ypur mobile site might be bound to 'm.myapp.com'. | ||
| 23 | * @author Keith Donald | ||
| 24 | */ | ||
| 25 | public class StandardSiteUrlFactory implements SiteUrlFactory { | ||
| 26 | |||
| 27 | private final String serverName; | ||
| 28 | |||
| 29 | /** | ||
| 30 | * Creates a new {@link StandardSiteUrlFactory}. | ||
| 31 | * @param serverName the server name | ||
| 32 | */ | ||
| 33 | public StandardSiteUrlFactory(String serverName) { | ||
| 34 | this.serverName = serverName; | ||
| 35 | } | ||
| 36 | |||
| 37 | public boolean isRequestForSite(HttpServletRequest request) { | ||
| 38 | return serverName.equals(request.getServerName()); | ||
| 39 | } | ||
| 40 | |||
| 41 | public String createSiteUrl(HttpServletRequest request) { | ||
| 42 | StringBuilder builder = new StringBuilder(); | ||
| 43 | builder.append(request.getScheme()).append("://").append(serverName); | ||
| 44 | String optionalPort = optionalPort(request); | ||
| 45 | if (optionalPort != null) { | ||
| 46 | builder.append(optionalPort); | ||
| 47 | } | ||
| 48 | builder.append(request.getRequestURI()); | ||
| 49 | return builder.toString(); | ||
| 50 | } | ||
| 51 | |||
| 52 | // internal helpers | ||
| 53 | |||
| 54 | private String optionalPort(HttpServletRequest request) { | ||
| 55 | if ("http".equals(request.getScheme()) && request.getServerPort() != 80 || "https".equals(request.getScheme()) && request.getServerPort() != 443) { | ||
| 56 | return ":" + request.getServerPort(); | ||
| 57 | } else { | ||
| 58 | return null; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | } |
spring-mobile-device/src/main/java/org/springframework/mobile/device/wurfl/wng/WngView.java
(2 / 2)
|   | |||
| 37 | 37 | ||
| 38 | 38 | import org.apache.commons.collections.CollectionUtils; | |
| 39 | 39 | import org.apache.commons.collections.functors.InstanceofPredicate; | |
| 40 | import org.springframework.mobile.device.mvc.DeviceResolvingHandlerInterceptor; | ||
| 40 | import org.springframework.mobile.device.mvc.DeviceResolverHandlerInterceptor; | ||
| 41 | 41 | import org.springframework.web.servlet.View; | |
| 42 | 42 | ||
| 43 | 43 | /** | |
| … | … | ||
| 74 | 74 | target.render(model, request, buffered); | |
| 75 | 75 | // logic adapted from WNGContextFilter which has the same responsibility | |
| 76 | 76 | if (isWngDocumentCreated(request)) { | |
| 77 | WNGDevice device = new WNGDevice((Device) DeviceResolvingHandlerInterceptor.getCurrentDevice(request)); | ||
| 77 | WNGDevice device = new WNGDevice((Device) DeviceResolverHandlerInterceptor.getCurrentDevice(request)); | ||
| 78 | 78 | Document document = resolveDocument(request); | |
| 79 | 79 | StyleContainer styleContainer = (StyleContainer)CollectionUtils.find(document.getHead().getChildren(), new InstanceofPredicate(StyleContainer.class)); | |
| 80 | 80 | if (styleContainer == null) { |
spring-mobile-device/src/test/java/org/springframework/mobile/device/mvc/DeviceResolvingHandlerInterceptorTest.java
(4 / 4)
|   | |||
| 15 | 15 | ||
| 16 | 16 | private Device device = new StubDevice(); | |
| 17 | 17 | ||
| 18 | private DeviceResolvingHandlerInterceptor interceptor = new DeviceResolvingHandlerInterceptor(new DeviceResolver() { | ||
| 18 | private DeviceResolverHandlerInterceptor interceptor = new DeviceResolverHandlerInterceptor(new DeviceResolver() { | ||
| 19 | 19 | public Device resolveDevice(HttpServletRequest request) { | |
| 20 | 20 | return device; | |
| 21 | 21 | } | |
| … | … | ||
| 28 | 28 | @Test | |
| 29 | 29 | public void resolve() throws Exception { | |
| 30 | 30 | assertTrue(interceptor.preHandle(request, response, null)); | |
| 31 | assertSame(device, DeviceResolvingHandlerInterceptor.getCurrentDevice(request)); | ||
| 31 | assertSame(device, DeviceResolverHandlerInterceptor.getCurrentDevice(request)); | ||
| 32 | 32 | } | |
| 33 | 33 | ||
| 34 | 34 | @Test | |
| 35 | 35 | public void resolveDefaultResolver() throws Exception { | |
| 36 | interceptor = new DeviceResolvingHandlerInterceptor(); | ||
| 36 | interceptor = new DeviceResolverHandlerInterceptor(); | ||
| 37 | 37 | request.addHeader("User-Agent", "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7"); | |
| 38 | 38 | assertTrue(interceptor.preHandle(request, response, null)); | |
| 39 | Device device = DeviceResolvingHandlerInterceptor.getCurrentDevice(request); | ||
| 39 | Device device = DeviceResolverHandlerInterceptor.getCurrentDevice(request); | ||
| 40 | 40 | assertTrue(device.isMobile()); | |
| 41 | 41 | } | |
| 42 | 42 |
spring-mobile-device/src/test/java/org/springframework/mobile/device/mvc/DeviceWebArgumentResolverTest.java
(2 / 2)
|   | |||
| 21 | 21 | ||
| 22 | 22 | @Test | |
| 23 | 23 | public void resolve() throws Exception { | |
| 24 | request.setAttribute(DeviceResolvingHandlerInterceptor.CURRENT_DEVICE_ATTRIBUTE, device, WebRequest.SCOPE_REQUEST); | ||
| 24 | request.setAttribute(DeviceResolverHandlerInterceptor.CURRENT_DEVICE_ATTRIBUTE, device, WebRequest.SCOPE_REQUEST); | ||
| 25 | 25 | MethodParameter parameter = new MethodParameter(getClass().getMethod("handlerMethod", Device.class), 0); | |
| 26 | 26 | Object resolved = resolver.resolveArgument(parameter, request); | |
| 27 | 27 | assertSame(device, resolved); | |
| … | … | ||
| 29 | 29 | ||
| 30 | 30 | @Test | |
| 31 | 31 | public void unresolved() throws Exception { | |
| 32 | request.setAttribute(DeviceResolvingHandlerInterceptor.CURRENT_DEVICE_ATTRIBUTE, device, WebRequest.SCOPE_REQUEST); | ||
| 32 | request.setAttribute(DeviceResolverHandlerInterceptor.CURRENT_DEVICE_ATTRIBUTE, device, WebRequest.SCOPE_REQUEST); | ||
| 33 | 33 | MethodParameter parameter = new MethodParameter(getClass().getMethod("handlerMethodUnresolved", String.class), 0); | |
| 34 | 34 | Object resolved = resolver.resolveArgument(parameter, request); | |
| 35 | 35 | assertSame(WebArgumentResolver.UNRESOLVED, resolved); |
spring-mobile-device/src/test/java/org/springframework/mobile/device/mvc/StubDevice.java
(5 / 1)
|   | |||
| 9 | 9 | public StubDevice() { | |
| 10 | 10 | this.mobile = true; | |
| 11 | 11 | } | |
| 12 | |||
| 12 | |||
| 13 | public StubDevice(boolean mobile) { | ||
| 14 | this.mobile = mobile; | ||
| 15 | } | ||
| 16 | |||
| 13 | 17 | public boolean isMobile() { | |
| 14 | 18 | return mobile; | |
| 15 | 19 | } |
spring-mobile-device/src/test/java/org/springframework/mobile/device/site/SitePreferenceResolverTest.java
(63 / 0)
|   | |||
| 1 | package org.springframework.mobile.device.site; | ||
| 2 | |||
| 3 | import static org.junit.Assert.assertEquals; | ||
| 4 | import static org.junit.Assert.assertNull; | ||
| 5 | |||
| 6 | import org.junit.Before; | ||
| 7 | import org.junit.Test; | ||
| 8 | import org.springframework.mobile.device.mvc.DeviceResolverHandlerInterceptor; | ||
| 9 | import org.springframework.mobile.device.mvc.StubDevice; | ||
| 10 | import org.springframework.mock.web.MockHttpServletRequest; | ||
| 11 | import org.springframework.mock.web.MockHttpServletResponse; | ||
| 12 | |||
| 13 | public class SitePreferenceResolverTest { | ||
| 14 | |||
| 15 | private SitePreferenceResolver sitePreferenceResolver; | ||
| 16 | |||
| 17 | private MockHttpServletRequest request = new MockHttpServletRequest(); | ||
| 18 | |||
| 19 | private MockHttpServletResponse response = new MockHttpServletResponse(); | ||
| 20 | |||
| 21 | private StubSitePreferenceRepository repository = new StubSitePreferenceRepository(); | ||
| 22 | |||
| 23 | @Before | ||
| 24 | public void setup() throws Exception { | ||
| 25 | sitePreferenceResolver = new SitePreferenceResolver(repository); | ||
| 26 | } | ||
| 27 | |||
| 28 | @Test | ||
| 29 | public void saveSitePreference() throws Exception { | ||
| 30 | request.addParameter("site_preference", "normal"); | ||
| 31 | assertEquals(SitePreference.NORMAL, sitePreferenceResolver.resolveSitePreference(request, response)); | ||
| 32 | assertEquals(SitePreference.NORMAL, repository.getSitePreference()); | ||
| 33 | assertEquals(SitePreference.NORMAL, SitePreferenceResolver.getCurrentSitePreference(request)); | ||
| 34 | } | ||
| 35 | |||
| 36 | @Test | ||
| 37 | public void loadSitePreference() throws Exception { | ||
| 38 | repository.setSitePreference(SitePreference.MOBILE); | ||
| 39 | assertEquals(SitePreference.MOBILE, sitePreferenceResolver.resolveSitePreference(request, response)); | ||
| 40 | assertEquals(SitePreference.MOBILE, SitePreferenceResolver.getCurrentSitePreference(request)); | ||
| 41 | } | ||
| 42 | |||
| 43 | @Test | ||
| 44 | public void defaultSitePreference() throws Exception { | ||
| 45 | assertNull(sitePreferenceResolver.resolveSitePreference(request, response)); | ||
| 46 | assertNull(SitePreferenceResolver.getCurrentSitePreference(request)); | ||
| 47 | } | ||
| 48 | |||
| 49 | @Test | ||
| 50 | public void defaultSitePreferenceMobileDevice() throws Exception { | ||
| 51 | request.setAttribute(DeviceResolverHandlerInterceptor.CURRENT_DEVICE_ATTRIBUTE, new StubDevice()); | ||
| 52 | assertEquals(SitePreference.MOBILE, sitePreferenceResolver.resolveSitePreference(request, response)); | ||
| 53 | assertEquals(SitePreference.MOBILE, SitePreferenceResolver.getCurrentSitePreference(request)); | ||
| 54 | } | ||
| 55 | |||
| 56 | @Test | ||
| 57 | public void defaultSitePreferenceNormalDevice() throws Exception { | ||
| 58 | request.setAttribute(DeviceResolverHandlerInterceptor.CURRENT_DEVICE_ATTRIBUTE, new StubDevice(false)); | ||
| 59 | assertEquals(SitePreference.NORMAL, sitePreferenceResolver.resolveSitePreference(request, response)); | ||
| 60 | assertEquals(SitePreference.NORMAL, SitePreferenceResolver.getCurrentSitePreference(request)); | ||
| 61 | } | ||
| 62 | |||
| 63 | } |
spring-mobile-device/src/test/java/org/springframework/mobile/device/site/SitePreferenceResolvingHandlerInterceptorTest.java
(0 / 57)
|   | |||
| 1 | package org.springframework.mobile.device.site; | ||
| 2 | |||
| 3 | import static org.junit.Assert.assertEquals; | ||
| 4 | import static org.junit.Assert.assertNull; | ||
| 5 | import static org.junit.Assert.assertTrue; | ||
| 6 | |||
| 7 | import org.junit.Before; | ||
| 8 | import org.junit.Test; | ||
| 9 | import org.springframework.mobile.device.mvc.DeviceResolvingHandlerInterceptor; | ||
| 10 | import org.springframework.mobile.device.mvc.StubDevice; | ||
| 11 | import org.springframework.mock.web.MockHttpServletRequest; | ||
| 12 | import org.springframework.mock.web.MockHttpServletResponse; | ||
| 13 | |||
| 14 | public class SitePreferenceResolvingHandlerInterceptorTest { | ||
| 15 | |||
| 16 | private SitePreferenceResolvingHandlerInterceptor interceptor; | ||
| 17 | |||
| 18 | private MockHttpServletRequest request = new MockHttpServletRequest(); | ||
| 19 | |||
| 20 | private MockHttpServletResponse response = new MockHttpServletResponse(); | ||
| 21 | |||
| 22 | private StubSitePreferenceRepository repository = new StubSitePreferenceRepository(); | ||
| 23 | |||
| 24 | @Before | ||
| 25 | public void setup() throws Exception { | ||
| 26 | interceptor = new SitePreferenceResolvingHandlerInterceptor(repository); | ||
| 27 | } | ||
| 28 | |||
| 29 | @Test | ||
| 30 | public void saveSitePreference() throws Exception { | ||
| 31 | request.addParameter("site_preference", "normal"); | ||
| 32 | assertTrue(interceptor.preHandle(request, response, null)); | ||
| 33 | assertEquals(SitePreference.NORMAL, repository.loadSitePreference(request)); | ||
| 34 | assertEquals(SitePreference.NORMAL, SitePreferenceResolvingHandlerInterceptor.getCurrentSitePreference(request)); | ||
| 35 | } | ||
| 36 | |||
| 37 | @Test | ||
| 38 | public void loadSitePreference() throws Exception { | ||
| 39 | repository.saveSitePreference(SitePreference.MOBILE, request, response); | ||
| 40 | assertTrue(interceptor.preHandle(request, response, null)); | ||
| 41 | assertEquals(SitePreference.MOBILE, SitePreferenceResolvingHandlerInterceptor.getCurrentSitePreference(request)); | ||
| 42 | } | ||
| 43 | |||
| 44 | @Test | ||
| 45 | public void defaultSitePreference() throws Exception { | ||
| 46 | assertTrue(interceptor.preHandle(request, response, null)); | ||
| 47 | assertNull(SitePreferenceResolvingHandlerInterceptor.getCurrentSitePreference(request)); | ||
| 48 | } | ||
| 49 | |||
| 50 | @Test | ||
| 51 | public void defaultSitePreferenceFromDevice() throws Exception { | ||
| 52 | request.setAttribute(DeviceResolvingHandlerInterceptor.CURRENT_DEVICE_ATTRIBUTE, new StubDevice()); | ||
| 53 | assertTrue(interceptor.preHandle(request, response, null)); | ||
| 54 | assertEquals(SitePreference.MOBILE, SitePreferenceResolvingHandlerInterceptor.getCurrentSitePreference(request)); | ||
| 55 | } | ||
| 56 | |||
| 57 | } |
spring-mobile-device/src/test/java/org/springframework/mobile/device/site/SitePreferenceWebArgumentResolverTest.java
(2 / 2)
|   | |||
| 19 | 19 | ||
| 20 | 20 | @Test | |
| 21 | 21 | public void resolve() throws Exception { | |
| 22 | request.setAttribute(SitePreferenceResolvingHandlerInterceptor.CURRENT_SITE_PREFERENCE_ATTRIBUTE, SitePreference.MOBILE, WebRequest.SCOPE_REQUEST); | ||
| 22 | request.setAttribute(SitePreferenceResolver.CURRENT_SITE_PREFERENCE_ATTRIBUTE, SitePreference.MOBILE, WebRequest.SCOPE_REQUEST); | ||
| 23 | 23 | MethodParameter parameter = new MethodParameter(getClass().getMethod("handlerMethod", SitePreference.class), 0); | |
| 24 | 24 | Object resolved = resolver.resolveArgument(parameter, request); | |
| 25 | 25 | assertEquals(SitePreference.MOBILE, resolved); | |
| … | … | ||
| 27 | 27 | ||
| 28 | 28 | @Test | |
| 29 | 29 | public void unresolved() throws Exception { | |
| 30 | request.setAttribute(SitePreferenceResolvingHandlerInterceptor.CURRENT_SITE_PREFERENCE_ATTRIBUTE, SitePreference.MOBILE, WebRequest.SCOPE_REQUEST); | ||
| 30 | request.setAttribute(SitePreferenceResolver.CURRENT_SITE_PREFERENCE_ATTRIBUTE, SitePreference.MOBILE, WebRequest.SCOPE_REQUEST); | ||
| 31 | 31 | MethodParameter parameter = new MethodParameter(getClass().getMethod("handlerMethodUnresolved", String.class), 0); | |
| 32 | 32 | Object resolved = resolver.resolveArgument(parameter, request); | |
| 33 | 33 | assertSame(WebArgumentResolver.UNRESOLVED, resolved); |
spring-mobile-device/src/test/java/org/springframework/mobile/device/site/StubSitePreferenceRepository.java
(8 / 1)
|   | |||
| 6 | 6 | import org.springframework.mobile.device.site.SitePreference; | |
| 7 | 7 | import org.springframework.mobile.device.site.SitePreferenceRepository; | |
| 8 | 8 | ||
| 9 | class StubSitePreferenceRepository implements SitePreferenceRepository { | ||
| 9 | public class StubSitePreferenceRepository implements SitePreferenceRepository { | ||
| 10 | 10 | ||
| 11 | 11 | private SitePreference sitePreference; | |
| 12 | 12 | ||
| … | … | ||
| 18 | 18 | return sitePreference; | |
| 19 | 19 | } | |
| 20 | 20 | ||
| 21 | public SitePreference getSitePreference() { | ||
| 22 | return sitePreference; | ||
| 23 | } | ||
| 24 | |||
| 25 | public void setSitePreference(SitePreference sitePreference) { | ||
| 26 | this.sitePreference = sitePreference; | ||
| 27 | } | ||
| 21 | 28 | } |
spring-mobile-device/src/test/java/org/springframework/mobile/device/switcher/ServerNameSiteUrlFactoryTest.java
(1 / 10)
|   | |||
| 9 | 9 | ||
| 10 | 10 | public class ServerNameSiteUrlFactoryTest { | |
| 11 | 11 | ||
| 12 | private ServerNameSiteUrlFactory factory = new ServerNameSiteUrlFactory("m.app.com"); | ||
| 12 | private StandardSiteUrlFactory factory = new StandardSiteUrlFactory("m.app.com"); | ||
| 13 | 13 | ||
| 14 | 14 | private MockHttpServletRequest request = new MockHttpServletRequest(); | |
| 15 | 15 | ||
| … | … | ||
| 31 | 31 | request.setServerPort(80); | |
| 32 | 32 | request.setRequestURI("/foo"); | |
| 33 | 33 | assertEquals("http://m.app.com/foo", factory.createSiteUrl(request)); | |
| 34 | } | ||
| 35 | |||
| 36 | @Test | ||
| 37 | public void createSiteUrlQueryParameters() { | ||
| 38 | request.setServerName("m.app.com"); | ||
| 39 | request.setServerPort(80); | ||
| 40 | request.setRequestURI("/foo"); | ||
| 41 | request.setQueryString("bar=baz"); | ||
| 42 | assertEquals("http://m.app.com/foo?bar=baz", factory.createSiteUrl(request)); | ||
| 43 | 34 | } | |
| 44 | 35 | ||
| 45 | 36 | } |
spring-mobile-device/src/test/java/org/springframework/mobile/device/switcher/SiteSwitcherHandlerInterceptorTest.java
(39 / 16)
|   | |||
| 9 | 9 | ||
| 10 | 10 | import org.junit.Before; | |
| 11 | 11 | import org.junit.Test; | |
| 12 | import org.springframework.mobile.device.mvc.DeviceResolvingHandlerInterceptor; | ||
| 12 | import org.springframework.mobile.device.mvc.DeviceResolverHandlerInterceptor; | ||
| 13 | 13 | import org.springframework.mobile.device.mvc.StubDevice; | |
| 14 | 14 | import org.springframework.mobile.device.site.SitePreference; | |
| 15 | import org.springframework.mobile.device.site.SitePreferenceResolvingHandlerInterceptor; | ||
| 15 | import org.springframework.mobile.device.site.StubSitePreferenceRepository; | ||
| 16 | 16 | import org.springframework.mock.web.MockHttpServletRequest; | |
| 17 | 17 | import org.springframework.mock.web.MockHttpServletResponse; | |
| 18 | 18 | ||
| … | … | ||
| 24 | 24 | ||
| 25 | 25 | private MockHttpServletResponse response = new MockHttpServletResponse(); | |
| 26 | 26 | ||
| 27 | private StubDevice device; | ||
| 27 | private StubDevice device = new StubDevice(); | ||
| 28 | 28 | ||
| 29 | private StubSitePreferenceRepository sitePreferenceRepository = new StubSitePreferenceRepository(); | ||
| 30 | |||
| 29 | 31 | @Before | |
| 30 | 32 | public void setup() throws Exception { | |
| 31 | device = new StubDevice(); | ||
| 32 | request.setAttribute(DeviceResolvingHandlerInterceptor.CURRENT_DEVICE_ATTRIBUTE, device); | ||
| 33 | request.setAttribute(DeviceResolverHandlerInterceptor.CURRENT_DEVICE_ATTRIBUTE, device); | ||
| 33 | 34 | SiteUrlFactory normalSiteUrlFactory = new SiteUrlFactory() { | |
| 34 | 35 | public boolean isRequestForSite(HttpServletRequest request) { | |
| 35 | 36 | return request.getServerName().equals("app.com"); | |
| … | … | ||
| 47 | 47 | return "http://m.app.com"; | |
| 48 | 48 | } | |
| 49 | 49 | }; | |
| 50 | siteSwitcher = new SiteSwitcherHandlerInterceptor(normalSiteUrlFactory, mobileSiteUrlFactory); | ||
| 50 | siteSwitcher = new SiteSwitcherHandlerInterceptor(normalSiteUrlFactory, mobileSiteUrlFactory, sitePreferenceRepository); | ||
| 51 | 51 | } | |
| 52 | 52 | ||
| 53 | 53 | @Test | |
| … | … | ||
| 58 | 58 | ||
| 59 | 59 | @Test | |
| 60 | 60 | public void mobileDeviceNormalSiteMobilePreference() throws Exception { | |
| 61 | request.setAttribute(SitePreferenceResolvingHandlerInterceptor.CURRENT_SITE_PREFERENCE_ATTRIBUTE, SitePreference.MOBILE); | ||
| 61 | sitePreferenceRepository.setSitePreference(SitePreference.MOBILE); | ||
| 62 | 62 | assertFalse(siteSwitcher.preHandle(request, response, null)); | |
| 63 | 63 | assertEquals("http://m.app.com", response.getRedirectedUrl()); | |
| 64 | 64 | } | |
| 65 | 65 | ||
| 66 | 66 | @Test | |
| 67 | 67 | public void mobileDeviceNormalSiteNormalPreference() throws Exception { | |
| 68 | request.setAttribute(SitePreferenceResolvingHandlerInterceptor.CURRENT_SITE_PREFERENCE_ATTRIBUTE, SitePreference.NORMAL); | ||
| 68 | sitePreferenceRepository.setSitePreference(SitePreference.NORMAL); | ||
| 69 | 69 | assertTrue(siteSwitcher.preHandle(request, response, null)); | |
| 70 | 70 | assertNull(response.getRedirectedUrl()); | |
| 71 | 71 | } | |
| … | … | ||
| 80 | 80 | @Test | |
| 81 | 81 | public void normalDeviceNormalSiteMobilePreference() throws Exception { | |
| 82 | 82 | device.setMobile(false); | |
| 83 | request.setAttribute(SitePreferenceResolvingHandlerInterceptor.CURRENT_SITE_PREFERENCE_ATTRIBUTE, SitePreference.MOBILE); | ||
| 83 | sitePreferenceRepository.setSitePreference(SitePreference.MOBILE); | ||
| 84 | 84 | assertFalse(siteSwitcher.preHandle(request, response, null)); | |
| 85 | 85 | assertEquals("http://m.app.com", response.getRedirectedUrl()); | |
| 86 | 86 | } | |
| … | … | ||
| 88 | 88 | @Test | |
| 89 | 89 | public void normalDeviceNormalSiteNormalPreference() throws Exception { | |
| 90 | 90 | device.setMobile(false); | |
| 91 | request.setAttribute(SitePreferenceResolvingHandlerInterceptor.CURRENT_SITE_PREFERENCE_ATTRIBUTE, SitePreference.NORMAL); | ||
| 91 | sitePreferenceRepository.setSitePreference(SitePreference.NORMAL); | ||
| 92 | 92 | assertTrue(siteSwitcher.preHandle(request, response, null)); | |
| 93 | 93 | assertNull(response.getRedirectedUrl()); | |
| 94 | 94 | } | |
| 95 | 95 | ||
| 96 | 96 | @Test | |
| 97 | public void mDot() throws Exception { | ||
| 97 | public void mDotMobileDeviceNormalSiteNoPreference() throws Exception { | ||
| 98 | 98 | SiteSwitcherHandlerInterceptor mDot = SiteSwitcherHandlerInterceptor.mDot("app.com"); | |
| 99 | 99 | assertFalse(mDot.preHandle(request, response, null)); | |
| 100 | 100 | assertEquals(0, response.getCookies().length); | |
| … | … | ||
| 102 | 102 | } | |
| 103 | 103 | ||
| 104 | 104 | @Test | |
| 105 | public void dotMobi() throws Exception { | ||
| 106 | SiteSwitcherHandlerInterceptor dotMobi = SiteSwitcherHandlerInterceptor.dotMobi("app.com"); | ||
| 107 | assertFalse(dotMobi.preHandle(request, response, null)); | ||
| 105 | public void mDotMobileDeviceNormalSiteNormalPreference() throws Exception { | ||
| 106 | SiteSwitcherHandlerInterceptor mDot = SiteSwitcherHandlerInterceptor.mDot("app.com"); | ||
| 107 | request.addParameter("site_preference", "normal"); | ||
| 108 | assertTrue(mDot.preHandle(request, response, null)); | ||
| 109 | assertEquals(1, response.getCookies().length); | ||
| 110 | assertEquals(".app.com", response.getCookies()[0].getDomain()); | ||
| 111 | assertEquals("NORMAL", response.getCookies()[0].getValue()); | ||
| 112 | assertNull(response.getRedirectedUrl()); | ||
| 113 | } | ||
| 114 | |||
| 115 | @Test | ||
| 116 | public void dotMobiMobileDeviceNormalSiteNoPreference() throws Exception { | ||
| 117 | SiteSwitcherHandlerInterceptor mDot = SiteSwitcherHandlerInterceptor.dotMobi("app.com"); | ||
| 118 | assertFalse(mDot.preHandle(request, response, null)); | ||
| 108 | 119 | assertEquals(0, response.getCookies().length); | |
| 109 | assertEquals("http://app.mobi", response.getRedirectedUrl()); | ||
| 120 | assertEquals("http://app.mobi", response.getRedirectedUrl()); | ||
| 110 | 121 | } | |
| 111 | |||
| 122 | |||
| 123 | @Test | ||
| 124 | public void dotMobiMobileDeviceNormalSiteNormalPreference() throws Exception { | ||
| 125 | SiteSwitcherHandlerInterceptor mDot = SiteSwitcherHandlerInterceptor.dotMobi("app.com"); | ||
| 126 | request.addParameter("site_preference", "normal"); | ||
| 127 | assertTrue(mDot.preHandle(request, response, null)); | ||
| 128 | assertEquals(1, response.getCookies().length); | ||
| 129 | assertEquals(".app.com", response.getCookies()[0].getDomain()); | ||
| 130 | assertEquals("NORMAL", response.getCookies()[0].getValue()); | ||
| 131 | assertNull(response.getRedirectedUrl()); | ||
| 132 | } | ||
| 133 | |||
| 112 | 134 | } |
spring-mobile-device/src/test/resources/org/springframework/mobile/device/config/device.xml
(1 / 1)
|   | |||
| 10 | 10 | <device:wurfl-device-resolver id="rootAndPatches" root-location="classpath:org/springframework/mobile/device/wurfl/test-wurfl.xml" | |
| 11 | 11 | patch-locations="classpath:org/springframework/mobile/device/wurfl/test-wurfl-patch.xml" /> | |
| 12 | 12 | ||
| 13 | <bean class="org.springframework.mobile.device.mvc.DeviceResolvingHandlerInterceptor"> | ||
| 13 | <bean class="org.springframework.mobile.device.mvc.DeviceResolverHandlerInterceptor"> | ||
| 14 | 14 | <constructor-arg> | |
| 15 | 15 | <device:wurfl-device-resolver root-location="classpath:org/springframework/mobile/device/wurfl/test-wurfl.xml" /> | |
| 16 | 16 | </constructor-arg> |
Comments
Add your comment
Please log in to comment



Add a new comment:
Login or create an account to post a comment