Browse code

BM-13940 Chore: don't reload our local ip on every api loading

Thomas Cataldo authored on 10/01/2019 07:51:22
Showing 2 changed files
... ...
@@ -18,8 +18,6 @@
18 18
  */
19 19
 package net.bluemind.core.rest.base;
20 20
 
21
-import java.net.Inet4Address;
22
-import java.net.UnknownHostException;
23 21
 import java.util.LinkedList;
24 22
 import java.util.List;
25 23
 
... ...
@@ -29,6 +27,7 @@ import org.vertx.java.core.http.HttpHeaders;
29 27
 
30 28
 import net.bluemind.core.api.BMVersion;
31 29
 import net.bluemind.core.context.SecurityContext;
30
+import net.bluemind.core.rest.internal.LocalIP;
32 31
 
33 32
 public class BasicClientProxy<S, T> {
34 33
 
... ...
@@ -44,12 +43,7 @@ public class BasicClientProxy<S, T> {
44 43
 	public BasicClientProxy(IRestCallHandler callHander, Class<S> api, Class<T> asyncApi) {
45 44
 		this.callHandler = callHander;
46 45
 		this.cpg = ClientProxyGenerator.generator(api, asyncApi);
47
-		try {
48
-			String localIp = Inet4Address.getLocalHost().getHostAddress();
49
-			remoteAddresses.add(localIp);
50
-		} catch (UnknownHostException e) {
51
-		}
52
-
46
+		LocalIP.VALUE.ifPresent(remoteAddresses::add);
53 47
 	}
54 48
 
55 49
 	public BasicClientProxy<S, T> setRemoteIps(List<String> ips) {
56 50
new file mode 100644
... ...
@@ -0,0 +1,35 @@
1
+/* BEGIN LICENSE
2
+  * Copyright © Blue Mind SAS, 2012-2019
3
+  *
4
+  * This file is part of BlueMind. BlueMind is a messaging and collaborative
5
+  * solution.
6
+  *
7
+  * This program is free software; you can redistribute it and/or modify
8
+  * it under the terms of either the GNU Affero General Public License as
9
+  * published by the Free Software Foundation (version 3 of the License).
10
+  *
11
+  * This program is distributed in the hope that it will be useful,
12
+  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
+  *
15
+  * See LICENSE.txt
16
+  * END LICENSE
17
+  */
18
+package net.bluemind.core.rest.internal;
19
+
20
+import java.net.Inet4Address;
21
+import java.net.UnknownHostException;
22
+import java.util.Optional;
23
+
24
+public class LocalIP {
25
+
26
+	public static final Optional<String> VALUE = Optional.ofNullable(loadValue());
27
+
28
+	private static final String loadValue() {
29
+		try {
30
+			return Inet4Address.getLocalHost().getHostAddress();
31
+		} catch (UnknownHostException e) {
32
+			return null;
33
+		}
34
+	}
35
+}