    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 *      https://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    /**
    049     * The default whois host to query. It is set to whois.internic.net.
    050     */
    051    public static final String DEFAULT_HOST = "whois.internic.net";
    052
    053    /**
    054     * The default whois port. It is set to 43 according to RFC 954.
    055     */
    056    public static final int DEFAULT_PORT = 43;
    057
    058    /**
    059     * The default whois constructor. Initializes the default port to {@code DEFAULT_PORT}.
    060     */
    061    public WhoisClient() {
    062        setDefaultPort(DEFAULT_PORT);
    063    }
    064
    065    /**
    066     * Queries the connected whois server for information regarding the given handle and returns the InputStream of the network connection. It is up to the
    067     * 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
    068     * should disconnect after finishing reading the stream.
    069     *
    070     * @param handle The handle to lookup.
    071     * @return The InputStream of the network connection of the whois query. Can be read to obtain whois results.
    072     * @throws IOException If an I/O error occurs during the operation.
    073     */
    074    public InputStream getInputStream(final String handle) throws IOException {
    075        return getInputStream(false, handle);
    076    }
    077
    078    /**
    079     * 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
    080     * whois server. You must first connect to a whois server before calling this method, and you should disconnect afterward.
    081     *
    082     * @param handle The handle to lookup.
    083     * @return The result of the whois query.
    084     * @throws IOException If an I/O error occurs during the operation.
    085     */
    086    public String query(final String handle) throws IOException {
    087        return query(false, handle);
    088    }
    089
    090}