Commit 39e359c76d7f083a5b1c3f3af4c6cb4e148278f4

  • avatar
  • Keith <keith @not-scott-and…-macbook.local>
  • Thu Dec 02 21:34:06 GMT 2010
improved design by encapsulating site preference resolver usage within site switcher; removed site preference resolver handler interceptor now as its not clear its needed anymore
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 */
16package org.springframework.mobile.device.mvc;
17
18import javax.servlet.http.HttpServletRequest;
19import javax.servlet.http.HttpServletResponse;
20
21import org.springframework.mobile.device.Device;
22import org.springframework.mobile.device.DeviceResolver;
23import org.springframework.mobile.device.lite.LiteDeviceResolver;
24import org.springframework.web.context.request.RequestAttributes;
25import org.springframework.web.servlet.HandlerInterceptor;
26import 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 */
34public 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 */
16package org.springframework.mobile.device.mvc;
17
18import javax.servlet.http.HttpServletRequest;
19import javax.servlet.http.HttpServletResponse;
20
21import org.springframework.mobile.device.Device;
22import org.springframework.mobile.device.DeviceResolver;
23import org.springframework.mobile.device.lite.LiteDeviceResolver;
24import org.springframework.web.context.request.RequestAttributes;
25import org.springframework.web.servlet.HandlerInterceptor;
26import 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 */
34public 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)
  
2222
2323/**
2424 * 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.
2626 * @author Keith Donald
2727 */
2828public class DeviceWebArgumentResolver implements WebArgumentResolver {
2929
3030 public Object resolveArgument(MethodParameter param, NativeWebRequest request) throws Exception {
3131 if (Device.class.isAssignableFrom(param.getParameterType())) {
32 return DeviceResolvingHandlerInterceptor.getCurrentDevice(request);
32 return DeviceResolverHandlerInterceptor.getCurrentDevice(request);
3333 } else {
3434 return WebArgumentResolver.UNRESOLVED;
3535 }
spring-mobile-device/src/main/java/org/springframework/mobile/device/site/CookieSitePreferenceRepository.java
(5 / 0)
  
3434 setCookieName(DEFAULT_COOKIE_NAME);
3535 }
3636
37 public CookieSitePreferenceRepository(String cookieDomain) {
38 setCookieName(DEFAULT_COOKIE_NAME);
39 setCookieDomain(cookieDomain);
40 }
41
3742 public void saveSitePreference(SitePreference preference, HttpServletRequest request, HttpServletResponse response) {
3843 addCookie(response, preference.name());
3944 }
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 */
16package org.springframework.mobile.device.site;
17
18import javax.servlet.http.HttpServletRequest;
19import javax.servlet.http.HttpServletResponse;
20
21import org.springframework.mobile.device.Device;
22import org.springframework.mobile.device.mvc.DeviceResolverHandlerInterceptor;
23import 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 */
35public 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 */
16package org.springframework.mobile.device.site;
17
18import javax.servlet.http.HttpServletRequest;
19import javax.servlet.http.HttpServletResponse;
20
21import org.springframework.mobile.device.Device;
22import org.springframework.mobile.device.mvc.DeviceResolvingHandlerInterceptor;
23import org.springframework.web.context.request.RequestAttributes;
24import 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 */
36public 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)
  
2929
3030 public Object resolveArgument(MethodParameter param, NativeWebRequest request) throws Exception {
3131 if (SitePreference.class.isAssignableFrom(param.getParameterType())) {
32 return SitePreferenceResolvingHandlerInterceptor.getCurrentSitePreference(request);
32 return SitePreferenceResolver.getCurrentSitePreference(request);
3333 } else {
3434 return WebArgumentResolver.UNRESOLVED;
3535 }
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 */
16package org.springframework.mobile.device.switcher;
17
18import 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 */
25public 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)
  
