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.

Downloader.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) 2006-2014 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.util.io;
  23. import java.io.BufferedReader;
  24. import java.io.DataOutputStream;
  25. import java.io.IOException;
  26. import java.io.InputStreamReader;
  27. import java.net.URL;
  28. import java.net.URLConnection;
  29. import java.nio.file.Files;
  30. import java.nio.file.Path;
  31. import java.util.List;
  32. import java.util.Map;
  33. import java.util.stream.Collectors;
  34. /**
  35. * Allows easy downloading of files from HTTP sites.
  36. */
  37. public class Downloader {
  38. /**
  39. * Retrieves the specified page.
  40. *
  41. * @param url The URL to retrieve
  42. * @return A list of lines received from the server
  43. * @throws IOException If there's an I/O error while downloading
  44. */
  45. public List<String> getPage(final String url) throws IOException {
  46. return getPage(url, "");
  47. }
  48. /**
  49. * Retrieves the specified page, sending the specified post data.
  50. *
  51. * @param url The URL to retrieve
  52. * @param postData The raw POST data to send
  53. * @return A list of lines received from the server
  54. * @throws IOException If there's an I/O error while downloading
  55. */
  56. public List<String> getPage(final String url, final String postData)
  57. throws IOException {
  58. final URLConnection urlConn = getConnection(url, postData);
  59. final List<String> res;
  60. try (BufferedReader in = new BufferedReader(new InputStreamReader(
  61. urlConn.getInputStream()))) {
  62. res = in.lines().collect(Collectors.toList());
  63. }
  64. return res;
  65. }
  66. /**
  67. * Retrieves the specified page, sending the specified post data.
  68. *
  69. * @param url The URL to retrieve
  70. * @param postData A map of post data that should be sent
  71. * @return A list of lines received from the server
  72. * @throws IOException If there's an I/O error while downloading
  73. */
  74. public List<String> getPage(final String url,
  75. final Map<String, String> postData) throws IOException {
  76. return getPage(url, postData.entrySet().stream().map(Object::toString)
  77. .collect(Collectors.joining("&")));
  78. }
  79. /**
  80. * Downloads the specified page to disk.
  81. *
  82. * @param url The URL to retrieve
  83. * @param file The file to save the page to
  84. * @throws IOException If there's an I/O error while downloading
  85. */
  86. public void downloadPage(final String url, final Path file)
  87. throws IOException {
  88. downloadPage(url, file, null);
  89. }
  90. /**
  91. * Downloads the specified page to disk.
  92. *
  93. * @param url The URL to retrieve
  94. * @param file The file to save the page to
  95. * @param listener The progress listener for this download
  96. * @throws IOException If there's an I/O error while downloading
  97. */
  98. public void downloadPage(final String url, final Path file, final DownloadListener listener)
  99. throws IOException {
  100. if (listener != null) {
  101. listener.setIndeterminate(true);
  102. }
  103. final URLConnection connection = getConnection(url, "");
  104. Files.copy(new ListenerInputStream(connection.getInputStream(), listener,
  105. connection.getContentLength()), file);
  106. }
  107. /**
  108. * Creates an URL connection for the specified URL and data.
  109. *
  110. * @param url The URL to connect to
  111. * @param postData The POST data to pass to the URL
  112. * @return An URLConnection for the specified URL/data
  113. * @throws IOException If an I/O exception occurs while connecting
  114. */
  115. private URLConnection getConnection(final String url, final String postData)
  116. throws IOException {
  117. final URLConnection urlConn = getURLConnection(url);
  118. urlConn.setUseCaches(false);
  119. urlConn.setDoInput(true);
  120. urlConn.setDoOutput(!postData.isEmpty());
  121. urlConn.setConnectTimeout(10000);
  122. if (!postData.isEmpty()) {
  123. urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  124. try (DataOutputStream out = new DataOutputStream(urlConn.getOutputStream())) {
  125. out.writeBytes(postData);
  126. out.flush();
  127. }
  128. }
  129. return urlConn;
  130. }
  131. protected URLConnection getURLConnection(final String url) throws IOException {
  132. final URL myUrl = new URL(url);
  133. return myUrl.openConnection();
  134. }
  135. }