diff --git a/README.md b/README.md index f4693f1..69f2f46 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,18 @@ Inside the application project there is a application.springboot.web package con Inside the wlpcfg project there is the it.springboot.web.HelloControllerTest that will test the Spring MVC endpoint to ensure it is working. +## Spring Boot with WebSocket +This provides you with a SpringBoot application that will run on WebSphere Liberty. + +Inside the application project there are four classes: +* application.springboot.Application: The entry point for the SpringBoot application. +* application.springboot.websocket.MvcConfig: Map each uri to static html resources +* application.springboot.websocket.SystemWebSocketHandler: Creating a WebSocket server by implementing WebSocketHandler +* application.springboot.websocket.WebSocketConfig: The WebSocket config class to map the WebSocket handler at a specific URL + +Inside the wlpcfg project there is the it.springboot.security.HelloControllerTest that will test the /springbootwebsocket endpoint to ensure it is working. + + ## Websockets This provides you with the websocket-1.1 feature. diff --git a/build.gradle b/build.gradle index 4b6b418..8cb5156 100644 --- a/build.gradle +++ b/build.gradle @@ -25,7 +25,7 @@ allprojects { mavenCentral() } dependencies { - classpath 'net.wasdev.wlp.gradle.plugins:liberty-gradle-plugin:1.1-SNAPSHOT' + classpath 'net.wasdev.wlp.gradle.plugins:liberty-gradle-plugin:1.0' classpath files('gradle/wlp-anttasks.jar') } } diff --git a/liberty-starter-application/build.gradle b/liberty-starter-application/build.gradle index 7fa228a..946960f 100644 --- a/liberty-starter-application/build.gradle +++ b/liberty-starter-application/build.gradle @@ -108,7 +108,7 @@ liberty { } clean.dependsOn(':liberty-starter-wlpcfg:cleanServer') -war.dependsOn(':liberty-filter-application:publishWar', ':starter-microservice-test:publishTestWar', ':starter-microservice-web:publishWar', ':starter-microservice-persistence:publishWar', ':starter-microservice-rest:publishWar', ':starter-microservice-websocket:publishWar', ':starter-microservice-springboot-web:publishWar', ':starter-microservice-watsonsdk:publishWar', 'compressSkeletonBuildImage', 'copySpecialFiles', 'createSwaggerJSON') +war.dependsOn(':liberty-filter-application:publishWar', ':starter-microservice-test:publishTestWar', ':starter-microservice-web:publishWar', ':starter-microservice-persistence:publishWar', ':starter-microservice-rest:publishWar', ':starter-microservice-websocket:publishWar', ':starter-microservice-springboot-web:publishWar', ':starter-microservice-springboot-websocket:publishWar', ':starter-microservice-watsonsdk:publishWar', 'compressSkeletonBuildImage', 'copySpecialFiles', 'createSwaggerJSON') installFeature.dependsOn('installLiberty') libertyStart.dependsOn('installFeature', 'addServerEnv', 'publishWar') fvt.dependsOn('libertyStart', 'testClasses') diff --git a/liberty-starter-application/src/main/resources/services.json b/liberty-starter-application/src/main/resources/services.json index c06aed3..9aaffc8 100644 --- a/liberty-starter-application/src/main/resources/services.json +++ b/liberty-starter-application/src/main/resources/services.json @@ -35,6 +35,12 @@ "name": "Watson SDK", "description": "Adds the Watson SDK dependency to the project, allowing you to integrate Watson services", "endpoint" : "/watsonsdk" + }, + { + "id": "springbootwebsocket", + "name": "Spring Boot with WebSocket", + "description": "Spring Boot with WebSocket", + "endpoint" : "/springbootwebsocket" } ] } diff --git a/liberty-starter-wlpcfg/servers/StarterServer/server.xml b/liberty-starter-wlpcfg/servers/StarterServer/server.xml index bd3020d..ae93ab2 100644 --- a/liberty-starter-wlpcfg/servers/StarterServer/server.xml +++ b/liberty-starter-wlpcfg/servers/StarterServer/server.xml @@ -39,4 +39,5 @@ + \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 5af1dad..0763292 100644 --- a/settings.gradle +++ b/settings.gradle @@ -22,4 +22,4 @@ include 'services:webservice' */ rootProject.name = 'LibertyUserExperience' -include 'liberty-starter-wlpcfg', 'liberty-filter-application', 'liberty-starter-application', 'starter-microservice-test', 'starter-microservice-web', 'starter-microservice-persistence', 'starter-microservice-rest', 'starter-microservice-websocket', 'starter-microservice-watsonsdk', 'starter-microservice-springboot-web', 'liberty-starter-model', 'liberty-starter-common' +include 'liberty-starter-wlpcfg', 'liberty-filter-application', 'liberty-starter-application', 'starter-microservice-test', 'starter-microservice-web', 'starter-microservice-persistence', 'starter-microservice-rest', 'starter-microservice-websocket', 'starter-microservice-watsonsdk', 'starter-microservice-springboot-web', 'starter-microservice-springboot-websocket', 'liberty-starter-model', 'liberty-starter-common' diff --git a/starter-microservice-springboot-websocket/.gitignore b/starter-microservice-springboot-websocket/.gitignore new file mode 100644 index 0000000..f5d14ab --- /dev/null +++ b/starter-microservice-springboot-websocket/.gitignore @@ -0,0 +1,7 @@ +/target +/pom.xml +/build +/.settings +/.classpath +/.project +/bin diff --git a/starter-microservice-springboot-websocket/build.gradle b/starter-microservice-springboot-websocket/build.gradle new file mode 100644 index 0000000..b5819d9 --- /dev/null +++ b/starter-microservice-springboot-websocket/build.gradle @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2016 IBM Corp. + * + * 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. +*/ + +apply from: '../common-java.gradle' +apply from: '../common-war.gradle' + +import com.ibm.liberty.starter.gradle.MavenTask +import com.ibm.liberty.starter.gradle.FvtTask + +dependencies { + providedCompile group:'javax.ws.rs', name:'javax.ws.rs-api', version: '2.0' + providedCompile group:'javax.json', name:'javax.json-api', version:'1.0' +} + +buildutils { + contextRoot = '/springbootwebsocket' +} + +task installAllPOMs(type: MavenTask) { + id 'springbootwebsocket' + hasProvided true + hasRuntime true + hasCompile true + hasServerSnippet true +} + +task fvt(type: FvtTask) { + id 'starter-microservice-springboot-websocket' + finalizedBy libertyStop +} + +apply from: '../tech-type-task-graph.gradle' diff --git a/starter-microservice-springboot-websocket/repository/0.0.1/compile-pom.xml b/starter-microservice-springboot-websocket/repository/0.0.1/compile-pom.xml new file mode 100644 index 0000000..a0718d2 --- /dev/null +++ b/starter-microservice-springboot-websocket/repository/0.0.1/compile-pom.xml @@ -0,0 +1,44 @@ + + 4.0.0 + + net.wasdev.wlp.starters.springbootwebsocket + compile-pom + 0.0.1 + + compile-pom + + + + The Apache Software License, Version 2.0 + https://raw.github.com/WASdev/tool.artisan.core/master/LICENSE + repo + + + + + scm:git:git@github.com:WASdev/tool.artisan.core.git + scm:git@github.com:WASdev/tool.artisan.core.git + git@github.com:WASdev/tool.artisan.core.git + + + + + org.springframework.boot + spring-boot-starter-websocket + 1.3.0.RELEASE + + + org.springframework.boot + spring-boot-starter-tomcat + + + + + org.springframework.boot + spring-boot-starter-thymeleaf + 1.3.0.RELEASE + + + + \ No newline at end of file diff --git a/starter-microservice-springboot-websocket/repository/0.0.1/provided-pom.xml b/starter-microservice-springboot-websocket/repository/0.0.1/provided-pom.xml new file mode 100644 index 0000000..b1ff891 --- /dev/null +++ b/starter-microservice-springboot-websocket/repository/0.0.1/provided-pom.xml @@ -0,0 +1,46 @@ + + 4.0.0 + + net.wasdev.wlp.starters.springbootwebsocket + provided-pom + 0.0.1 + + provided-pom + + + + The Apache Software License, Version 2.0 + https://raw.github.com/WASdev/tool.artisan.core/master/LICENSE + repo + + + + + scm:git:git@github.com:WASdev/tool.artisan.core.git + scm:git@github.com:WASdev/tool.artisan.core.git + git@github.com:WASdev/tool.artisan.core.git + + + + + ibm-maven-repo + ibm-maven-repo + https://public.dhe.ibm.com/ibmdl/export/pub/software/websphere/wasdev/maven/repository/ + + + + + + javax.servlet + javax.servlet-api + 3.1.0 + + + com.ibm.websphere.appserver.api + com.ibm.websphere.appserver.api.servlet + 1.0.10 + + + + \ No newline at end of file diff --git a/starter-microservice-springboot-websocket/repository/0.0.1/runtime-pom.xml b/starter-microservice-springboot-websocket/repository/0.0.1/runtime-pom.xml new file mode 100644 index 0000000..96da6db --- /dev/null +++ b/starter-microservice-springboot-websocket/repository/0.0.1/runtime-pom.xml @@ -0,0 +1,34 @@ + + 4.0.0 + + net.wasdev.wlp.starters.springbootwebsocket + runtime-pom + 0.0.1 + + runtime-pom + + + + The Apache Software License, Version 2.0 + https://raw.github.com/WASdev/tool.artisan.core/master/LICENSE + repo + + + + + scm:git:git@github.com:WASdev/tool.artisan.core.git + scm:git@github.com:WASdev/tool.artisan.core.git + git@github.com:WASdev/tool.artisan.core.git + + + + + net.wasdev.wlp.starters.springbootwebsocket + server-snippet + xml + 0.0.1 + + + + \ No newline at end of file diff --git a/starter-microservice-springboot-websocket/repository/0.0.1/server-snippet.xml b/starter-microservice-springboot-websocket/repository/0.0.1/server-snippet.xml new file mode 100644 index 0000000..0fcd50e --- /dev/null +++ b/starter-microservice-springboot-websocket/repository/0.0.1/server-snippet.xml @@ -0,0 +1,22 @@ + + + + + + websocket-1.1 + servlet-3.1 + + + \ No newline at end of file diff --git a/starter-microservice-springboot-websocket/src/main/java/com/ibm/liberty/starter/service/springboot/websocket/api/TechnologyAPI.java b/starter-microservice-springboot-websocket/src/main/java/com/ibm/liberty/starter/service/springboot/websocket/api/TechnologyAPI.java new file mode 100644 index 0000000..bea7dba --- /dev/null +++ b/starter-microservice-springboot-websocket/src/main/java/com/ibm/liberty/starter/service/springboot/websocket/api/TechnologyAPI.java @@ -0,0 +1,25 @@ +/******************************************************************************* + * Copyright (c) 2016 IBM Corp. + * + * 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 com.ibm.liberty.starter.service.springboot.websocket.api; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; + +@ApplicationPath("api") +public class TechnologyAPI extends Application { + + +} diff --git a/starter-microservice-springboot-websocket/src/main/java/com/ibm/liberty/starter/service/springboot/websocket/api/v1/ProviderEndpoint.java b/starter-microservice-springboot-websocket/src/main/java/com/ibm/liberty/starter/service/springboot/websocket/api/v1/ProviderEndpoint.java new file mode 100644 index 0000000..26dd6ff --- /dev/null +++ b/starter-microservice-springboot-websocket/src/main/java/com/ibm/liberty/starter/service/springboot/websocket/api/v1/ProviderEndpoint.java @@ -0,0 +1,105 @@ +/******************************************************************************* + * Copyright (c) 2016 IBM Corp. + * + * 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 com.ibm.liberty.starter.service.springboot.websocket.api.v1; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; + +import com.ibm.liberty.starter.api.v1.model.provider.Dependency; +import com.ibm.liberty.starter.api.v1.model.provider.Dependency.Scope; +import com.ibm.liberty.starter.api.v1.model.provider.Location; +import com.ibm.liberty.starter.api.v1.model.provider.Provider; +import com.ibm.liberty.starter.api.v1.model.provider.ServerConfig; +import com.ibm.liberty.starter.api.v1.model.provider.Tag; + +@Path("v1/provider") +public class ProviderEndpoint { + + @GET + @Path("/") + @Produces(MediaType.APPLICATION_JSON) + public Provider details(@Context UriInfo info) { + Provider details = new Provider(); + String description = getStringResource("/description.html"); + details.setDescription(description); + + Location repoLocation = new Location(); + String url = info.getBaseUri().resolve("../artifacts").toString(); + repoLocation.setUrl(url); + details.setRepoUrl(repoLocation); + Dependency providedDependency = new Dependency(); + providedDependency.setScope(Scope.PROVIDED); + providedDependency.setGroupId("net.wasdev.wlp.starters.springbootwebsocket"); + providedDependency.setArtifactId("provided-pom"); + providedDependency.setVersion("0.0.1"); + + Dependency runtimeDependency = new Dependency(); + runtimeDependency.setScope(Scope.RUNTIME); + runtimeDependency.setGroupId("net.wasdev.wlp.starters.springbootwebsocket"); + runtimeDependency.setArtifactId("runtime-pom"); + runtimeDependency.setVersion("0.0.1"); + + Dependency compileDependency = new Dependency(); + compileDependency.setScope(Scope.COMPILE); + compileDependency.setGroupId("net.wasdev.wlp.starters.springbootwebsocket"); + compileDependency.setArtifactId("compile-pom"); + compileDependency.setVersion("0.0.1"); + + Dependency[] dependencies = {providedDependency, runtimeDependency, compileDependency}; + details.setDependencies(dependencies); + return details; + } + + //read the description contained in the index.html file + private String getStringResource(String path) { + InputStream in = getClass().getResourceAsStream(path); + + StringBuilder index = new StringBuilder(); + char[] buffer = new char[1024]; + int read = 0; + try(InputStreamReader reader = new InputStreamReader(in)){ + while((read = reader.read(buffer)) != -1) { + index.append(buffer, 0, read); + } + } catch (IOException e) { + //just return what we've got + return index.toString(); + } + return index.toString(); + } + + @GET + @Path("samples") + @Produces(MediaType.APPLICATION_JSON) + public Response constructSample(@Context UriInfo info) { + StringBuilder json = new StringBuilder("{\n"); + String base = info.getBaseUri().resolve("../sample").toString(); + json.append("\"base\" : \"" + base + "\",\n"); + json.append(getStringResource("/locations.json")); + json.append("}\n"); + return Response.ok(json.toString()).build(); + } + +} \ No newline at end of file diff --git a/starter-microservice-springboot-websocket/src/main/webapp/.gitignore b/starter-microservice-springboot-websocket/src/main/webapp/.gitignore new file mode 100644 index 0000000..5d43caf --- /dev/null +++ b/starter-microservice-springboot-websocket/src/main/webapp/.gitignore @@ -0,0 +1 @@ +/artifacts diff --git a/starter-microservice-springboot-websocket/src/main/webapp/META-INF/.gitignore b/starter-microservice-springboot-websocket/src/main/webapp/META-INF/.gitignore new file mode 100644 index 0000000..f67faf7 --- /dev/null +++ b/starter-microservice-springboot-websocket/src/main/webapp/META-INF/.gitignore @@ -0,0 +1 @@ +/swagger.json diff --git a/starter-microservice-springboot-websocket/src/main/webapp/WEB-INF/classes/.gitignore b/starter-microservice-springboot-websocket/src/main/webapp/WEB-INF/classes/.gitignore new file mode 100644 index 0000000..5342fdc --- /dev/null +++ b/starter-microservice-springboot-websocket/src/main/webapp/WEB-INF/classes/.gitignore @@ -0,0 +1 @@ +/locations.json diff --git a/starter-microservice-springboot-websocket/src/main/webapp/WEB-INF/classes/description.html b/starter-microservice-springboot-websocket/src/main/webapp/WEB-INF/classes/description.html new file mode 100644 index 0000000..8cf2319 --- /dev/null +++ b/starter-microservice-springboot-websocket/src/main/webapp/WEB-INF/classes/description.html @@ -0,0 +1,29 @@ + +
+

