PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/heritrix-1.14.4/src/java/org/archive/net/ClientFTP.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 179 lines | 112 code | 18 blank | 49 comment | 9 complexity | 385a6909424aa66a9f5083e77e8c7617 MD5 | raw file
  1. /* ClientFTP.java
  2. *
  3. * $Id: ClientFTP.java 6662 2009-11-17 02:27:20Z nlevitt $
  4. *
  5. * Created on Jun 5, 2003
  6. *
  7. * Copyright (C) 2003 Internet Archive.
  8. *
  9. * This file is part of the Heritrix web crawler (crawler.archive.org).
  10. *
  11. * Heritrix is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser Public License as published by
  13. * the Free Software Foundation; either version 2.1 of the License, or
  14. * any later version.
  15. *
  16. * Heritrix is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Lesser Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser Public License
  22. * along with Heritrix; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. package org.archive.net;
  26. import java.io.BufferedReader;
  27. import java.io.IOException;
  28. import java.io.Reader;
  29. import java.io.StringReader;
  30. import java.net.Socket;
  31. import java.util.Iterator;
  32. import java.util.logging.Level;
  33. import java.util.logging.Logger;
  34. import org.apache.commons.io.LineIterator;
  35. import org.apache.commons.net.ProtocolCommandEvent;
  36. import org.apache.commons.net.ProtocolCommandListener;
  37. import org.apache.commons.net.ftp.FTPClient;
  38. /**
  39. * Client for FTP operations. Saves the commands sent to the server and replies
  40. * received, which can be retrieved with {@link #getControlConversation()}.
  41. *
  42. * @author pjack
  43. * @author nlevitt
  44. */
  45. public class ClientFTP extends FTPClient implements ProtocolCommandListener {
  46. private final Logger logger = Logger.getLogger(this.getClass().getName());
  47. // Records the conversation on the ftp control channel. The format is based on "curl -v".
  48. protected StringBuilder controlConversation;
  49. protected Socket dataSocket;
  50. /**
  51. * Constructs a new <code>ClientFTP</code>.
  52. */
  53. public ClientFTP() {
  54. controlConversation = new StringBuilder();
  55. addProtocolCommandListener(this);
  56. }
  57. /**
  58. * Opens a data connection.
  59. *
  60. * @param command
  61. * the data command (eg, RETR or LIST)
  62. * @param path
  63. * the path of the file to retrieve
  64. * @return the socket to read data from, or null if server says not found,
  65. * permission denied, etc
  66. * @throws IOException
  67. * if a network error occurs
  68. */
  69. public Socket openDataConnection(int command, String path)
  70. throws IOException {
  71. try {
  72. dataSocket = _openDataConnection_(command, path);
  73. if (dataSocket != null) {
  74. recordAdditionalInfo("Opened data connection to "
  75. + dataSocket.getInetAddress().getHostAddress() + ":"
  76. + dataSocket.getPort());
  77. }
  78. return dataSocket;
  79. } catch (IOException e) {
  80. if (getPassiveHost() != null) {
  81. recordAdditionalInfo("Failed to open data connection to "
  82. + getPassiveHost() + ":" + getPassivePort() + ": "
  83. + e.getMessage());
  84. } else {
  85. recordAdditionalInfo("Failed to open data connection: "
  86. + e.getMessage());
  87. }
  88. throw e;
  89. }
  90. }
  91. public void closeDataConnection() {
  92. if (dataSocket != null) {
  93. String dataHostPort = dataSocket.getInetAddress().getHostAddress()
  94. + ":" + dataSocket.getPort();
  95. try {
  96. dataSocket.close();
  97. recordAdditionalInfo("Closed data connection to "
  98. + dataHostPort);
  99. } catch (IOException e) {
  100. recordAdditionalInfo("Problem closing data connection to "
  101. + dataHostPort + ": " + e.getMessage());
  102. }
  103. }
  104. }
  105. protected void _connectAction_() throws IOException {
  106. try {
  107. recordAdditionalInfo("Opening control connection to "
  108. + getRemoteAddress().getHostAddress() + ":"
  109. + getRemotePort());
  110. super._connectAction_();
  111. } catch (IOException e) {
  112. recordAdditionalInfo("Failed to open control connection to "
  113. + getRemoteAddress().getHostAddress() + ":"
  114. + getRemotePort() + ": " + e.getMessage());
  115. throw e;
  116. }
  117. }
  118. public void disconnect() throws IOException {
  119. String remoteHostPort = getRemoteAddress().getHostAddress() + ":"
  120. + getRemotePort();
  121. super.disconnect();
  122. recordAdditionalInfo("Closed control connection to " + remoteHostPort);
  123. }
  124. public String getControlConversation() {
  125. return controlConversation.toString();
  126. }
  127. private class IterableLineIterator extends LineIterator implements Iterable<String> {
  128. public IterableLineIterator(final Reader reader) throws IllegalArgumentException {
  129. super(reader);
  130. }
  131. @SuppressWarnings("unchecked")
  132. public Iterator<String> iterator() {
  133. return this;
  134. }
  135. }
  136. protected void recordControlMessage(String linePrefix, String message) {
  137. for (String line: new IterableLineIterator(new BufferedReader(new StringReader(message)))) {
  138. controlConversation.append(linePrefix);
  139. controlConversation.append(line);
  140. controlConversation.append(NETASCII_EOL);
  141. if (logger.isLoggable(Level.FINEST)) {
  142. logger.finest(linePrefix + line);
  143. }
  144. }
  145. }
  146. public void protocolCommandSent(ProtocolCommandEvent event) {
  147. recordControlMessage("> ", event.getMessage());
  148. }
  149. public void protocolReplyReceived(ProtocolCommandEvent event) {
  150. recordControlMessage("< ", event.getMessage());
  151. }
  152. // for noting things like successful/unsuccessful connection to data port
  153. private void recordAdditionalInfo(String message) {
  154. recordControlMessage("* ", message);
  155. }
  156. // XXX see https://issues.apache.org/jira/browse/NET-257
  157. @Override
  158. public String[] getReplyStrings() {
  159. return _replyLines.toArray(new String[0]);
  160. }
  161. }