1919import javax.servlet.http.HttpServletResponse;
2020
2121import org.springframework.mobile.device.Device;
22import org.springframework.mobile.device.mvc.DeviceResolvingHandlerInterceptor;
22import org.springframework.mobile.device.mvc.DeviceResolverHandlerInterceptor;
2323import org.springframework.mobile.device.site.CookieSitePreferenceRepository;
2424import org.springframework.mobile.device.site.SitePreference;
25import org.springframework.mobile.device.site.SitePreferenceResolvingHandlerInterceptor;
25import org.springframework.mobile.device.site.SitePreferenceRepository;
26import org.springframework.mobile.device.site.SitePreferenceResolver;
2627import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
2728
2829/**
2930 * 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}.
3231 * @author Keith Donald
3332 */
3433public class SiteSwitcherHandlerInterceptor extends HandlerInterceptorAdapter {
3838
3939 private final SiteUrlFactory mobileSiteUrlFactory;
4040
41 private final SitePreferenceResolver sitePreferenceResolver;
42
4143 /**
4244 * Creates a new site switcher.
4345 * @param normalSiteUrlFactory the factory for a "normal" site URL e.g. http://app.com
4446 * @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
4648 */
47 public SiteSwitcherHandlerInterceptor(SiteUrlFactory normalSiteUrlFactory, SiteUrlFactory mobileSiteUrlFactory) {
49 public SiteSwitcherHandlerInterceptor(SiteUrlFactory normalSiteUrlFactory, SiteUrlFactory mobileSiteUrlFactory,
50 SitePreferenceRepository sitePreferenceRepository) {
4851 this.normalSiteUrlFactory = normalSiteUrlFactory;
4952 this.mobileSiteUrlFactory = mobileSiteUrlFactory;
53 this.sitePreferenceResolver = new SitePreferenceResolver(sitePreferenceRepository);
5054 }
5155
5256 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
53 SitePreference sitePreference = SitePreferenceResolvingHandlerInterceptor.getCurrentSitePreference(request);
57 SitePreference sitePreference = sitePreferenceResolver.resolveSitePreference(request, response);
5458 if (mobileSiteUrlFactory.isRequestForSite(request)) {
5559 if (sitePreference == SitePreference.NORMAL) {
5660 response.sendRedirect(response.encodeRedirectURL(normalSiteUrlFactory.createSiteUrl(request)));
5761 return false;
5862 }
5963 } else {
60 Device device = DeviceResolvingHandlerInterceptor.getRequiredCurrentDevice(request);
64 Device device = DeviceResolverHandlerInterceptor.getRequiredCurrentDevice(request);
6165 if (sitePreference == SitePreference.MOBILE || device.isMobile() && sitePreference == null) {
6266 response.sendRedirect(response.encodeRedirectURL(mobileSiteUrlFactory.createSiteUrl(request)));
6367 return false;
7373 // static factory methods
7474
7575 /**
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.
7879 */
7980 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));
8183 }
8284
8385 /**
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.
8791 */
8892 public static SiteSwitcherHandlerInterceptor dotMobi(String serverName) {
8993 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));
9197 }
9298
9399}
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 */
16package org.springframework.mobile.device.switcher;
17
18import 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 */
25public 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)
  
