View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package examples;
18  
19  import java.io.IOException;
20  import java.net.InetAddress;
21  import java.net.UnknownHostException;
22  import org.apache.commons.net.WhoisClient;
23  
24  /***
25   * This is an example of how you would implement the Linux fwhois command
26   * in Java using NetComponents.  The Java version is much shorter.
27   * <p>
28   ***/
29  public final class fwhois
30  {
31  
32      public static final void main(String[] args)
33      {
34          int index;
35          String handle, host;
36          InetAddress address = null;
37          WhoisClient whois;
38  
39          if (args.length != 1)
40          {
41              System.err.println("usage: fwhois handle[@<server>]");
42              System.exit(1);
43          }
44  
45          index = args[0].lastIndexOf("@");
46  
47          whois = new WhoisClient();
48          // We want to timeout if a response takes longer than 60 seconds
49          whois.setDefaultTimeout(60000);
50  
51          if (index == -1)
52          {
53              handle = args[0];
54              host = WhoisClient.DEFAULT_HOST;
55          }
56          else
57          {
58              handle = args[0].substring(0, index);
59              host = args[0].substring(index + 1);
60          }
61  
62          try
63          {
64              address = InetAddress.getByName(host);
65          }
66          catch (UnknownHostException e)
67          {
68              System.err.println("Error unknown host: " + e.getMessage());
69              System.exit(1);
70          }
71  
72          System.out.println("[" + address.getHostName() + "]");
73  
74          try
75          {
76              whois.connect(address);
77              System.out.print(whois.query(handle));
78              whois.disconnect();
79          }
80          catch (IOException e)
81          {
82              System.err.println("Error I/O exception: " + e.getMessage());
83              System.exit(1);
84          }
85      }
86  
87  }