View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2025 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.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  import com.fasterxml.jackson.core.JsonProcessingException;
34  import com.fasterxml.jackson.databind.json.JsonMapper;
35  
36  import io.wcm.tooling.commons.packmgr.PackageManagerHttpActionException;
37  
38  /**
39   * Get bundle status from web console.
40   */
41  public final class SystemReadyStatusCall implements HttpCall<SystemReadyStatus> {
42  
43    private final CloseableHttpClient httpClient;
44    private final HttpClientContext context;
45    private final String systemReadyURL;
46  
47    private static final Logger log = LoggerFactory.getLogger(SystemReadyStatusCall.class);
48    private static final JsonMapper JSON_MAPPER = JsonMapper.builder().build();
49  
50    /**
51     * @param httpClient HTTP client
52     * @param context HTTP client context
53     * @param systemReadyURL System ready URL
54     */
55    public SystemReadyStatusCall(CloseableHttpClient httpClient, HttpClientContext context, String systemReadyURL) {
56      this.httpClient = httpClient;
57      this.context = context;
58      this.systemReadyURL = systemReadyURL;
59    }
60  
61    @Override
62    public SystemReadyStatus execute() {
63      log.debug("Call URL: {}", systemReadyURL);
64  
65      HttpGet method = new HttpGet(systemReadyURL);
66      try (CloseableHttpResponse response = httpClient.execute(method, context)) {
67  
68        String responseString = EntityUtils.toString(response.getEntity());
69        switch (response.getStatusLine().getStatusCode()) {
70          case HttpStatus.SC_OK, HttpStatus.SC_INTERNAL_SERVER_ERROR, HttpStatus.SC_SERVICE_UNAVAILABLE:
71            return toSystemReadyStatus(responseString, systemReadyURL);
72          case HttpStatus.SC_NOT_FOUND:
73            // AEM version that does not support system ready endpoint - accept as valid response
74            return new SystemReadyStatus(null, null);
75          default:
76            throw PackageManagerHttpActionException.forHttpError(systemReadyURL, response.getStatusLine(), responseString);
77        }
78      }
79      catch (IOException ex) {
80        throw PackageManagerHttpActionException.forIOException(systemReadyURL, ex);
81      }
82    }
83  
84    static SystemReadyStatus toSystemReadyStatus(String jsonString, String systemReadyURL) {
85      try {
86        return JSON_MAPPER.readValue(jsonString, SystemReadyStatus.class);
87      }
88      catch (JsonProcessingException ex) {
89        throw PackageManagerHttpActionException.forJSONException(systemReadyURL, jsonString, ex);
90      }
91    }
92  
93  }