You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NightlyChecker.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.updater.checking;
  23. import com.dmdirc.config.ConfigBinding;
  24. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  25. import com.dmdirc.updater.UpdateChannel;
  26. import com.dmdirc.updater.UpdateComponent;
  27. import com.dmdirc.updater.Version;
  28. import com.dmdirc.util.io.Downloader;
  29. import com.google.gson.Gson;
  30. import com.google.gson.reflect.TypeToken;
  31. import java.io.IOException;
  32. import java.net.MalformedURLException;
  33. import java.net.URL;
  34. import java.util.Collection;
  35. import java.util.Collections;
  36. import java.util.HashMap;
  37. import java.util.List;
  38. import java.util.Map;
  39. import java.util.Objects;
  40. import java.util.function.Function;
  41. import java.util.regex.Matcher;
  42. import java.util.regex.Pattern;
  43. import java.util.stream.Collectors;
  44. import javax.inject.Inject;
  45. import org.slf4j.Logger;
  46. import org.slf4j.LoggerFactory;
  47. import static com.dmdirc.ClientModule.GlobalConfig;
  48. /**
  49. * Nightly update checker.
  50. */
  51. public class NightlyChecker implements UpdateCheckStrategy {
  52. private static final Logger LOG = LoggerFactory.getLogger(NightlyChecker.class);
  53. /** Name matching regex. */
  54. private final Pattern pattern = Pattern.compile(
  55. "^(.*?)-([^-]+(-[0-9]+-g[0-9a-f]+)?)(-SNAPSHOT).jar?$");
  56. /** The URL to request to check for updates. */
  57. private static final String UPDATE_URL = "https://nightlies.dmdirc.com/json/latest";
  58. /** The update channel to check for updates on. */
  59. private UpdateChannel channel;
  60. /** Downloader to download files. */
  61. private final Downloader downloader;
  62. /**
  63. * Creates a new instance of {@link NightlyChecker}.
  64. *
  65. * @param configProvider The provider to use to retrieve update channel information.
  66. * @param downloader Used to download files
  67. */
  68. @Inject
  69. public NightlyChecker(@GlobalConfig final AggregateConfigProvider configProvider,
  70. final Downloader downloader) {
  71. configProvider.getBinder().bind(this, NightlyChecker.class);
  72. this.downloader = downloader;
  73. }
  74. /**
  75. * Sets the channel which will be used by the {@link NightlyChecker}.
  76. *
  77. * @param channel The new channel to use
  78. */
  79. @ConfigBinding(domain = "updater", key = "channel")
  80. public void setChannel(final String channel) {
  81. LOG.info("Changing channel to {}", channel);
  82. try {
  83. this.channel = UpdateChannel.valueOf(channel.toUpperCase());
  84. } catch (IllegalArgumentException ex) {
  85. this.channel = null;
  86. LOG.warn("Unknown channel {}", channel, ex);
  87. }
  88. }
  89. @Override
  90. public Map<UpdateComponent, UpdateCheckResult> checkForUpdates(
  91. final Collection<UpdateComponent> components) {
  92. if (channel != UpdateChannel.NIGHTLY) {
  93. LOG.info("Channel {} is not nightly, aborting", channel);
  94. return Collections.emptyMap();
  95. }
  96. LOG.info("Retrieving latest versions.");
  97. final List<NightlyResult> resultsList = new Gson().fromJson(getJson(),
  98. new TypeToken<List<NightlyResult>>(){}.getType());
  99. if (resultsList == null) {
  100. return Collections.emptyMap();
  101. }
  102. resultsList.stream()
  103. .filter(Objects::nonNull) //This is incase the JSON is broken
  104. .forEach(e -> {
  105. final Matcher matcher = pattern.matcher(e.getName());
  106. if (matcher.matches()) {
  107. e.setOtherName(matcher.group(1));
  108. e.setVersion(new Version(matcher.group(2)));
  109. e.setUrl(UPDATE_URL + '/' + e.getName());
  110. }
  111. });
  112. final Map<String, NightlyResult> resultsMap = resultsList.stream()
  113. .collect(Collectors.toMap(NightlyResult::getOtherName, Function.identity()));
  114. final Map<UpdateComponent, UpdateCheckResult> returns = new HashMap<>();
  115. components.forEach(e -> {
  116. if (resultsMap.containsKey(e.getName())) {
  117. if (resultsMap.get(e.getName()).getVersion().compareTo(e.getVersion()) > 0) {
  118. final String name = e.getName();
  119. final NightlyResult result = resultsMap.get(e.getName());
  120. try {
  121. returns.put(e, new BaseDownloadableResult(e, getURL(result),
  122. result.getOtherName(), result.getVersion()));
  123. } catch (MalformedURLException e1) {
  124. LOG.error("Unable to create a URL for {}", name);
  125. }
  126. }
  127. }
  128. });
  129. return returns;
  130. }
  131. private URL getURL(final NightlyResult result) throws MalformedURLException {
  132. return new URL(result.getUrl());
  133. }
  134. private String getJson() {
  135. try {
  136. return downloader.getPage(UPDATE_URL).stream().map(String::toString)
  137. .collect(Collectors.joining("\r\n"));
  138. } catch (IOException e) {
  139. LOG.warn("Error when getting update page: {}", e.getMessage());
  140. return "";
  141. }
  142. }
  143. /**
  144. * Wrapper class for GSON to deserialise the JSON.
  145. */
  146. private static class NightlyResult {
  147. private final String name;
  148. private final String type;
  149. private final String mtime;
  150. private final int size;
  151. private String otherName;
  152. private Version version;
  153. private String url;
  154. NightlyResult(final String name, final String type, final String mtime,
  155. final int size) {
  156. this.name = name;
  157. this.type = type;
  158. this.mtime = mtime;
  159. this.size = size;
  160. }
  161. String getName() {
  162. return name;
  163. }
  164. String getUrl() {
  165. return url;
  166. }
  167. String getOtherName() {
  168. return otherName;
  169. }
  170. void setOtherName(final String otherName) {
  171. this.otherName = otherName;
  172. }
  173. Version getVersion() {
  174. return version;
  175. }
  176. void setVersion(final Version version) {
  177. this.version = version;
  178. }
  179. void setUrl(final String url) {
  180. this.url = url;
  181. }
  182. }
  183. }