Sample code for BAEL-1148 - earth001@gmail.com (#3268)
* Sample code for BAEL-1148 - earth001@gmail.com * Change tabs for spaces in non java files * Change tabs for spaces in non java files * Removed unnecessary argument
This commit is contained in:
		
							parent
							
								
									423486bce1
								
							
						
					
					
						commit
						09cbc1c6ee
					
				
							
								
								
									
										1
									
								
								spring-mvc-push/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								spring-mvc-push/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1 @@ | |||||||
|  | /.tern-project | ||||||
							
								
								
									
										91
									
								
								spring-mvc-push/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										91
									
								
								spring-mvc-push/pom.xml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,91 @@ | |||||||
|  | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||||
|  |     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||||||
|  |     <modelVersion>4.0.0</modelVersion> | ||||||
|  |     <groupId>com.baeldung</groupId> | ||||||
|  |     <artifactId>spring-mvc-push</artifactId> | ||||||
|  |     <packaging>war</packaging> | ||||||
|  |     <version>0.0.1-SNAPSHOT</version> | ||||||
|  |     <name>spring-mvc-push</name> | ||||||
|  |     <properties> | ||||||
|  |         <maven.compiler.source>1.8</maven.compiler.source> | ||||||
|  |         <maven.compiler.target>1.8</maven.compiler.target> | ||||||
|  |         <maven-surefire-plugin.version>2.20</maven-surefire-plugin.version> | ||||||
|  |         <maven.compiler.version>3.7.0</maven.compiler.version> | ||||||
|  |         <maven-war-plugin.version>3.2.0</maven-war-plugin.version> | ||||||
|  |         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||||||
|  |         <junit.jupiter.version>5.0.2</junit.jupiter.version> | ||||||
|  |         <spring.version>5.0.2.RELEASE</spring.version> | ||||||
|  |         <servlet.version>4.0.0</servlet.version> | ||||||
|  |         <jstl.version>1.2</jstl.version> | ||||||
|  |         <jsp-api.version>2.3.2-b02</jsp-api.version> | ||||||
|  |         <junit.jupiter.version>5.0.2</junit.jupiter.version> | ||||||
|  |         <junit.platform.version>1.0.2</junit.platform.version> | ||||||
|  |     </properties> | ||||||
|  |     <dependencies> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>org.springframework</groupId> | ||||||
|  |             <artifactId>spring-webmvc</artifactId> | ||||||
|  |             <version>${spring.version}</version> | ||||||
|  |         </dependency> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>javax.servlet</groupId> | ||||||
|  |             <artifactId>javax.servlet-api</artifactId> | ||||||
|  |             <version>${servlet.version}</version> | ||||||
|  |             <scope>provided</scope> | ||||||
|  |         </dependency> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>javax.servlet</groupId> | ||||||
|  |             <artifactId>jstl</artifactId> | ||||||
|  |             <version>${jstl.version}</version> | ||||||
|  |         </dependency> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>javax.servlet.jsp</groupId> | ||||||
|  |             <artifactId>javax.servlet.jsp-api</artifactId> | ||||||
|  |             <version>${jsp-api.version}</version> | ||||||
|  |         </dependency> | ||||||
|  |         <!--Testing --> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>org.springframework</groupId> | ||||||
|  |             <artifactId>spring-test</artifactId> | ||||||
|  |             <version>${spring.version}</version> | ||||||
|  |             <scope>test</scope> | ||||||
|  |         </dependency> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>org.junit.jupiter</groupId> | ||||||
|  |             <artifactId>junit-jupiter-engine</artifactId> | ||||||
|  |             <version>${junit.jupiter.version}</version> | ||||||
|  |             <scope>test</scope> | ||||||
|  |         </dependency> | ||||||
|  |     </dependencies> | ||||||
|  |     <build> | ||||||
|  |         <plugins> | ||||||
|  |             <plugin> | ||||||
|  |                 <groupId>org.apache.maven.plugins</groupId> | ||||||
|  |                 <artifactId>maven-compiler-plugin</artifactId> | ||||||
|  |                 <version>${maven.compiler.version}</version> | ||||||
|  |             </plugin> | ||||||
|  |             <plugin> | ||||||
|  |                 <groupId>org.apache.maven.plugins</groupId> | ||||||
|  |                 <artifactId>maven-war-plugin</artifactId> | ||||||
|  |                 <version>${maven-war-plugin.version}</version> | ||||||
|  |                 <configuration> | ||||||
|  |                     <warName>spring-mvc-push</warName> | ||||||
|  |                     <failOnMissingWebXml>false</failOnMissingWebXml> | ||||||
|  |                     <outputDirectory>${deploy-path}</outputDirectory> | ||||||
|  |                 </configuration> | ||||||
|  |             </plugin> | ||||||
|  |             <plugin> | ||||||
|  |                 <artifactId>maven-surefire-plugin</artifactId> | ||||||
|  |                 <version>${maven-surefire-plugin.version}</version> | ||||||
|  |                 <dependencies> | ||||||
|  |                     <dependency> | ||||||
|  |                         <groupId>org.junit.platform</groupId> | ||||||
|  |                         <artifactId>junit-platform-surefire-provider</artifactId> | ||||||
|  |                         <version>${junit.platform.version}</version> | ||||||
|  |                     </dependency> | ||||||
|  |                 </dependencies> | ||||||
|  |             </plugin> | ||||||
|  |         </plugins> | ||||||
|  |         <finalName>spring-mvc-push</finalName> | ||||||
|  |     </build> | ||||||
|  | </project> | ||||||
| @ -0,0 +1,48 @@ | |||||||
|  | package com.baeldung.config; | ||||||
|  | 
 | ||||||
|  | import javax.servlet.ServletContext; | ||||||
|  | import javax.servlet.ServletException; | ||||||
|  | import javax.servlet.ServletRegistration; | ||||||
|  | 
 | ||||||
|  | import org.springframework.context.annotation.Bean; | ||||||
|  | import org.springframework.context.annotation.ComponentScan; | ||||||
|  | import org.springframework.context.annotation.Configuration; | ||||||
|  | import org.springframework.web.WebApplicationInitializer; | ||||||
|  | import org.springframework.web.context.ContextLoaderListener; | ||||||
|  | import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; | ||||||
|  | import org.springframework.web.servlet.DispatcherServlet; | ||||||
|  | import org.springframework.web.servlet.config.annotation.EnableWebMvc; | ||||||
|  | import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; | ||||||
|  | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||||||
|  | import org.springframework.web.servlet.view.InternalResourceViewResolver; | ||||||
|  | 
 | ||||||
|  | @Configuration | ||||||
|  | @EnableWebMvc | ||||||
|  | @ComponentScan(basePackages = "com.baeldung.controller") | ||||||
|  | public class PushConfiguration implements WebApplicationInitializer, WebMvcConfigurer { | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void onStartup(ServletContext container) throws ServletException { | ||||||
|  |         AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); | ||||||
|  |         context.register(PushConfiguration.class); | ||||||
|  |         container.addListener(new ContextLoaderListener(context)); | ||||||
|  |         ServletRegistration.Dynamic dispatcher = container.addServlet("DispatcherServlet", new DispatcherServlet(context)); | ||||||
|  |         dispatcher.setLoadOnStartup(1); | ||||||
|  |         dispatcher.addMapping("/"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Bean | ||||||
|  |     public InternalResourceViewResolver jspViewResolver() { | ||||||
|  |         InternalResourceViewResolver bean = new InternalResourceViewResolver(); | ||||||
|  |         bean.setPrefix("/WEB-INF/views/"); | ||||||
|  |         bean.setSuffix(".jsp"); | ||||||
|  |         return bean; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void addResourceHandlers(ResourceHandlerRegistry registry) { | ||||||
|  |         registry.addResourceHandler("/resources/**") | ||||||
|  |             .addResourceLocations("/resources/"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,31 @@ | |||||||
|  | package com.baeldung.controller; | ||||||
|  | 
 | ||||||
|  | import javax.servlet.http.PushBuilder; | ||||||
|  | 
 | ||||||
|  | import org.springframework.stereotype.Controller; | ||||||
|  | import org.springframework.web.bind.annotation.RequestMapping; | ||||||
|  | 
 | ||||||
|  | @Controller | ||||||
|  | public class PushController { | ||||||
|  | 
 | ||||||
|  |     @RequestMapping(value = "/demoWithPush") | ||||||
|  |     public String demoWithPush(PushBuilder pushBuilder) { | ||||||
|  |         if (null != pushBuilder) { | ||||||
|  |             pushBuilder.path("resources/logo.png") | ||||||
|  |                 .addHeader("Content-Type", "image/png") | ||||||
|  |                 .push(); | ||||||
|  |             pushBuilder.path("resources/script.js") | ||||||
|  |                 .addHeader("Content-Type", "text/javascript") | ||||||
|  |                 .push(); | ||||||
|  |             pushBuilder.path("resources/style.css") | ||||||
|  |                 .addHeader("Content-Type", "text/css") | ||||||
|  |                 .push(); | ||||||
|  |         } | ||||||
|  |         return "demo"; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @RequestMapping(value = "/demoWithoutPush") | ||||||
|  |     public String demoWithoutPush() { | ||||||
|  |         return "demo"; | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										22
									
								
								spring-mvc-push/src/main/webapp/WEB-INF/views/demo.jsp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								spring-mvc-push/src/main/webapp/WEB-INF/views/demo.jsp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,22 @@ | |||||||
|  | <%@ page language="java" contentType="text/html; charset=UTF-8" | ||||||
|  | 	pageEncoding="UTF-8"%> | ||||||
|  | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> | ||||||
|  | <html> | ||||||
|  | <head> | ||||||
|  | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||||||
|  | <title>PushBuilder demo</title> | ||||||
|  | <link href="<c:url value="/resources/style.css"/>" rel="stylesheet" /> | ||||||
|  | </head> | ||||||
|  | <body> | ||||||
|  | 
 | ||||||
|  | 	<span class="single-title">PushBuilder demo</span> | ||||||
|  | 	<br> | ||||||
|  | 	<img src="<c:url value="/resources/logo.png"/>" alt="Logo" height="126" | ||||||
|  | 		width="411"> | ||||||
|  | 	<br> | ||||||
|  | 	<script type="text/javascript" | ||||||
|  | 		src="<c:url value="/resources/script.js"/>"></script> | ||||||
|  | 	<br> Go to | ||||||
|  | 	<a href="/spring-mvc-push/">index</a> | ||||||
|  | </body> | ||||||
|  | </html> | ||||||
							
								
								
									
										14
									
								
								spring-mvc-push/src/main/webapp/index.jsp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								spring-mvc-push/src/main/webapp/index.jsp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,14 @@ | |||||||
|  | <%@ page language="java" contentType="text/html; charset=UTF-8" | ||||||
|  |     pageEncoding="UTF-8"%> | ||||||
|  | <html> | ||||||
|  | <head> | ||||||
|  | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||||||
|  | <title>PushBuilder demo</title> | ||||||
|  | </head> | ||||||
|  | <body> | ||||||
|  |     <h4> | ||||||
|  |         Go to <a href="demoWithPush">PushBuilder demo</a><br> Go to | ||||||
|  |         <a href="demoWithoutPush">Simple demo</a> | ||||||
|  |     </h4> | ||||||
|  | </body> | ||||||
|  | </html> | ||||||
							
								
								
									
										
											BIN
										
									
								
								spring-mvc-push/src/main/webapp/resources/logo.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								spring-mvc-push/src/main/webapp/resources/logo.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 7.2 KiB | 
							
								
								
									
										1
									
								
								spring-mvc-push/src/main/webapp/resources/script.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								spring-mvc-push/src/main/webapp/resources/script.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1 @@ | |||||||
|  | console.log('Script') | ||||||
							
								
								
									
										9
									
								
								spring-mvc-push/src/main/webapp/resources/style.css
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								spring-mvc-push/src/main/webapp/resources/style.css
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,9 @@ | |||||||
|  | .single-title { | ||||||
|  |     font-size: 30px; | ||||||
|  |     color: #535353; | ||||||
|  |     font-weight: 200; | ||||||
|  |     letter-spacing: -1.5px; | ||||||
|  |     line-height: 64px; | ||||||
|  |     max-width: 750px; | ||||||
|  |     font-family: "Helvetica Neue", Helvetica, Arial, sans-serif | ||||||
|  | } | ||||||
| @ -0,0 +1,41 @@ | |||||||
|  | package com.baeldung.controller; | ||||||
|  | 
 | ||||||
|  | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||||||
|  | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||||||
|  | 
 | ||||||
|  | import org.junit.jupiter.api.BeforeEach; | ||||||
|  | import org.junit.jupiter.api.Disabled; | ||||||
|  | import org.junit.jupiter.api.Test; | ||||||
|  | import org.springframework.beans.factory.annotation.Autowired; | ||||||
|  | import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; | ||||||
|  | import org.springframework.test.web.servlet.MockMvc; | ||||||
|  | import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||||||
|  | import org.springframework.web.context.WebApplicationContext; | ||||||
|  | 
 | ||||||
|  | import com.baeldung.config.PushConfiguration; | ||||||
|  | 
 | ||||||
|  | @Disabled | ||||||
|  | @SpringJUnitWebConfig(PushConfiguration.class) | ||||||
|  | public class PushControllerIntegrationTest { | ||||||
|  |     @Autowired | ||||||
|  |     private WebApplicationContext webAppContext; | ||||||
|  |     private MockMvc mockMvc; | ||||||
|  | 
 | ||||||
|  |     @BeforeEach | ||||||
|  |     public void setup() { | ||||||
|  |         mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext) | ||||||
|  |             .build(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void whenDemoWithPushGETisPerformed_thenRetrievedStatusOk() throws Exception { | ||||||
|  |         mockMvc.perform(get("/demoWithPush")) | ||||||
|  |             .andExpect(status().isOk()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void whenDemoWithoutPushGETisPerformed_thenRetrievedStatusOk() throws Exception { | ||||||
|  |         mockMvc.perform(get("/demoWithoutPush")) | ||||||
|  |             .andExpect(status().isOk()); | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user