1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.net;
  17. import java.io.BufferedReader;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.InputStreamReader;
  21. import java.io.BufferedOutputStream;
  22. import java.io.DataOutputStream;
  23. /***
  24. * The FingerClient class implements the client side of the Internet Finger
  25. * Protocol defined in RFC 1288. To finger a host you create a
  26. * FingerClient instance, connect to the host, query the host, and finally
  27. * disconnect from the host. If the finger service you want to query is on
  28. * a non-standard port, connect to the host at that port.
  29. * Here's a sample use:
  30. * <pre>
  31. * FingerClient finger;
  32. *
  33. * finger = new FingerClient();
  34. *
  35. * try {
  36. * finger.connect("foo.bar.com");
  37. * System.out.println(finger.query("foobar", false));
  38. * finger.disconnect();
  39. * } catch(IOException e) {
  40. * System.err.println("Error I/O exception: " + e.getMessage());
  41. * return;
  42. * }
  43. * </pre>
  44. * <p>
  45. * <p>
  46. * @author Daniel F. Savarese
  47. ***/
  48. public class FingerClient extends SocketClient
  49. {
  50. /***
  51. * The default FINGER port. Set to 79 according to RFC 1288.
  52. ***/
  53. public static final int DEFAULT_PORT = 79;
  54. private static final String __LONG_FLAG = "/W ";
  55. private transient StringBuffer __query = new StringBuffer(64);
  56. private transient char[] __buffer = new char[1024];
  57. /***
  58. * The default FingerClient constructor. Initializes the
  59. * default port to <code> DEFAULT_PORT </code>.
  60. ***/
  61. public FingerClient()
  62. {
  63. setDefaultPort(DEFAULT_PORT);
  64. }
  65. /***
  66. * Fingers a user at the connected host and returns the output
  67. * as a String. You must first connect to a finger server before
  68. * calling this method, and you should disconnect afterward.
  69. * <p>
  70. * @param longOutput Set to true if long output is requested, false if not.
  71. * @param username The name of the user to finger.
  72. * @return The result of the finger query.
  73. * @exception IOException If an I/O error occurs while reading the socket.
  74. ***/
  75. public String query(boolean longOutput, String username) throws IOException
  76. {
  77. int read;
  78. StringBuffer result = new StringBuffer(__buffer.length);
  79. BufferedReader input;
  80. input =
  81. new BufferedReader(new InputStreamReader(getInputStream(longOutput,
  82. username)));
  83. while (true)
  84. {
  85. read = input.read(__buffer, 0, __buffer.length);
  86. if (read <= 0)
  87. break;
  88. result.append(__buffer, 0, read);
  89. }
  90. input.close();
  91. return result.toString();
  92. }
  93. /***
  94. * Fingers the connected host and returns the output
  95. * as a String. You must first connect to a finger server before
  96. * calling this method, and you should disconnect afterward.
  97. * This is equivalent to calling <code> query(longOutput, "") </code>.
  98. * <p>
  99. * @param longOutput Set to true if long output is requested, false if not.
  100. * @return The result of the finger query.
  101. * @exception IOException If an I/O error occurs while reading the socket.
  102. ***/
  103. public String query(boolean longOutput) throws IOException
  104. {
  105. return query(longOutput, "");
  106. }
  107. /***
  108. * Fingers a user and returns the input stream from the network connection
  109. * of the finger query. You must first connect to a finger server before
  110. * calling this method, and you should disconnect after finishing reading
  111. * the stream.
  112. * <p>
  113. * @param longOutput Set to true if long output is requested, false if not.
  114. * @param username The name of the user to finger.
  115. * @return The InputStream of the network connection of the finger query.
  116. * Can be read to obtain finger results.
  117. * @exception IOException If an I/O error during the operation.
  118. ***/
  119. public InputStream getInputStream(boolean longOutput, String username)
  120. throws IOException
  121. {
  122. DataOutputStream output;
  123. __query.setLength(0);
  124. if (longOutput)
  125. __query.append(__LONG_FLAG);
  126. __query.append(username);
  127. __query.append(SocketClient.NETASCII_EOL);
  128. output =
  129. new DataOutputStream(new BufferedOutputStream(_output_, 1024));
  130. output.writeBytes(__query.toString());
  131. output.flush();
  132. return _input_;
  133. }
  134. /***
  135. * Fingers the connected host and returns the input stream from
  136. * the network connection of the finger query. This is equivalent to
  137. * calling getInputStream(longOutput, ""). You must first connect to a
  138. * finger server before calling this method, and you should disconnect
  139. * after finishing reading the stream.
  140. * <p>
  141. * @param longOutput Set to true if long output is requested, false if not.
  142. * @return The InputStream of the network connection of the finger query.
  143. * Can be read to obtain finger results.
  144. * @exception IOException If an I/O error during the operation.
  145. ***/
  146. public InputStream getInputStream(boolean longOutput) throws IOException
  147. {
  148. return getInputStream(longOutput, "");
  149. }
  150. }