/* * @(#)FingerServlet.java 1.3 96/08/19 David Connelly * * Copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL purposes and without * fee is hereby granted provided that this copyright notice * appears in all copies. Please refer to the file "copyright.html" * for further important copyright and licensing information. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ import java.servlet.*; import java.io.*; import java.net.*; import java.util.*; /** * Finger servlet. This servlet uses the finger protocol to query * information about users on specified hosts. The query string * parameters user, hosts, and verbose * can be used to specify the user and hosts to query. The parameter * user is the user name, hosts is a comma-separated * list of host names to query, and verbose if specified will * cause verbose output to be generated. For example, *
* http:/goa/finger.html?user=dac&hosts=eno,doppio&verbose=yes ** This URL will request full information about user 'dac' on both * hosts 'eno' and 'doppio'. * * @version 1.3, 08/19/96 * @author David Connelly */ public class FingerServlet extends Servlet { /* * Port number for finger daemon. */ static final int FINGER_PORT = 79; /** * Handles a single finger request from the client. */ public void service(ServletRequest req, ServletResponse res) throws IOException { String user = req.getQueryParameter("user"); String hosts = req.getQueryParameter("hosts"); String verbose = req.getQueryParameter("verbose"); res.setContentType("text/html"); res.setHeader("Pragma", "no-cache"); res.writeHeaders(); PrintStream out = new PrintStream(res.getOutputStream()); out.println(""); out.println("
");
if (hosts == null) {
finger(out, user, null, "yes".equalsIgnoreCase(verbose)) ;
} else {
StringTokenizer st = new StringTokenizer(hosts, ",");
while (st.hasMoreTokens()) {
String host = st.nextToken();
out.println("[" + host + "]");
try {
finger(out, user, host, "yes".equalsIgnoreCase(verbose));
} catch (IOException e) {
out.println(e);
}
out.println();
}
}
out.println("");
out.println("");
}
/*
* Sends finger output for a user and host to the specified output
* stream.
*/
void finger(OutputStream out, String user, String host, boolean verbose)
throws IOException
{
// open connection to finger daemon
Socket s;
if (host == null) {
s = new Socket(InetAddress.getLocalHost(), FINGER_PORT);
} else {
s = new Socket(host, FINGER_PORT);
}
// send finger command
PrintStream ps = new PrintStream(s.getOutputStream());
if (verbose) {
ps.print("/W ");
}
if (user != null) {
ps.print(user);
}
ps.print("\r\n");
ps.flush();
// copy results to output stream
InputStream in = s.getInputStream();
byte[] buf = new byte[512];
int len;
while ((len = in.read(buf, 0, buf.length)) != -1) {
out.write(buf, 0, len);
}
s.close();
}
}