Permalink
Browse files

MOBILE-27 added DeviceResolverHandlerFilter for supporting web.xml co…

…nfigurations
  • Loading branch information...
1 parent 8d1bc4e commit b8fdf53d9b652218c75c26b7c3a7d6d48ea5096e @royclarkson royclarkson committed Feb 3, 2012
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2012 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.mobile.device;
+
+import java.io.IOException;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.springframework.web.filter.OncePerRequestFilter;
+
+/**
+ * A Servlet 2.3 Filter that resolves the Device that originated the web request. The resolved Device is exported as a
+ * request attribute under the well-known name of {@link DeviceUtils#CURRENT_DEVICE_ATTRIBUTE}. Request handlers such as @Controllers
+ * and views may then access the currentDevice to vary their control and rendering logic, respectively.
+ *
+ * @author Roy Clarkson
+ */
+public class DeviceResolverHandlerFilter extends OncePerRequestFilter {
+
+ private final DeviceResolver deviceResolver;
+
+ /**
+ * Create a device resolving {@link Filter} that defaults to a {@link LiteDeviceResolver} implementation.
+ */
+ public DeviceResolverHandlerFilter() {
+ this(new LiteDeviceResolver());
+ }
+
+ /**
+ * Create a device resolving {@link Filter}.
+ * @param deviceResolver the device resolver to delegate to.
+ */
+ public DeviceResolverHandlerFilter(DeviceResolver deviceResolver) {
+ this.deviceResolver = deviceResolver;
+ }
+
+ @Override
+ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
+ throws ServletException, IOException {
+ Device device = deviceResolver.resolveDevice(request);
+ request.setAttribute(DeviceUtils.CURRENT_DEVICE_ATTRIBUTE, device);
+ filterChain.doFilter(request, response);
+ }
+
+}
@@ -0,0 +1,44 @@
+package org.springframework.mobile.device;
+
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.junit.Test;
+import org.springframework.mock.web.MockFilterChain;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+public class DeviceResolverHandlerFilterTest {
+
+ private Device device = new StubDevice();
+
+ private DeviceResolverHandlerFilter filter = new DeviceResolverHandlerFilter(new DeviceResolver() {
+ public Device resolveDevice(HttpServletRequest request) {
+ return device;
+ }
+ });
+
+ private MockHttpServletRequest request = new MockHttpServletRequest();
+
+ private MockHttpServletResponse response = new MockHttpServletResponse();
+
+ private MockFilterChain filterChain = new MockFilterChain();
+
+ @Test
+ public void resolve() throws Exception {
+ filter.doFilter(request, response, filterChain);
+ assertSame(device, DeviceUtils.getCurrentDevice(request));
+ }
+
+ @Test
+ public void resolveDefaultResolver() throws Exception {
+ filter = new DeviceResolverHandlerFilter();
+ 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");
+ filter.doFilterInternal(request, response, filterChain);
+ Device device = DeviceUtils.getCurrentDevice(request);
+ assertTrue(device.isMobile());
+ }
+
+}

0 comments on commit b8fdf53

Please sign in to comment.