This is the fifth of a series of blog posts in which I walk through the creation of a simple chat app with Solace PubSub+. In the last part, I explained how to add a simple authentication server and send a REST POST request as a Publish/Subscribe type of message. In this tutorial, you will learn how to send a REST POST request as a request/reply message. Specifically, you will learn how to:
Prerequisite
Level
The accompanying sample code for this tutorial is for sending a REST POST request as a Publish/Subscribe message. Since you will send a REST POST request as a Request/Reply message instead this time, you need to modify the sample code.
To modify the sample code, do the following:
git checkout remotes/origin/developer-exercise-5 -f
SolaceCloudProxy.java
//This header determines that the Http Request needs a response set(“Solace-Reply-Wait-Time-In-ms”,”3000”);
The Solace-specific HTTP header “Solace-Reply-Wait-Time-In-ms” specifies that the message exchange pattern is Request/Reply and the Solace message broker application must wait for the specified timeout value (e.g. 3 seconds) for a reply before it stops.
Learn about Solace-Reply-Wait-Time-In-ms.
//Pass through a response code based on the result of the REST-ful request AuthenticatedObject authenticatedObject = restTemplate.postForObject(solaceRESTHost + “/LOGIN/MESSAGE/REQUEST”, request, AuthenticatedObject.class);
“AuthenticatedObject” is defined in the AuthenticatedObject.java
file under solace-chat-common. It validates a client and determines whether to allow it to connect or not.
if (authenticatedObject.isAuthenticated()) { return new ResponseEntity(HttpStatus.OK); }else { return new ResponseEntity(HttpStatus.FORBIDDEN); }
To connect the authentication server to the PubSub+ instance, you need to define the connection details.
application-template.properties
file under src > main > resources.application.properties
.application.properties
file.When you receive a request to log in to the chat application, you need to authenticate the user and then send the response back to the application. To change the login response message, follow the steps below:
LoginMessageReplier.java
file.session.addSubscription(JCSMPFactory.onlyInstance().createTopic( REQUEST_TOPIC), true);
createReplyMessage
method, enter the following highlighted code to create a message object for the reply message and then convert the Json text in the request message to a user object type (Line 110).
private XMLMessage createReplyMessage(TextMessage request) throws JCSMPException { TextMessage replyMessage = JCSMPFactory.onlyInstance().createMessage(TextMessage.class); UserObject userObject = gson.fromJson(request.getText(), UserObject.class); }
private XMLMessage createReplyMessage(TextMessage request) throws JCSMPException { TextMessage replyMessage = JCSMPFactory.onlyInstance().createMessage(TextMessage.class); UserObject userObject = gson.fromJson(request.getText(), UserObject.class); //Validate the user boolean validUser = credentialsRepository.isValidUser(userObject.getUsername(), userObject.getPassword()); if (validUser) System.out.println(“Successfully validated a user”); else System.out.println(“Authentication failed”); }
if (validUser) System.out.println(“Successfully validated a user”); else System.out.println(“Authentication failed”); AuthenticatedObject authenticatedObject = new AuthenticatedObject(); authenticatedObject.setAuthenticated(validUser); replyMessage.setHTTPContentType(“application/json”); replyMessage.setText(gson.toJson(authenticatedObject)); replyMessage.setApplicationMessageId(request.getApplicationMessageId()); replyMessage.setDeliverToOne(true); replyMessage.setDeliveryMode(DeliveryMode.DIRECT); return replyMessage; }
To test the authentication and login request response message, follow these steps:
mvn spring-boot:run
to run the Web application.mvn spring-boot:run
to run the authentication server.localhost:8081
to view the application.HashMapCredentialRepository.java
file under the server directory in auth-server (e.g. ValidUser; solace).Congratulations! You now have a chat application that allows you to authenticate your user with a request/reply message pattern.
In the next part, you will learn how to modify the Web application to load balance requests.