001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }