/
PropertyMapper.java
81 lines (70 loc) · 2.84 KB
/
PropertyMapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.redhat.refarch.vertx.lambdaair.edge.mapping;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonObject;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
public class PropertyMapper extends AbstractVerticle {
private static final String HOST_KEY_PREFIX = "edge.proxy.";
private static final String HOST_KEY_SUFFIX = ".address";
private static Logger logger = Logger.getLogger(PropertyMapper.class.getName());
@Override
public void start(Future<Void> startFuture) {
vertx.eventBus().consumer(PropertyMapper.class.getName(), PropertyMapper::mapAddress);
startFuture.complete();
}
private static void mapAddress(Message<JsonObject> message) {
JsonObject jsonObject = message.body();
String path = jsonObject.getString("path");
String serviceName = getServiceName(path);
String hostUrl = getHost(serviceName);
if (hostUrl == null) {
logger.fine(serviceName + " not mapped through properties");
} else {
try {
URL url = new URL(hostUrl);
jsonObject.put("scheme", url.getProtocol());
jsonObject.put("host", url.getHost() + ":" + url.getPort());
logger.fine("Mapped to host " + jsonObject.getString("host"));
jsonObject.put("path", getProxyPath(path));
} catch (MalformedURLException e) {
logger.log(Level.SEVERE, "Could not parse mapped URL " + hostUrl, e);
}
}
message.reply(jsonObject);
}
private static String getHost(String context) {
return System.getProperty(HOST_KEY_PREFIX + context + HOST_KEY_SUFFIX);
}
private static String getServiceName(String fullPath) {
logger.fine("Path " + fullPath);
if (fullPath == null || fullPath.length() == 0) {
return null;
} else {
int separatorIndex = fullPath.indexOf("/", 1);
logger.fine("Service name separator found at position " + separatorIndex);
if (separatorIndex > 0) {
String serviceName = fullPath.substring(1, separatorIndex);
logger.fine("Service name determined as " + serviceName);
return serviceName;
} else {
return null;
}
}
}
private static String getProxyPath(String fullPath) {
if (fullPath == null || fullPath.length() == 0) {
return null;
} else {
int separatorIndex = fullPath.indexOf("/", 1);
if (separatorIndex > 0) {
return fullPath.substring(separatorIndex);
} else {
return null;
}
}
}
}