1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.network.util;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
18  import com.liferay.portal.kernel.util.StringBundler;
19  import com.liferay.portal.kernel.util.StringUtil;
20  import com.liferay.portal.kernel.util.Time;
21  import com.liferay.portal.kernel.webcache.WebCacheException;
22  import com.liferay.portal.kernel.webcache.WebCacheItem;
23  import com.liferay.portlet.network.model.Whois;
24  
25  import java.io.InputStreamReader;
26  import java.io.PrintStream;
27  
28  import java.net.Socket;
29  
30  /**
31   * <a href="WhoisWebCacheItem.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public class WhoisWebCacheItem implements WebCacheItem {
36  
37      public static String WHOIS_SERVER = "whois.geektools.com";
38  
39      public static int WHOIS_SERVER_PORT = 43;
40  
41      public WhoisWebCacheItem(String domain) {
42          _domain = domain;
43      }
44  
45      public Object convert(String key) throws WebCacheException {
46          Whois whois = null;
47  
48          try {
49              Socket socket = new Socket(WHOIS_SERVER, WHOIS_SERVER_PORT);
50  
51              UnsyncBufferedReader unsyncBufferedReader =
52                  new UnsyncBufferedReader(
53                      new InputStreamReader(socket.getInputStream()));
54  
55              PrintStream out = new PrintStream(socket.getOutputStream());
56  
57              out.println(_domain);
58  
59              StringBundler sb = new StringBundler();
60              String line = null;
61  
62              while ((line = unsyncBufferedReader.readLine()) != null) {
63                  if (line.startsWith("Results ")) {
64                      break;
65                  }
66  
67                  sb.append(line).append("\n");
68              }
69  
70              unsyncBufferedReader.close();
71              socket.close();
72  
73              whois = new Whois(
74                  _domain,
75                  StringUtil.replace(sb.toString().trim(), "\n\n", "\n"));
76          }
77          catch (Exception e) {
78              throw new WebCacheException(_domain + " " + e.toString());
79          }
80  
81          return whois;
82      }
83  
84      public long getRefreshTime() {
85          return _REFRESH_TIME;
86      }
87  
88      private static final long _REFRESH_TIME = Time.DAY;
89  
90      private String _domain;
91  
92  }