View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2014 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  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   * Get bundle status from web console.
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     * @param httpClient HTTP client
52     * @param context HTTP client context
53     * @param bundleStatusURL Bundle status URL
54     * @param bundleStatusWhitelistBundleNames Patterns of bundle names to be ignored
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  }