View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2019 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.util.HashSet;
23  import java.util.List;
24  import java.util.Set;
25  import java.util.regex.Pattern;
26  
27  import org.apache.commons.lang3.StringUtils;
28  import org.json.JSONArray;
29  import org.json.JSONObject;
30  
31  /**
32   * Parses bundle status JSON response.
33   */
34  final class BundleStatusParser {
35  
36    private final List<Pattern> bundleStatusWhitelistBundleNames;
37  
38    BundleStatusParser(List<Pattern> bundleStatusWhitelistBundleNames) {
39      this.bundleStatusWhitelistBundleNames = bundleStatusWhitelistBundleNames;
40    }
41  
42    BundleStatus parse(String jsonString) {
43      JSONObject json = new JSONObject(jsonString);
44  
45      String statusLine = json.getString("status");
46  
47      // get bundle stats
48      int total = 0;
49      int active = 0;
50      int activeFragment = 0;
51      int resolved = 0;
52      int installed = 0;
53      int ignored = 0;
54  
55      // get list of all bundle names
56      Set<String> bundleSymbolicNames = new HashSet<>();
57      JSONArray data = json.getJSONArray("data");
58      for (int i = 0; i < data.length(); i++) {
59        JSONObject item = data.getJSONObject(i);
60  
61        String symbolicName = item.optString("symbolicName");
62        String state = item.optString("state");
63        boolean fragment = item.optBoolean("fragment");
64        boolean whitelisted = isWhitelisted(symbolicName);
65  
66        total++;
67        if (fragment) {
68          activeFragment++;
69        }
70        else if (isActive(state)) {
71          active++;
72        }
73        else if (isResolved(state)) {
74          if (whitelisted) {
75            ignored++;
76          }
77          else {
78            resolved++;
79          }
80        }
81        else if (isInstalled(state)) {
82          if (whitelisted) {
83            ignored++;
84          }
85          else {
86            installed++;
87          }
88        }
89  
90        if (StringUtils.isNotBlank(symbolicName) && !whitelisted) {
91          bundleSymbolicNames.add(symbolicName);
92        }
93      }
94  
95      return new BundleStatus(
96          statusLine,
97          total, active, activeFragment, resolved, installed, ignored,
98          bundleSymbolicNames);
99    }
100 
101   private boolean isActive(String actual) {
102     return StringUtils.equalsIgnoreCase(actual, "Active");
103   }
104 
105   private boolean isResolved(String actual) {
106     return StringUtils.equalsIgnoreCase(actual, "Resolved");
107   }
108 
109   private boolean isInstalled(String actual) {
110     return StringUtils.equalsIgnoreCase(actual, "Installed");
111   }
112 
113   private boolean isWhitelisted(String symbolicName) {
114     for (Pattern pattern : bundleStatusWhitelistBundleNames) {
115       if (pattern.matcher(symbolicName).matches()) {
116         return true;
117       }
118     }
119     return false;
120   }
121 
122 }