-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBIDTenant.java
More file actions
166 lines (137 loc) · 4.79 KB
/
Copy pathBIDTenant.java
File metadata and controls
166 lines (137 loc) · 4.79 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Copyright (c) 2018, 1Kosmos Inc. All rights reserved.
* Licensed under 1Kosmos Open Source Public License version 1.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of this license at
* https://github.com/1Kosmos/1Kosmos_License/blob/main/LICENSE.txt
*/
package com.bidsdk;
import com.bidsdk.model.BIDCommunityInfo;
import com.bidsdk.model.BIDKeyPair;
import com.bidsdk.model.BIDSD;
import com.bidsdk.model.BIDTenantInfo;
import com.bidsdk.utils.InMemCache;
import com.bidsdk.utils.WTM;
import com.google.gson.Gson;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
public class BIDTenant {
private static final BIDTenant instance = new BIDTenant();
public BIDTenant() {}
public static BIDTenant getInstance() {
return instance;
}
private BIDKeyPair keySet;
public BIDCommunityInfo getCommunityInfo(BIDTenantInfo tenantInfo) { BIDCommunityInfo communityInfo = null;
try {
Map<String, Object> body = new HashMap<>();
String cache_key = "communityCache_" + tenantInfo.dns;
if (tenantInfo.tenantId != null) {
body.put("tenantId", tenantInfo.tenantId);
cache_key = cache_key + "_" + tenantInfo.tenantId;
} else {
body.put("dns", tenantInfo.dns);
}
if (tenantInfo.communityId != null) {
body.put("communityId", tenantInfo.communityId);
cache_key = cache_key + "_" + tenantInfo.communityId;
} else {
body.put("communityName", tenantInfo.communityName);
cache_key = cache_key + "_" + tenantInfo.communityName;
}
String cache_str = InMemCache.getInstance().get(cache_key);
if (cache_str != null) {
communityInfo = new Gson().fromJson(cache_str, BIDCommunityInfo.class);
return communityInfo;
}
String url = "https://" + tenantInfo.dns + "/api/r1/system/community_info/fetch";
Boolean keepAlive = false;
Map<String, Object> response = WTM.execute(
"post",
url,
WTM.defaultHeaders(),
new Gson().toJson(body),
keepAlive
);
String responseStr = (String) response.get("response");
int statusCode = (Integer) response.get("status");
if (statusCode == HttpStatus.SC_OK) {
InMemCache.getInstance().set(cache_key, responseStr, 24*60*60*1000);
communityInfo = new Gson().fromJson(responseStr, BIDCommunityInfo.class);
} else {
throw new Exception("Unable to load communityInfo code" + statusCode + " with message: " + responseStr);
}
} catch (Exception e) {
e.printStackTrace();
}
return communityInfo;
}
public BIDSD getSD(BIDTenantInfo tenantInfo) {
BIDSD sd = null;
try {
String cache_key = "sdCache_" + tenantInfo.dns;
if (tenantInfo.tenantId != null) {
cache_key = cache_key + "_" + tenantInfo.tenantId;
}
if (tenantInfo.communityId != null) {
cache_key = cache_key + "_" + tenantInfo.communityId;
} else {
cache_key = (tenantInfo.communityName != null) ? cache_key + "_" + tenantInfo.communityName : cache_key;
}
String sdUrl = "https://" + tenantInfo.dns + "/caas/sd";
String cache_str = InMemCache.getInstance().get(cache_key);
if (cache_str != null) {
sd = new Gson().fromJson(cache_str, BIDSD.class);
return sd;
}
Boolean keepAlive = true;
Map<String, Object> response = WTM.execute(
"get",
sdUrl,
WTM.defaultHeaders(),
null,
keepAlive
);
String responseStr = (String) response.get("response");
int statusCode = (Integer) response.get("status");
if (statusCode == HttpStatus.SC_OK) {
sd = new Gson().fromJson(responseStr, BIDSD.class);
InMemCache.getInstance().set(cache_key, responseStr, 60*60*1000);
} else {
throw new Exception(
"Unable to load sd code" +
statusCode +
" with message: " +
responseStr
);
}
} catch (Exception e) {
e.printStackTrace();
}
return sd;
}
public BIDKeyPair getKeySet() {
try{
if (this.keySet == null) {
this.keySet = BIDECDSA.generateKeyPair();
}
} catch (Exception e) {
e.printStackTrace();
}
return keySet;
}
public void setKeySet(BIDKeyPair keySet) {
this.keySet = keySet;
}
}