Browse code

Merge origin/master

devnewton authored on 17/10/2017 07:30:59
Showing 7 changed files
... ...
@@ -106,21 +106,11 @@
106 106
             <version>3.3.2</version>
107 107
         </dependency>
108 108
         <dependency>
109
-            <groupId>org.apache.commons</groupId>
110
-            <artifactId>commons-csv</artifactId>
111
-            <version>1.4</version>
112
-        </dependency>
113
-        <dependency>
114 109
             <groupId>commons-io</groupId>
115 110
             <artifactId>commons-io</artifactId>
116 111
             <version>2.5</version>
117 112
         </dependency>
118 113
         <dependency>
119
-            <groupId>org.apache.commons</groupId>
120
-            <artifactId>commons-math3</artifactId>
121
-            <version>3.5</version>
122
-        </dependency>
123
-        <dependency>
124 114
             <groupId>org.unix4j</groupId>
125 115
             <artifactId>unix4j-command</artifactId>
126 116
             <version>0.3</version>
... ...
@@ -4,8 +4,6 @@ import java.util.Collections;
4 4
 import java.util.List;
5 5
 import org.apache.commons.lang3.StringUtils;
6 6
 import org.joda.time.DateTime;
7
-import org.joda.time.Period;
8
-import org.joda.time.format.ISOPeriodFormat;
9 7
 import org.springframework.beans.factory.annotation.Autowired;
10 8
 import org.springframework.beans.factory.annotation.Value;
11 9
 import org.springframework.data.domain.PageRequest;
... ...
@@ -104,7 +104,7 @@ public abstract class AbstractWebdirectcoinGateway extends WebSocketListener imp
104 104
                     message.setPost(post);
105 105
                     ws.send(objectMapper.writeValueAsString(message));
106 106
                 }
107
-            } catch (JsonProcessingException ex) {
107
+            } catch (Exception ex) {
108 108
                 LOGGER.error(ex);
109 109
             }
110 110
             return true;
... ...
@@ -32,6 +32,7 @@ import org.joda.time.DateTime;
32 32
 
33 33
 import org.springframework.beans.factory.annotation.Autowired;
34 34
 import org.springframework.beans.factory.annotation.Value;
35
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
35 36
 import org.springframework.context.ApplicationEventPublisher;
36 37
 import org.springframework.scheduling.TaskScheduler;
37 38
 import org.springframework.stereotype.Component;
... ...
@@ -42,6 +43,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
42 43
  *
43 44
  * @author devnewton <devnewton@bci.im>
44 45
  */
46
+@ConditionalOnProperty(name = "jb3.anatid.url")
45 47
 @Component
