Java tutorial
/** * Copyright 2010 the original author or authors. * * This file is part of Zksample2. http://zksample2.sourceforge.net/ * * Zksample2 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. * * Zksample2 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 Zksample2. If not, see <http://www.gnu.org/licenses/gpl.html>. */ package org.homemotion.util.impl; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; import javax.inject.Singleton; import org.apache.commons.lang.StringUtils; import org.homemotion.util.IpLocation; import org.homemotion.util.IpLocator; /** * The IPLocator is a java wrapper for the hostip.info ip locator web service. * * @author <a href="mailto:pillvin@iit.edu">Vinod Pillai</a> * @version $Revision: 1.0 $ */ @Singleton public class IpLocatorImpl implements IpLocator { private static final String HOSTIP_LOOKUP_URL = "http://api.hostip.info/get_html.php?position=true&ip="; private static final String KEY_COUNTRY = "COUNTRY"; private static final String KEY_CITY = "CITY"; private static final String KEY_LATITUDE = "LATITUDE"; private static final String KEY_LONGITUDE = "LONGITUDE"; /** * Other than the getters & setters, this is the only method visible to the * outside world * * @param ip * The ip address to be located * @return IPLocator instance * @throws IOException * in case of any error/exception */ public IpLocation locate(String ip) throws IOException { final String url = HOSTIP_LOOKUP_URL + ip; final URL u = new URL(url); final List<String> response = getContent(u); final Pattern splitterPattern = Pattern.compile(":"); final IpLocation ipl = new IpLocation(); for (final String token : response) { final String[] keyValue = splitterPattern.split(token); if (keyValue.length != 2) { continue; } final String key = StringUtils.upperCase(keyValue[0]); final String value = keyValue[1]; if (KEY_COUNTRY.equals(key)) { ipl.setCountry(value); } else if (KEY_CITY.equals(key)) { ipl.setCity(value); } else if (KEY_LATITUDE.equals(key)) { ipl.setLatitude(stringToFloat(value)); } else if (KEY_LONGITUDE.equals(key)) { ipl.setLongitude(stringToFloat(value)); } } return ipl; } private float stringToFloat(String fString) { try { return Float.parseFloat(fString); } catch (final NumberFormatException e) { return -1; } } /** * Gets the content for a given url. This method makes a connection, gets * the response from the url. * * A RuntimeException is throws is the status code of the response is not * 200. * * @param url * The url to open. * @return HTML response * @throws IOException */ private List<String> getContent(URL url) throws IOException { final HttpURLConnection http = (HttpURLConnection) url.openConnection(); try { http.connect(); final int code = http.getResponseCode(); if (code != 200) throw new IOException( "IP Locator failed to get the location. Http Status code : " + code + " [" + url + "]"); return getContent(http); } finally { http.disconnect(); } } /** * Gets the content for a given HttpURLConnection. * * @param connection * Http URL Connection. * @return HTML response * @exception IOException */ private List<String> getContent(HttpURLConnection connection) throws IOException { final InputStream in = connection.getInputStream(); try { final List<String> result = new ArrayList<String>(5); final InputStreamReader isr = new InputStreamReader(in); final BufferedReader bufRead = new BufferedReader(isr); String aLine = null; while (null != (aLine = bufRead.readLine())) { result.add(aLine.trim()); } bufRead.close(); return result; } finally { try { in.close(); } catch (final IOException e) { } } } /** * For unit testing purposes only * * @param args */ public static void main(String args[]) { try { final IpLocation ipl = new IpLocatorImpl().locate("12.215.42.19"); System.out.println("City=" + ipl.getCity()); System.out.println("Country=" + ipl.getCountry()); System.out.println("CountryCode=" + ipl.getCountryCode()); System.out.println("Latitude=" + ipl.getLatitude()); System.out.println("Longitude=" + ipl.getLongitude()); } catch (final Exception e) { System.err.println(Thread.currentThread().getName() + " FEHLER - " + e); e.printStackTrace(); } } }