1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.tooling.commons.packmgr.httpaction;
21
22 import java.io.IOException;
23 import java.util.List;
24 import java.util.regex.Pattern;
25
26 import org.apache.http.HttpStatus;
27 import org.apache.http.client.methods.CloseableHttpResponse;
28 import org.apache.http.client.methods.HttpGet;
29 import org.apache.http.client.protocol.HttpClientContext;
30 import org.apache.http.impl.client.CloseableHttpClient;
31 import org.apache.http.util.EntityUtils;
32 import org.json.JSONException;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import io.wcm.tooling.commons.packmgr.PackageManagerHttpActionException;
37
38
39
40
41 public final class BundleStatusCall implements HttpCall<BundleStatus> {
42
43 private final CloseableHttpClient httpClient;
44 private final HttpClientContext context;
45 private final String bundleStatusURL;
46 private final List<Pattern> bundleStatusWhitelistBundleNames;
47
48 private static final Logger log = LoggerFactory.getLogger(BundleStatusCall.class);
49
50
51
52
53
54
55
56 public BundleStatusCall(CloseableHttpClient httpClient, HttpClientContext context, String bundleStatusURL,
57 List<Pattern> bundleStatusWhitelistBundleNames) {
58 this.httpClient = httpClient;
59 this.context = context;
60 this.bundleStatusURL = bundleStatusURL;
61 this.bundleStatusWhitelistBundleNames = bundleStatusWhitelistBundleNames;
62 }
63
64 @Override
65 public BundleStatus execute() {
66 log.debug("Call URL: {}", bundleStatusURL);
67
68 HttpGet method = new HttpGet(bundleStatusURL);
69 try (CloseableHttpResponse response = httpClient.execute(method, context)) {
70
71 String responseString = EntityUtils.toString(response.getEntity());
72 if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
73 throw PackageManagerHttpActionException.forHttpError(bundleStatusURL, response.getStatusLine(), responseString);
74 }
75
76 return toBundleStatus(responseString);
77 }
78 catch (IOException ex) {
79 throw PackageManagerHttpActionException.forIOException(bundleStatusURL, ex);
80 }
81 }
82
83 private BundleStatus toBundleStatus(String jsonString) {
84 try {
85 BundleStatusParser parser = new BundleStatusParser(bundleStatusWhitelistBundleNames);
86 return parser.parse(jsonString);
87 }
88 catch (JSONException ex) {
89 throw PackageManagerHttpActionException.forJSONException(bundleStatusURL, jsonString, ex);
90 }
91 }
92
93 }