Spring Boot with WebSocket

+

This project demonstrate how to accessing relational data with Spring technology and run on IBM WebSphere Liberty Server

+

Learn more about IBM WebSphere Liberty, visit https://developer.ibm.com/wasdev/

+

Inside the application project there are four classes:

+ +

Inside the wlpcfg project there is the it.springboot.security.HelloControllerTest that will test the Spring WebSocket endpoint to ensure it is working.

+
diff --git a/starter-microservice-springboot-websocket/src/main/webapp/sample/myProject-application/src/main/java/application/springboot/Application.java b/starter-microservice-springboot-websocket/src/main/webapp/sample/myProject-application/src/main/java/application/springboot/Application.java new file mode 100644 index 0000000..23eb45c --- /dev/null +++ b/starter-microservice-springboot-websocket/src/main/webapp/sample/myProject-application/src/main/java/application/springboot/Application.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * Copyright (c) 2016 IBM Corp. + * + * 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 application.springboot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.context.web.SpringBootServletInitializer; + + +@SpringBootApplication + +public class Application extends SpringBootServletInitializer { + + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { + return application.sources(Application.class); + } + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} \ No newline at end of file diff --git a/starter-microservice-springboot-websocket/src/main/webapp/sample/myProject-application/src/main/java/application/springboot/websocket/MvcConfig.java b/starter-microservice-springboot-websocket/src/main/webapp/sample/myProject-application/src/main/java/application/springboot/websocket/MvcConfig.java new file mode 100644 index 0000000..8e9cec0 --- /dev/null +++ b/starter-microservice-springboot-websocket/src/main/webapp/sample/myProject-application/src/main/java/application/springboot/websocket/MvcConfig.java @@ -0,0 +1,30 @@ +/******************************************************************************* + * Copyright (c) 2016 IBM Corp. + * + * 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 application.springboot.websocket; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; + +@Configuration +public class MvcConfig extends WebMvcConfigurerAdapter { + + @Override + public void addViewControllers(ViewControllerRegistry registry) { + registry.addViewController("/springbootwebsocket").setViewName("websocketsample"); + } + +} diff --git a/starter-microservice-springboot-websocket/src/main/webapp/sample/myProject-application/src/main/java/application/springboot/websocket/SystemWebSocketHandler.java b/starter-microservice-springboot-websocket/src/main/webapp/sample/myProject-application/src/main/java/application/springboot/websocket/SystemWebSocketHandler.java new file mode 100644 index 0000000..435bd1a --- /dev/null +++ b/starter-microservice-springboot-websocket/src/main/webapp/sample/myProject-application/src/main/java/application/springboot/websocket/SystemWebSocketHandler.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * Copyright (c) 2016 IBM Corp. + * + * 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 application.springboot.websocket; + +import java.io.IOException; +import java.util.ArrayList; + +import org.apache.log4j.spi.LoggerFactory; +import org.slf4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cglib.core.Constants; +import org.springframework.web.socket.CloseStatus; +import org.springframework.web.socket.TextMessage; +import org.springframework.web.socket.WebSocketHandler; +import org.springframework.web.socket.WebSocketMessage; +import org.springframework.web.socket.WebSocketSession; + +public class SystemWebSocketHandler implements WebSocketHandler { + + + @Override + public void afterConnectionEstablished(WebSocketSession session) throws Exception { + + session.sendMessage(new TextMessage("Connection Established successfully!")); + + } + + @Override + public void handleMessage(WebSocketSession session, WebSocketMessage message) throws Exception { + + session.sendMessage(new TextMessage("Hello from server!")); + } + + @Override + public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { + + } + + @Override + public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception { + + + } + + @Override + public boolean supportsPartialMessages() { + return false; + } + +} diff --git a/starter-microservice-springboot-websocket/src/main/webapp/sample/myProject-application/src/main/java/application/springboot/websocket/WebSocketConfig.java b/starter-microservice-springboot-websocket/src/main/webapp/sample/myProject-application/src/main/java/application/springboot/websocket/WebSocketConfig.java new file mode 100644 index 0000000..d20bc0d --- /dev/null +++ b/starter-microservice-springboot-websocket/src/main/webapp/sample/myProject-application/src/main/java/application/springboot/websocket/WebSocketConfig.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2016 IBM Corp. + * + * 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 application.springboot.websocket; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.messaging.simp.config.MessageBrokerRegistry; +import org.springframework.web.socket.WebSocketHandler; +import org.springframework.web.socket.config.annotation.EnableWebSocket; +import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; +import org.springframework.web.socket.config.annotation.StompEndpointRegistry; +import org.springframework.web.socket.config.annotation.WebSocketConfigurer; +import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; + +@Configuration +@EnableWebSocket +public class WebSocketConfig implements WebSocketConfigurer { + @Override + public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { + registry.addHandler(systemWebSocketHandler(),"/webSocketServer"); + } + + @Bean + public WebSocketHandler systemWebSocketHandler(){ + return new SystemWebSocketHandler(); + } + + + +} + diff --git a/starter-microservice-springboot-websocket/src/main/webapp/sample/myProject-application/src/main/resources/application.properties b/starter-microservice-springboot-websocket/src/main/webapp/sample/myProject-application/src/main/resources/application.properties new file mode 100644 index 0000000..409b428 --- /dev/null +++ b/starter-microservice-springboot-websocket/src/main/webapp/sample/myProject-application/src/main/resources/application.properties @@ -0,0 +1 @@ +server.contextPath=/myLibertyApp \ No newline at end of file diff --git a/starter-microservice-springboot-websocket/src/main/webapp/sample/myProject-application/src/main/resources/templates/websocketsample.html b/starter-microservice-springboot-websocket/src/main/webapp/sample/myProject-application/src/main/resources/templates/websocketsample.html new file mode 100644 index 0000000..b0a86be --- /dev/null +++ b/starter-microservice-springboot-websocket/src/main/webapp/sample/myProject-application/src/main/resources/templates/websocketsample.html @@ -0,0 +1,60 @@ + + + + WebSocket Stomp Receiving Example + + +

Welcome to Spring WebSocket!

+

+ The text you type in will get sent to the server application using WebSockets. +

+

+ +
+
+ +
+
+ + + + \ No newline at end of file diff --git a/starter-microservice-springboot-websocket/src/main/webapp/sample/myProject-wlpcfg/src/test/java/it/springboot/websocket/HelloControllerTest.java b/starter-microservice-springboot-websocket/src/main/webapp/sample/myProject-wlpcfg/src/test/java/it/springboot/websocket/HelloControllerTest.java new file mode 100644 index 0000000..23be95e --- /dev/null +++ b/starter-microservice-springboot-websocket/src/main/webapp/sample/myProject-wlpcfg/src/test/java/it/springboot/websocket/HelloControllerTest.java @@ -0,0 +1,36 @@ +/******************************************************************************* + * Copyright (c) 2016 IBM Corp. + * + * 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 it.springboot.websocket; + +import static org.junit.Assert.assertTrue; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.Response; +import it.EndpointTest; + +import org.junit.Test; + +public class HelloControllerTest extends EndpointTest { + + + @Test + public void testDeployment() { + testEndpoint("/springbootwebsocket", "Welcome to Spring WebSocket!"); + } +} \ No newline at end of file diff --git a/starter-microservice-springboot-websocket/src/test/java/com/ibm/liberty/starter/service/springboot/websocket/api/v1/it/EndpointTest.java b/starter-microservice-springboot-websocket/src/test/java/com/ibm/liberty/starter/service/springboot/websocket/api/v1/it/EndpointTest.java new file mode 100644 index 0000000..02f80bd --- /dev/null +++ b/starter-microservice-springboot-websocket/src/test/java/com/ibm/liberty/starter/service/springboot/websocket/api/v1/it/EndpointTest.java @@ -0,0 +1,87 @@ +/******************************************************************************* + * Copyright (c) 2016 IBM Corp. + * + * 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 com.ibm.liberty.starter.service.springboot.api.v1.it; + +import static org.junit.Assert.assertTrue; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.Response; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class EndpointTest { + + //use this to give the REST endpoint a chance to come up after the server has reported it's started + public void checkAvailability(String endpoint) { + String ep = getEndPoint(endpoint); + for(int x = 0; x < 3; x++) { + Response response = null; + try { + response = sendRequest(ep, "GET"); + if(response.getStatus() == 200) { + return; + } + } finally { + if(response != null) { + response.close(); + } + } + try { + Thread.sleep(3000); + } catch (InterruptedException e) { + //just exit + } + } + } + + private String getEndPoint(String endpoint) { + String port = System.getProperty("liberty.test.port"); + String war = System.getProperty("war.name"); + return "http://localhost:" + port + "/" + war + endpoint; + } + + @SuppressWarnings("unchecked") + public T testEndpoint(String endpoint, Class entity) throws Exception { + String url = getEndPoint(endpoint); + System.out.println("Testing " + url); + Response response = null; + try { + response = sendRequest(url, "GET"); + int responseCode = response.getStatus(); + assertTrue("Incorrect response code for " + url + ": " + responseCode, + responseCode == 200); + String json = response.readEntity(String.class); + ObjectMapper mapper = new ObjectMapper(); + return (T) mapper.readValue(json, entity); + } finally { + if(response != null) { + response.close(); + } + } + } + + public Response sendRequest(String url, String requestType) { + Client client = ClientBuilder.newClient(); + System.out.println("Testing " + url); + WebTarget target = client.target(url); + Invocation.Builder invoBuild = target.request(); + Response response = invoBuild.build(requestType).invoke(); + return response; + } +} diff --git a/starter-microservice-springboot-websocket/src/test/java/com/ibm/liberty/starter/service/springboot/websocket/api/v1/it/TestApplication.java b/starter-microservice-springboot-websocket/src/test/java/com/ibm/liberty/starter/service/springboot/websocket/api/v1/it/TestApplication.java new file mode 100644 index 0000000..4e4242a --- /dev/null +++ b/starter-microservice-springboot-websocket/src/test/java/com/ibm/liberty/starter/service/springboot/websocket/api/v1/it/TestApplication.java @@ -0,0 +1,81 @@ +/******************************************************************************* + * Copyright (c) 2016 IBM Corp. + * + * 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 com.ibm.liberty.starter.service.springboot.api.v1.it; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.ibm.liberty.starter.api.v1.model.provider.Dependency; +import com.ibm.liberty.starter.api.v1.model.provider.Provider; +import com.ibm.liberty.starter.api.v1.model.provider.Sample; +import com.ibm.liberty.starter.api.v1.model.provider.ServerConfig; + +/** + * Test the deployed service responds as expected + * + */ +public class TestApplication extends EndpointTest { + + @Before + public void checkSetup() { + checkAvailability("/api/v1/provider/"); + } + + @Test + public void testProvider() throws Exception { + Provider provider = testEndpoint("/api/v1/provider/", Provider.class); + assertNotNull("No response from API for provider", provider); + assertTrue("Description was not found.", provider.getDescription().contains("

Spring Boot with WebSocket

")); + Dependency[] dependencies = provider.getDependencies(); + boolean providedDependency = false; + boolean runtimeDependency = false; + boolean compileDependency = false; + for (Dependency dependency : dependencies) { + if (Dependency.Scope.PROVIDED.equals(dependency.getScope())) { + assertTrue("groupId incorrect.", "net.wasdev.wlp.starters.springbootwebsocket".equals(dependency.getGroupId())); + assertTrue("artifactId incorrect.", "provided-pom".equals(dependency.getArtifactId())); + assertTrue("version incorrect.", "0.0.1".equals(dependency.getVersion())); + providedDependency = true; + } + if (Dependency.Scope.RUNTIME.equals(dependency.getScope())) { + assertTrue("groupId incorrect.", "net.wasdev.wlp.starters.springbootwebsocket".equals(dependency.getGroupId())); + assertTrue("artifactId incorrect.", "runtime-pom".equals(dependency.getArtifactId())); + assertTrue("version incorrect.", "0.0.1".equals(dependency.getVersion())); + runtimeDependency = true; + } + if (Dependency.Scope.COMPILE.equals(dependency.getScope())) { + assertTrue("groupId incorrect.", "net.wasdev.wlp.starters.springbootwebsocket".equals(dependency.getGroupId())); + assertTrue("artifactId incorrect.", "compile-pom".equals(dependency.getArtifactId())); + assertTrue("version incorrect.", "0.0.1".equals(dependency.getVersion())); + compileDependency = true; + } + } + assertTrue("Provided dependencies weren't specified.", providedDependency); + assertTrue("Runtime dependencies weren't specified.", runtimeDependency); + assertTrue("Compile dependencies weren't specified.", compileDependency); + } + + @Test + public void testSamples() throws Exception { + Sample sample = testEndpoint("/api/v1/provider/samples", Sample.class); + assertNotNull("No response from API for sample", sample); + assertNotNull("Expected locations", sample.getLocations()); + assertEquals("7 files were expected for sample", 7, sample.getLocations().length); + } + +}