| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
package net.sourceforge.pebble.web.action; |
| 33 | |
|
| 34 | |
import com.maxmind.geoip.LookupService; |
| 35 | |
import net.sourceforge.pebble.logging.Log; |
| 36 | |
import net.sourceforge.pebble.logging.LogEntry; |
| 37 | |
import net.sourceforge.pebble.logging.Request; |
| 38 | |
import net.sourceforge.pebble.web.view.View; |
| 39 | |
import net.sourceforge.pebble.web.view.impl.CountriesView; |
| 40 | |
import net.sourceforge.pebble.domain.Blog; |
| 41 | |
import net.sourceforge.pebble.Constants; |
| 42 | |
|
| 43 | |
import javax.servlet.ServletException; |
| 44 | |
import javax.servlet.http.HttpServletRequest; |
| 45 | |
import javax.servlet.http.HttpServletResponse; |
| 46 | |
import java.io.IOException; |
| 47 | |
import java.util.*; |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | 0 | public class ViewCountriesAction extends AbstractLogAction { |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
public View process(HttpServletRequest request, HttpServletResponse response) throws ServletException { |
| 64 | 0 | Blog blog = (Blog)getModel().get(Constants.BLOG_KEY); |
| 65 | 0 | Log log = getLog(request, response); |
| 66 | |
|
| 67 | 0 | Set<String> countries = new TreeSet<String>(new Comparator<String>() { |
| 68 | |
public int compare(String s1, String s2) { |
| 69 | 0 | return s1 != null ? s1.compareToIgnoreCase(s2) : -1; |
| 70 | |
} |
| 71 | |
}); |
| 72 | 0 | Map<String,Integer> consolidatedCountries = new HashMap<String,Integer>(); |
| 73 | 0 | Map<String,Integer> countriesForNewsFeeds = new HashMap<String,Integer>(); |
| 74 | 0 | Map<String,Integer> countriesForPageViews = new HashMap<String,Integer>(); |
| 75 | 0 | Map<String,Integer> countriesForFileDownloads = new HashMap<String,Integer>(); |
| 76 | |
|
| 77 | 0 | LookupService lookupService = null; |
| 78 | |
|
| 79 | |
try { |
| 80 | 0 | String filename = getClass().getResource("/geo-ip.dat").toExternalForm().substring(5); |
| 81 | 0 | lookupService = new LookupService(filename, LookupService.GEOIP_MEMORY_CACHE); |
| 82 | |
|
| 83 | 0 | for (LogEntry logEntry : log.getLogEntries()) { |
| 84 | 0 | String country = lookupService.getCountry(logEntry.getHost()).getName(); |
| 85 | 0 | countries.add(country); |
| 86 | 0 | register(country, countriesForNewsFeeds); |
| 87 | 0 | register(country, countriesForPageViews); |
| 88 | 0 | register(country, countriesForFileDownloads); |
| 89 | 0 | register(country, consolidatedCountries); |
| 90 | |
|
| 91 | 0 | Request req = new Request(logEntry.getRequestUri(), blog); |
| 92 | 0 | if (req.isNewsFeed()) { |
| 93 | 0 | increment(country, countriesForNewsFeeds); |
| 94 | 0 | increment(country, consolidatedCountries); |
| 95 | 0 | } else if (req.isPageView()) { |
| 96 | 0 | increment(country, countriesForPageViews); |
| 97 | 0 | increment(country, consolidatedCountries); |
| 98 | 0 | } else if (req.isFileDownload()) { |
| 99 | 0 | increment(country, countriesForFileDownloads); |
| 100 | 0 | increment(country, consolidatedCountries); |
| 101 | |
} |
| 102 | 0 | } |
| 103 | 0 | } catch (IOException ioe) { |
| 104 | 0 | throw new ServletException(ioe); |
| 105 | |
} finally { |
| 106 | 0 | if (lookupService != null) { |
| 107 | 0 | lookupService.close(); |
| 108 | |
} |
| 109 | |
} |
| 110 | |
|
| 111 | 0 | getModel().put("logAction", "viewCountries"); |
| 112 | 0 | getModel().put("countries", countries); |
| 113 | 0 | getModel().put("consolidatedCountries", consolidatedCountries); |
| 114 | 0 | getModel().put("countriesForNewsFeeds", countriesForNewsFeeds); |
| 115 | 0 | getModel().put("countriesForPageViews", countriesForPageViews); |
| 116 | 0 | getModel().put("countriesForFileDownloads", countriesForFileDownloads); |
| 117 | |
|
| 118 | 0 | return new CountriesView(); |
| 119 | |
} |
| 120 | |
|
| 121 | |
private void register(String country, Map<String,Integer> map) { |
| 122 | 0 | Integer count = map.get(country); |
| 123 | 0 | if (count == null) { |
| 124 | 0 | count = 0; |
| 125 | |
} |
| 126 | 0 | map.put(country, count); |
| 127 | 0 | } |
| 128 | |
|
| 129 | |
private void increment(String country, Map<String,Integer> map) { |
| 130 | 0 | Integer count = map.get(country); |
| 131 | 0 | count = count + 1; |
| 132 | 0 | map.put(country, count); |
| 133 | 0 | } |
| 134 | |
|
| 135 | |
} |