Coverage Summary for Class: BootstrapIndexRetriever (co.rsk.db.importer.provider.index)
Class |
Class, %
|
Method, %
|
Line, %
|
BootstrapIndexRetriever |
0%
(0/1)
|
0%
(0/3)
|
0%
(0/14)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2019 RSK Labs Ltd.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package co.rsk.db.importer.provider.index;
20
21 import co.rsk.db.importer.BootstrapImportException;
22 import co.rsk.db.importer.BootstrapURLProvider;
23 import co.rsk.db.importer.provider.index.data.BootstrapDataIndex;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25
26 import java.io.IOException;
27 import java.net.URL;
28 import java.util.ArrayList;
29 import java.util.List;
30
31 public class BootstrapIndexRetriever {
32
33 private static final String INDEX_NAME = "index.json";
34
35 private final List<String> publicKeys;
36 private final BootstrapURLProvider bootstrapUrlProvider;
37 private final ObjectMapper objectMapper;
38
39 public BootstrapIndexRetriever(
40 List<String> publicKeys,
41 BootstrapURLProvider bootstrapUrlProvider, ObjectMapper objectMapper) {
42 this.publicKeys = new ArrayList<>(publicKeys);
43 this.bootstrapUrlProvider = bootstrapUrlProvider;
44 this.objectMapper = objectMapper;
45 }
46
47 public List<BootstrapDataIndex> retrieve() {
48 List<BootstrapDataIndex> indices = new ArrayList<>();
49
50 for (String pk : publicKeys) {
51 String indexSuffix = pk +"/" + INDEX_NAME;
52 URL indexURL = bootstrapUrlProvider.getFullURL(indexSuffix);
53
54 indices.add(readJson(indexURL));
55 }
56 return indices;
57 }
58
59 private BootstrapDataIndex readJson(URL indexURL) {
60 try {
61 return objectMapper.readValue(indexURL, BootstrapDataIndex.class);
62 } catch (IOException e) {
63 throw new BootstrapImportException(String.format("Failed to download and parse index from %s", indexURL), e);
64 }
65 }
66
67 }