001    /**
002     * Copyright (c) 2000-2010 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 String WHOIS_SERVER = "whois.geektools.com";
036    
037            public static int WHOIS_SERVER_PORT = 43;
038    
039            public WhoisWebCacheItem(String domain) {
040                    _domain = domain;
041            }
042    
043            public Object convert(String key) throws WebCacheException {
044                    Whois whois = null;
045    
046                    try {
047                            Socket socket = new Socket(WHOIS_SERVER, WHOIS_SERVER_PORT);
048    
049                            UnsyncBufferedReader unsyncBufferedReader =
050                                    new UnsyncBufferedReader(
051                                            new InputStreamReader(socket.getInputStream()));
052    
053                            PrintStream out = new PrintStream(socket.getOutputStream());
054    
055                            out.println(_domain);
056    
057                            StringBundler sb = new StringBundler();
058                            String line = null;
059    
060                            while ((line = unsyncBufferedReader.readLine()) != null) {
061                                    if (line.startsWith("Results ")) {
062                                            break;
063                                    }
064    
065                                    sb.append(line).append("\n");
066                            }
067    
068                            unsyncBufferedReader.close();
069                            socket.close();
070    
071                            whois = new Whois(
072                                    _domain,
073                                    StringUtil.replace(sb.toString().trim(), "\n\n", "\n"));
074                    }
075                    catch (Exception e) {
076                            throw new WebCacheException(_domain + " " + e.toString());
077                    }
078    
079                    return whois;
080            }
081    
082            public long getRefreshTime() {
083                    return _REFRESH_TIME;
084            }
085    
086            private static final long _REFRESH_TIME = Time.DAY;
087    
088            private String _domain;
089    
090    }