PageRenderTime 25ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/amps-maven-plugin/src/main/java/com/atlassian/maven/plugins/updater/MarketplaceSdkResource.java

https://bitbucket.org/atlassian/amps
Java | 169 lines | 148 code | 18 blank | 3 comment | 26 complexity | b185d620ebd8cee582b6914e9832b79c MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. package com.atlassian.maven.plugins.updater;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.google.common.annotations.VisibleForTesting;
  4. import com.google.common.collect.ImmutableMap;
  5. import org.apache.commons.io.IOUtils;
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.codehaus.plexus.logging.AbstractLogEnabled;
  8. import java.io.BufferedInputStream;
  9. import java.io.File;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.net.ConnectException;
  14. import java.net.HttpURLConnection;
  15. import java.net.MalformedURLException;
  16. import java.net.URL;
  17. import java.net.UnknownHostException;
  18. import java.nio.charset.StandardCharsets;
  19. import java.util.List;
  20. import java.util.Map;
  21. /**
  22. * Implements an SdkResource for the Atlassian Marketplace.
  23. */
  24. public class MarketplaceSdkResource extends AbstractLogEnabled implements SdkResource {
  25. private static final String SDK_DOWNLOAD_URL_ROOT =
  26. "https://marketplace.atlassian.com/rest/1.0/plugins/atlassian-plugin-sdk-";
  27. private final ObjectMapper mapper;
  28. public MarketplaceSdkResource() {
  29. mapper = new ObjectMapper();
  30. }
  31. @Override
  32. public File downloadLatestSdk(SdkPackageType packageType) {
  33. return downloadSdk(packageType, getLatestSdkVersion(packageType));
  34. }
  35. @Override
  36. public File downloadSdk(SdkPackageType packageType, String version) {
  37. Map<?, ?> rootAsMap = getPluginJsonAsMap(packageType);
  38. String versionDownloadPath = null;
  39. Map<?, ?> versionsElement = (Map<?, ?>) rootAsMap.get("versions");
  40. List<Map<?, ?>> versions = (List<Map<?, ?>>) versionsElement.get("versions");
  41. for (Map<?, ?> versionData : versions) {
  42. if (versionData.get("version").equals(version)) {
  43. List<Map<?, ?>> links = (List<Map<?, ?>>) versionData.get("links");
  44. for (Map<?, ?> link : links) {
  45. if (link.get("rel").equals("binary")) {
  46. versionDownloadPath = (String) link.get("href");
  47. break;
  48. }
  49. }
  50. }
  51. }
  52. if (versionDownloadPath == null) {
  53. throw new RuntimeException("Couldn't find SDK version for " + packageType.key()
  54. + " on marketplace with version " + version);
  55. }
  56. File sdkDownloadTempFile;
  57. HttpURLConnection conn = null;
  58. try {
  59. String tempFileSuffix;
  60. if (packageType == SdkPackageType.WINDOWS) {
  61. tempFileSuffix = ".exe";
  62. } else if (packageType == SdkPackageType.MAC) {
  63. tempFileSuffix = ".pkg";
  64. } else if (packageType == SdkPackageType.RPM) {
  65. tempFileSuffix = ".rpm";
  66. } else if (packageType == SdkPackageType.DEB) {
  67. tempFileSuffix = ".deb";
  68. } else {
  69. tempFileSuffix = ".tar.gz";
  70. }
  71. sdkDownloadTempFile = File.createTempFile("atlassian-plugin-sdk-" + version, tempFileSuffix);
  72. URL url;
  73. try {
  74. url = new URL(versionDownloadPath);
  75. } catch (MalformedURLException e) {
  76. throw new RuntimeException(e);
  77. }
  78. conn = (HttpURLConnection) url.openConnection();
  79. try (InputStream inputStream = conn.getInputStream()) {
  80. copyResponseStreamToFile(inputStream, sdkDownloadTempFile);
  81. }
  82. } catch (IOException e) {
  83. throw new RuntimeException(e);
  84. } finally {
  85. if (conn != null) {
  86. conn.disconnect();
  87. }
  88. }
  89. return sdkDownloadTempFile;
  90. }
  91. @Override
  92. public String getLatestSdkVersion(SdkPackageType packageType) {
  93. Map<?, ?> rootAsMap = getPluginJsonAsMap(packageType);
  94. if (rootAsMap.containsKey("version")) {
  95. Map<?, ?> version = (Map<?, ?>) rootAsMap.get("version");
  96. return (String) version.get("version");
  97. } else {
  98. return "";
  99. }
  100. }
  101. private void copyResponseStreamToFile(InputStream stream, File file) {
  102. try (FileOutputStream fos = new FileOutputStream(file)) {
  103. IOUtils.copy(stream, fos);
  104. } catch (IOException ioe) {
  105. throw new RuntimeException(ioe);
  106. }
  107. }
  108. private Map<?, ?> getPluginJsonAsMap(SdkPackageType packageType) {
  109. URL url;
  110. try {
  111. url = new URL(SDK_DOWNLOAD_URL_ROOT + packageType.key());
  112. } catch (MalformedURLException e) {
  113. throw new RuntimeException(e);
  114. }
  115. String json;
  116. HttpURLConnection conn = null;
  117. try {
  118. conn = (HttpURLConnection) url.openConnection();
  119. try (InputStream jsonStream = new BufferedInputStream(conn.getInputStream())) {
  120. json = IOUtils.toString(jsonStream, StandardCharsets.UTF_8);
  121. }
  122. } catch (UnknownHostException e) {
  123. this.getLogger().info("Unknown host " + url.getHost());
  124. json = "";
  125. } catch (ConnectException e) {
  126. this.getLogger().info("Fail to connect to host " + url.getHost());
  127. json = "";
  128. } catch (Exception e) {
  129. throw new RuntimeException(e);
  130. } finally {
  131. if (conn != null) {
  132. conn.disconnect();
  133. }
  134. }
  135. return parseJsonToMap(json);
  136. }
  137. @VisibleForTesting
  138. Map<?, ?> parseJsonToMap(String json) {
  139. Map<?, ?> rootAsMap;
  140. try {
  141. if (StringUtils.isNotEmpty(json)) {
  142. rootAsMap = mapper.readValue(json, Map.class);
  143. } else {
  144. rootAsMap = ImmutableMap.of();
  145. }
  146. return rootAsMap;
  147. } catch (Exception e) {
  148. throw new RuntimeException(e);
  149. }
  150. }
  151. }