-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added methods to check database type and date
- Loading branch information
1 parent
4e1b317
commit 157cc39
Showing
9 changed files
with
2,622 additions
and
2,585 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,81 @@ | ||
package com.ip2location; | ||
|
||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.io.File; | ||
import com.opencsv.*; | ||
import com.opencsv.exceptions.*; | ||
|
||
/** | ||
* This class parses country information CSV and returns the country information. | ||
* <p> | ||
* Copyright (c) 2002-2023 IP2Location.com | ||
* <p> | ||
* | ||
* @author IP2Location.com | ||
* @version 8.11.2 | ||
*/ | ||
public class Country { | ||
private final Map<String, Map<String, String>> records = new HashMap<>(); | ||
|
||
/** | ||
* This constructor reads the country information CSV and store the parsed info. | ||
* | ||
* @param CSVFile The full path to the country information CSV file. | ||
*/ | ||
public Country(String CSVFile) throws IOException, CsvValidationException { | ||
Map<String, String> line; | ||
File file = new File(CSVFile); | ||
if (!file.exists()) { | ||
throw new IOException("The CSV file '" + CSVFile + "' is not found."); | ||
} | ||
FileReader fr = new FileReader(file); | ||
if (fr.read() == -1) { | ||
throw new IOException("Unable to read '" + CSVFile + "'."); | ||
} | ||
CSVReaderHeaderAware reader = new CSVReaderHeaderAware(new FileReader(file)); | ||
while ((line = reader.readMap()) != null) { | ||
if (line.containsKey("country_code")) { | ||
records.put(line.get("country_code"), line); | ||
} else { | ||
throw new IOException("Invalid country information CSV file."); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* This function gets the country information for the supplied country code. | ||
* | ||
* @param CountryCode ISO-3166 country code | ||
* @return Map | ||
*/ | ||
public Map<String, String> GetCountryInfo(String CountryCode) throws IOException { | ||
if (records.isEmpty()) { | ||
throw new IOException("No record available."); | ||
} else { | ||
return records.getOrDefault(CountryCode, null); | ||
} | ||
} | ||
|
||
/** | ||
* This function gets the country information for all countries. | ||
* | ||
* @return List | ||
*/ | ||
public List<Map<String, String>> GetCountryInfo() throws IOException { | ||
List<Map<String, String>> results = new ArrayList<>(); | ||
if (records.isEmpty()) { | ||
throw new IOException("No record available."); | ||
} else { | ||
for (Map.Entry<String, Map<String, String>> entry : records.entrySet()) { | ||
results.add(entry.getValue()); | ||
} | ||
} | ||
return results; | ||
} | ||
|
||
package com.ip2location; | ||
|
||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.io.File; | ||
import com.opencsv.*; | ||
import com.opencsv.exceptions.*; | ||
|
||
/** | ||
* This class parses country information CSV and returns the country information. | ||
* <p> | ||
* Copyright (c) 2002-2024 IP2Location.com | ||
* <p> | ||
* | ||
* @author IP2Location.com | ||
* @version 8.12.0 | ||
*/ | ||
public class Country { | ||
private final Map<String, Map<String, String>> records = new HashMap<>(); | ||
|
||
/** | ||
* This constructor reads the country information CSV and store the parsed info. | ||
* | ||
* @param CSVFile The full path to the country information CSV file. | ||
*/ | ||
public Country(String CSVFile) throws IOException, CsvValidationException { | ||
Map<String, String> line; | ||
File file = new File(CSVFile); | ||
if (!file.exists()) { | ||
throw new IOException("The CSV file '" + CSVFile + "' is not found."); | ||
} | ||
FileReader fr = new FileReader(file); | ||
if (fr.read() == -1) { | ||
throw new IOException("Unable to read '" + CSVFile + "'."); | ||
} | ||
CSVReaderHeaderAware reader = new CSVReaderHeaderAware(new FileReader(file)); | ||
while ((line = reader.readMap()) != null) { | ||
if (line.containsKey("country_code")) { | ||
records.put(line.get("country_code"), line); | ||
} else { | ||
throw new IOException("Invalid country information CSV file."); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* This function gets the country information for the supplied country code. | ||
* | ||
* @param CountryCode ISO-3166 country code | ||
* @return Map | ||
*/ | ||
public Map<String, String> GetCountryInfo(String CountryCode) throws IOException { | ||
if (records.isEmpty()) { | ||
throw new IOException("No record available."); | ||
} else { | ||
return records.getOrDefault(CountryCode, null); | ||
} | ||
} | ||
|
||
/** | ||
* This function gets the country information for all countries. | ||
* | ||
* @return List | ||
*/ | ||
public List<Map<String, String>> GetCountryInfo() throws IOException { | ||
List<Map<String, String>> results = new ArrayList<>(); | ||
if (records.isEmpty()) { | ||
throw new IOException("No record available."); | ||
} else { | ||
for (Map.Entry<String, Map<String, String>> entry : records.entrySet()) { | ||
results.add(entry.getValue()); | ||
} | ||
} | ||
return results; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,35 @@ | ||
package com.ip2location; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
|
||
class Http { | ||
public static String get(URL url) { | ||
try { | ||
java.lang.System.setProperty("https.protocols", "TLSv1.2"); | ||
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | ||
conn.setRequestMethod("GET"); | ||
conn.setRequestProperty("Accept", "application/json"); | ||
|
||
if (conn.getResponseCode() != 200) { | ||
return ("Failed : HTTP error code : " + conn.getResponseCode()); | ||
} | ||
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); | ||
|
||
String output; | ||
StringBuilder resultFromHttp = new StringBuilder(); | ||
while ((output = br.readLine()) != null) { | ||
resultFromHttp.append(output).append("\n"); | ||
} | ||
|
||
br.close(); | ||
conn.disconnect(); | ||
return resultFromHttp.toString(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
} | ||
package com.ip2location; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
|
||
class Http { | ||
public static String get(URL url) { | ||
try { | ||
java.lang.System.setProperty("https.protocols", "TLSv1.2"); | ||
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | ||
conn.setRequestMethod("GET"); | ||
conn.setRequestProperty("Accept", "application/json"); | ||
|
||
if (conn.getResponseCode() != 200) { | ||
return ("Failed : HTTP error code : " + conn.getResponseCode()); | ||
} | ||
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); | ||
|
||
String output; | ||
StringBuilder resultFromHttp = new StringBuilder(); | ||
while ((output = br.readLine()) != null) { | ||
resultFromHttp.append(output).append("\n"); | ||
} | ||
|
||
br.close(); | ||
conn.disconnect(); | ||
return resultFromHttp.toString(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.