001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.net.whois;
019
020import java.io.IOException;
021import java.io.InputStream;
022
023import org.apache.commons.net.finger.FingerClient;
024
025/**
026 * 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,
027 * 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
028 * host at that port. Here's a sample use:
029 *
030 * <pre>
031 * WhoisClient whois;
032 *
033 * whois = new WhoisClient();
034 *
035 * try {
036 *     whois.connect(WhoisClient.DEFAULT_HOST);
037 *     System.out.println(whois.query("foobar"));
038 *     whois.disconnect();
039 * } catch (IOException e) {
040 *     System.err.println("Error I/O exception: " + e.getMessage());
041 *     return;
042 * }
043 * </pre>
044 */
045
046public final class WhoisClient extends FingerClient {
047    /**
048     * The default whois host to query. It is set to whois.internic.net.
049     */
050    public static final String DEFAULT_HOST = "whois.internic.net";
051
052    /**
053     * The default whois port. It is set to 43 according to RFC 954.
054     */
055    public static final int DEFAULT_PORT = 43;
056
057    /**
058     * The default whois constructor. Initializes the default port to <code>DEFAULT_PORT</code>.
059     */
060    public WhoisClient() {
061        setDefaultPort(DEFAULT_PORT);
062    }
063
064    /**
065     * Queries the connected whois server for information regarding the given handle and returns the InputStream of the network connection. It is up to the
066     * 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
067     * should disconnect after finishing reading the stream.
068     *
069     * @param handle The handle to lookup.
070     * @return The InputStream of the network connection of the whois query. Can be read to obtain whois results.
071     * @throws IOException If an I/O error occurs during the operation.
072     */
073    public InputStream getInputStream(final String handle) throws IOException {
074        return getInputStream(false, handle);
075    }
076
077    /**
078     * 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
079     * whois server. You must first connect to a whois server before calling this method, and you should disconnect afterward.
080     *
081     * @param handle The handle to lookup.
082     * @return The result of the whois query.
083     * @throws IOException If an I/O error occurs during the operation.
084     */
085    public String query(final String handle) throws IOException {
086        return query(false, handle);
087    }
088
089}