View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2018 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.install.crx;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.commons.lang3.StringUtils;
26  import org.json.JSONArray;
27  import org.json.JSONException;
28  import org.json.JSONObject;
29  
30  import io.wcm.tooling.commons.packmgr.PackageManagerException;
31  
32  /**
33   * Check if given package and version is already installed by inspecting the JSON result of
34   * <code>/crx/packmgr/list.jsp</code>.
35   */
36  class PackageInstalledChecker {
37  
38    private final JSONArray results;
39  
40    static final String PACKMGR_LIST_URL = "/crx/packmgr/list.jsp";
41  
42    private static final long NOT_FOUND_DATE = -1;
43    private static final long NOT_UNPACKED_DATE = 0;
44  
45    PackageInstalledChecker(JSONObject result) {
46      try {
47        this.results = result.getJSONArray("results");
48      }
49      catch (JSONException ex) {
50        throw new PackageManagerException("JSON response from " + PACKMGR_LIST_URL + " does not contain 'results' array.", ex);
51      }
52    }
53  
54    public PackageInstalledStatus getStatus(String group, String name, String version) {
55      Map<String, Long> map = getVersionUnpackedDates(group, name);
56  
57      if (map.isEmpty()) {
58        return PackageInstalledStatus.NOT_FOUND;
59      }
60  
61      long versionUnpackDate = getVersionUnpackDate(map, version);
62      if (versionUnpackDate == NOT_FOUND_DATE) {
63        return PackageInstalledStatus.NOT_FOUND;
64      }
65      if (versionUnpackDate == NOT_UNPACKED_DATE) {
66        return PackageInstalledStatus.UPLOADED;
67      }
68  
69      long lastUnpackDate = getLastUnpackDate(map);
70      if (lastUnpackDate > versionUnpackDate) {
71        return PackageInstalledStatus.INSTALLED_OTHER_VERSION;
72      }
73      else {
74        return PackageInstalledStatus.INSTALLED;
75      }
76    }
77  
78    private Map<String, Long> getVersionUnpackedDates(String group, String name) {
79      Map<String, Long> map = new HashMap<>();
80      for (int i = 0; i < results.length(); i++) {
81        JSONObject item = results.getJSONObject(i);
82        String itemGroup = item.optString("group");
83        String itemName = item.optString("name");
84        String itemVersion = item.optString("version");
85        long itemLastUnpacked = item.optLong("lastUnpacked", NOT_UNPACKED_DATE);
86        if (StringUtils.equals(group, itemGroup) && StringUtils.equals(name, itemName) && StringUtils.isNotBlank(itemVersion)) {
87          map.put(itemVersion, itemLastUnpacked);
88        }
89      }
90      return map;
91    }
92  
93    private long getVersionUnpackDate(Map<String, Long> map, String version) {
94      Long value = map.get(version);
95      if (value == null) {
96        return NOT_FOUND_DATE;
97      }
98      else {
99        return value;
100     }
101   }
102 
103   private long getLastUnpackDate(Map<String, Long> map) {
104     long last = 0;
105     for (Long date : map.values()) {
106       if (date > last) {
107         last = date;
108       }
109     }
110     return last;
111   }
112 
113 }