Skip to content

Commit

Permalink
Tests updated for the last version of DB-IP file( Feb version) , Supp…
Browse files Browse the repository at this point in the history
…ort added for IPV6 and tests included for IPV6 , country code added to GeoEntity.
  • Loading branch information
ankushs92 committed Feb 8, 2017
1 parent 62d24bd commit 3278353
Show file tree
Hide file tree
Showing 12 changed files with 323 additions and 102 deletions.
3 changes: 2 additions & 1 deletion src/main/java/in/ankushs/dbip/api/DbIpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

Expand Down Expand Up @@ -90,5 +92,4 @@ public GeoEntity lookup(final InetAddress inetAddress){
PreConditions.checkNull(inetAddress, "inetAddress cannot be null");
return lookupService.lookup(inetAddress);
}

}
24 changes: 20 additions & 4 deletions src/main/java/in/ankushs/dbip/api/GeoEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,25 @@ public final class GeoEntity {
private final String city;
private final String country;
private final String province;

private final String countryCode;

public GeoEntity(final Builder builder){
this.city = builder.city;
this.country = builder.country;
this.province = builder.province;
this.countryCode = builder.countryCode;
}

public static class Builder{
private String countryCode;
private String city;
private String country;
private String province;

public Builder withCountryCode(final String countryCode){
this.countryCode = countryCode;
return this;
}

public Builder withCity(final String city ){
this.city = city;
Expand Down Expand Up @@ -54,10 +62,18 @@ public String getProvince() {
return province;
}


public String getCountryCode() {
return countryCode;
}

@Override
public String toString() {
return "GeoEntity [city=" + city + ", country=" + country + ", province=" + province + "]";
return "GeoEntity{" +
"city='" + city + '\'' +
", country='" + country + '\'' +
", province='" + province + '\'' +
", countryCode='" + countryCode + '\'' +
'}';
}


}
23 changes: 14 additions & 9 deletions src/main/java/in/ankushs/dbip/importer/ResourceImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,30 +63,35 @@ public void load(final File file) {
logger.error("",ex);
}

try (InputStream fis = new FileInputStream(file);
InputStream gis = new GZIPInputStream(fis);
Reader decorator = new InputStreamReader(gis, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(decorator);)
try (final InputStream fis = new FileInputStream(file);
final InputStream gis = new GZIPInputStream(fis);
final Reader decorator = new InputStreamReader(gis, StandardCharsets.UTF_8);
final BufferedReader reader = new BufferedReader(decorator);
)
{
logger.debug("Reading dbip data from {}", file.getName());
String line = null;
int i = 0;
while ((line = reader.readLine()) != null) {
i++;
final String[] array = csvParser.parseRecord(line);
final GeoAttributes geoAttributes = new GeoAttributesImpl.Builder()
.withCity(interner.intern(array[4]))
final GeoAttributes geoAttributes = new GeoAttributesImpl
.Builder()
.withStartInetAddress(InetAddresses.forString(array[0]))
.withEndInetAddress(InetAddresses.forString(array[1]))
.withCountryCode(array[2])
.withCountry(CountryResolver.resolveToFullName(array[2]))
.withProvince(interner.intern(array[3]))
.withEndIp(InetAddresses.coerceToInteger(InetAddresses.forString(array[1])))
.withStartIp(InetAddresses.coerceToInteger(InetAddresses.forString(array[0])))
.withCity(interner.intern(array[4]))
.build();
repository.save(geoAttributes);
if (i % 100000 == 0) {
logger.debug("Loaded {} entries", i);
}
}
} catch (final Exception e) {
}

catch (final Exception e) {
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package in.ankushs.dbip.lookup;

import java.io.File;
import java.net.InetAddress;

import in.ankushs.dbip.api.DbIpClient;
import in.ankushs.dbip.api.GeoEntity;
import in.ankushs.dbip.repository.DbIpRepository;
import in.ankushs.dbip.repository.JavaMapDbIpRepositoryImpl;
Expand Down Expand Up @@ -33,14 +35,14 @@ public GeoEntity lookup(final InetAddress inetAddress) {
PreConditions.checkNull(inetAddress, "inetAddress cannot be null ");
GeoEntity geoEntity = repository.get(inetAddress);
if( geoEntity == null ){
geoEntity = new GeoEntity.Builder()
.withCity(UNKNOWN).withCountry(UNKNOWN)
.withProvince(UNKNOWN).build();
geoEntity = new GeoEntity
.Builder()
.withCity(UNKNOWN)
.withCountry(UNKNOWN)
.withCountryCode(UNKNOWN)
.withProvince(UNKNOWN)
.build();
}
return geoEntity;
}

public static void main(String[] args) {
GeoEntityLookupService g1 = getInstance();
}
}
7 changes: 5 additions & 2 deletions src/main/java/in/ankushs/dbip/model/GeoAttributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import in.ankushs.dbip.api.GeoEntity;

public interface GeoAttributes {
int getStartIp();
int getEndIp();

InetAddress getStartInetAddress();

InetAddress getEndInetAddress();

GeoEntity getGeoEntity();
}
55 changes: 35 additions & 20 deletions src/main/java/in/ankushs/dbip/model/GeoAttributesImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,48 @@
import in.ankushs.dbip.api.GeoEntity;

public final class GeoAttributesImpl implements GeoAttributes {
private final int startIp;
private final int endIp;

private final String city;
private final String country;
private final String province ;
private final String countryCode;
private final InetAddress startInetAddress;
private final InetAddress endInetAddress;


private GeoAttributesImpl(final Builder builder){
this.startIp = builder.startIp;
this.endIp = builder.endIp;
this.startInetAddress = builder.startInetAddress;
this.endInetAddress = builder.endInetAddress;
this.city = builder.city;
this.country = builder.country;
this.province = builder.province;
this.countryCode = builder.countryCode;
}

public static class Builder{
private int startIp;
private int endIp;
private InetAddress startInetAddress;
private InetAddress endInetAddress;
private String city;
private String country;
private String province ;

public Builder withStartIp(final int startIp){
this.startIp = startIp;
private String countryCode;


public Builder withStartInetAddress(final InetAddress startInetAddress){
this.startInetAddress = startInetAddress;
return this;
}
public Builder withEndIp(final int endIp){
this.endIp = endIp;

public Builder withCountryCode(final String countryCode){
this.countryCode = countryCode;
return this;
}


public Builder withEndInetAddress(final InetAddress endInetAddress){
this.endInetAddress = endInetAddress;
return this;
}


public Builder withCity(final String city){
this.city = city;
return this;
Expand All @@ -59,21 +69,26 @@ public GeoAttributesImpl build(){
}
}



@Override
public int getStartIp() {
return startIp;
public InetAddress getStartInetAddress() {
return startInetAddress;
}

@Override
public int getEndIp() {
return endIp;
public InetAddress getEndInetAddress() {
return endInetAddress;
}

@Override
public GeoEntity getGeoEntity() {
return new GeoEntity.Builder()
.withCity(city).withCountry(country)
.withProvince(province).build();
.withCity(city)
.withCountry(country)
.withCountryCode(countryCode)
.withProvince(province)
.build();
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package in.ankushs.dbip.repository;

import java.math.BigInteger;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.util.TreeMap;
import java.util.concurrent.Executor;
Expand All @@ -9,14 +12,19 @@

import in.ankushs.dbip.api.GeoEntity;
import in.ankushs.dbip.model.GeoAttributes;
import in.ankushs.dbip.utils.IPUtils;
import in.ankushs.dbip.utils.PreConditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
* Singletonclass that uses a <a href="https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html">TreeMap</a>
* Singletonthat uses a <a href="https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html">TreeMap</a>
* as repository.
* @author Ankush Sharma
*/
public final class JavaMapDbIpRepositoryImpl implements DbIpRepository {
private static final Logger logger = LoggerFactory.getLogger(JavaMapDbIpRepositoryImpl.class);

private static JavaMapDbIpRepositoryImpl instance = null;

Expand All @@ -28,8 +36,9 @@ public static JavaMapDbIpRepositoryImpl getInstance(){
return instance;
}

private static final TreeMap<Integer,GeoEntity> repository = new TreeMap<>();

private static final TreeMap<Integer,GeoEntity> IPV4_REPOSITORY = new TreeMap<>();
private static final TreeMap<BigInteger,GeoEntity> IPV6_REPOSITORY = new TreeMap<>();

/**
* Lookup GeoEntity for an InetAddress
* @param inetAddress The InetAddress to be resolved.
Expand All @@ -38,10 +47,19 @@ public static JavaMapDbIpRepositoryImpl getInstance(){
@Override
public GeoEntity get(final InetAddress inetAddress) {
PreConditions.checkNull(inetAddress, "inetAddress must not be null");
final Integer startIpNum = InetAddresses.coerceToInteger(inetAddress);

return repository.floorEntry(startIpNum) == null ? null
: repository.floorEntry(startIpNum).getValue() ;
GeoEntity result = null;
if(inetAddress instanceof Inet4Address){
final Integer startIpNum = InetAddresses.coerceToInteger(inetAddress);

return IPV4_REPOSITORY.floorEntry(startIpNum) == null ? null
: IPV4_REPOSITORY.floorEntry(startIpNum).getValue() ;
}
else{
final BigInteger startIpBigInt = IPUtils.ipv6ToBigInteger(inetAddress);
return IPV6_REPOSITORY.floorEntry(startIpBigInt) == null ? null
: IPV6_REPOSITORY.floorEntry(startIpBigInt).getValue();

}
}

/**
Expand All @@ -52,8 +70,25 @@ public GeoEntity get(final InetAddress inetAddress) {
@Override
public void save(final GeoAttributes geoAttributes) {
PreConditions.checkNull(geoAttributes, "geoAttributes must not be null");
final Integer startIpNum = geoAttributes.getStartIp();
final InetAddress startInetAddress = geoAttributes.getStartInetAddress();
final InetAddress endInetAddress = geoAttributes.getEndInetAddress();
final GeoEntity geoEntity = geoAttributes.getGeoEntity();
repository.put(startIpNum,geoEntity);

if(startInetAddress instanceof Inet6Address
&& endInetAddress instanceof Inet6Address)
{
final BigInteger startIpBigInt = IPUtils.ipv6ToBigInteger(startInetAddress);
IPV6_REPOSITORY.put(startIpBigInt,geoEntity);
}
else if (startInetAddress instanceof Inet4Address
&& endInetAddress instanceof Inet4Address)
{
final Integer startIpNum = InetAddresses.coerceToInteger(startInetAddress);
IPV4_REPOSITORY.put(startIpNum,geoEntity);
}
else{
//Well, this case should never happen. Maybe I'll throw in an exception later.
logger.warn("This shouldn't ever happen");
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/in/ankushs/dbip/utils/IPUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package in.ankushs.dbip.utils;

import java.math.BigInteger;
import java.net.InetAddress;

/**
* Created by Ankush on 07/02/17.
*/
public class IPUtils {

public static BigInteger ipv6ToBigInteger(final InetAddress inetAddress){
PreConditions.checkNull(inetAddress,"inetAddress cannot be null");
final byte[] bytes = inetAddress.getAddress();
return new BigInteger(1, bytes);
}
}
13 changes: 0 additions & 13 deletions src/main/java/in/ankushs/dbip/utils/Tes.java

This file was deleted.

11 changes: 0 additions & 11 deletions src/test/groovy/in/ankushs/dbip/BaseSpec.groovy

This file was deleted.

Loading

0 comments on commit 3278353

Please sign in to comment.