-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #167 from assimbly/develop
- REST get Event and Alerts
- Loading branch information
Showing
34 changed files
with
626 additions
and
95 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
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
131 changes: 131 additions & 0 deletions
131
src/main/java/org/assimbly/gateway/config/WebsocketConfiguration.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,131 @@ | ||
package org.assimbly.gateway.config; | ||
|
||
import org.assimbly.gateway.security.AuthoritiesConstants; | ||
|
||
import java.security.Principal; | ||
import java.util.*; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.server.*; | ||
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.security.authentication.AnonymousAuthenticationToken; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.web.socket.WebSocketHandler; | ||
import org.springframework.web.socket.config.annotation.*; | ||
import org.springframework.web.socket.server.HandshakeInterceptor; | ||
import org.springframework.web.socket.server.support.DefaultHandshakeHandler; | ||
|
||
import io.github.jhipster.config.JHipsterProperties; | ||
|
||
@Configuration | ||
@EnableWebSocketMessageBroker | ||
public class WebsocketConfiguration implements WebSocketMessageBrokerConfigurer { | ||
|
||
public static final String IP_ADDRESS = "IP_ADDRESS"; | ||
|
||
private final JHipsterProperties jHipsterProperties; | ||
|
||
public WebsocketConfiguration(JHipsterProperties jHipsterProperties) { | ||
this.jHipsterProperties = jHipsterProperties; | ||
} | ||
|
||
@Override | ||
public void configureMessageBroker(MessageBrokerRegistry config) { | ||
config.enableSimpleBroker("/topic"); | ||
} | ||
|
||
@Override | ||
public void registerStompEndpoints(StompEndpointRegistry registry) { | ||
|
||
String[] allowedOrigins = Optional.ofNullable(jHipsterProperties.getCors().getAllowedOrigins()).map(origins -> origins.toArray(new String[0])).orElse(new String[0]); | ||
|
||
registry.addEndpoint("/websocket/alert") | ||
.setHandshakeHandler(defaultHandshakeHandler()) | ||
.setAllowedOrigins(allowedOrigins) | ||
.withSockJS() | ||
.setInterceptors(httpSessionHandshakeInterceptor()); | ||
|
||
registry.addEndpoint("/topic/alert") | ||
.setHandshakeHandler(defaultHandshakeHandler()) | ||
.setAllowedOrigins(allowedOrigins) | ||
.withSockJS() | ||
.setInterceptors(httpSessionHandshakeInterceptor()); | ||
|
||
} | ||
|
||
@Bean | ||
public HandshakeInterceptor httpSessionHandshakeInterceptor() { | ||
return new HandshakeInterceptor() { | ||
|
||
@Override | ||
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception { | ||
if (request instanceof ServletServerHttpRequest) { | ||
ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request; | ||
attributes.put(IP_ADDRESS, servletRequest.getRemoteAddress()); | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) { | ||
|
||
} | ||
}; | ||
} | ||
|
||
private DefaultHandshakeHandler defaultHandshakeHandler() { | ||
return new DefaultHandshakeHandler() { | ||
@Override | ||
protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map<String, Object> attributes) { | ||
Principal principal = request.getPrincipal(); | ||
if (principal == null) { | ||
Collection<SimpleGrantedAuthority> authorities = new ArrayList<>(); | ||
authorities.add(new SimpleGrantedAuthority(AuthoritiesConstants.ANONYMOUS)); | ||
principal = new AnonymousAuthenticationToken("WebsocketConfiguration", "anonymous", authorities); | ||
} | ||
return principal; | ||
} | ||
}; | ||
} | ||
|
||
@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<HandlerMethodArgumentResolver> argumentResolvers) { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
@Override | ||
public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
@Override | ||
public boolean configureMessageConverters(List<MessageConverter> messageConverters) { | ||
// TODO Auto-generated method stub | ||
return false; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/org/assimbly/gateway/config/WebsocketSecurityConfiguration.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,28 @@ | ||
package org.assimbly.gateway.config; | ||
|
||
import org.assimbly.gateway.security.AuthoritiesConstants; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.messaging.simp.SimpMessageType; | ||
import org.springframework.security.config.annotation.web.messaging.MessageSecurityMetadataSourceRegistry; | ||
import org.springframework.security.config.annotation.web.socket.AbstractSecurityWebSocketMessageBrokerConfigurer; | ||
|
||
@Configuration | ||
public class WebsocketSecurityConfiguration extends AbstractSecurityWebSocketMessageBrokerConfigurer { | ||
|
||
@Override | ||
protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) { | ||
messages | ||
.nullDestMatcher().authenticated() | ||
.simpDestMatchers("/topic/tracker").hasAuthority(AuthoritiesConstants.ADMIN) | ||
// matches any destination that starts with /topic/ | ||
// (i.e. cannot send messages directly to /topic/) | ||
// (i.e. cannot subscribe to /topic/messages/* to get messages sent to | ||
// /topic/messages-user<id>) | ||
.simpDestMatchers("/topic/**").authenticated() | ||
// message types other than MESSAGE and SUBSCRIBE | ||
.simpTypeMatchers(SimpMessageType.MESSAGE, SimpMessageType.SUBSCRIBE).denyAll() | ||
// catch all | ||
.anyMessage().denyAll(); | ||
} | ||
} |
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
63 changes: 63 additions & 0 deletions
63
src/main/java/org/assimbly/gateway/event/FailureListener.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,63 @@ | ||
package org.assimbly.gateway.event; | ||
|
||
import java.util.EventObject; | ||
|
||
import org.apache.camel.management.event.ExchangeFailedEvent; | ||
import org.apache.camel.management.event.ExchangeFailureHandledEvent; | ||
import org.apache.camel.support.EventNotifierSupport; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.messaging.simp.SimpMessageSendingOperations; | ||
import org.springframework.stereotype.Component; | ||
|
||
// This class listens to failure events in camel exchanges (routes) and send them to the websocket topic: topic/alert | ||
// Check the following page for all EventObject instances of Camel: http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/management/event/package-summary.html | ||
|
||
@Component | ||
public class FailureListener extends EventNotifierSupport { | ||
|
||
private final Logger log = LoggerFactory.getLogger(FailureListener.class); | ||
|
||
@Autowired | ||
private SimpMessageSendingOperations messagingTemplate; | ||
|
||
public void notify(EventObject eventObject) throws Exception { | ||
|
||
if (eventObject instanceof ExchangeFailureHandledEvent) { | ||
|
||
ExchangeFailureHandledEvent exchangeFailedEvent = (ExchangeFailureHandledEvent) eventObject; | ||
String flowId = exchangeFailedEvent.getExchange().getFromRouteId(); | ||
|
||
if(this.messagingTemplate!=null) { | ||
this.messagingTemplate.convertAndSend("/topic/alert", flowId); | ||
}else { | ||
log.warn("Can't send alert to websocket. messagingTemplate=null"); | ||
} | ||
|
||
}if (eventObject instanceof ExchangeFailedEvent) { | ||
|
||
ExchangeFailedEvent exchangeFailedEvent = (ExchangeFailedEvent) eventObject; | ||
String flowId = exchangeFailedEvent.getExchange().getFromRouteId(); | ||
|
||
if(this.messagingTemplate!=null) { | ||
this.messagingTemplate.convertAndSend("/topic/alert", flowId); | ||
}else { | ||
log.warn("Can't send alert to websocket. messagingTemplate=null"); | ||
} | ||
} | ||
} | ||
|
||
public boolean isEnabled(EventObject event) { | ||
return true; | ||
} | ||
|
||
protected void doStart() throws Exception { | ||
// noop | ||
} | ||
|
||
protected void doStop() throws Exception { | ||
// noop | ||
} | ||
|
||
} |
Oops, something went wrong.