1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.activeinstalls.internal.client

File DefaultPingSender.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

2
22
3
1
112
64
4
0.18
7.33
3
1.33

Classes

Class Line # Actions
DefaultPingSender 49 22 0% 4 2
0.925925992.6%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Default implementation using the Jest API to connect to a remote Elastic Search instance.
43    *
44    * @version $Id: ef8745c841164a9c82ae34900386fe477316665c $
45    * @since 5.2M2
46    */
47    @Component
48    @Singleton
 
49    public class DefaultPingSender implements PingSender
50    {
51    @Inject
52    private JestClientManager jestClientManager;
53   
54    @Inject
55    private Provider<List<PingDataProvider>> pingDataProviderProvider;
56   
 
57  5 toggle @Override
58    public void sendPing() throws Exception
59    {
60  5 JestClient client = this.jestClientManager.getClient();
61   
62    // Step 1: Create index (if already exists then it'll just be ignored)
63  5 client.execute(new CreateIndex.Builder(JestClientManager.INDEX).build());
64   
65    // Step 2: Create a mapping so that we can search distribution versions containing hyphens (otherwise they
66    // are removed by the default tokenizer/analyzer). If mapping already exists then it'll just be ignored.
67  4 PutMapping putMapping =
68    new PutMapping.Builder(JestClientManager.INDEX, JestClientManager.TYPE, constructJSONMapping()).build();
69  4 client.execute(putMapping);
70   
71    // Step 3: Index the data
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   
 
83  4 toggle 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   
 
102  4 toggle 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    }