diff --git a/lite-site-switcher-handler/README.md b/lite-site-switcher-handler/README.md new file mode 100644 index 0000000..59e021b --- /dev/null +++ b/lite-site-switcher-handler/README.md @@ -0,0 +1,23 @@ +# Spring Mobile Lite Site Switcher Handler + +## Overview + +This sample app demonstrates many of the Site Switcher capabilities of the Spring Mobile Device module. + +## Build and Run the Sample Application + +1. Build the app: + + $ mvn clean install + +2. Deploy the .war to a Servlet 2.5 or > ServletContainer. This can be done via Maven on the command-line by running: + + $ mvn tomcat:run + + _Note: Alternatively, you may import the project into your IDE and deploy to a Servlet 2.5 or > container such as Tomcat 6 or 7._ + +3. Access the project at the following URL: + + http://localhost:8080/lite-site-switcher-handler + + _Note: Accessing this URL from your browser versus an Android emulator or iOS simulator will demonstrate the functionality_ diff --git a/lite-site-switcher-handler/pom.xml b/lite-site-switcher-handler/pom.xml new file mode 100644 index 0000000..394c14c --- /dev/null +++ b/lite-site-switcher-handler/pom.xml @@ -0,0 +1,16 @@ + + + 4.0.0 + + + org.springframework.mobile + spring-mobile-samples + 1.0.1-BUILD-SNAPSHOT + + + lite-site-switcher-handler + war + lite-site-switcher-handler + + \ No newline at end of file diff --git a/lite-site-switcher-handler/src/main/java/org/springframework/showcases/lite/AboutController.java b/lite-site-switcher-handler/src/main/java/org/springframework/showcases/lite/AboutController.java new file mode 100644 index 0000000..a2b4fd2 --- /dev/null +++ b/lite-site-switcher-handler/src/main/java/org/springframework/showcases/lite/AboutController.java @@ -0,0 +1,37 @@ +/* + * 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.showcases.lite; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +/** + * Handles requests for the About page. + * @author Roy Clarkson + */ +@Controller +public class AboutController { + + /** + * Show the About page to the user. + */ + @RequestMapping("/about") + public String home(Model model) { + return "about"; + } + +} \ No newline at end of file diff --git a/lite-site-switcher-handler/src/main/java/org/springframework/showcases/lite/HomeController.java b/lite-site-switcher-handler/src/main/java/org/springframework/showcases/lite/HomeController.java new file mode 100644 index 0000000..e7d2769 --- /dev/null +++ b/lite-site-switcher-handler/src/main/java/org/springframework/showcases/lite/HomeController.java @@ -0,0 +1,37 @@ +/* + * 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.showcases.lite; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +/** + * Handles requests for the Home page. + * @author Roy Clarkson + */ +@Controller +public class HomeController { + + /** + * Show the home page to the user. + */ + @RequestMapping("/") + public String home(Model model) { + return "home"; + } + +} \ No newline at end of file diff --git a/lite-site-switcher-handler/src/main/java/org/springframework/showcases/lite/config/ComponentConfig.java b/lite-site-switcher-handler/src/main/java/org/springframework/showcases/lite/config/ComponentConfig.java new file mode 100644 index 0000000..2730f43 --- /dev/null +++ b/lite-site-switcher-handler/src/main/java/org/springframework/showcases/lite/config/ComponentConfig.java @@ -0,0 +1,28 @@ +/* + * 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.showcases.lite.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +/** + * @author Roy Clarkson + */ +@Configuration +@ComponentScan(basePackages = "org.springframework.showcases.lite") +public class ComponentConfig { + +} \ No newline at end of file diff --git a/lite-site-switcher-handler/src/main/java/org/springframework/showcases/lite/config/WebConfig.java b/lite-site-switcher-handler/src/main/java/org/springframework/showcases/lite/config/WebConfig.java new file mode 100644 index 0000000..05c024c --- /dev/null +++ b/lite-site-switcher-handler/src/main/java/org/springframework/showcases/lite/config/WebConfig.java @@ -0,0 +1,66 @@ +/* + * 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.showcases.lite.config; + +import java.util.List; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.mobile.device.DeviceHandlerMethodArgumentResolver; +import org.springframework.mobile.device.DeviceResolverHandlerInterceptor; +import org.springframework.mobile.device.switcher.SiteSwitcherHandlerInterceptor; +import org.springframework.web.method.support.HandlerMethodArgumentResolver; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.view.InternalResourceViewResolver; + +/** + * @author Roy Clarkson + * @see WebMvcConfigurer + */ +@Configuration +@EnableWebMvc +public class WebConfig extends WebMvcConfigurerAdapter { + + // implementing WebMvcConfigurer + + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(new DeviceResolverHandlerInterceptor()); + registry.addInterceptor(SiteSwitcherHandlerInterceptor.mDot("testdomain.com")); + } + + public void addArgumentResolvers(List argumentResolvers) { + argumentResolvers.add(new DeviceHandlerMethodArgumentResolver()); + } + + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); + } + + // additional webmvc-related beans + + @Bean + public InternalResourceViewResolver internalResourceViewResolver() { + InternalResourceViewResolver resolver = new InternalResourceViewResolver(); + resolver.setPrefix("/WEB-INF/views/"); + resolver.setSuffix(".jsp"); + return resolver; + } + +} \ No newline at end of file diff --git a/lite-site-switcher-handler/src/main/resources/log4j.xml b/lite-site-switcher-handler/src/main/resources/log4j.xml new file mode 100644 index 0000000..97e5edc --- /dev/null +++ b/lite-site-switcher-handler/src/main/resources/log4j.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/lite-site-switcher-handler/src/main/webapp/META-INF/MANIFEST.MF b/lite-site-switcher-handler/src/main/webapp/META-INF/MANIFEST.MF new file mode 100644 index 0000000..5e94951 --- /dev/null +++ b/lite-site-switcher-handler/src/main/webapp/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Class-Path: + diff --git a/lite-site-switcher-handler/src/main/webapp/WEB-INF/views/about.jsp b/lite-site-switcher-handler/src/main/webapp/WEB-INF/views/about.jsp new file mode 100644 index 0000000..a076097 --- /dev/null +++ b/lite-site-switcher-handler/src/main/webapp/WEB-INF/views/about.jsp @@ -0,0 +1,20 @@ +<%@ page session="false" %> +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> + + + + Spring Mobile | Lite Site Switcher Handler + + +
+

