1 package com.kodexa.client.store;
2
3 import com.fasterxml.jackson.core.type.TypeReference;
4 import com.kodexa.client.KodexaException;
5 import com.kodexa.client.remote.AbstractKodexaConnection;
6 import com.kodexa.client.remote.KodexaPlatform;
7 import com.kodexa.client.remote.Page;
8 import lombok.SneakyThrows;
9 import lombok.extern.slf4j.Slf4j;
10 import org.apache.commons.io.IOUtils;
11 import org.apache.http.HttpResponse;
12 import org.apache.http.client.methods.HttpGet;
13 import org.apache.http.client.utils.URIBuilder;
14 import org.apache.http.impl.client.CloseableHttpClient;
15 import org.apache.http.impl.client.HttpClientBuilder;
16
17 import java.io.IOException;
18 import java.nio.charset.StandardCharsets;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.stream.Collectors;
22
23
24
25
26 @Slf4j
27 public class RemoteTableDataStore extends AbstractKodexaConnection {
28
29 private final String ref;
30
31 public RemoteTableDataStore(String ref) {
32 this.ref = ref;
33 }
34
35
36
37
38
39
40
41
42
43 @SneakyThrows
44 public List<Map<String, String>> getTable(String tableName, int page, int pageSize) {
45 try (CloseableHttpClient client = HttpClientBuilder.create()
46 .setDefaultRequestConfig(getRequestConfig())
47 .build()) {
48 String url = KodexaPlatform.getUrl() + "/api/stores/" + ref.replace(":", "/") + "/rows";
49 log.info("Connecting to [" + url + "]");
50
51 URIBuilder builder = new URIBuilder(url);
52 builder.setParameter("page", String.valueOf(page)).setParameter("pageSize", String.valueOf(pageSize)).setParameter("table", tableName);
53
54 HttpGet httpGet = new HttpGet(builder.build());
55 httpGet.addHeader("x-access-token", KodexaPlatform.getAccessToken());
56
57 HttpResponse response = client.execute(httpGet);
58
59 if (response.getStatusLine().getStatusCode() != 200) {
60 throw new KodexaException("Unable to create a session, check your access token and URL [" + response.getStatusLine().getStatusCode() + "]");
61 }
62
63 String responseJson = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8);
64 Page<StoredRow> rowPage = jsonOm.readValue(responseJson, new TypeReference<Page<StoredRow>>() {
65 });
66
67 return rowPage.getContent().stream().map(StoredRow::getData).collect(Collectors.toList());
68
69
70 } catch (IOException e) {
71 throw new KodexaException("Unable to create a session on Kodexa", e);
72 }
73 }
74
75 public List<Map<String, String>> getTable(String tableName) {
76
77
78 return getTable(tableName, 1, 9999);
79
80 }
81 }