Skip to content

Commit 3278353

Browse files
committed
Tests updated for the last version of DB-IP file( Feb version) , Support added for IPV6 and tests included for IPV6 , country code added to GeoEntity.
1 parent 62d24bd commit 3278353

File tree

12 files changed

+323
-102
lines changed

12 files changed

+323
-102
lines changed

src/main/java/in/ankushs/dbip/api/DbIpClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.io.File;
44
import java.net.InetAddress;
5+
import java.net.UnknownHostException;
6+
import java.util.Arrays;
57
import java.util.concurrent.Executor;
68
import java.util.concurrent.Executors;
79

@@ -90,5 +92,4 @@ public GeoEntity lookup(final InetAddress inetAddress){
9092
PreConditions.checkNull(inetAddress, "inetAddress cannot be null");
9193
return lookupService.lookup(inetAddress);
9294
}
93-
9495
}

src/main/java/in/ankushs/dbip/api/GeoEntity.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,25 @@ public final class GeoEntity {
1010
private final String city;
1111
private final String country;
1212
private final String province;
13-
13+
private final String countryCode;
14+
1415
public GeoEntity(final Builder builder){
1516
this.city = builder.city;
1617
this.country = builder.country;
1718
this.province = builder.province;
19+
this.countryCode = builder.countryCode;
1820
}
1921

2022
public static class Builder{
23+
private String countryCode;
2124
private String city;
2225
private String country;
2326
private String province;
27+
28+
public Builder withCountryCode(final String countryCode){
29+
this.countryCode = countryCode;
30+
return this;
31+
}
2432

2533
public Builder withCity(final String city ){
2634
this.city = city;
@@ -54,10 +62,18 @@ public String getProvince() {
5462
return province;
5563
}
5664

65+
66+
public String getCountryCode() {
67+
return countryCode;
68+
}
69+
5770
@Override
5871
public String toString() {
59-
return "GeoEntity [city=" + city + ", country=" + country + ", province=" + province + "]";
72+
return "GeoEntity{" +
73+
"city='" + city + '\'' +
74+
", country='" + country + '\'' +
75+
", province='" + province + '\'' +
76+
", countryCode='" + countryCode + '\'' +
77+
'}';
6078
}
61-
62-
6379
}

src/main/java/in/ankushs/dbip/importer/ResourceImporter.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,30 +63,35 @@ public void load(final File file) {
6363
logger.error("",ex);
6464
}
6565

66-
try (InputStream fis = new FileInputStream(file);
67-
InputStream gis = new GZIPInputStream(fis);
68-
Reader decorator = new InputStreamReader(gis, StandardCharsets.UTF_8);
69-
BufferedReader reader = new BufferedReader(decorator);)
66+
try (final InputStream fis = new FileInputStream(file);
67+
final InputStream gis = new GZIPInputStream(fis);
68+
final Reader decorator = new InputStreamReader(gis, StandardCharsets.UTF_8);
69+
final BufferedReader reader = new BufferedReader(decorator);
70+
)
7071
{
7172
logger.debug("Reading dbip data from {}", file.getName());
7273
String line = null;
7374
int i = 0;
7475
while ((line = reader.readLine()) != null) {
7576
i++;
7677
final String[] array = csvParser.parseRecord(line);
77-
final GeoAttributes geoAttributes = new GeoAttributesImpl.Builder()
78-
.withCity(interner.intern(array[4]))
78+
final GeoAttributes geoAttributes = new GeoAttributesImpl
79+
.Builder()
80+
.withStartInetAddress(InetAddresses.forString(array[0]))
81+
.withEndInetAddress(InetAddresses.forString(array[1]))
82+
.withCountryCode(array[2])
7983
.withCountry(CountryResolver.resolveToFullName(array[2]))
8084
.withProvince(interner.intern(array[3]))
81-
.withEndIp(InetAddresses.coerceToInteger(InetAddresses.forString(array[1])))
82-
.withStartIp(InetAddresses.coerceToInteger(InetAddresses.forString(array[0])))
85+
.withCity(interner.intern(array[4]))
8386
.build();
8487
repository.save(geoAttributes);
8588
if (i % 100000 == 0) {
8689
logger.debug("Loaded {} entries", i);
8790
}
8891
}
89-
} catch (final Exception e) {
92+
}
93+
94+
catch (final Exception e) {
9095
throw new RuntimeException(e);
9196
}
9297
}

src/main/java/in/ankushs/dbip/lookup/GeoEntityLookupServiceImpl.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package in.ankushs.dbip.lookup;
22

3+
import java.io.File;
34
import java.net.InetAddress;
45

6+
import in.ankushs.dbip.api.DbIpClient;
57
import in.ankushs.dbip.api.GeoEntity;
68
import in.ankushs.dbip.repository.DbIpRepository;
79
import in.ankushs.dbip.repository.JavaMapDbIpRepositoryImpl;
@@ -33,14 +35,14 @@ public GeoEntity lookup(final InetAddress inetAddress) {
3335
PreConditions.checkNull(inetAddress, "inetAddress cannot be null ");
3436
GeoEntity geoEntity = repository.get(inetAddress);
3537
if( geoEntity == null ){
36-
geoEntity = new GeoEntity.Builder()
37-
.withCity(UNKNOWN).withCountry(UNKNOWN)
38-
.withProvince(UNKNOWN).build();
38+
geoEntity = new GeoEntity
39+
.Builder()
40+
.withCity(UNKNOWN)
41+
.withCountry(UNKNOWN)
42+
.withCountryCode(UNKNOWN)
43+
.withProvince(UNKNOWN)
44+
.build();
3945
}
4046
return geoEntity;
4147
}
42-
43-
public static void main(String[] args) {
44-
GeoEntityLookupService g1 = getInstance();
45-
}
4648
}