46 48
 public class AnatidGateway extends WebSocketListener implements Gateway {
47 49
 
... ...
@@ -61,20 +63,20 @@ public class AnatidGateway extends WebSocketListener implements Gateway {
61 63
 
62 64
 	@Resource(name = "mouleScheduler")
63 65
 	private TaskScheduler scheduler;
64
-	
65
-    @Autowired
66
-    private ApplicationEventPublisher publisher;
67 66
 
68
-	private HttpUrl plopToUrl, plopFromUrl;
67
+	@Autowired
68
+	private ApplicationEventPublisher publisher;
69
+
70
+	private HttpUrl postUrl, pollUrl;
69 71
 	private List<String> rooms;
70 72
 	private final BouchotPostCallBack bouchotPostCallback = new BouchotPostCallBack();
71 73
 	private int nbConnexionFailOrClose;
72 74
 
73
-	@Value("${jb3.anatid.url:}")
75
+	@Value("${jb3.anatid.url}")
74 76
 	public void setPlopToUrl(String anatidUrl) {
75 77
 		if (StringUtils.isNotBlank(anatidUrl)) {
76
-			this.plopToUrl = HttpUrl.parse(anatidUrl).newBuilder().addPathSegments("plop/to").build();
77
-			this.plopFromUrl = HttpUrl.parse(anatidUrl).newBuilder().addPathSegments("plop/from").build();
78
+			this.postUrl = HttpUrl.parse(anatidUrl).newBuilder().addPathSegments("post").build();
79
+			this.pollUrl = HttpUrl.parse(anatidUrl).newBuilder().addPathSegments("poll").build();
78 80
 		}
79 81
 	}
80 82
 
... ...
@@ -86,7 +88,7 @@ public class AnatidGateway extends WebSocketListener implements Gateway {
86 88
 	@PostConstruct
87 89
 	public void connect() {
88 90
 		scheduler.schedule(() -> {
89
-			Request request = new Request.Builder().url(plopFromUrl).build();
91
+			Request request = new Request.Builder().url(pollUrl).build();
90 92
 			httpClient.newWebSocket(request, this);
91 93
 		}, DateTime.now().plusMinutes(nbConnexionFailOrClose).toDate());
92 94
 	}
... ...
@@ -101,7 +103,7 @@ public class AnatidGateway extends WebSocketListener implements Gateway {
101 103
 	public void onMessage(WebSocket webSocket, String text) {
102 104
 		try {
103 105
 			LegacyPost legacyPost = objectMapper.readValue(text, LegacyPost.class);
104
-			if(rooms.contains(legacyPost.getTribune())) {
106
+			if (rooms.contains(legacyPost.getTribune())) {
105 107
 				importPost(legacyPost);
106 108
 			}
107 109
 		} catch (IOException e) {
... ...
@@ -133,8 +135,8 @@ public class AnatidGateway extends WebSocketListener implements Gateway {
133 135
 				Post post = new Post();
134 136
 				post.setGatewayPostId(gatewayPostId);
135 137
 				post.setRoom(legacyPost.getTribune());
136
-				DateTime postTimeRounded = LegacyUtils.legacyPostTimeFormatter.parseDateTime(legacyPost.getTime()).secondOfMinute()
137
-						.roundFloorCopy();
138
+				DateTime postTimeRounded = LegacyUtils.legacyPostTimeFormatter.parseDateTime(legacyPost.getTime())
139
+						.secondOfMinute().roundFloorCopy();
138 140
 				long nbPostsAtSameSecond = postPepository.countPosts(postTimeRounded, postTimeRounded.plusSeconds(1),
139 141
 						legacyPost.getTribune());
140 142
 				post.setTime(postTimeRounded.withMillisOfSecond((int) nbPostsAtSameSecond));
... ...
@@ -144,7 +146,8 @@ public class AnatidGateway extends WebSocketListener implements Gateway {
144 146
 				}
145 147
 				post.setNickname(CleanUtils.cleanNickname(nickname));
146 148
 				post.setMessage(legacyUtils.convertFromLegacyNorloges(
147
-						CleanUtils.cleanMessage(CleanUtils.truncateMessage(legacyPost.getMessage())), post.getTime(), legacyPost.getTribune()));
149
+						CleanUtils.cleanMessage(CleanUtils.truncateMessage(legacyPost.getMessage())), post.getTime(),
150
+						legacyPost.getTribune()));
148 151
 				postPepository.save(post);
149 152
 				publisher.publishEvent(new NewPostsEvent(post));
150 153
 			}
... ...
@@ -156,15 +159,21 @@ public class AnatidGateway extends WebSocketListener implements Gateway {
156 159
 	@Override
157 160
 	public boolean handlePost(String nickname, String message, String room, String auth) {
158 161
 		if (rooms.contains(room)) {
159
-			if (null != plopToUrl) {
160
-				okhttp3.FormBody.Builder body = new FormBody.Builder()
161
-						.add("message",
162
-								legacyUtils.convertToLegacyNorloges(message, DateTime.now()
163
-										.withZone(LegacyUtils.legacyTimeZone).secondOfMinute().roundFloorCopy(), room))
164
-						.add("tribune", room);
165
-				Builder request = new Request.Builder().url(plopToUrl).header("User-Agent", nickname)
166
-						.post(body.build());
167
-				httpClient.newCall(request.build()).enqueue(bouchotPostCallback);
162
+			try {
163
+				if (null != postUrl) {
164
+					okhttp3.FormBody.Builder body = new FormBody.Builder().add("message",
165
+							legacyUtils.convertToLegacyNorloges(message, DateTime.now()
166
+									.withZone(LegacyUtils.legacyTimeZone).secondOfMinute().roundFloorCopy(), room))
167
+							.add("tribune", room);
168
+					if (StringUtils.isNotBlank(auth)) {
169
+						body.add("auth", auth);
170
+					}
171
+					Builder request = new Request.Builder().url(postUrl).header("User-Agent", nickname)
172
+							.post(body.build());
173
+					httpClient.newCall(request.build()).enqueue(bouchotPostCallback);
174
+				}
175
+			} catch (Exception e) {
176
+				LOGGER.error("post error", e);
168 177
 			}
169 178
 			return true;
170 179
 		} else {
... ...
@@ -1,6 +1,5 @@
1 1
 package im.bci.jb3.bouchot.gateway;
2 2
 
3
-import org.apache.commons.lang3.StringUtils;
4 3
 import org.springframework.beans.factory.annotation.Autowired;
5 4
 import org.springframework.stereotype.Component;
6 5
 
... ...
@@ -3,6 +3,7 @@ package im.bci.jb3.bouchot.websocket;
3 3
 import java.io.IOException;
4 4
 import java.util.List;
5 5
 
6
+import org.apache.commons.logging.Log;
6 7
 import org.apache.commons.logging.LogFactory;
7 8
 import org.joda.time.DateTime;
8 9
 import org.joda.time.DateTimeZone;
... ...
@@ -32,86 +33,92 @@ import org.springframework.context.event.EventListener;
32 33
 
33 34
 @Component
34 35
 public class WebDirectCoinHandler extends TextWebSocketHandler {
35
-    
36
-    @Autowired
37
-    private ObjectMapper objectMapper;
38
-    
39
-    @Autowired
40
-    private UserPostHandler tribune;
41
-    
42
-    @Autowired
43
-    private PostRepository postRepository;
44
-    
45
-    private Period postsGetPeriod;
46
-    
47
-    @Autowired
48
-    private WebDirectCoinConnectedMoules moules;
49
-    
50
-    @Override
51
-    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
52
-        moules.add(session);
53
-    }
54
-    
55
-    @Override
56
-    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
57
-        moules.remove(session);
58
-    }
59
-    
60
-    @EventListener
61
-    public void notify(NewPostsEvent event) {
62
-    	if(!event.getPosts().isEmpty()) {
63
-    		LogFactory.getLog(this.getClass()).info("post request notify: " + event.getPosts().get(0).getMessage());
64
-    	}
65
-        moules.dispatch(event);
66
-    }
67
-    
68
-    @Override
69
-    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
70
-        MessageC2S webDirectCoinMessage = objectMapper.readValue(message.getPayload(), MessageC2S.class);
71
-        if (null != webDirectCoinMessage.getGet()) {
72
-            get(session, webDirectCoinMessage.getGet());
73
-        }
74
-        if (null != webDirectCoinMessage.getPost()) {
75
-            post(session, webDirectCoinMessage.getPost());
76
-        }
77
-        if (null != webDirectCoinMessage.getPresence()) {
78
-            presence(session, webDirectCoinMessage.getPresence());
79
-        }
80
-        if (null != webDirectCoinMessage.getGetNorloge()) {
81
-            getNorloge(session, webDirectCoinMessage.getGetNorloge());
82
-        }
83
-    }
84
-    
85
-    private void getNorloge(WebSocketSession moule, GetNorlogeC2S getNorloge) throws IOException {
86
-        Post post = postRepository.findOne(getNorloge.getMessageId());
87
-        if (null != post) {
88
-            moules.sendNorloge(moule, post);
89
-        }
90
-    }
91
-    
92
-    private void presence(WebSocketSession moule, Presence presence) throws IOException {
93
-        moules.ackMoulePresence(moule, presence);
94
-    }
95
-    
96
-    private void get(WebSocketSession moule, GetC2S rq) throws IOException {
97
-        DateTime end = DateTime.now(DateTimeZone.UTC).plusHours(1);
98
-        DateTime start = end.minus(postsGetPeriod);
99
-        List<Post> posts = postRepository.findPosts(start, end, rq.getRoom());
100
-        moules.sendPostsToMoule(moule, posts);
101
-        return;
102
-    }
103
-    
104
-    private void post(WebSocketSession moule, PostC2S rq) {
105
-    	LogFactory.getLog(this.getClass()).info("receive post request: " + rq.getMessage());
106
-        UriComponentsBuilder uriBuilder = (UriComponentsBuilder) moule.getAttributes()
107
-                .get(WebDirectCoinSessionAttributes.URI_BUILDER);
108
-        tribune.post(rq.getNickname(), rq.getMessage(), rq.getRoom(), rq.getAuth(), uriBuilder);
109
-        LogFactory.getLog(this.getClass()).info("post request stored: " + rq.getMessage());
110
-    }
111
-    
112
-    @Value("${jb3.posts.get.period}")
113
-    public void setPostsGetPeriod(String p) {
114
-        postsGetPeriod = ISOPeriodFormat.standard().parsePeriod(p);
115
-    }
116
-    
36
+
37
+	private static final Log LOGGER = LogFactory.getLog(WebDirectCoinHandler.class);
38
+
39
+	@Autowired
40
+	private ObjectMapper objectMapper;
41
+
42
+	@Autowired
43
+	private UserPostHandler tribune;
44
+
45
+	@Autowired
46
+	private PostRepository postRepository;
47
+
48
+	private Period postsGetPeriod;
49
+
50
+	@Autowired
51
+	private WebDirectCoinConnectedMoules moules;
52
+
53
+	@Override
54
+	public void afterConnectionEstablished(WebSocketSession session) throws Exception {
55
+		moules.add(session);
56
+	}
57
+
58
+	@Override
59
+	public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
60
+		moules.remove(session);
61
+	}
62
+
63
+	@EventListener
64
+	public void notify(NewPostsEvent event) {
65
+		if (!event.getPosts().isEmpty()) {
66
+			LOGGER.info("post request notify: " + event.getPosts().get(0).getMessage());
67
+		}
68
+		moules.dispatch(event);
69
+	}
70
+
71
+	@Override
72
+	protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
73
+		try {
74
+			MessageC2S webDirectCoinMessage = objectMapper.readValue(message.getPayload(), MessageC2S.class);
75
+			if (null != webDirectCoinMessage.getGet()) {
76
+				get(session, webDirectCoinMessage.getGet());
77
+			}
78
+			if (null != webDirectCoinMessage.getPost()) {
79
+				post(session, webDirectCoinMessage.getPost());
80
+			}
81
+			if (null != webDirectCoinMessage.getPresence()) {
82
+				presence(session, webDirectCoinMessage.getPresence());
83
+			}
84
+			if (null != webDirectCoinMessage.getGetNorloge()) {
85
+				getNorloge(session, webDirectCoinMessage.getGetNorloge());
86
+			}
87
+		} catch (Exception e) {
88
+			LOGGER.error("handleTextMessage error", e);
89
+		}
90
+	}
91
+
92
+	private void getNorloge(WebSocketSession moule, GetNorlogeC2S getNorloge) throws IOException {
93
+		Post post = postRepository.findOne(getNorloge.getMessageId());
94
+		if (null != post) {
95
+			moules.sendNorloge(moule, post);
96
+		}
97
+	}
98
+
99
+	private void presence(WebSocketSession moule, Presence presence) throws IOException {
100
+		moules.ackMoulePresence(moule, presence);
101
+	}
102
+
103
+	private void get(WebSocketSession moule, GetC2S rq) throws IOException {
104
+		DateTime end = DateTime.now(DateTimeZone.UTC).plusHours(1);
105
+		DateTime start = end.minus(postsGetPeriod);
106
+		List<Post> posts = postRepository.findPosts(start, end, rq.getRoom());
107
+		moules.sendPostsToMoule(moule, posts);
108
+		return;
109
+	}
110
+
111
+	private void post(WebSocketSession moule, PostC2S rq) {
112
+		LOGGER.info("receive post request: " + rq.getMessage());
113
+		UriComponentsBuilder uriBuilder = (UriComponentsBuilder) moule.getAttributes()
114
+				.get(WebDirectCoinSessionAttributes.URI_BUILDER);
115
+		tribune.post(rq.getNickname(), rq.getMessage(), rq.getRoom(), rq.getAuth(), uriBuilder);
116
+		LOGGER.info("post request stored: " + rq.getMessage());
117
+	}
118
+
119
+	@Value("${jb3.posts.get.period}")
120
+	public void setPostsGetPeriod(String p) {
121
+		postsGetPeriod = ISOPeriodFormat.standard().parsePeriod(p);
122
+	}
123
+
117 124
 }
... ...
@@ -1,11 +1,17 @@
1
-jb3.defaults.room=devnewton
2
-jb3.defaults.rooms=cppfrug,devnewton,dlfp,euromussels,ototu,gabuzomeu,moules,sveetch,batavie
3
-jb3.anatid.rooms=dlfp,euromussels,ototu,moules,sveetch,batavie
4
-jb3.anatid.url=http://localhost:1880/anatid
1
+jb3.defaults.room=changeme
2
+jb3.defaults.rooms=changeme,devnewton,gabuzomeu
3
+
4
+#jb3.anatid.url=http://localhost:1880/anatid
5
+#jb3.anatid.rooms=dlfp,euromussels,ototu,moules,sveetch,batavie
6
+
5 7
 jb3.posts.get.period=P1M
6 8
 jb3.room.history.size=200
7 9
 jb3.wro=false
8 10
 
11
+#jb3.paste.dir = /srv/paste
12
+#jb3.paste.maxFiles = 128
13
+#jb3.totoz.dir = /srv/totoz
14
+
9 15
 handlebars.suffix=.html
10 16
 handlebars.failOnMissingFile=false
11 17
 handlebars.prettyPrint=true
... ...
@@ -25,9 +31,4 @@ logging.level.root=WARN
25 31
 logging.level.org.springframework.web=WARN
26 32
 
27 33
 spring.http.multipart.max-file-size=16Mb
28
-spring.http.multipart.max-request-size=17Mb
29
-
30
-#jb3.paste.dir = /srv/paste
31
-#jb3.paste.maxFiles = 128
32
-
33
-#jb3.totoz.dir = /srv/totoz
34
+spring.http.multipart.max-request-size=17Mb
34 35
\ No newline at end of file