About

+
+ <%@include file="includes/content.jsp" %> + + + + \ No newline at end of file diff --git a/lite-site-switcher-handler/src/main/webapp/WEB-INF/views/home.jsp b/lite-site-switcher-handler/src/main/webapp/WEB-INF/views/home.jsp new file mode 100644 index 0000000..201e212 --- /dev/null +++ b/lite-site-switcher-handler/src/main/webapp/WEB-INF/views/home.jsp @@ -0,0 +1,20 @@ +<%@ page session="false" %> +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> + + + + Spring Mobile | Lite Site Switcher Handler + + +
+

Home

+
+ <%@include file="includes/content.jsp" %> + + + + \ No newline at end of file diff --git a/lite-site-switcher-handler/src/main/webapp/WEB-INF/views/includes/content.jsp b/lite-site-switcher-handler/src/main/webapp/WEB-INF/views/includes/content.jsp new file mode 100644 index 0000000..f45c196 --- /dev/null +++ b/lite-site-switcher-handler/src/main/webapp/WEB-INF/views/includes/content.jsp @@ -0,0 +1,21 @@ +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> +

+ + + You are currently viewing this page from a normal device. The site switcher has returned + the normal site. + + + You are currently viewing this page from a mobile device. Note the URL begins with "m.". + This means the site switcher has redirected you to the mobile version of the site. + + + You are currently viewing this page from a tablet device. Depending on how you have the + site switcher configured tablets are either presented with the normal site, or the mobile site. + + + No device was detected. This usually means that the device resolver is not configured properly. + In this case, the site switcher will return the normal site. + + +

\ No newline at end of file diff --git a/lite-site-switcher-handler/src/main/webapp/WEB-INF/web.xml b/lite-site-switcher-handler/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..2a2b02b --- /dev/null +++ b/lite-site-switcher-handler/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,51 @@ + + + + + + contextClass + org.springframework.web.context.support.AnnotationConfigWebApplicationContext + + + + + contextConfigLocation + org.springframework.showcases.lite.config + + + + + spring.profiles.default + simple + + + + + org.springframework.web.context.ContextLoaderListener + + + + + appServlet + org.springframework.web.servlet.DispatcherServlet + + contextConfigLocation + + + + spring.profiles.default + simple + + 1 + + + + appServlet + / + + + \ No newline at end of file diff --git a/lite-site-switcher-handler/src/test/java/org/springframework/showcases/lite/AboutControllerTest.java b/lite-site-switcher-handler/src/test/java/org/springframework/showcases/lite/AboutControllerTest.java new file mode 100644 index 0000000..672de5f --- /dev/null +++ b/lite-site-switcher-handler/src/test/java/org/springframework/showcases/lite/AboutControllerTest.java @@ -0,0 +1,31 @@ +package org.springframework.showcases.lite; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.springframework.ui.ExtendedModelMap; +import org.springframework.ui.Model; + +public class AboutControllerTest { + + private AboutController controller = new AboutController(); + + @Test + public void homePageNormalSitePreference() { + Model model = new ExtendedModelMap(); + assertEquals("about", controller.home(model)); + } + + @Test + public void homePageMobileSitePreference() { + Model model = new ExtendedModelMap(); + assertEquals("about", controller.home(model)); + } + + @Test + public void homePageTabletSitePreference() { + Model model = new ExtendedModelMap(); + assertEquals("about", controller.home(model)); + } + +} diff --git a/lite-site-switcher-handler/src/test/java/org/springframework/showcases/lite/HomeControllerTest.java b/lite-site-switcher-handler/src/test/java/org/springframework/showcases/lite/HomeControllerTest.java new file mode 100644 index 0000000..6faffc3 --- /dev/null +++ b/lite-site-switcher-handler/src/test/java/org/springframework/showcases/lite/HomeControllerTest.java @@ -0,0 +1,31 @@ +package org.springframework.showcases.lite; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.springframework.ui.ExtendedModelMap; +import org.springframework.ui.Model; + +public class HomeControllerTest { + + private HomeController controller = new HomeController(); + + @Test + public void homePageNormalSitePreference() { + Model model = new ExtendedModelMap(); + assertEquals("home", controller.home(model)); + } + + @Test + public void homePageMobileSitePreference() { + Model model = new ExtendedModelMap(); + assertEquals("home", controller.home(model)); + } + + @Test + public void homePageTabletSitePreference() { + Model model = new ExtendedModelMap(); + assertEquals("home", controller.home(model)); + } + +} diff --git a/lite-site-switcher-handler/src/test/resources/log4j.xml b/lite-site-switcher-handler/src/test/resources/log4j.xml new file mode 100644 index 0000000..63bf928 --- /dev/null +++ b/lite-site-switcher-handler/src/test/resources/log4j.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml index c83fb66..66bd0fc 100644 --- a/pom.xml +++ b/pom.xml @@ -18,6 +18,7 @@ /lite-device-resolver /lite-site-preference-handler + /lite-site-switcher-handler /lite-showcase