1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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  }