Skip to content

Commit

Permalink
Added methods to check database type and date
Browse files Browse the repository at this point in the history
  • Loading branch information
ip2location committed Nov 15, 2024
1 parent 4e1b317 commit 157cc39
Show file tree
Hide file tree
Showing 9 changed files with 2,622 additions and 2,585 deletions.
160 changes: 80 additions & 80 deletions com/ip2location/Country.java
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;
}

}
70 changes: 35 additions & 35 deletions com/ip2location/Http.java
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);
}
}

}
Loading

0 comments on commit 157cc39

Please sign in to comment.