View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2022 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.tooling.commons.packmgr.httpaction;
21  
22  import java.io.IOException;
23  
24  import org.apache.http.HttpStatus;
25  import org.apache.http.client.methods.CloseableHttpResponse;
26  import org.apache.http.client.methods.HttpGet;
27  import org.apache.http.client.protocol.HttpClientContext;
28  import org.apache.http.impl.client.CloseableHttpClient;
29  import org.apache.http.util.EntityUtils;
30  import org.json.JSONException;
31  import org.json.JSONObject;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  import io.wcm.tooling.commons.packmgr.PackageManagerHttpActionException;
36  
37  /**
38   * Get install status from package manager.
39   */
40  public final class PackageManagerInstallStatusCall implements HttpCall<PackageManagerInstallStatus> {
41  
42    private final CloseableHttpClient httpClient;
43    private final HttpClientContext context;
44    private final String packageManagerInstallStatusURL;
45  
46    private static final Logger log = LoggerFactory.getLogger(PackageManagerInstallStatusCall.class);
47  
48    /**
49     * @param httpClient HTTP client
50     * @param context HTTP client context
51     * @param packageManagerInstallStatusURL Bundle status URL
52     */
53    public PackageManagerInstallStatusCall(CloseableHttpClient httpClient, HttpClientContext context,
54        String packageManagerInstallStatusURL) {
55      this.httpClient = httpClient;
56      this.context = context;
57      this.packageManagerInstallStatusURL = packageManagerInstallStatusURL;
58    }
59  
60    @Override
61    public PackageManagerInstallStatus execute() {
62      log.debug("Call URL: {}", packageManagerInstallStatusURL);
63  
64      HttpGet method = new HttpGet(packageManagerInstallStatusURL);
65      try (CloseableHttpResponse response = httpClient.execute(method, context)) {
66  
67        String responseString = EntityUtils.toString(response.getEntity());
68        if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
69          throw PackageManagerHttpActionException.forHttpError(packageManagerInstallStatusURL, response.getStatusLine(), responseString);
70        }
71  
72        return toPackageManagerInstallStatus(responseString);
73      }
74      catch (IOException ex) {
75        throw PackageManagerHttpActionException.forIOException(packageManagerInstallStatusURL, ex);
76      }
77    }
78  
79    private PackageManagerInstallStatus toPackageManagerInstallStatus(String jsonString) {
80      boolean finished = false;
81      int itemCount = 0;
82  
83      try {
84        JSONObject json = new JSONObject(jsonString);
85        JSONObject status = json.optJSONObject("status");
86        if (status != null) {
87          finished = status.optBoolean("finished");
88          itemCount = status.optInt("itemCount");
89        }
90      }
91      catch (JSONException ex) {
92        throw PackageManagerHttpActionException.forJSONException(packageManagerInstallStatusURL, jsonString, ex);
93      }
94  
95      return new PackageManagerInstallStatus(finished, itemCount);
96    }
97  
98  }