From 33e7f0d8e3354499ebec908a96309c108055fad7 Mon Sep 17 00:00:00 2001 From: Kumar Chandrakant Date: Mon, 1 Apr 2019 01:21:01 +0530 Subject: [PATCH] Adding code for the tutorial tracked under BAEL-2488 (#6598) --- .../config/WebSocketMessageBrokerConfig.java | 67 +++++++++++++++++++ .../controllers/RestAPIController.java | 17 +++++ .../controllers/WebSocketController.java | 20 ++++++ .../springsockets/models/Greeting.java | 18 +++++ .../springsockets/models/Message.java | 22 ++++++ .../src/main/resources/static/rest.html | 39 +++++++++++ .../src/main/resources/static/rest.js | 21 ++++++ .../src/main/resources/static/ws.html | 39 +++++++++++ .../src/main/resources/static/ws.js | 26 +++++++ 9 files changed, 269 insertions(+) create mode 100644 spring-security-mvc-socket/src/main/java/com/baeldung/springsockets/config/WebSocketMessageBrokerConfig.java create mode 100644 spring-security-mvc-socket/src/main/java/com/baeldung/springsockets/controllers/RestAPIController.java create mode 100644 spring-security-mvc-socket/src/main/java/com/baeldung/springsockets/controllers/WebSocketController.java create mode 100644 spring-security-mvc-socket/src/main/java/com/baeldung/springsockets/models/Greeting.java create mode 100644 spring-security-mvc-socket/src/main/java/com/baeldung/springsockets/models/Message.java create mode 100644 spring-security-mvc-socket/src/main/resources/static/rest.html create mode 100644 spring-security-mvc-socket/src/main/resources/static/rest.js create mode 100644 spring-security-mvc-socket/src/main/resources/static/ws.html create mode 100644 spring-security-mvc-socket/src/main/resources/static/ws.js diff --git a/spring-security-mvc-socket/src/main/java/com/baeldung/springsockets/config/WebSocketMessageBrokerConfig.java b/spring-security-mvc-socket/src/main/java/com/baeldung/springsockets/config/WebSocketMessageBrokerConfig.java new file mode 100644 index 0000000000..f1fb6549ed --- /dev/null +++ b/spring-security-mvc-socket/src/main/java/com/baeldung/springsockets/config/WebSocketMessageBrokerConfig.java @@ -0,0 +1,67 @@ +package com.baeldung.springsockets.config; + +import java.util.List; + +import org.springframework.context.annotation.Configuration; +import org.springframework.messaging.converter.MessageConverter; +import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver; +import org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler; +import org.springframework.messaging.simp.config.ChannelRegistration; +import org.springframework.messaging.simp.config.MessageBrokerRegistry; +import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; +import org.springframework.web.socket.config.annotation.StompEndpointRegistry; +import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; +import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration; + +@Configuration +@EnableWebSocketMessageBroker +public class WebSocketMessageBrokerConfig implements WebSocketMessageBrokerConfigurer { + + @Override + public void registerStompEndpoints(StompEndpointRegistry registry) { + registry.addEndpoint("/ws"); + } + + @Override + public void configureMessageBroker(MessageBrokerRegistry config) { + config.setApplicationDestinationPrefixes("/app"); + config.enableSimpleBroker("/topic"); + } + + @Override + public void configureWebSocketTransport(WebSocketTransportRegistration registry) { + // TODO Auto-generated method stub + + } + + @Override + public void configureClientInboundChannel(ChannelRegistration registration) { + // TODO Auto-generated method stub + + } + + @Override + public void configureClientOutboundChannel(ChannelRegistration registration) { + // TODO Auto-generated method stub + + } + + @Override + public void addArgumentResolvers(List argumentResolvers) { + // TODO Auto-generated method stub + + } + + @Override + public void addReturnValueHandlers(List returnValueHandlers) { + // TODO Auto-generated method stub + + } + + @Override + public boolean configureMessageConverters(List messageConverters) { + // TODO Auto-generated method stub + return false; + } + +} \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/java/com/baeldung/springsockets/controllers/RestAPIController.java b/spring-security-mvc-socket/src/main/java/com/baeldung/springsockets/controllers/RestAPIController.java new file mode 100644 index 0000000000..a8d3d5ffae --- /dev/null +++ b/spring-security-mvc-socket/src/main/java/com/baeldung/springsockets/controllers/RestAPIController.java @@ -0,0 +1,17 @@ +package com.baeldung.springsockets.controllers; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping(path = "/rest") +public class RestAPIController { + + @GetMapping(path = "/{name}", produces = "application/json") + public String getGreeting(@PathVariable("name") String name) { + return "{\"greeting\" : \"Hello, " + name + "!\"}"; + } + +} diff --git a/spring-security-mvc-socket/src/main/java/com/baeldung/springsockets/controllers/WebSocketController.java b/spring-security-mvc-socket/src/main/java/com/baeldung/springsockets/controllers/WebSocketController.java new file mode 100644 index 0000000000..26a67e4236 --- /dev/null +++ b/spring-security-mvc-socket/src/main/java/com/baeldung/springsockets/controllers/WebSocketController.java @@ -0,0 +1,20 @@ +package com.baeldung.springsockets.controllers; + +import org.springframework.messaging.handler.annotation.MessageMapping; +import org.springframework.messaging.handler.annotation.SendTo; +import org.springframework.stereotype.Controller; +import org.springframework.web.util.HtmlUtils; + +import com.baeldung.springsockets.models.Greeting; +import com.baeldung.springsockets.models.Message; + +@Controller +public class WebSocketController { + + @MessageMapping("/hello") + @SendTo("/topic/greetings") + public Greeting greeting(Message message) throws Exception { + return new Greeting("Hello, " + HtmlUtils.htmlEscape(message.getName()) + "!"); + } + +} \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/java/com/baeldung/springsockets/models/Greeting.java b/spring-security-mvc-socket/src/main/java/com/baeldung/springsockets/models/Greeting.java new file mode 100644 index 0000000000..b6122ac1e7 --- /dev/null +++ b/spring-security-mvc-socket/src/main/java/com/baeldung/springsockets/models/Greeting.java @@ -0,0 +1,18 @@ +package com.baeldung.springsockets.models; + +public class Greeting { + + private String content; + + public Greeting() { + } + + public Greeting(String content) { + this.content = content; + } + + public String getContent() { + return content; + } + +} diff --git a/spring-security-mvc-socket/src/main/java/com/baeldung/springsockets/models/Message.java b/spring-security-mvc-socket/src/main/java/com/baeldung/springsockets/models/Message.java new file mode 100644 index 0000000000..4dd002606a --- /dev/null +++ b/spring-security-mvc-socket/src/main/java/com/baeldung/springsockets/models/Message.java @@ -0,0 +1,22 @@ +package com.baeldung.springsockets.models; + +public class Message { + + private String name; + + public Message() { + } + + public Message(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/resources/static/rest.html b/spring-security-mvc-socket/src/main/resources/static/rest.html new file mode 100644 index 0000000000..476d053eea --- /dev/null +++ b/spring-security-mvc-socket/src/main/resources/static/rest.html @@ -0,0 +1,39 @@ + + + + Demo for RESTful Service for Comparison to WebSocket + + + + + + + +
+
+
+
+
+ + +
+ +
+
+
+
+
+ + + + + + + + +
Greetings
+
+
+
+ + \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/resources/static/rest.js b/spring-security-mvc-socket/src/main/resources/static/rest.js new file mode 100644 index 0000000000..403cc63585 --- /dev/null +++ b/spring-security-mvc-socket/src/main/resources/static/rest.js @@ -0,0 +1,21 @@ +var request = new XMLHttpRequest() + +function sendName() { + request.open('GET', 'http://localhost:8080/rest/'+$("#name").val(), true) + request.onload = function () { + var data = JSON.parse(this.response) + showGreeting(data.greeting) + } + request.send() +} + +function showGreeting(message) { + $("#greetings").append("" + message + ""); +} + +$(function () { + $("form").on('submit', function (e) { + e.preventDefault(); + }); + $( "#send" ).click(function() { sendName(); }); +}); \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/resources/static/ws.html b/spring-security-mvc-socket/src/main/resources/static/ws.html new file mode 100644 index 0000000000..638b8c0857 --- /dev/null +++ b/spring-security-mvc-socket/src/main/resources/static/ws.html @@ -0,0 +1,39 @@ + + + + Demo for WebSocket for Comparison to RESTful Service + + + + + + + +
+
+
+
+
+ + +
+ +
+
+
+
+
+ + + + + + + + +
Greetings
+
+
+
+ + \ No newline at end of file diff --git a/spring-security-mvc-socket/src/main/resources/static/ws.js b/spring-security-mvc-socket/src/main/resources/static/ws.js new file mode 100644 index 0000000000..538faf4e61 --- /dev/null +++ b/spring-security-mvc-socket/src/main/resources/static/ws.js @@ -0,0 +1,26 @@ +var stompClient = null; + +function connect() { + stompClient = Stomp.client('ws://localhost:8080/ws'); + stompClient.connect({}, function (frame) { + stompClient.subscribe('/topic/greetings', function (response) { + showGreeting(JSON.parse(response.body).content); + }); + }); +} + +function sendName() { + stompClient.send("/app/hello", {}, JSON.stringify({'name': $("#name").val()})); +} + +function showGreeting(message) { + $("#greetings").append("" + message + ""); +} + +$(function () { + connect(); + $("form").on('submit', function (e) { + e.preventDefault(); + }); + $( "#send" ).click(function() { sendName(); }); +}); \ No newline at end of file