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