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 * https://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
18 package org.apache.commons.net.whois;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22
23 import org.apache.commons.net.finger.FingerClient;
24
25 /**
26 * The WhoisClient class implements the client side of the Internet Whois Protocol defined in RFC 954. To query a host you create a WhoisClient instance,
27 * connect to the host, query the host, and finally disconnect from the host. If the whois service you want to query is on a non-standard port, connect to the
28 * host at that port. Here's a sample use:
29 *
30 * <pre>
31 * WhoisClient whois;
32 *
33 * whois = new WhoisClient();
34 *
35 * try {
36 * whois.connect(WhoisClient.DEFAULT_HOST);
37 * System.out.println(whois.query("foobar"));
38 * whois.disconnect();
39 * } catch (IOException e) {
40 * System.err.println("Error I/O exception: " + e.getMessage());
41 * return;
42 * }
43 * </pre>
44 */
45
46 public final class WhoisClient extends FingerClient {
47
48 /**
49 * The default whois host to query. It is set to whois.internic.net.
50 */
51 public static final String DEFAULT_HOST = "whois.internic.net";
52
53 /**
54 * The default whois port. It is set to 43 according to RFC 954.
55 */
56 public static final int DEFAULT_PORT = 43;
57
58 /**
59 * The default whois constructor. Initializes the default port to {@code DEFAULT_PORT}.
60 */
61 public WhoisClient() {
62 setDefaultPort(DEFAULT_PORT);
63 }
64
65 /**
66 * Queries the connected whois server for information regarding the given handle and returns the InputStream of the network connection. It is up to the
67 * programmer to be familiar with the handle syntax of the whois server. You must first connect to a finger server before calling this method, and you
68 * should disconnect after finishing reading the stream.
69 *
70 * @param handle The handle to lookup.
71 * @return The InputStream of the network connection of the whois query. Can be read to obtain whois results.
72 * @throws IOException If an I/O error occurs during the operation.
73 */
74 public InputStream getInputStream(final String handle) throws IOException {
75 return getInputStream(false, handle);
76 }
77
78 /**
79 * Queries the connected whois server for information regarding the given handle. It is up to the programmer to be familiar with the handle syntax of the
80 * whois server. You must first connect to a whois server before calling this method, and you should disconnect afterward.
81 *
82 * @param handle The handle to lookup.
83 * @return The result of the whois query.
84 * @throws IOException If an I/O error occurs during the operation.
85 */
86 public String query(final String handle) throws IOException {
87 return query(false, handle);
88 }
89
90 }