3737
3838import org.apache.commons.collections.CollectionUtils;
3939import org.apache.commons.collections.functors.InstanceofPredicate;
40import org.springframework.mobile.device.mvc.DeviceResolvingHandlerInterceptor;
40import org.springframework.mobile.device.mvc.DeviceResolverHandlerInterceptor;
4141import org.springframework.web.servlet.View;
4242
4343/**
7474 target.render(model, request, buffered);
7575 // logic adapted from WNGContextFilter which has the same responsibility
7676 if (isWngDocumentCreated(request)) {
77 WNGDevice device = new WNGDevice((Device) DeviceResolvingHandlerInterceptor.getCurrentDevice(request));
77 WNGDevice device = new WNGDevice((Device) DeviceResolverHandlerInterceptor.getCurrentDevice(request));
7878 Document document = resolveDocument(request);
7979 StyleContainer styleContainer = (StyleContainer)CollectionUtils.find(document.getHead().getChildren(), new InstanceofPredicate(StyleContainer.class));
8080 if (styleContainer == null) {
spring-mobile-device/src/test/java/org/springframework/mobile/device/mvc/DeviceResolvingHandlerInterceptorTest.java
(4 / 4)
  
1515
1616 private Device device = new StubDevice();
1717
18 private DeviceResolvingHandlerInterceptor interceptor = new DeviceResolvingHandlerInterceptor(new DeviceResolver() {
18 private DeviceResolverHandlerInterceptor interceptor = new DeviceResolverHandlerInterceptor(new DeviceResolver() {
1919 public Device resolveDevice(HttpServletRequest request) {
2020 return device;
2121 }
2828 @Test
2929 public void resolve() throws Exception {
3030 assertTrue(interceptor.preHandle(request, response, null));
31 assertSame(device, DeviceResolvingHandlerInterceptor.getCurrentDevice(request));
31 assertSame(device, DeviceResolverHandlerInterceptor.getCurrentDevice(request));
3232 }
3333
3434 @Test
3535 public void resolveDefaultResolver() throws Exception {
36 interceptor = new DeviceResolvingHandlerInterceptor();
36 interceptor = new DeviceResolverHandlerInterceptor();
3737 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");
3838 assertTrue(interceptor.preHandle(request, response, null));
39 Device device = DeviceResolvingHandlerInterceptor.getCurrentDevice(request);
39 Device device = DeviceResolverHandlerInterceptor.getCurrentDevice(request);
4040 assertTrue(device.isMobile());
4141 }
4242
spring-mobile-device/src/test/java/org/springframework/mobile/device/mvc/DeviceWebArgumentResolverTest.java
(2 / 2)
  
2121
2222 @Test
2323 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);
2525 MethodParameter parameter = new MethodParameter(getClass().getMethod("handlerMethod", Device.class), 0);
2626 Object resolved = resolver.resolveArgument(parameter, request);
2727 assertSame(device, resolved);
2929
3030 @Test
3131 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);
3333 MethodParameter parameter = new MethodParameter(getClass().getMethod("handlerMethodUnresolved", String.class), 0);
3434 Object resolved = resolver.resolveArgument(parameter, request);
3535 assertSame(WebArgumentResolver.UNRESOLVED, resolved);
spring-mobile-device/src/test/java/org/springframework/mobile/device/mvc/StubDevice.java
(5 / 1)
  
99 public StubDevice() {
1010 this.mobile = true;
1111 }
12
12
13 public StubDevice(boolean mobile) {
14 this.mobile = mobile;
15 }
16
1317 public boolean isMobile() {
1418 return mobile;
1519 }
spring-mobile-device/src/test/java/org/springframework/mobile/device/site/SitePreferenceResolverTest.java
(63 / 0)
  
1package org.springframework.mobile.device.site;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertNull;
5
6import org.junit.Before;
7import org.junit.Test;
8import org.springframework.mobile.device.mvc.DeviceResolverHandlerInterceptor;
9import org.springframework.mobile.device.mvc.StubDevice;
10import org.springframework.mock.web.MockHttpServletRequest;
11import org.springframework.mock.web.MockHttpServletResponse;
12
13public 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)
  
1package org.springframework.mobile.device.site;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertNull;
5import static org.junit.Assert.assertTrue;
6
7import org.junit.Before;
8import org.junit.Test;
9import org.springframework.mobile.device.mvc.DeviceResolvingHandlerInterceptor;
10import org.springframework.mobile.device.mvc.StubDevice;
11import org.springframework.mock.web.MockHttpServletRequest;
12import org.springframework.mock.web.MockHttpServletResponse;
13
14public 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)
  
1919
2020 @Test
2121 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);
2323 MethodParameter parameter = new MethodParameter(getClass().getMethod("handlerMethod", SitePreference.class), 0);
2424 Object resolved = resolver.resolveArgument(parameter, request);
2525 assertEquals(SitePreference.MOBILE, resolved);
2727
2828 @Test
2929 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);
3131 MethodParameter parameter = new MethodParameter(getClass().getMethod("handlerMethodUnresolved", String.class), 0);
3232 Object resolved = resolver.resolveArgument(parameter, request);
3333 assertSame(WebArgumentResolver.UNRESOLVED, resolved);
spring-mobile-device/src/test/java/org/springframework/mobile/device/site/StubSitePreferenceRepository.java
(8 / 1)
  
66import org.springframework.mobile.device.site.SitePreference;
77import org.springframework.mobile.device.site.SitePreferenceRepository;
88
9class StubSitePreferenceRepository implements SitePreferenceRepository {
9public class StubSitePreferenceRepository implements SitePreferenceRepository {
1010
1111 private SitePreference sitePreference;
1212
1818 return sitePreference;
1919 }
2020
21 public SitePreference getSitePreference() {
22 return sitePreference;
23 }
24
25 public void setSitePreference(SitePreference sitePreference) {
26 this.sitePreference = sitePreference;
27 }
2128}
spring-mobile-device/src/test/java/org/springframework/mobile/device/switcher/ServerNameSiteUrlFactoryTest.java
(1 / 10)
  
99
1010public class ServerNameSiteUrlFactoryTest {
1111
12 private ServerNameSiteUrlFactory factory = new ServerNameSiteUrlFactory("m.app.com");
12 private StandardSiteUrlFactory factory = new StandardSiteUrlFactory("m.app.com");
1313
1414 private MockHttpServletRequest request = new MockHttpServletRequest();
1515
3131 request.setServerPort(80);
3232 request.setRequestURI("/foo");
3333 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));
4334 }
4435
4536}
spring-mobile-device/src/test/java/org/springframework/mobile/device/switcher/SiteSwitcherHandlerInterceptorTest.java
(39 / 16)
  
99
1010import org.junit.Before;
1111import org.junit.Test;
12import org.springframework.mobile.device.mvc.DeviceResolvingHandlerInterceptor;
12import org.springframework.mobile.device.mvc.DeviceResolverHandlerInterceptor;
1313import org.springframework.mobile.device.mvc.StubDevice;
1414import org.springframework.mobile.device.site.SitePreference;
15import org.springframework.mobile.device.site.SitePreferenceResolvingHandlerInterceptor;
15import org.springframework.mobile.device.site.StubSitePreferenceRepository;
1616import org.springframework.mock.web.MockHttpServletRequest;
1717import org.springframework.mock.web.MockHttpServletResponse;
1818
2424
2525 private MockHttpServletResponse response = new MockHttpServletResponse();
2626
27 private StubDevice device;
27 private StubDevice device = new StubDevice();
2828
29 private StubSitePreferenceRepository sitePreferenceRepository = new StubSitePreferenceRepository();
30
2931 @Before
3032 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);
3334 SiteUrlFactory normalSiteUrlFactory = new SiteUrlFactory() {
3435 public boolean isRequestForSite(HttpServletRequest request) {
3536 return request.getServerName().equals("app.com");
4747 return "http://m.app.com";
4848 }
4949 };
50 siteSwitcher = new SiteSwitcherHandlerInterceptor(normalSiteUrlFactory, mobileSiteUrlFactory);
50 siteSwitcher = new SiteSwitcherHandlerInterceptor(normalSiteUrlFactory, mobileSiteUrlFactory, sitePreferenceRepository);
5151 }
5252
5353 @Test
5858
5959 @Test
6060 public void mobileDeviceNormalSiteMobilePreference() throws Exception {
61 request.setAttribute(SitePreferenceResolvingHandlerInterceptor.CURRENT_SITE_PREFERENCE_ATTRIBUTE, SitePreference.MOBILE);
61 sitePreferenceRepository.setSitePreference(SitePreference.MOBILE);
6262 assertFalse(siteSwitcher.preHandle(request, response, null));
6363 assertEquals("http://m.app.com", response.getRedirectedUrl());
6464 }
6565
6666 @Test
6767 public void mobileDeviceNormalSiteNormalPreference() throws Exception {
68 request.setAttribute(SitePreferenceResolvingHandlerInterceptor.CURRENT_SITE_PREFERENCE_ATTRIBUTE, SitePreference.NORMAL);
68 sitePreferenceRepository.setSitePreference(SitePreference.NORMAL);
6969 assertTrue(siteSwitcher.preHandle(request, response, null));
7070 assertNull(response.getRedirectedUrl());
7171 }
8080 @Test
8181 public void normalDeviceNormalSiteMobilePreference() throws Exception {
8282 device.setMobile(false);
83 request.setAttribute(SitePreferenceResolvingHandlerInterceptor.CURRENT_SITE_PREFERENCE_ATTRIBUTE, SitePreference.MOBILE);
83 sitePreferenceRepository.setSitePreference(SitePreference.MOBILE);
8484 assertFalse(siteSwitcher.preHandle(request, response, null));
8585 assertEquals("http://m.app.com", response.getRedirectedUrl());
8686 }
8888 @Test
8989 public void normalDeviceNormalSiteNormalPreference() throws Exception {
9090 device.setMobile(false);
91 request.setAttribute(SitePreferenceResolvingHandlerInterceptor.CURRENT_SITE_PREFERENCE_ATTRIBUTE, SitePreference.NORMAL);
91 sitePreferenceRepository.setSitePreference(SitePreference.NORMAL);
9292 assertTrue(siteSwitcher.preHandle(request, response, null));
9393 assertNull(response.getRedirectedUrl());
9494 }
9595
9696 @Test
97 public void mDot() throws Exception {
97 public void mDotMobileDeviceNormalSiteNoPreference() throws Exception {
9898 SiteSwitcherHandlerInterceptor mDot = SiteSwitcherHandlerInterceptor.mDot("app.com");
9999 assertFalse(mDot.preHandle(request, response, null));
100100 assertEquals(0, response.getCookies().length);
102102 }
103103
104104 @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));
108119 assertEquals(0, response.getCookies().length);
109 assertEquals("http://app.mobi", response.getRedirectedUrl());
120 assertEquals("http://app.mobi", response.getRedirectedUrl());
110121 }
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
112134}
spring-mobile-device/src/test/resources/org/springframework/mobile/device/config/device.xml
(1 / 1)
  
1010 <device:wurfl-device-resolver id="rootAndPatches" root-location="classpath:org/springframework/mobile/device/wurfl/test-wurfl.xml"
1111 patch-locations="classpath:org/springframework/mobile/device/wurfl/test-wurfl-patch.xml" />
1212
13 <bean class="org.springframework.mobile.device.mvc.DeviceResolvingHandlerInterceptor">
13 <bean class="org.springframework.mobile.device.mvc.DeviceResolverHandlerInterceptor">
1414 <constructor-arg>
1515 <device:wurfl-device-resolver root-location="classpath:org/springframework/mobile/device/wurfl/test-wurfl.xml" />
1616 </constructor-arg>

Comments

Add a new comment:

Login or create an account to post a comment

Add your comment