1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.activeinstalls.internal.client; |
21 |
|
|
22 |
|
import java.util.Collections; |
23 |
|
import java.util.HashMap; |
24 |
|
import java.util.List; |
25 |
|
import java.util.Map; |
26 |
|
|
27 |
|
import javax.inject.Inject; |
28 |
|
import javax.inject.Provider; |
29 |
|
import javax.inject.Singleton; |
30 |
|
|
31 |
|
import org.xwiki.activeinstalls.internal.JestClientManager; |
32 |
|
import org.xwiki.component.annotation.Component; |
33 |
|
|
34 |
|
import io.searchbox.client.JestClient; |
35 |
|
import io.searchbox.client.JestResult; |
36 |
|
import io.searchbox.core.Index; |
37 |
|
import io.searchbox.indices.CreateIndex; |
38 |
|
import io.searchbox.indices.mapping.PutMapping; |
39 |
|
import net.sf.json.JSONObject; |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
@version |
45 |
|
@since |
46 |
|
|
47 |
|
@Component |
48 |
|
@Singleton |
|
|
| 92.6% |
Uncovered Elements: 2 (27) |
Complexity: 4 |
Complexity Density: 0.18 |
|
49 |
|
public class DefaultPingSender implements PingSender |
50 |
|
{ |
51 |
|
@Inject |
52 |
|
private JestClientManager jestClientManager; |
53 |
|
|
54 |
|
@Inject |
55 |
|
private Provider<List<PingDataProvider>> pingDataProviderProvider; |
56 |
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
57 |
5 |
@Override... |
58 |
|
public void sendPing() throws Exception |
59 |
|
{ |
60 |
5 |
JestClient client = this.jestClientManager.getClient(); |
61 |
|
|
62 |
|
|
63 |
5 |
client.execute(new CreateIndex.Builder(JestClientManager.INDEX).build()); |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
4 |
PutMapping putMapping = |
68 |
|
new PutMapping.Builder(JestClientManager.INDEX, JestClientManager.TYPE, constructJSONMapping()).build(); |
69 |
4 |
client.execute(putMapping); |
70 |
|
|
71 |
|
|
72 |
4 |
Index index = new Index.Builder(constructIndexJSON()) |
73 |
|
.index(JestClientManager.INDEX) |
74 |
|
.type(JestClientManager.TYPE) |
75 |
|
.build(); |
76 |
4 |
JestResult result = client.execute(index); |
77 |
|
|
78 |
3 |
if (!result.isSucceeded()) { |
79 |
0 |
throw new Exception(result.getErrorMessage()); |
80 |
|
} |
81 |
|
} |
82 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
|
83 |
4 |
private String constructJSONMapping()... |
84 |
|
{ |
85 |
4 |
Map<String, Object> jsonMap = new HashMap<>(); |
86 |
|
|
87 |
4 |
Map<String, Object> timestampMap = new HashMap<>(); |
88 |
4 |
timestampMap.put("enabled", true); |
89 |
4 |
timestampMap.put("store", true); |
90 |
|
|
91 |
4 |
Map<String, Object> propertiesMap = new HashMap<>(); |
92 |
4 |
for (PingDataProvider pingDataProvider : this.pingDataProviderProvider.get()) { |
93 |
9 |
propertiesMap.putAll(pingDataProvider.provideMapping()); |
94 |
|
} |
95 |
|
|
96 |
4 |
jsonMap.put("_timestamp", timestampMap); |
97 |
4 |
jsonMap.put("properties", propertiesMap); |
98 |
|
|
99 |
4 |
return JSONObject.fromObject(Collections.singletonMap(JestClientManager.TYPE, jsonMap)).toString(); |
100 |
|
} |
101 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
102 |
4 |
private String constructIndexJSON()... |
103 |
|
{ |
104 |
4 |
Map<String, Object> jsonMap = new HashMap<>(); |
105 |
|
|
106 |
4 |
for (PingDataProvider pingDataProvider : this.pingDataProviderProvider.get()) { |
107 |
9 |
jsonMap.putAll(pingDataProvider.provideData()); |
108 |
|
} |
109 |
|
|
110 |
4 |
return JSONObject.fromObject(jsonMap).toString(); |
111 |
|
} |
112 |
|
} |