001: /*
002: * Copyright 2001-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.net;
017:
018: import java.io.BufferedReader;
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.InputStreamReader;
022: import java.io.BufferedOutputStream;
023: import java.io.DataOutputStream;
024:
025: /***
026: * The FingerClient class implements the client side of the Internet Finger
027: * Protocol defined in RFC 1288. To finger a host you create a
028: * FingerClient instance, connect to the host, query the host, and finally
029: * disconnect from the host. If the finger service you want to query is on
030: * a non-standard port, connect to the host at that port.
031: * Here's a sample use:
032: * <pre>
033: * FingerClient finger;
034: *
035: * finger = new FingerClient();
036: *
037: * try {
038: * finger.connect("foo.bar.com");
039: * System.out.println(finger.query("foobar", false));
040: * finger.disconnect();
041: * } catch(IOException e) {
042: * System.err.println("Error I/O exception: " + e.getMessage());
043: * return;
044: * }
045: * </pre>
046: * <p>
047: * <p>
048: * @author Daniel F. Savarese
049: ***/
050:
051: public class FingerClient extends SocketClient {
052: /***
053: * The default FINGER port. Set to 79 according to RFC 1288.
054: ***/
055: public static final int DEFAULT_PORT = 79;
056:
057: private static final String __LONG_FLAG = "/W ";
058:
059: private transient StringBuffer __query = new StringBuffer(64);
060: private transient char[] __buffer = new char[1024];
061:
062: /***
063: * The default FingerClient constructor. Initializes the
064: * default port to <code> DEFAULT_PORT </code>.
065: ***/
066: public FingerClient() {
067: setDefaultPort(DEFAULT_PORT);
068: }
069:
070: /***
071: * Fingers a user at the connected host and returns the output
072: * as a String. You must first connect to a finger server before
073: * calling this method, and you should disconnect afterward.
074: * <p>
075: * @param longOutput Set to true if long output is requested, false if not.
076: * @param username The name of the user to finger.
077: * @return The result of the finger query.
078: * @exception IOException If an I/O error occurs while reading the socket.
079: ***/
080: public String query(boolean longOutput, String username)
081: throws IOException {
082: int read;
083: StringBuffer result = new StringBuffer(__buffer.length);
084: BufferedReader input;
085:
086: input = new BufferedReader(new InputStreamReader(
087: getInputStream(longOutput, username)));
088:
089: while (true) {
090: read = input.read(__buffer, 0, __buffer.length);
091: if (read <= 0)
092: break;
093: result.append(__buffer, 0, read);
094: }
095:
096: input.close();
097:
098: return result.toString();
099: }
100:
101: /***
102: * Fingers the connected host and returns the output
103: * as a String. You must first connect to a finger server before
104: * calling this method, and you should disconnect afterward.
105: * This is equivalent to calling <code> query(longOutput, "") </code>.
106: * <p>
107: * @param longOutput Set to true if long output is requested, false if not.
108: * @return The result of the finger query.
109: * @exception IOException If an I/O error occurs while reading the socket.
110: ***/
111: public String query(boolean longOutput) throws IOException {
112: return query(longOutput, "");
113: }
114:
115: /***
116: * Fingers a user and returns the input stream from the network connection
117: * of the finger query. You must first connect to a finger server before
118: * calling this method, and you should disconnect after finishing reading
119: * the stream.
120: * <p>
121: * @param longOutput Set to true if long output is requested, false if not.
122: * @param username The name of the user to finger.
123: * @return The InputStream of the network connection of the finger query.
124: * Can be read to obtain finger results.
125: * @exception IOException If an I/O error during the operation.
126: ***/
127: public InputStream getInputStream(boolean longOutput,
128: String username) throws IOException {
129: DataOutputStream output;
130:
131: __query.setLength(0);
132: if (longOutput)
133: __query.append(__LONG_FLAG);
134: __query.append(username);
135: __query.append(SocketClient.NETASCII_EOL);
136:
137: output = new DataOutputStream(new BufferedOutputStream(
138: _output_, 1024));
139: output.writeBytes(__query.toString());
140: output.flush();
141:
142: return _input_;
143: }
144:
145: /***
146: * Fingers the connected host and returns the input stream from
147: * the network connection of the finger query. This is equivalent to
148: * calling getInputStream(longOutput, ""). You must first connect to a
149: * finger server before calling this method, and you should disconnect
150: * after finishing reading the stream.
151: * <p>
152: * @param longOutput Set to true if long output is requested, false if not.
153: * @return The InputStream of the network connection of the finger query.
154: * Can be read to obtain finger results.
155: * @exception IOException If an I/O error during the operation.
156: ***/
157: public InputStream getInputStream(boolean longOutput)
158: throws IOException {
159: return getInputStream(longOutput, "");
160: }
161:
162: }
|