Java tutorial
/** * Copyright (c) 2001-2012 "Redbasin Networks, INC" [http://redbasin.org] * * This file is part of Redbasin OpenDocShare community project. * * Redbasin OpenDocShare is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package util; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Cookie; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.regex.Pattern; import java.util.regex.Matcher; import java.net.URLConnection; import java.net.URL; import java.util.List; import java.util.ArrayList; import java.util.StringTokenizer; import java.io.InputStream; /** * * @author Smitha Gudur * @version $Revision: 1.1 $ */ public class HttpUrlUtils { /** Logger for this class and subclasses */ protected final Log logger = LogFactory.getLog("util.HttpUtil"); private final String GEO_URL = "http://www.geobytes.com/IpLocator.htm?GetLocation&ipaddress="; public String getContent(String url) throws HttpErrException { byte[] buf = new byte[1024]; byte[] result = new byte[0]; int length = 0; try { ContentStream contentStream = new ContentStream(); Thread urlThread = new URLThread(contentStream, url); urlThread.start(); URLConnection conn = contentStream.get(); try { urlThread.interrupt(); } catch (Exception e) { logger.info(e.getMessage()); throw new HttpErrException("Could not get the content of the URL: ", e); } if (conn == null) return null; InputStream is = conn.getInputStream(); while ((length = is.read(buf)) != -1) { byte[] temp = new byte[result.length + length]; System.arraycopy(result, 0, temp, 0, result.length); System.arraycopy(buf, 0, temp, result.length, length); result = temp; } } catch (Exception e) { throw new HttpErrException("Could not get the content of the URL: " + url.toString(), e); } logger.info("content size (in bytes) = " + result.length); return new String(result); } public boolean isAllowed(String ipAddress, List geoDataList) { logger.info("ipAddress = " + ipAddress); if (ipAddress.startsWith("192.168.0")) return true; String geoData = getContent(GEO_URL + ipAddress); geoDataList.add(geoData); if (geoData == null) { return false; } return (!(geoData.toLowerCase().contains("california")) && !(geoData.toLowerCase().contains("new york"))); } /** * Parses an IP4 ip address into A, B, C, D. * * @param ip4 ip address * @return List; */ public List parseIP4(String ip4) { StringTokenizer st = new StringTokenizer(ip4, "."); List iplist = new ArrayList(); while (st.hasMoreTokens()) { iplist.add(st.nextToken()); } return iplist; } } class ContentStream { private URLConnection conn; private boolean available = false; private final int MAX_WAIT = 5000; protected final Log logger = LogFactory.getLog("util.HttpUtil"); public synchronized URLConnection get() { long ctime = System.currentTimeMillis(); while ((available == false) && ((System.currentTimeMillis() - ctime) < MAX_WAIT)) { try { // wait for Producer to put value logger.info("Consumer waiting..."); wait(MAX_WAIT); logger.info("Consumer timed out..."); } catch (InterruptedException e) { throw new HttpErrException("UrlConnection error", e); } } available = false; // notify Producer that value has been retrieved notifyAll(); return conn; } public synchronized void put(URLConnection value) { while (available == true) { try { logger.info("Producer waiting..."); // wait for Consumer to get value wait(MAX_WAIT); logger.info("Producer timed out..."); } catch (InterruptedException e) { throw new HttpErrException("Thread error in put", e); } } logger.info("Producer putting value " + value); conn = value; available = true; // notify Consumer that value has been set notifyAll(); } } class URLThread extends Thread { private ContentStream contentStream; private String url; protected final Log logger = LogFactory.getLog("util.HttpUtil"); public URLThread(ContentStream contentStream, String url) { this.contentStream = contentStream; this.url = url; logger.info("Num Active Threads: " + activeCount()); } public void run() { try { logger.info("Opening connection: " + url); URLConnection conn = new URL(url).openConnection(); logger.info("Calling put: " + url); contentStream.put(conn); logger.info("Opened connection: " + url); } catch (Exception e) { throw new HttpErrException("Thread error in content stream", e); } logger.info("run method exiting."); this.contentStream = null; } }