1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.network.util;
21  
22  import com.liferay.portal.kernel.util.StringUtil;
23  import com.liferay.portal.kernel.util.Time;
24  import com.liferay.portal.kernel.webcache.WebCacheException;
25  import com.liferay.portal.kernel.webcache.WebCacheItem;
26  import com.liferay.portlet.network.model.Whois;
27  
28  import java.io.BufferedReader;
29  import java.io.InputStreamReader;
30  import java.io.PrintStream;
31  
32  import java.net.Socket;
33  
34  /**
35   * <a href="WhoisWebCacheItem.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class WhoisWebCacheItem implements WebCacheItem {
41  
42      public static String WHOIS_SERVER = "whois.geektools.com";
43  
44      public static int WHOIS_SERVER_PORT = 43;
45  
46      public WhoisWebCacheItem(String domain) {
47          _domain = domain;
48      }
49  
50      public Object convert(String key) throws WebCacheException {
51          Whois whois = null;
52  
53          try {
54              Socket socket = new Socket(WHOIS_SERVER, WHOIS_SERVER_PORT);
55  
56              BufferedReader br = new BufferedReader(
57                  new InputStreamReader(socket.getInputStream()));
58  
59              PrintStream out = new PrintStream(socket.getOutputStream());
60  
61              out.println(_domain);
62  
63              StringBuilder sb = new StringBuilder();
64              String line = null;
65  
66              while ((line = br.readLine()) != null) {
67                  if (line.startsWith("Results ")) {
68                      break;
69                  }
70  
71                  sb.append(line).append("\n");
72              }
73  
74              br.close();
75              socket.close();
76  
77              whois = new Whois(
78                  _domain,
79                  StringUtil.replace(sb.toString().trim(), "\n\n", "\n"));
80          }
81          catch (Exception e) {
82              throw new WebCacheException(_domain + " " + e.toString());
83          }
84  
85          return whois;
86      }
87  
88      public long getRefreshTime() {
89          return _REFRESH_TIME;
90      }
91  
92      private static final long _REFRESH_TIME = Time.DAY;
93  
94      private String _domain;
95  
96  }