-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inform admins when new oauth2 account is created using spring rabbitm…
…q events
- Loading branch information
1 parent
90507cb
commit c56c0f0
Showing
12 changed files
with
173 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
gateway/src/main/java/org/georchestra/gateway/events/RabbitmqEventsAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.georchestra.gateway.events; | ||
|
||
import org.springframework.amqp.core.AmqpTemplate; | ||
import org.springframework.amqp.rabbit.connection.ConnectionFactory; | ||
import org.springframework.amqp.rabbit.listener.MessageListenerContainer; | ||
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; | ||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
import org.springframework.amqp.core.Queue; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; | ||
import org.springframework.cloud.gateway.config.GatewayAutoConfiguration; | ||
import org.springframework.context.annotation.*; | ||
|
||
@Profile("!test && !it") | ||
@Configuration(proxyBeanMethods = false) | ||
@AutoConfigureAfter(GatewayAutoConfiguration.class) | ||
@ImportResource({ "classpath:rabbit-listener-context.xml", "classpath:rabbit-sender-context.xml" }) | ||
@ConditionalOnExpression("${georchestra.gateway.security.enableRabbitmqEvents:true}") | ||
public class RabbitmqEventsAutoConfiguration { | ||
|
||
@Bean | ||
@DependsOn({ "eventTemplate" }) | ||
public RabbitmqEventsSender eventsSender(AmqpTemplate eventTemplate) { | ||
return new RabbitmqEventsSender(eventTemplate); | ||
} | ||
|
||
Queue OAuth2ReplyQueue() { | ||
return new Queue("OAuth2ReplyQueue", false); | ||
} | ||
|
||
MessageListenerContainer messageListenerContainer(ConnectionFactory connectionFactory) { | ||
SimpleMessageListenerContainer simpleMessageListenerContainer = new SimpleMessageListenerContainer(); | ||
simpleMessageListenerContainer.setConnectionFactory(connectionFactory); | ||
simpleMessageListenerContainer.setQueues(OAuth2ReplyQueue()); | ||
simpleMessageListenerContainer.setMessageListener(new RabbitmqEventsListener()); | ||
return simpleMessageListenerContainer; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
gateway/src/main/java/org/georchestra/gateway/events/RabbitmqEventsListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.georchestra.gateway.events; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.json.JSONObject; | ||
import org.springframework.amqp.core.Message; | ||
import org.springframework.amqp.core.MessageListener; | ||
|
||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
@Slf4j(topic = "org.georchestra.gateway.events") | ||
public class RabbitmqEventsListener implements MessageListener { | ||
|
||
public static final String OAUTH2_ACCOUNT_CREATION_RECEIVED = "OAUTH2-ACCOUNT-CREATION-RECEIVED"; | ||
|
||
private static Set<String> synReceivedMessageUid = Collections.synchronizedSet(new HashSet<String>()); | ||
|
||
public void onMessage(Message message) { | ||
String messageBody = new String(message.getBody()); | ||
JSONObject jsonObj = new JSONObject(messageBody); | ||
String uid = jsonObj.getString("uid"); | ||
String subject = jsonObj.getString("subject"); | ||
if (subject.equals(OAUTH2_ACCOUNT_CREATION_RECEIVED) | ||
&& !synReceivedMessageUid.stream().anyMatch(s -> s.equals(uid))) { | ||
String msg = jsonObj.getString("msg"); | ||
synReceivedMessageUid.add(uid); | ||
log.info(msg); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
gateway/src/main/java/org/georchestra/gateway/events/RabbitmqEventsSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.georchestra.gateway.events; | ||
|
||
import org.json.JSONObject; | ||
import org.springframework.amqp.core.AmqpTemplate; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.ApplicationContext; | ||
|
||
import java.util.UUID; | ||
|
||
public class RabbitmqEventsSender { | ||
|
||
public static final String OAUTH2_ACCOUNT_CREATION = "OAUTH2-ACCOUNT-CREATION"; | ||
|
||
@Autowired | ||
private ApplicationContext applicationContext; | ||
|
||
private AmqpTemplate eventTemplate; | ||
|
||
public RabbitmqEventsSender(AmqpTemplate eventTemplate) { | ||
this.eventTemplate = eventTemplate; | ||
} | ||
|
||
public void sendNewOAuthAccountMessage(String username, String email, String provider) throws Exception { | ||
// beans | ||
// getting a reference to | ||
// the sender | ||
JSONObject jsonObj = new JSONObject(); | ||
jsonObj.put("uid", UUID.randomUUID()); | ||
jsonObj.put("subject", OAUTH2_ACCOUNT_CREATION); | ||
jsonObj.put("username", username); // bean | ||
jsonObj.put("email", email); // bean | ||
jsonObj.put("provider", provider); // bean | ||
eventTemplate.convertAndSend("routing-gateway", jsonObj.toString());// send | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd | ||
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd"> | ||
<rabbit:connection-factory id="connectionFactory" host="${rabbitmqHost}" username="${rabbitmqUser}" password="${rabbitmqPassword}" /> | ||
<rabbit:admin connection-factory="connectionFactory" /> | ||
<!-- Create OAuth2Queue queue --> | ||
<rabbit:queue id="OAuth2ReplyQueue" /> | ||
<!-- create OAuth2Exchange and bind OAuth2Queue with routing-gateway to the OAUTH2-EXCHANGE--> | ||
<rabbit:topic-exchange id="OAuth2Exchange" name="OAUTH2-EXCHANGE-GATEWAY"> | ||
<rabbit:bindings> | ||
<rabbit:binding queue="OAuth2ReplyQueue" pattern="routing-console"></rabbit:binding> | ||
</rabbit:bindings> | ||
</rabbit:topic-exchange> | ||
<!-- instantiate eventsListener --> | ||
<bean id="eventsListener" class="org.georchestra.gateway.events.RabbitmqEventsListener" > | ||
</bean> | ||
<!-- glue the listener and OAuth2Queue to the container--> | ||
<rabbit:listener-container connection-factory="connectionFactory"> | ||
<rabbit:listener ref="eventsListener" queues="OAuth2ReplyQueue" /></rabbit:listener-container> | ||
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit" | ||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd | ||
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd"> | ||
<!-- obtain admin rights to create the an exchange --> | ||
<rabbit:admin connection-factory="connectionFactory" /> | ||
|
||
<!-- create a bean which can send message to OAUTH2-EXCHANGE for the program to call --> | ||
<rabbit:template id="eventTemplate" connection-factory="connectionFactory" exchange="OAUTH2-EXCHANGE"/> | ||
</beans> |