View Javadoc

1   /*
2    * Copyright (c) 2004-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.mercury.finger;
14  
15  import java.io.IOException;
16  import java.io.InputStream;
17  import java.io.InterruptedIOException;
18  import java.io.OutputStream;
19  import java.net.InetAddress;
20  import java.net.Socket;
21  import java.net.UnknownHostException;
22  
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  /**
27   * Simple finger
28   *
29   * @author Daniel Sendula
30   */
31  public class SimpleFinger {
32  
33      /** Logger */
34      protected final Logger logger = LoggerFactory.getLogger(SimpleFinger.class);
35  
36      /** Username */
37      protected String userName;
38  
39      /** Hostname */
40      protected String hostName;
41  
42      /** Host address */
43      protected InetAddress hostAddress;
44  
45      /** Timeout. Default is 60 seconds */
46      protected int timeout = 60000;
47  
48      /** Last invocation's result */
49      protected String lastResult;
50  
51      /**
52       * Constructor
53       */
54      public SimpleFinger() {
55      }
56  
57      /**
58       * Returns host name
59       * @return host name
60       */
61      public String getHostName() {
62          return hostName;
63      }
64  
65      /**
66       * Sets host name
67       * @param host host name
68       */
69      public void setHostName(String host) {
70          this.hostName = host;
71      }
72  
73      /**
74       * Returns host address
75       * @return host address
76       */
77      public InetAddress getHostAddress() {
78          return hostAddress;
79      }
80  
81      /**
82       * Sets host address
83       * @param address host address
84       */
85      public void setHostAddress(InetAddress address) {
86          this.hostAddress = address;
87      }
88  
89      /**
90       * Returns user
91       * @return user
92       */
93      public String getUser() {
94          return userName;
95      }
96  
97      /**
98       * Sets user
99       * @param user user
100      */
101     public void setUser(String user) {
102         this.userName = user;
103     }
104 
105     /**
106      * Returns timeout
107      * @return timeout
108      */
109     public int getTimeout() {
110         return timeout;
111     }
112 
113     /**
114      * Sets timeout
115      * @param timeout timeout
116      */
117     public void setTimeout(int timeout) {
118         this.timeout = timeout;
119     }
120 
121     /**
122      * Returns last result
123      * @return last result
124      */
125     public String getLastResult() {
126         return lastResult;
127     }
128 
129     /**
130      * Note: Address or host must be populated.
131      * @throws UnknownHostException
132      * @throws IOException
133      */
134     public void finger() throws UnknownHostException, IOException {
135         if ((hostAddress == null) && (hostName == null)) {
136             throw new IOException("Address or Host must be supplied");
137         } else if (hostAddress == null) {
138             hostAddress = InetAddress.getByName(hostName);
139         }
140 
141         StringBuffer buf = new StringBuffer(64);
142         logger.debug("Connecting to: " + hostAddress + ":79");
143 
144         Socket socket = new Socket(hostAddress, 79);
145         socket.setSoTimeout(timeout); // One minute
146         try {
147             OutputStream out = socket.getOutputStream();
148             String fingerCommand = null;
149             if ((userName != null) && (userName.length() > 0) && (hostName != null) && (hostName.length() > 0)) {
150                 fingerCommand = "/W " + userName + "@" + hostName + "\r\n";
151             } else if ((userName != null) && (userName.length() > 0)) {
152                 fingerCommand = "/W " + userName + "\r\n";
153             } else if ((hostName != null) && (hostName.length() > 0)) {
154                 fingerCommand = "/W @" + hostName + "\r\n";
155             } else {
156                 fingerCommand = "/W\r\n";
157             }
158             out.write(fingerCommand.getBytes());
159             out.flush();
160 
161             InputStream is = socket.getInputStream();
162             logger.debug("Sent command '" + fingerCommand + "'");
163 
164             int i = is.read();
165             while (i > 0) {
166                 buf.append((char)i);
167                 i = is.read();
168             }
169             logger.info("Finger to:" + hostAddress + ":79 response:" + buf);
170         } catch (InterruptedIOException e) {
171             logger.info("Finger timeout to:" + hostAddress + ":79 response:" + buf);
172         } catch (IOException e) {
173             logger.error("Finger exception to:" + hostAddress + ":79 " + e.getMessage());
174         } finally {
175             try {
176                 socket.close();
177             } catch (IOException e) {
178             }
179         }
180         lastResult = buf.toString();
181     }
182 
183     /**
184      * Invokes finger function
185      * @param user username in format user@host
186      * @return result
187      * @throws UnknownHostException
188      * @throws IOException
189      */
190     public static String finger(String user) throws UnknownHostException, IOException {
191         if ((user == null) || (user.length() == 0)) {
192             return finger(null, null);
193         }
194         int i = user.indexOf('@');
195         String u = "";
196         String h = "";
197         if (i >= 0) {
198             u = user.substring(0, i);
199             h = user.substring(i+1);
200         }
201         return finger(u, h);
202     }
203 
204     /**
205      * Invokes finger
206      * @param user username
207      * @param host hostname
208      * @return result
209      * @throws UnknownHostException
210      * @throws IOException
211      */
212     public static String finger(String user, String host) throws UnknownHostException, IOException {
213         StringBuffer buf = new StringBuffer(64);
214         Socket socket = new Socket(host, 79);
215         socket.setSoTimeout(60000); // One minute
216         try {
217             OutputStream out = socket.getOutputStream();
218             if ((user != null) && (user.length() > 0) && (host != null) && (host.length() > 0)) {
219                 out.write(("/W "+user+"\r\n").getBytes());
220             } else if ((user != null) && (user.length() > 0)) {
221                 out.write(("/W @"+host+"\r\n").getBytes());
222             } else {
223                 out.write(("/W\r\n").getBytes());
224             }
225             out.flush();
226 
227             InputStream is = socket.getInputStream();
228 
229             int i = is.read();
230             while (i >= 0) {
231                 buf.append((char)i);
232                 i = is.read();
233             }
234         } catch (IOException e) {
235         } finally {
236             try {
237                 socket.close();
238             } catch (IOException e) {
239             }
240         }
241         return buf.toString();
242     }
243 
244 
245     public static void main(String[] args) throws Exception {
246         System.out.print(finger(args[0], args[1]));
247     }
248 
249 }