1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.network.util;
24  
25  import com.liferay.portal.kernel.util.StringMaker;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.util.WebCacheable;
28  import com.liferay.portlet.network.model.Whois;
29  import com.liferay.util.ConverterException;
30  import com.liferay.util.Time;
31  
32  import java.io.BufferedReader;
33  import java.io.InputStreamReader;
34  import java.io.PrintStream;
35  
36  import java.net.Socket;
37  
38  /**
39   * <a href="WhoisConverter.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class WhoisConverter implements WebCacheable {
45  
46      public static String WHOIS_SERVER = "whois.geektools.com";
47  
48      public static int WHOIS_SERVER_PORT = 43;
49  
50      public WhoisConverter(String domain) {
51          _domain = domain;
52      }
53  
54      public Object convert(String id) throws ConverterException {
55          Whois whois = null;
56  
57          try {
58              Socket socket = new Socket(WHOIS_SERVER, WHOIS_SERVER_PORT);
59  
60              BufferedReader br = new BufferedReader(
61                  new InputStreamReader(socket.getInputStream()));
62  
63              PrintStream out = new PrintStream(socket.getOutputStream());
64  
65              out.println(_domain);
66  
67              StringMaker sm = new StringMaker();
68              String line = null;
69  
70              while ((line = br.readLine()) != null) {
71                  if (line.startsWith("Results ")) {
72                      break;
73                  }
74  
75                  sm.append(line).append("\n");
76              }
77  
78              br.close();
79              socket.close();
80  
81              whois = new Whois(
82                  _domain,
83                  StringUtil.replace(sm.toString().trim(), "\n\n", "\n"));
84          }
85          catch (Exception e) {
86              throw new ConverterException(_domain + " " + e.toString());
87          }
88  
89          return whois;
90      }
91  
92      public long getRefreshTime() {
93          return _REFRESH_TIME;
94      }
95  
96      private static final long _REFRESH_TIME = Time.DAY;
97  
98      private String _domain;
99  
100 }