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