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