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