View Javadoc
1 package org.apache.commons.net; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Commons" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.io.BufferedReader; 58 import java.io.IOException; 59 import java.io.InputStream; 60 import java.io.InputStreamReader; 61 import java.net.Socket; 62 import java.io.DataOutputStream; 63 64 /**** 65 * The FingerClient class implements the client side of the Internet Finger 66 * Protocol defined in RFC 1288. To finger a host you create a 67 * FingerClient instance, connect to the host, query the host, and finally 68 * disconnect from the host. If the finger service you want to query is on 69 * a non-standard port, connect to the host at that port. 70 * Here's a sample use: 71 * <pre> 72 * FingerClient finger; 73 * 74 * finger = new FingerClient(); 75 * 76 * try { 77 * finger.connect("foo.bar.com"); 78 * System.out.println(finger.query("foobar", false)); 79 * finger.disconnect(); 80 * } catch(IOException e) { 81 * System.err.println("Error I/O exception: " + e.getMessage()); 82 * return; 83 * } 84 * </pre> 85 * <p> 86 * <p> 87 * @author Daniel F. Savarese 88 ***/ 89 90 public class FingerClient extends SocketClient 91 { 92 /**** 93 * The default FINGER port. Set to 79 according to RFC 1288. 94 ***/ 95 public static final int DEFAULT_PORT = 79; 96 97 private static final String __LONG_FLAG = "/W "; 98 99 private transient StringBuffer __query = new StringBuffer(64); 100 private transient char[] __buffer = new char[1024]; 101 102 /**** 103 * The default FingerClient constructor. Initializes the 104 * default port to <code> DEFAULT_PORT </code>. 105 ***/ 106 public FingerClient() 107 { 108 setDefaultPort(DEFAULT_PORT); 109 } 110 111 112 /**** 113 * Fingers a user at the connected host and returns the output 114 * as a String. You must first connect to a finger server before 115 * calling this method, and you should disconnect afterward. 116 * <p> 117 * @param longOutput Set to true if long output is requested, false if not. 118 * @param username The name of the user to finger. 119 * @return The result of the finger query. 120 * @exception IOException If an I/O error occurs while reading the socket. 121 ***/ 122 public String query(boolean longOutput, String username) throws IOException 123 { 124 int read; 125 StringBuffer result = new StringBuffer(__buffer.length); 126 BufferedReader input; 127 128 input = 129 new BufferedReader(new InputStreamReader(getInputStream(longOutput, 130 username))); 131 132 while (true) 133 { 134 read = input.read(__buffer, 0, __buffer.length); 135 if (read <= 0) 136 break; 137 result.append(__buffer, 0, read); 138 } 139 140 input.close(); 141 142 return result.toString(); 143 } 144 145 146 /**** 147 * Fingers the connected host and returns the output 148 * as a String. You must first connect to a finger server before 149 * calling this method, and you should disconnect afterward. 150 * This is equivalent to calling <code> query(longOutput, "") </code>. 151 * <p> 152 * @param longOutput Set to true if long output is requested, false if not. 153 * @return The result of the finger query. 154 * @exception IOException If an I/O error occurs while reading the socket. 155 ***/ 156 public String query(boolean longOutput) throws IOException 157 { 158 return query(longOutput, ""); 159 } 160 161 162 /**** 163 * Fingers a user and returns the input stream from the network connection 164 * of the finger query. You must first connect to a finger server before 165 * calling this method, and you should disconnect after finishing reading 166 * the stream. 167 * <p> 168 * @param longOutput Set to true if long output is requested, false if not. 169 * @param username The name of the user to finger. 170 * @return The InputStream of the network connection of the finger query. 171 * Can be read to obtain finger results. 172 * @exception IOException If an I/O error during the operation. 173 ***/ 174 public InputStream getInputStream(boolean longOutput, String username) 175 throws IOException 176 { 177 Socket socket; 178 DataOutputStream output; 179 180 __query.setLength(0); 181 if (longOutput) 182 __query.append(__LONG_FLAG); 183 __query.append(username); 184 __query.append(SocketClient.NETASCII_EOL); 185 186 output = new DataOutputStream(_output_); 187 output.writeBytes(__query.toString()); 188 output.flush(); 189 190 return _input_; 191 } 192 193 194 /**** 195 * Fingers the connected host and returns the input stream from 196 * the network connection of the finger query. This is equivalent to 197 * calling getInputStream(longOutput, ""). You must first connect to a 198 * finger server before calling this method, and you should disconnect 199 * after finishing reading the stream. 200 * <p> 201 * @param longOutput Set to true if long output is requested, false if not. 202 * @return The InputStream of the network connection of the finger query. 203 * Can be read to obtain finger results. 204 * @exception IOException If an I/O error during the operation. 205 ***/ 206 public InputStream getInputStream(boolean longOutput) throws IOException 207 { 208 return getInputStream(longOutput, ""); 209 } 210 211 }

This page was automatically generated by Maven