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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.util.io;
  18. import com.dmdirc.util.collections.CollectionFunctions;
  19. import java.io.BufferedReader;
  20. import java.io.DataOutputStream;
  21. import java.io.IOException;
  22. import java.io.InputStreamReader;
  23. import java.net.URL;
  24. import java.net.URLConnection;
  25. import java.nio.file.Files;
  26. import java.nio.file.Path;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.stream.Collectors;
  30. /**
  31. * Allows easy downloading of files from HTTP sites.
  32. */
  33. public class Downloader {
  34. /**
  35. * Retrieves the specified page.
  36. *
  37. * @param url The URL to retrieve
  38. * @return A list of lines received from the server
  39. * @throws IOException If there's an I/O error while downloading
  40. */
  41. public List<String> getPage(final String url) throws IOException {
  42. return getPage(url, "");
  43. }
  44. /**
  45. * Retrieves the specified page, sending the specified post data.
  46. *
  47. * @param url The URL to retrieve
  48. * @param postData The raw POST data to send
  49. * @return A list of lines received from the server
  50. * @throws IOException If there's an I/O error while downloading
  51. */
  52. public List<String> getPage(final String url, final String postData)
  53. throws IOException {
  54. final URLConnection urlConn = getConnection(url, postData);
  55. final List<String> res;
  56. try (BufferedReader in = new BufferedReader(new InputStreamReader(
  57. urlConn.getInputStream()))) {
  58. res = in.lines().collect(Collectors.toList());
  59. }
  60. return res;
  61. }
  62. /**
  63. * Retrieves the specified page, sending the specified post data.
  64. *
  65. * @param url The URL to retrieve
  66. * @param postData A map of post data that should be sent
  67. * @return A list of lines received from the server
  68. * @throws IOException If there's an I/O error while downloading
  69. */
  70. public List<String> getPage(final String url,
  71. final Map<String, String> postData) throws IOException {
  72. return getPage(url, postData.entrySet().stream()
  73. .flatMap(CollectionFunctions::flattenAndEncodeKeyPair)
  74. .collect(Collectors.joining("&")));
  75. }
  76. /**
  77. * Downloads the specified page to disk.
  78. *
  79. * @param url The URL to retrieve
  80. * @param file The file to save the page to
  81. * @throws IOException If there's an I/O error while downloading
  82. */
  83. public void downloadPage(final String url, final Path file)
  84. throws IOException {
  85. downloadPage(url, file, null);
  86. }
  87. /**
  88. * Downloads the specified page to disk.
  89. *
  90. * @param url The URL to retrieve
  91. * @param file The file to save the page to
  92. * @param listener The progress listener for this download
  93. * @throws IOException If there's an I/O error while downloading
  94. */
  95. public void downloadPage(final String url, final Path file, final DownloadListener listener)
  96. throws IOException {
  97. final URLConnection connection = getConnection(url, "");
  98. Files.copy(new ListenerInputStream(connection.getInputStream(), listener,
  99. connection.getContentLength()), file);
  100. }
  101. /**
  102. * Creates an URL connection for the specified URL and data.
  103. *
  104. * @param url The URL to connect to
  105. * @param postData The POST data to pass to the URL
  106. * @return An URLConnection for the specified URL/data
  107. * @throws IOException If an I/O exception occurs while connecting
  108. */
  109. private URLConnection getConnection(final String url, final String postData)
  110. throws IOException {
  111. final URLConnection urlConn = getURLConnection(url);
  112. urlConn.setUseCaches(false);
  113. urlConn.setDoInput(true);
  114. urlConn.setDoOutput(!postData.isEmpty());
  115. urlConn.setConnectTimeout(10000);
  116. if (!postData.isEmpty()) {
  117. urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  118. try (DataOutputStream out = new DataOutputStream(urlConn.getOutputStream())) {
  119. out.writeBytes(postData);
  120. out.flush();
  121. }
  122. }
  123. return urlConn;
  124. }
  125. protected URLConnection getURLConnection(final String url) throws IOException {
  126. final URL myUrl = new URL(url);
  127. return myUrl.openConnection();
  128. }
  129. }