001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.network.util;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.kernel.util.Time;
021    import com.liferay.portal.kernel.webcache.WebCacheException;
022    import com.liferay.portal.kernel.webcache.WebCacheItem;
023    import com.liferay.portlet.network.model.Whois;
024    
025    import java.io.InputStreamReader;
026    import java.io.PrintStream;
027    
028    import java.net.Socket;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class WhoisWebCacheItem implements WebCacheItem {
034    
035            public static final String WHOIS_SERVER = "whois.geektools.com";
036    
037            public static final int WHOIS_SERVER_PORT = 43;
038    
039            public WhoisWebCacheItem(String domain) {
040                    _domain = domain;
041            }
042    
043            @Override
044            public Object convert(String key) throws WebCacheException {
045                    Whois whois = null;
046    
047                    try {
048                            Socket socket = new Socket(WHOIS_SERVER, WHOIS_SERVER_PORT);
049    
050                            UnsyncBufferedReader unsyncBufferedReader =
051                                    new UnsyncBufferedReader(
052                                            new InputStreamReader(socket.getInputStream()));
053    
054                            PrintStream out = new PrintStream(socket.getOutputStream());
055    
056                            out.println(_domain);
057    
058                            StringBundler sb = new StringBundler();
059                            String line = null;
060    
061                            while ((line = unsyncBufferedReader.readLine()) != null) {
062                                    if (line.startsWith("Results ")) {
063                                            break;
064                                    }
065    
066                                    sb.append(line).append("\n");
067                            }
068    
069                            unsyncBufferedReader.close();
070                            socket.close();
071    
072                            whois = new Whois(
073                                    _domain,
074                                    StringUtil.replace(sb.toString().trim(), "\n\n", "\n"));
075                    }
076                    catch (Exception e) {
077                            throw new WebCacheException(_domain + " " + e.toString());
078                    }
079    
080                    return whois;
081            }
082    
083            @Override
084            public long getRefreshTime() {
085                    return _REFRESH_TIME;
086            }
087    
088            private static final long _REFRESH_TIME = Time.DAY;
089    
090            private String _domain;
091    
092    }