src/main/java/in/ankushs/dbip/model/GeoAttributes.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import in.ankushs.dbip.api.GeoEntity;
66

77
public interface GeoAttributes {
8-
int getStartIp();
9-
int getEndIp();
8+
9+
InetAddress getStartInetAddress();
10+
11+
InetAddress getEndInetAddress();
12+
1013
GeoEntity getGeoEntity();
1114
}

src/main/java/in/ankushs/dbip/model/GeoAttributesImpl.java

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,48 @@
55
import in.ankushs.dbip.api.GeoEntity;
66

77
public final class GeoAttributesImpl implements GeoAttributes {
8-
private final int startIp;
9-
private final int endIp;
8+
109
private final String city;
1110
private final String country;
1211
private final String province ;
12+
private final String countryCode;
13+
private final InetAddress startInetAddress;
14+
private final InetAddress endInetAddress;
15+
1316

1417
private GeoAttributesImpl(final Builder builder){
15-
this.startIp = builder.startIp;
16-
this.endIp = builder.endIp;
18+
this.startInetAddress = builder.startInetAddress;
19+
this.endInetAddress = builder.endInetAddress;
1720
this.city = builder.city;
1821
this.country = builder.country;
1922
this.province = builder.province;
23+
this.countryCode = builder.countryCode;
2024
}
2125

2226
public static class Builder{
23-
private int startIp;
24-
private int endIp;
27+
private InetAddress startInetAddress;
28+
private InetAddress endInetAddress;
2529
private String city;
2630
private String country;
2731
private String province ;
28-
29-
public Builder withStartIp(final int startIp){
30-
this.startIp = startIp;
32+
private String countryCode;
33+
34+
35+
public Builder withStartInetAddress(final InetAddress startInetAddress){
36+
this.startInetAddress = startInetAddress;
3137
return this;
3238
}
33-
34-
public Builder withEndIp(final int endIp){
35-
this.endIp = endIp;
39+
40+
public Builder withCountryCode(final String countryCode){
41+
this.countryCode = countryCode;
3642
return this;
3743
}
38-
39-
44+
public Builder withEndInetAddress(final InetAddress endInetAddress){
45+
this.endInetAddress = endInetAddress;
46+
return this;
47+
}
48+
49+
4050
public Builder withCity(final String city){
4151
this.city = city;
4252
return this;
@@ -59,21 +69,26 @@ public GeoAttributesImpl build(){
5969
}
6070
}
6171

72+
73+
6274
@Override
63-
public int getStartIp() {
64-
return startIp;
75+
public InetAddress getStartInetAddress() {
76+
return startInetAddress;
6577
}
6678

6779
@Override
68-
public int getEndIp() {
69-
return endIp;
80+
public InetAddress getEndInetAddress() {
81+
return endInetAddress;
7082
}
7183

7284
@Override
7385
public GeoEntity getGeoEntity() {
7486
return new GeoEntity.Builder()
75-
.withCity(city).withCountry(country)
76-
.withProvince(province).build();
87+
.withCity(city)
88+
.withCountry(country)
89+
.withCountryCode(countryCode)
90+
.withProvince(province)
91+
.build();
7792
}
7893

7994

src/main/java/in/ankushs/dbip/repository/JavaMapDbIpRepositoryImpl.java

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package in.ankushs.dbip.repository;
22

3+
import java.math.BigInteger;
4+
import java.net.Inet4Address;
5+
import java.net.Inet6Address;
36
import java.net.InetAddress;
47
import java.util.TreeMap;
58
import java.util.concurrent.Executor;
@@ -9,14 +12,19 @@
912

1013
import in.ankushs.dbip.api.GeoEntity;
1114
import in.ankushs.dbip.model.GeoAttributes;
15+
import in.ankushs.dbip.utils.IPUtils;
1216
import in.ankushs.dbip.utils.PreConditions;
17+
import org.slf4j.Logger;
18+
import org.slf4j.LoggerFactory;
19+
1320
/**
1421
*
15-
* Singletonclass that uses a <a href="https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html">TreeMap</a>
22+
* Singletonthat uses a <a href="https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html">TreeMap</a>
1623
* as repository.
1724
* @author Ankush Sharma
1825
*/
1926
public final class JavaMapDbIpRepositoryImpl implements DbIpRepository {
27+
private static final Logger logger = LoggerFactory.getLogger(JavaMapDbIpRepositoryImpl.class);
2028

2129
private static JavaMapDbIpRepositoryImpl instance = null;
2230

@@ -28,8 +36,9 @@ public static JavaMapDbIpRepositoryImpl getInstance(){
2836
return instance;
2937
}
3038

31-
private static final TreeMap<Integer,GeoEntity> repository = new TreeMap<>();
32-
39+
private static final TreeMap<Integer,GeoEntity> IPV4_REPOSITORY = new TreeMap<>();
40+
private static final TreeMap<BigInteger,GeoEntity> IPV6_REPOSITORY = new TreeMap<>();
41+
3342
/**
3443
* Lookup GeoEntity for an InetAddress
3544
* @param inetAddress The InetAddress to be resolved.
@@ -38,10 +47,19 @@ public static JavaMapDbIpRepositoryImpl getInstance(){
3847
@Override
3948
public GeoEntity get(final InetAddress inetAddress) {
4049
PreConditions.checkNull(inetAddress, "inetAddress must not be null");
41-
final Integer startIpNum = InetAddresses.coerceToInteger(inetAddress);
42-
43-
return repository.floorEntry(startIpNum) == null ? null
44-
: repository.floorEntry(startIpNum).getValue() ;
50+
GeoEntity result = null;
51+
if(inetAddress instanceof Inet4Address){
52+
final Integer startIpNum = InetAddresses.coerceToInteger(inetAddress);
53+
54+
return IPV4_REPOSITORY.floorEntry(startIpNum) == null ? null
55+
: IPV4_REPOSITORY.floorEntry(startIpNum).getValue() ;
56+
}
57+
else{
58+
final BigInteger startIpBigInt = IPUtils.ipv6ToBigInteger(inetAddress);
59+
return IPV6_REPOSITORY.floorEntry(startIpBigInt) == null ? null
60+
: IPV6_REPOSITORY.floorEntry(startIpBigInt).getValue();
61+
62+
}
4563
}
4664

4765
/**
@@ -52,8 +70,25 @@ public GeoEntity get(final InetAddress inetAddress) {
5270
@Override
5371
public void save(final GeoAttributes geoAttributes) {
5472
PreConditions.checkNull(geoAttributes, "geoAttributes must not be null");
55-
final Integer startIpNum = geoAttributes.getStartIp();
73+
final InetAddress startInetAddress = geoAttributes.getStartInetAddress();
74+
final InetAddress endInetAddress = geoAttributes.getEndInetAddress();
5675
final GeoEntity geoEntity = geoAttributes.getGeoEntity();
57-
repository.put(startIpNum,geoEntity);
76+
77+
if(startInetAddress instanceof Inet6Address
78+
&& endInetAddress instanceof Inet6Address)
79+
{
80+
final BigInteger startIpBigInt = IPUtils.ipv6ToBigInteger(startInetAddress);
81+
IPV6_REPOSITORY.put(startIpBigInt,geoEntity);
82+
}
83+
else if (startInetAddress instanceof Inet4Address
84+
&& endInetAddress instanceof Inet4Address)
85+
{
86+
final Integer startIpNum = InetAddresses.coerceToInteger(startInetAddress);
87+
IPV4_REPOSITORY.put(startIpNum,geoEntity);
88+
}
89+
else{
90+
//Well, this case should never happen. Maybe I'll throw in an exception later.
91+
logger.warn("This shouldn't ever happen");
92+
}
5893
}
5994
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package in.ankushs.dbip.utils;
2+
3+
import java.math.BigInteger;
4+
import java.net.InetAddress;
5+
6+
/**
7+
* Created by Ankush on 07/02/17.
8+
*/
9+
public class IPUtils {
10+
11+
public static BigInteger ipv6ToBigInteger(final InetAddress inetAddress){
12+
PreConditions.checkNull(inetAddress,"inetAddress cannot be null");
13+
final byte[] bytes = inetAddress.getAddress();
14+
return new BigInteger(1, bytes);
15+
}
16+
}

src/main/java/in/ankushs/dbip/utils/Tes.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/test/groovy/in/ankushs/dbip/BaseSpec.groovy

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)