home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / jsdk / FingerSe.jav < prev    next >
Encoding:
Text File  |  1997-08-17  |  3.9 KB  |  136 lines

  1. /*
  2.  * @(#)FingerServlet.java    1.12 97/05/22
  3.  * 
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.0
  20.  */
  21.  
  22. import java.io.*;
  23. import java.net.*;
  24. import java.util.*;
  25.  
  26. import javax.servlet.*;
  27. import javax.servlet.http.*;
  28.  
  29. /**
  30.  * Finger servlet. This servlet uses the finger protocol to query
  31.  * information about users on specified hosts. The query string
  32.  * parameters <tt>user</tt>, <tt>hosts</tt>, and <tt>verbose</tt>
  33.  * can be used to specify the user and hosts to query. The parameter
  34.  * <tt>user</tt> is the user name, <tt>hosts</tt> is a comma-separated
  35.  * list of host names to query, and <tt>verbose</tt> if specified will
  36.  * cause verbose output to be generated. For example,
  37.  * <pre>
  38.  *     http:/goa/finger.html?user=dac&hosts=eno,doppio&verbose=yes
  39.  * </pre>
  40.  * This URL will request full information about user 'dac' on both
  41.  * hosts 'eno' and 'doppio'.
  42.  *
  43.  * @version     1.12, 05/22/97
  44.  * @author     David Connelly
  45.  */
  46. public
  47. class FingerServlet extends HttpServlet {
  48.     /*
  49.      * Port number for finger daemon.
  50.      */
  51.     static final int FINGER_PORT = 79;
  52.     private static final String timeout_msg = "Timeout reading from server";
  53.  
  54.     /**
  55.      * Handles a single finger request from the client.
  56.      */
  57.     public void doGet (HttpServletRequest req, HttpServletResponse res)
  58.     throws ServletException, IOException
  59.     {
  60.     String user = req.getParameter("user");
  61.     String hosts = req.getParameter("hosts");
  62.     String verbose = req.getParameter("verbose");
  63.  
  64.     res.setContentType("text/html");
  65.  
  66.     ServletOutputStream out = res.getOutputStream();
  67.     out.println("<html>");
  68.     out.println("<head><title>Finger Servlet</title></head>");
  69.     out.println("<body>");
  70.     out.println("<h2>Finger results:</h2>");
  71.     out.println("<pre>");
  72.     if (hosts == null) {
  73.         finger(out, user, null, "yes".equalsIgnoreCase(verbose)) ;
  74.     } else {
  75.         StringTokenizer st = new StringTokenizer(hosts, ",");
  76.         while (st.hasMoreTokens()) {
  77.         String host = st.nextToken();
  78.         out.println("[" + host + "]");
  79.         try {
  80.             finger(out, user, host, "yes".equalsIgnoreCase(verbose));
  81.         } catch (IOException e) {
  82.             out.println(e.toString());
  83.         }
  84.         out.println();
  85.         }
  86.     }
  87.     out.println("</pre>");
  88.     out.println("</body></html>");
  89.     }
  90.  
  91.     /*
  92.      * Sends finger output for a user and host to the specified output
  93.      * stream.
  94.      */
  95.     private void finger(ServletOutputStream out, String user, String host,
  96.         boolean verbose)
  97.     throws IOException
  98.     {
  99.     // open connection to finger daemon
  100.     Socket s;
  101.     if (host == null) {
  102.         s = new Socket(InetAddress.getLocalHost(), FINGER_PORT);
  103.     } else {
  104.         s = new Socket(host, FINGER_PORT);
  105.     }
  106.     // send finger command
  107.     PrintStream socket_out = new PrintStream(s.getOutputStream());
  108.     if (verbose) {
  109.         socket_out.print("/W ");
  110.     }
  111.     if (user != null) {
  112.         socket_out.print(user);
  113.     }
  114.     socket_out.print("\r\n");
  115.     socket_out.flush();
  116.     // copy results to output stream
  117.     s.setSoTimeout(30000);
  118.     InputStream in = s.getInputStream();
  119.     byte[] buf = new byte[2048];
  120.     int len;
  121.     try {
  122.         while ((len = in.read(buf, 0, buf.length)) != -1) {
  123.         out.write(buf, 0, len);
  124.         }
  125.     }
  126.     catch (InterruptedIOException ioe) {
  127.         timeout_msg.getBytes(0, timeout_msg.length(), buf, 0);
  128.         out.write(buf, 0, timeout_msg.length());
  129.     }
  130.     finally {
  131.         socket_out.close();
  132.         s.close();
  133.     }
  134.     }
  135. }
  136.