From 42014c322d03cdfdfebed87b59b4a1f1143776b6 Mon Sep 17 00:00:00 2001 From: doufenghu Date: Fri, 26 Jan 2024 17:16:50 +0800 Subject: [Fix][IPLookupV2] fix infoLookupToJSON and infoLookup, using correct return type. if return value is empty, will return null. --- pom.xml | 4 +- .../geedgenetworks/domain/LocationResponse.java | 214 ------------------ src/main/java/com/geedgenetworks/domain/Nets.java | 31 --- .../com/geedgenetworks/model/LocationResponse.java | 208 +++++++++++++++++ src/main/java/com/geedgenetworks/model/Nets.java | 31 +++ .../java/com/geedgenetworks/utils/AsnLookup.java | 6 +- .../geedgenetworks/utils/GalaxyDataBaseReader.java | 5 +- src/main/java/com/geedgenetworks/utils/IPUtil.java | 4 +- .../java/com/geedgenetworks/utils/IpLookupV2.java | 245 ++++++++++----------- .../com/geedgenetworks/test/IpLookupV2Test.java | 53 +++++ 10 files changed, 412 insertions(+), 389 deletions(-) delete mode 100644 src/main/java/com/geedgenetworks/domain/LocationResponse.java delete mode 100644 src/main/java/com/geedgenetworks/domain/Nets.java create mode 100644 src/main/java/com/geedgenetworks/model/LocationResponse.java create mode 100644 src/main/java/com/geedgenetworks/model/Nets.java diff --git a/pom.xml b/pom.xml index fbc79c2..750333e 100644 --- a/pom.xml +++ b/pom.xml @@ -7,13 +7,13 @@ galaxy jar galaxy - 1.2.2 + 1.2.3 galaxy tools for common http://www.zdjizhi.com/galaxy-tool - The geedge networks Software License, Version 21.0 + The geedgenetworks Software License, Version 21.0 http://www.apache.org/licenses/LICENSE-1.0.txt repo diff --git a/src/main/java/com/geedgenetworks/domain/LocationResponse.java b/src/main/java/com/geedgenetworks/domain/LocationResponse.java deleted file mode 100644 index 8d9256a..0000000 --- a/src/main/java/com/geedgenetworks/domain/LocationResponse.java +++ /dev/null @@ -1,214 +0,0 @@ -package com.geedgenetworks.domain;/** - * Created by dell on 2018-10-8. - */ - -import com.google.common.base.MoreObjects; -import com.maxmind.db.MaxMindDbConstructor; -import com.maxmind.db.MaxMindDbParameter; - -/** - * @ClassName CountryResponse - * @Description TODO - * @Author dell - * @Date 2018-10-8 18:56 - * @Version 1.0 - **/ -public class LocationResponse implements java.io.Serializable { - private String areaCode; - - private String asn; - - private String isp; - - private String latitude; - - private String longitude; - - private String country; - - private String superAdministrativeArea; - - private String administrativeArea; - - private String subAdministrativeArea; - - private String locality; - - private String dependentLocality; - - private String doubleDependentLocality; - - private String organization; - - private String countryCode; - - private boolean isPrivateIP; - - - public LocationResponse() { - this("", "", "", "", "", "", "", "", "", "", "", "", "", ""); - } - - public LocationResponse(boolean isPrivateIP) { - this.setPrivateIP(isPrivateIP); - } - @MaxMindDbConstructor - public LocationResponse(@MaxMindDbParameter(name="AREA_CODE") String areaCode, @MaxMindDbParameter(name= "ASN") String asn, - @MaxMindDbParameter(name= "ISP") String isp, @MaxMindDbParameter(name= "LATITUDE") String latitude, - @MaxMindDbParameter(name= "LONGITUDE") String longitude, @MaxMindDbParameter(name= "COUNTRY") String country, - @MaxMindDbParameter(name= "SUPER_ADMINISTRATIVE_AREA") String superAdministrativeArea, @MaxMindDbParameter(name= "ADMINISTRATIVE_AREA") String administrativeArea, - @MaxMindDbParameter(name= "SUB_ADMINISTRATIVE_AREA") String subAdministrativeArea, @MaxMindDbParameter(name= "LOCALITY") String locality, - @MaxMindDbParameter(name= "DEPENDENT_LOCALITY") String dependentLocality, @MaxMindDbParameter(name="DOUBLE_DEPENDENT_LOCALITY") String doubleDependentLocality, - @MaxMindDbParameter(name= "ORGANIZATION") String organization, @MaxMindDbParameter(name="COUNTRY_CODE") String countryCode) { - this.areaCode = areaCode; - this.asn = asn; - this.isp = isp; - this.latitude = latitude; - this.longitude = longitude; - this.country = country; - this.superAdministrativeArea = superAdministrativeArea; - this.administrativeArea = administrativeArea; - this.subAdministrativeArea = subAdministrativeArea; - this.locality = locality; - this.dependentLocality = dependentLocality; - this.doubleDependentLocality = doubleDependentLocality; - this.organization = organization; - this.countryCode = countryCode; - } - - public String getAreaCode() { - return areaCode; - } - - public void setAreaCode(String areaCode) { - this.areaCode = areaCode; - } - - public String getAsn() { - return asn; - } - - public void setAsn(String asn) { - this.asn = asn; - } - - public String getIsp() { - return isp; - } - - public void setIsp(String isp) { - this.isp = isp; - } - - public String getLatitude() { - return latitude; - } - - public void setLatitude(String latitude) { - this.latitude = latitude; - } - - public String getLongitude() { - return longitude; - } - - public void setLongitude(String longitude) { - this.longitude = longitude; - } - - public String getCountry() { - return country; - } - - public void setCountry(String country) { - this.country = country; - } - - public String getSuperAdministrativeArea() { - return superAdministrativeArea; - } - - public void setSuperAdministrativeArea(String superAdministrativeArea) { - this.superAdministrativeArea = superAdministrativeArea; - } - - public String getAdministrativeArea() { - return administrativeArea; - } - - public void setAdministrativeArea(String administrativeArea) { - this.administrativeArea = administrativeArea; - } - - public String getSubAdministrativeArea() { - return subAdministrativeArea; - } - - public void setSubAdministrativeArea(String subAdministrativeArea) { - this.subAdministrativeArea = subAdministrativeArea; - } - - public String getLocality() { - return locality; - } - - public void setLocality(String locality) { - this.locality = locality; - } - - public String getDependentLocality() { - return dependentLocality; - } - - public void setDependentLocality(String dependentLocality) { - this.dependentLocality = dependentLocality; - } - - public String getDoubleDependentLocality() { - return doubleDependentLocality; - } - - public void setDoubleDependentLocality(String doubleDependentLocality) { - this.doubleDependentLocality = doubleDependentLocality; - } - - public String getOrganization() { - return organization; - } - - public void setOrganization(String organization) { - this.organization = organization; - } - - public String getCountryCode() { - return countryCode; - } - - public void setCountryCode(String countryCode) { - this.countryCode = countryCode; - } - - public boolean isPrivateIP() { - return isPrivateIP; - } - - public void setPrivateIP(boolean privateIP) { - isPrivateIP = privateIP; - } - - @Override - public String toString() { - return MoreObjects.toStringHelper(this) - .add("country", getCountry()) - .add("superAdministrativeArea", getSuperAdministrativeArea()) - .add("administrativeArea", getAdministrativeArea()) - .add("subAdministrativeArea", getSubAdministrativeArea()) - .add("organization", getOrganization()) - .add("isp", getIsp()) - .add("asn", getAsn()) - .add("latitude", getLatitude()) - .add("longitude", getLongitude()) - .toString(); - } -} - diff --git a/src/main/java/com/geedgenetworks/domain/Nets.java b/src/main/java/com/geedgenetworks/domain/Nets.java deleted file mode 100644 index 0f83bed..0000000 --- a/src/main/java/com/geedgenetworks/domain/Nets.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.geedgenetworks.domain; - -/** - * Created by dell on 2018-8-30. - */ -public class Nets { - - private String StartIP; - private String EndIP; - private String NetMask; - public String getStartIP() { - return StartIP; - } - public void setStartIP(String startIP) { - StartIP = startIP; - } - public String getEndIP() { - return EndIP; - } - public void setEndIP(String endIP) { - EndIP = endIP; - } - public String getNetMask() { - return NetMask; - } - public void setNetMask(String netMask) { - NetMask = netMask; - } - - -} diff --git a/src/main/java/com/geedgenetworks/model/LocationResponse.java b/src/main/java/com/geedgenetworks/model/LocationResponse.java new file mode 100644 index 0000000..9b5b2bb --- /dev/null +++ b/src/main/java/com/geedgenetworks/model/LocationResponse.java @@ -0,0 +1,208 @@ +package com.geedgenetworks.model;/** + * Created by dell on 2018-10-8. + */ + +import com.google.common.base.MoreObjects; +import com.maxmind.db.MaxMindDbConstructor; +import com.maxmind.db.MaxMindDbParameter; + +/** + * @ClassName CountryResponse + * @Description TODO + * @Author dell + * @Date 2018-10-8 18:56 + * @Version 1.0 + **/ +public class LocationResponse implements java.io.Serializable { + private String areaCode; + + private String asn; + + private String isp; + + private String latitude; + + private String longitude; + + private String country; + + private String superAdministrativeArea; + + private String administrativeArea; + + private String subAdministrativeArea; + + private String locality; + + private String dependentLocality; + + private String doubleDependentLocality; + + private String organization; + + private String countryCode; + + private boolean isPrivateIP; + public LocationResponse() { + this("", "", "", "", "", "", "", "", "", "", "", "", "", ""); + } + @MaxMindDbConstructor + public LocationResponse(@MaxMindDbParameter(name="AREA_CODE") String areaCode, @MaxMindDbParameter(name= "ASN") String asn, + @MaxMindDbParameter(name= "ISP") String isp, @MaxMindDbParameter(name= "LATITUDE") String latitude, + @MaxMindDbParameter(name= "LONGITUDE") String longitude, @MaxMindDbParameter(name= "COUNTRY") String country, + @MaxMindDbParameter(name= "SUPER_ADMINISTRATIVE_AREA") String superAdministrativeArea, @MaxMindDbParameter(name= "ADMINISTRATIVE_AREA") String administrativeArea, + @MaxMindDbParameter(name= "SUB_ADMINISTRATIVE_AREA") String subAdministrativeArea, @MaxMindDbParameter(name= "LOCALITY") String locality, + @MaxMindDbParameter(name= "DEPENDENT_LOCALITY") String dependentLocality, @MaxMindDbParameter(name="DOUBLE_DEPENDENT_LOCALITY") String doubleDependentLocality, + @MaxMindDbParameter(name= "ORGANIZATION") String organization, @MaxMindDbParameter(name="COUNTRY_CODE") String countryCode) { + this.areaCode = areaCode; + this.asn = asn; + this.isp = isp; + this.latitude = latitude; + this.longitude = longitude; + this.country = country; + this.superAdministrativeArea = superAdministrativeArea; + this.administrativeArea = administrativeArea; + this.subAdministrativeArea = subAdministrativeArea; + this.locality = locality; + this.dependentLocality = dependentLocality; + this.doubleDependentLocality = doubleDependentLocality; + this.organization = organization; + this.countryCode = countryCode; + } + + public String getAreaCode() { + return areaCode; + } + + public void setAreaCode(String areaCode) { + this.areaCode = areaCode; + } + + public String getAsn() { + return asn; + } + + public void setAsn(String asn) { + this.asn = asn; + } + + public String getIsp() { + return isp; + } + + public void setIsp(String isp) { + this.isp = isp; + } + + public String getLatitude() { + return latitude; + } + + public void setLatitude(String latitude) { + this.latitude = latitude; + } + + public String getLongitude() { + return longitude; + } + + public void setLongitude(String longitude) { + this.longitude = longitude; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + public String getSuperAdministrativeArea() { + return superAdministrativeArea; + } + + public void setSuperAdministrativeArea(String superAdministrativeArea) { + this.superAdministrativeArea = superAdministrativeArea; + } + + public String getAdministrativeArea() { + return administrativeArea; + } + + public void setAdministrativeArea(String administrativeArea) { + this.administrativeArea = administrativeArea; + } + + public String getSubAdministrativeArea() { + return subAdministrativeArea; + } + + public void setSubAdministrativeArea(String subAdministrativeArea) { + this.subAdministrativeArea = subAdministrativeArea; + } + + public String getLocality() { + return locality; + } + + public void setLocality(String locality) { + this.locality = locality; + } + + public String getDependentLocality() { + return dependentLocality; + } + + public void setDependentLocality(String dependentLocality) { + this.dependentLocality = dependentLocality; + } + + public String getDoubleDependentLocality() { + return doubleDependentLocality; + } + + public void setDoubleDependentLocality(String doubleDependentLocality) { + this.doubleDependentLocality = doubleDependentLocality; + } + + public String getOrganization() { + return organization; + } + + public void setOrganization(String organization) { + this.organization = organization; + } + + public String getCountryCode() { + return countryCode; + } + + public void setCountryCode(String countryCode) { + this.countryCode = countryCode; + } + + public boolean isPrivateIP() { + return isPrivateIP; + } + + public void setPrivateIP(boolean privateIP) { + isPrivateIP = privateIP; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("country", getCountry()) + .add("superAdministrativeArea", getSuperAdministrativeArea()) + .add("administrativeArea", getAdministrativeArea()) + .add("subAdministrativeArea", getSubAdministrativeArea()) + .add("organization", getOrganization()) + .add("isp", getIsp()) + .add("asn", getAsn()) + .add("latitude", getLatitude()) + .add("longitude", getLongitude()) + .toString(); + } +} + diff --git a/src/main/java/com/geedgenetworks/model/Nets.java b/src/main/java/com/geedgenetworks/model/Nets.java new file mode 100644 index 0000000..1a0520c --- /dev/null +++ b/src/main/java/com/geedgenetworks/model/Nets.java @@ -0,0 +1,31 @@ +package com.geedgenetworks.model; + +/** + * Created by dell on 2018-8-30. + */ +public class Nets { + + private String StartIP; + private String EndIP; + private String NetMask; + public String getStartIP() { + return StartIP; + } + public void setStartIP(String startIP) { + StartIP = startIP; + } + public String getEndIP() { + return EndIP; + } + public void setEndIP(String endIP) { + EndIP = endIP; + } + public String getNetMask() { + return NetMask; + } + public void setNetMask(String netMask) { + NetMask = netMask; + } + + +} diff --git a/src/main/java/com/geedgenetworks/utils/AsnLookup.java b/src/main/java/com/geedgenetworks/utils/AsnLookup.java index 11bf870..fa824af 100644 --- a/src/main/java/com/geedgenetworks/utils/AsnLookup.java +++ b/src/main/java/com/geedgenetworks/utils/AsnLookup.java @@ -3,7 +3,7 @@ package com.geedgenetworks.utils; import com.google.common.base.Joiner; import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.geedgenetworks.domain.LocationResponse; +import com.geedgenetworks.model.LocationResponse; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -307,7 +307,7 @@ public class AsnLookup extends AbstractDatabaseReader { if (ipAddress instanceof Inet4Address) { //Iterate through each 'dbReader' and execute them in the following order: private v4 -> private -> public v4 -> public. for (GalaxyDataBaseReader reader : availableDBReaders.get(AbstractDatabaseReader.IpVersion.IPv4.name())) { - LocationResponse response = reader.locationV2(ipAddress); + LocationResponse response = reader.location(ipAddress); if(StringUtil.isNotEmpty(response)) { return response; } @@ -315,7 +315,7 @@ public class AsnLookup extends AbstractDatabaseReader { } else if (ipAddress instanceof Inet6Address) { //Iterate through each 'dbReader' and execute them in the following order: private v6 -> private -> public v6 -> public. for(GalaxyDataBaseReader reader : availableDBReaders.get(AbstractDatabaseReader.IpVersion.IPv6.name())) { - LocationResponse response = reader.locationV2(ipAddress); + LocationResponse response = reader.location(ipAddress); if(StringUtil.isNotEmpty(response)) { return response; } diff --git a/src/main/java/com/geedgenetworks/utils/GalaxyDataBaseReader.java b/src/main/java/com/geedgenetworks/utils/GalaxyDataBaseReader.java index 47f16fb..809b38c 100644 --- a/src/main/java/com/geedgenetworks/utils/GalaxyDataBaseReader.java +++ b/src/main/java/com/geedgenetworks/utils/GalaxyDataBaseReader.java @@ -1,7 +1,7 @@ package com.geedgenetworks.utils; import com.maxmind.db.*; -import com.geedgenetworks.domain.LocationResponse; +import com.geedgenetworks.model.LocationResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; @@ -114,9 +114,8 @@ public class GalaxyDataBaseReader { } } - public LocationResponse locationV2(InetAddress ipAddress) throws IOException { + public LocationResponse location(InetAddress ipAddress) throws IOException { return reader.get(ipAddress, LocationResponse.class); - } public void close() throws IOException { diff --git a/src/main/java/com/geedgenetworks/utils/IPUtil.java b/src/main/java/com/geedgenetworks/utils/IPUtil.java index e351b46..2d77f3b 100644 --- a/src/main/java/com/geedgenetworks/utils/IPUtil.java +++ b/src/main/java/com/geedgenetworks/utils/IPUtil.java @@ -1,14 +1,12 @@ package com.geedgenetworks.utils; -import com.geedgenetworks.domain.Nets; +import com.geedgenetworks.model.Nets; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletRequest; import java.io.StringWriter; -import java.net.InetAddress; -import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; diff --git a/src/main/java/com/geedgenetworks/utils/IpLookupV2.java b/src/main/java/com/geedgenetworks/utils/IpLookupV2.java index 08b1c71..41a5204 100644 --- a/src/main/java/com/geedgenetworks/utils/IpLookupV2.java +++ b/src/main/java/com/geedgenetworks/utils/IpLookupV2.java @@ -3,10 +3,11 @@ package com.geedgenetworks.utils; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONWriter; +import com.geedgenetworks.model.LocationResponse; +import com.google.common.base.Function; import com.google.common.base.Joiner; import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.geedgenetworks.domain.LocationResponse; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -22,7 +23,7 @@ import java.util.*; * @Date 2023-08-22 20:00 Geedge Networks * @Version 1.2 **/ -public final class IpLookupV2 extends AbstractDatabaseReader implements IPLocator{ +public final class IpLookupV2 extends AbstractDatabaseReader implements IPLocator { private Map> availableDBReaders = Maps.newHashMap(); private GalaxyDataBaseReader publicDatabaseReader; private GalaxyDataBaseReader publicV4DatabaseReader;; @@ -432,196 +433,173 @@ public final class IpLookupV2 extends AbstractDatabaseReader implements IPLocato } @Override - public String countryLookup(String ip) { - LocationResponse response = getResponse(ip); - if (StringUtil.isEmpty(response)) { - return UNKNOWN; - } - if (response.isPrivateIP()) { - return PRIVATE_IP; - } + public String countryLookup(String ip) { + return getValueWithPrivateIP(getResponse(ip), LocationResponse::getCountry); + } - return StringUtil.setDefaultIfEmpty(response.getCountry(), UNKNOWN).toString() ; + @Override + public String provinceLookup(String ip) { + return getValueWithPrivateIP(getResponse(ip), LocationResponse::getSuperAdministrativeArea); } @Override - public String cityLookupDetail(String ip) { + public String cityLookup(String ip) { + return getValueWithPrivateIP(getResponse(ip), LocationResponse::getAdministrativeArea); + } - LocationResponse response = getResponse(ip); - if (StringUtil.isEmpty(response)) { - return UNKNOWN; - } - if (response.isPrivateIP()) { + @Override + public String cityLookupDetail(String ip) { + Optional response = getResponse(ip); + if (response.isPresent() && response.get().isPrivateIP()) { return PRIVATE_IP; } - - return Joiner.on(LOCATION_SEPARATOR).useForNull("").join(response.getCountry(), - response.getSuperAdministrativeArea(), response.getAdministrativeArea()); + return Joiner.on(LOCATION_SEPARATOR).useForNull("").join( + getValue(response, LocationResponse::getCountry), + getValue(response, LocationResponse::getSuperAdministrativeArea), + getValue(response, LocationResponse::getAdministrativeArea)); } @Override public String administrativeAreaLookupDetail(String ip) { - LocationResponse response = getResponse(ip); - - if (StringUtil.isEmpty(response)) { - return UNKNOWN; - } - if (response.isPrivateIP()) { + Optional response = getResponse(ip); + if (response.isPresent() && response.get().isPrivateIP()) { return PRIVATE_IP; } - - return Joiner.on(LOCATION_SEPARATOR).useForNull("").join(response.getCountry(), - response.getSuperAdministrativeArea(), response.getAdministrativeArea(), - response.getSubAdministrativeArea()); + return Joiner.on(LOCATION_SEPARATOR).useForNull("").join( + getValue(response, LocationResponse::getCountry), + getValue(response, LocationResponse::getSuperAdministrativeArea), + getValue(response, LocationResponse::getAdministrativeArea), + getValue(response, LocationResponse::getSubAdministrativeArea)); } @Override public String locationLookupDetail(String ip) { - LocationResponse response = getResponse(ip); - - if (StringUtil.isEmpty(response)) { - return UNKNOWN; - } - if (response.isPrivateIP()) { + Optional response = getResponse(ip); + if (response.isPresent() && response.get().isPrivateIP()) { return PRIVATE_IP; } + return Joiner.on(LOCATION_SEPARATOR).useForNull("").join( + getValue(response, LocationResponse::getCountry), + getValue(response, LocationResponse::getSuperAdministrativeArea), + getValue(response, LocationResponse::getAdministrativeArea), + getValue(response, LocationResponse::getSubAdministrativeArea), + getValue(response, LocationResponse::getLocality), + getValue(response, LocationResponse::getDependentLocality), + getValue(response, LocationResponse::getDoubleDependentLocality)); - return Joiner.on(LOCATION_SEPARATOR).useForNull("").join(response.getCountry(), - response.getSuperAdministrativeArea(), response.getAdministrativeArea(), - response.getSubAdministrativeArea(), response.getLocality(), - response.getDependentLocality(), response.getDoubleDependentLocality()); } @Override - public String cityLookup(String ip) { - LocationResponse response = getResponse(ip); - - if (StringUtil.isEmpty(response)) { - return UNKNOWN; - } - if (response.isPrivateIP()) { - return PRIVATE_IP; - } - - return StringUtil.setDefaultIfEmpty(response.getAdministrativeArea(), UNKNOWN).toString() ; - + public String countryCodeLookup(String ip) { + return getValue(getResponse(ip), LocationResponse::getCountryCode); } @Override - public String countryCodeLookup(String ip) { - LocationResponse response = getResponse(ip); - if (StringUtil.isEmpty(response)) { - return UNKNOWN; + public String regionCodeLookup(String ip) { + return getValue(getResponse(ip), LocationResponse::getAreaCode); + } + + private String getValue(Optional response, Function valueExtractor) { + if (response.isPresent()) { + return StringUtil.setDefaultIfEmpty(valueExtractor.apply(response.get()), UNKNOWN).toString(); } - return StringUtil.setDefaultIfEmpty(response.getCountryCode(), UNKNOWN).toString() ; + return UNKNOWN; } - @Override - public String regionCodeLookup(String ip) { - LocationResponse response = getResponse(ip); - if (StringUtil.isEmpty(response)) { - return UNKNOWN; + private String getValueWithPrivateIP(Optional response, Function valueExtractor) { + if (response.isPresent()) { + LocationResponse locationResponse = response.get(); + if (locationResponse.isPrivateIP()) { + return PRIVATE_IP; + } + return StringUtil.setDefaultIfEmpty(valueExtractor.apply(locationResponse), UNKNOWN).toString(); } - return StringUtil.setDefaultIfEmpty(response.getAreaCode(), UNKNOWN).toString() ; + return UNKNOWN; } + @Override public String infoLookupToCSV(String ip) { - LocationResponse response = getResponse(ip); - if (StringUtil.isEmpty(response)) { - return UNKNOWN; - } + Optional response = getResponse(ip); StringBuilder ipInfo = new StringBuilder(); ipInfo.append( - Joiner.on(LOCATION_SEPARATOR).useForNull("").join(response.getCountry(), - response.getSuperAdministrativeArea(), response.getAdministrativeArea(), - response.getSubAdministrativeArea(), response.getLocality(), - response.getDependentLocality(), response.getDoubleDependentLocality())) + Joiner.on(LOCATION_SEPARATOR).useForNull("").join( + getValue(response, LocationResponse::getCountry), + getValue(response, LocationResponse::getSuperAdministrativeArea), + getValue(response, LocationResponse::getAdministrativeArea), + getValue(response, LocationResponse::getSubAdministrativeArea), + getValue(response, LocationResponse::getLocality), + getValue(response, LocationResponse::getDependentLocality), + getValue(response, LocationResponse::getDoubleDependentLocality))) .append(OBJECT_SEPARATOR).append( - StringUtil.setDefaultIfEmpty(response.getIsp(), UNKNOWN).toString() + getValue(response, LocationResponse::getIsp) ).append(OBJECT_SEPARATOR).append( - StringUtil.setDefaultIfEmpty(response.getOrganization(), UNKNOWN).toString() + getValue(response, LocationResponse::getOrganization) ).append(OBJECT_SEPARATOR).append( asnLookup.asnLookup(ip) ).append(OBJECT_SEPARATOR).append( - Joiner.on(LAT_LNG_SEPARATOR).skipNulls().join(response.getLatitude(), response.getLongitude()) + Joiner.on(LAT_LNG_SEPARATOR).skipNulls().join( + getValue(response, LocationResponse::getLatitude), + getValue(response, LocationResponse::getLongitude)) ); return ipInfo.toString(); } @Override public String infoLookupToJSONString(String ip) { - LocationResponse response = getResponse(ip); - if (StringUtil.isEmpty(response)) { - return UNKNOWN; + Optional response = getResponse(ip); + if (response.isPresent()) { + return JSON.toJSONString(response.get(), JSONWriter.Feature.WriteNullStringAsEmpty); } - return JSON.toJSONString(response, JSONWriter.Feature.WriteNullStringAsEmpty); + return UNKNOWN; } @Override - public Object infoLookupToJSON(String ip) { - LocationResponse response = getResponse(ip); - if (StringUtil.isEmpty(response)) { - return UNKNOWN; + public JSONObject infoLookupToJSON(String ip) { + Optional response = getResponse(ip); + if (response.isPresent()) { + return JSONObject.from(response.get()); } - return JSONObject.from(response); + return null; } @Override - public Object infoLookup(String ip) { - LocationResponse response = getResponse(ip); - if (StringUtil.isEmpty(response)) { - return UNKNOWN; + public LocationResponse infoLookup(String ip) { + Optional response = getResponse(ip); + if (response.isPresent()) { + return response.get(); } - return response; - } - - @Override - public String provinceLookup(String ip) { - LocationResponse response = getResponse(ip); - if (StringUtil.isEmpty(response)) { - return UNKNOWN; - } - if (response.isPrivateIP()) { - return PRIVATE_IP; - } - return StringUtil.setDefaultIfEmpty(response.getSuperAdministrativeArea(), UNKNOWN).toString() ; + return null; } @Override public String ispLookup(String ip) { - LocationResponse response = getResponse(ip); - if (StringUtil.isEmpty(response)) { - return UNKNOWN; - } - if (response.isPrivateIP()) { - return PRIVATE_IP; - } - return StringUtil.setDefaultIfEmpty(response.getIsp(), UNKNOWN).toString() ; + Optional response = getResponse(ip); + response.ifPresent(locationResponse -> { + if (locationResponse.isPrivateIP()) { + locationResponse.setIsp(PRIVATE_IP); + } + }); + return StringUtil.setDefaultIfEmpty(response.orElseGet(()->new LocationResponse()).getIsp(), UNKNOWN).toString(); } @Override public String organizationLookup(String ip) { - LocationResponse response = getResponse(ip); - if (StringUtil.isEmpty(response)) { - return UNKNOWN; - } - if (response.isPrivateIP()) { - return PRIVATE_IP; - } - return StringUtil.setDefaultIfEmpty(response.getOrganization(), UNKNOWN).toString() ; + Optional response = getResponse(ip); + response.ifPresent(locationResponse -> { + if (locationResponse.isPrivateIP()) { + locationResponse.setOrganization(PRIVATE_IP); + } + }); + return StringUtil.setDefaultIfEmpty(response.orElseGet(()->new LocationResponse()).getOrganization(), UNKNOWN).toString(); } @Override public String latLngLookup(String ip) { - LocationResponse response = getResponse(ip); - if (StringUtil.isEmpty(response) || response.isPrivateIP()) { - return UNKNOWN; - } - - return Joiner.on(LAT_LNG_SEPARATOR).skipNulls().join(response.getLatitude(), response.getLongitude()); - + Optional response = getResponse(ip); + LocationResponse locationResponse = response.orElseGet(()->new LocationResponse()); + return Joiner.on(LAT_LNG_SEPARATOR).skipNulls().join(locationResponse.getLatitude(), locationResponse.getLongitude()); } @Override @@ -648,8 +626,7 @@ public final class IpLookupV2 extends AbstractDatabaseReader implements IPLocato return asnLookup.asnLookupDetail(ip); } - - private LocationResponse getResponse(String ip) { + private Optional getResponse(String ip) { try { ip = Optional.ofNullable(ip).orElse("").trim(); InetAddress ipAddress = InetAddress.getByName(ip); @@ -657,30 +634,32 @@ public final class IpLookupV2 extends AbstractDatabaseReader implements IPLocato //Iterate through each 'dbReader' and execute them in the following order: private v4 -> private -> public v4 -> public. for (GalaxyDataBaseReader reader : availableDBReaders.get(IpVersion.IPv4.name())) { - LocationResponse response = reader.locationV2(ipAddress); + LocationResponse response = reader.location(ipAddress); if(StringUtil.isNotEmpty(response)) { - return response; + return Optional.of(response); } } //Is it a private IP if (IPUtil.internalIp(ip)) { - return new LocationResponse(true); + LocationResponse response = new LocationResponse(); + response.setPrivateIP(true); + return Optional.of(response); } } else if (ipAddress instanceof Inet6Address) { //Iterate through each 'dbReader' and execute them in the following order: private v6 -> private -> public v6 -> public. for(GalaxyDataBaseReader reader : availableDBReaders.get(IpVersion.IPv6.name())) { - LocationResponse response = reader.locationV2(ipAddress); + LocationResponse response = reader.location(ipAddress); if(StringUtil.isNotEmpty(response)) { - return response; + return Optional.of(response); } } } else { - throw new IllegalArgumentException("IP address <" + ip +">. "); + throw new IllegalArgumentException(String.format("IP address <%s>. ", ip)); } - return null; + return Optional.empty(); } catch (Exception e) { - throw new IllegalArgumentException("IP address <" + ip +">. " + e); + throw new IllegalArgumentException(String.format("IP address <%s>. ", ip) + e); } } diff --git a/src/test/java/com/geedgenetworks/test/IpLookupV2Test.java b/src/test/java/com/geedgenetworks/test/IpLookupV2Test.java index 6a1481b..846012f 100644 --- a/src/test/java/com/geedgenetworks/test/IpLookupV2Test.java +++ b/src/test/java/com/geedgenetworks/test/IpLookupV2Test.java @@ -1,6 +1,7 @@ package com.geedgenetworks.test; import com.alibaba.fastjson2.JSONObject; +import com.geedgenetworks.model.LocationResponse; import com.geedgenetworks.utils.AbstractDatabaseReader; import com.geedgenetworks.utils.AsnLookup; import com.geedgenetworks.utils.IpLookupV2; @@ -18,6 +19,7 @@ import java.net.InetAddress; import java.net.UnknownHostException; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; public class IpLookupV2Test { @@ -163,6 +165,57 @@ public class IpLookupV2Test { assertEquals(JSONObject.from(ipLookupByDatabase.infoLookupToJSON("120.221.155.223")).get("country"),"China"); } + @Test + public void testCountryLookup() { + assertEquals(ipLookupByDatabase.countryLookup("120.221.155.223"), "China"); + assertEquals(ipLookupByDatabase.countryLookup("192.168.12.1"), "Private IP"); + assertEquals(ipLookupByDatabase.countryLookup("2001:db8:3333:4444:5555:6666:7777:8888"), ""); + System.out.println(ipLookupByDatabase.infoLookupToCSV("120.221.155.223")); + } + + @Test + public void testProvinceLookup() { + assertEquals(ipLookupByDatabase.provinceLookup("120.221.155.223"), "Shandong"); + assertEquals(ipLookupByDatabase.provinceLookup("192.168.12.1"), "Private IP"); + assertEquals(ipLookupByDatabase.provinceLookup("2001:db8:3333:4444:5555:6666:7777:8888"), ""); + } + + @Test + public void testCityLookup() { + assertEquals(ipLookupByDatabase.cityLookup("120.221.155.223"), "Other"); + assertEquals(ipLookupByDatabase.cityLookup("192.168.12.1"), "Private IP"); + assertEquals(ipLookupByDatabase.cityLookup("2001:db8:3333:4444:5555:6666:7777:8888"), ""); + } + + @Test + public void testCityLookupDetail() { + assertEquals(ipLookupByStream.cityLookupDetail("120.221.155.223").split("\\" + AbstractDatabaseReader.LOCATION_SEPARATOR).length, 3); + assertEquals(ipLookupByDatabase.cityLookupDetail("192.168.12.1"), "Private IP"); + assertEquals(ipLookupByDatabase.cityLookupDetail("2001:db8:3333:4444:5555:6666:7777:8888"), ".."); + } + + @Test + public void testCountryCodeLookup() { + assertEquals(ipLookupByDatabase.countryCodeLookup("120.221.155.223"), ""); + assertEquals(ipLookupByDatabase.countryCodeLookup("192.168.12.1"), ""); + assertEquals(ipLookupByDatabase.countryCodeLookup("2001:db8:3333:4444:5555:6666:7777:8888"), ""); + } + @Test + public void testRegionCodeLookup() { + assertEquals(ipLookupByDatabase.regionCodeLookup("120.221.155.223"), "-"); + assertEquals(ipLookupByDatabase.regionCodeLookup("192.168.12.1"), ""); + assertEquals(ipLookupByDatabase.regionCodeLookup("2001:db8:3333:4444:5555:6666:7777:8888"), ""); + } + + @Test + public void testInfoLookupJsonString() { + assertTrue(JSONObject.parseObject(ipLookupByDatabase.infoLookupToJSONString("120.221.155.223"), LocationResponse.class) instanceof LocationResponse); + assertEquals(ipLookupByDatabase.infoLookupToJSONString("2001:db8:3333:4444:5555:6666:7777:8888"), ""); + } + + + + @Test public void testOutputJson() { logger.info(ipLookupByStream.infoLookupToJSON("120.221.155.223")); -- cgit v1.2.3