Skip to content

Commit

Permalink
Merge pull request #167 from assimbly/develop
Browse files Browse the repository at this point in the history
- REST get Event and Alerts
  • Loading branch information
assimbly committed Oct 30, 2018
2 parents 38b426e + e6b7d3a commit 555c959
Show file tree
Hide file tree
Showing 34 changed files with 626 additions and 95 deletions.
8 changes: 7 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ dependencyManagement {
defaultTasks 'bootRun'

group = 'org.assimbly.gateway'
version = '0.9.0-SNAPSHOT'
version = '1.0.0'

description = ''

Expand Down Expand Up @@ -206,6 +206,12 @@ dependencies {
compile "org.assimbly:connector:1.0.0"
compile "com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.9.5"
compile "org.jdom:jdom2:2.0.6"
compile "org.aspectj:aspectjweaver:1.9.1"
compile "org.aspectj:aspectjrt:1.9.1"
compile "org.springframework:spring-websocket:4.3.20.RELEASE"
compile "org.springframework.security:spring-security-messaging:4.2.3.RELEASE"


// end custom dependencies

testCompile "com.jayway.jsonpath:json-path"
Expand Down
22 changes: 12 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
{
"name": "gateway",
"version": "0.9.0",
"version": "1.0.0",
"description": "A message gateway based on Apache camel",
"private": true,
"license": "Apache License 2.0",
"cacheDirectories": [
"node_modules"
],
"dependencies": {
"@angular/common": "5.1.3",
"@angular/compiler": "5.1.3",
"@angular/core": "5.1.3",
"@angular/forms": "5.1.3",
"@angular/http": "5.1.3",
"@angular/platform-browser": "5.1.3",
"@angular/platform-browser-dynamic": "5.1.3",
"@angular/router": "5.1.3",
"@angular/common": "5.2.11",
"@angular/compiler": "5.2.11",
"@angular/core": "5.2.11",
"@angular/forms": "5.2.11",
"@angular/http": "5.2.11",
"@angular/platform-browser": "5.2.11",
"@angular/platform-browser-dynamic": "5.2.11",
"@angular/router": "5.2.11",
"@ng-bootstrap/ng-bootstrap": "1.0.4",
"@ng-select/ng-select": "^1.4.2",
"bootstrap": "^4.1.1",
Expand All @@ -33,14 +33,16 @@
"reflect-metadata": "0.1.10",
"rxjs": "5.5.12",
"swagger-ui": "2.2.10",
"sockjs-client": "1.1.4",
"webstomp-client": "1.2.5",
"tether": "1.4.0",
"xml-js": "^1.6.7",
"yaml-js": "^0.2.3",
"zone.js": "0.8.16"
},
"devDependencies": {
"@angular/cli": "1.6.8",
"@angular/compiler-cli": "5.1.3",
"@angular/compiler-cli": "5.2.11",
"@ngtools/webpack": "1.8.5",
"@types/jasmine": "2.5.53",
"@types/node": "8.0.18",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ public class LoggingAspectConfiguration {
public LoggingAspect loggingAspect(Environment env) {
return new LoggingAspect(env);
}

}
131 changes: 131 additions & 0 deletions src/main/java/org/assimbly/gateway/config/WebsocketConfiguration.java
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;
}
}
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,6 @@ private void getEndpointPropertiesFromDB(Flow flow) throws Exception{
}else if(properties.get("to.uri").contains("wastebin")){

String uri = properties.get("to.uri");
System.out.println("komt hier: " + uri);
uri = uri.replace("wastebin:", "mock:wastebin");
properties.put("to.uri",uri);
}
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/org/assimbly/gateway/event/FailureListener.java
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
}

}
Loading

0 comments on commit 555c959

Please sign in to comment.