diff options
| author | doufenghu <[email protected]> | 2021-06-15 18:40:58 +0800 |
|---|---|---|
| committer | doufenghu <[email protected]> | 2021-06-15 18:40:58 +0800 |
| commit | 84891f5815eec454798042d9284885dd83fcbe74 (patch) | |
| tree | 65da768e55bde48a8bfe8989518603f4506fdbfa | |
| parent | 51f420e3c7d76213be34953f695205bc5fa6983b (diff) | |
* 删除S3接口相关测试类与依赖库
* 删除废弃的日期工具类 DateUtil
* 优化IP与ASN工具类,不存在返回空字符,私有IP标识为Private IP
| -rw-r--r-- | pom.xml | 8 | ||||
| -rw-r--r-- | src/main/java/com/zdjizhi/domain/LocationResponse.java | 12 | ||||
| -rw-r--r-- | src/main/java/com/zdjizhi/utils/AbstractIpLookup.java | 5 | ||||
| -rw-r--r-- | src/main/java/com/zdjizhi/utils/AsnLookup.java | 42 | ||||
| -rw-r--r-- | src/main/java/com/zdjizhi/utils/DateUtil.java | 470 | ||||
| -rw-r--r-- | src/main/java/com/zdjizhi/utils/IpLookup.java | 111 | ||||
| -rw-r--r-- | src/test/java/com/zdjizhi/test/DateUtilTest.java | 58 | ||||
| -rw-r--r-- | src/test/java/com/zdjizhi/test/FormatUtilTest.java | 3 | ||||
| -rw-r--r-- | src/test/java/com/zdjizhi/test/IpLookupTest.java | 43 | ||||
| -rw-r--r-- | src/test/java/com/zdjizhi/test/S3Test.java | 140 |
10 files changed, 125 insertions, 767 deletions
@@ -257,15 +257,7 @@ <version>23.0</version> </dependency> - <dependency> - - <groupId>com.amazonaws</groupId> - - <artifactId>aws-java-sdk</artifactId> - <version>1.11.781</version> - - </dependency> </dependencies> diff --git a/src/main/java/com/zdjizhi/domain/LocationResponse.java b/src/main/java/com/zdjizhi/domain/LocationResponse.java index 2c882d6..e2b6c63 100644 --- a/src/main/java/com/zdjizhi/domain/LocationResponse.java +++ b/src/main/java/com/zdjizhi/domain/LocationResponse.java @@ -34,6 +34,10 @@ public class LocationResponse { private String countryCode; + private boolean isPrivateIP; + + + public LocationResponse() { this("", "", "", "", "", "", "", "", "", ""); @@ -138,5 +142,13 @@ public class LocationResponse { public void setCountryCode(String countryCode) { this.countryCode = countryCode; } + + public boolean isPrivateIP() { + return isPrivateIP; + } + + public void setPrivateIP(boolean privateIP) { + isPrivateIP = privateIP; + } } diff --git a/src/main/java/com/zdjizhi/utils/AbstractIpLookup.java b/src/main/java/com/zdjizhi/utils/AbstractIpLookup.java index 6de18ff..6a8d264 100644 --- a/src/main/java/com/zdjizhi/utils/AbstractIpLookup.java +++ b/src/main/java/com/zdjizhi/utils/AbstractIpLookup.java @@ -12,7 +12,10 @@ import org.slf4j.LoggerFactory; **/ public abstract class AbstractIpLookup { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); - protected static final String UNKNOW = "Unknown"; + protected static final String UNKNOWN = ""; + protected static final String SEPARATOR = ","; + protected static final String PRIVATE_IP = "Private IP"; + /** * 给定IP库文件路径,获取国家代码 * @param ip ip地址 diff --git a/src/main/java/com/zdjizhi/utils/AsnLookup.java b/src/main/java/com/zdjizhi/utils/AsnLookup.java index fbd44b8..3b3c400 100644 --- a/src/main/java/com/zdjizhi/utils/AsnLookup.java +++ b/src/main/java/com/zdjizhi/utils/AsnLookup.java @@ -1,5 +1,6 @@ package com.zdjizhi.utils; +import com.google.common.base.Joiner; import com.maxmind.db.CHMCache; import com.maxmind.geoip2.exception.AddressNotFoundException; import com.zdjizhi.domain.LocationResponse; @@ -22,12 +23,13 @@ public class AsnLookup { enum ServiceEnum { PRIVATE, PUBLIC } - protected static final String UNKNOW = "N/A"; + protected static final String UNKNOWN = ""; + protected static final String SEPARATOR = ","; private final Logger logger = LoggerFactory.getLogger(this.getClass()); public final static String DEFAULT_DATABASE_PATH = "dat"; private final static String DEFAULT_DB_ASN_PUBLIC = "asn.mmdb"; - private final static String DEFAULT_DB_ASN_PUBLIC_V4 = "asn_v4_201901.mmdb"; - private final static String DEFAULT_DB_ASN_PUBLIC_V6 = "asn_v6_201901.mmdb"; + private final static String DEFAULT_DB_ASN_PUBLIC_V4 = "asn_v4.mmdb"; + private final static String DEFAULT_DB_ASN_PUBLIC_V6 = "asn_v6.mmdb"; private final static String DEFAULT_DB_ASN_PRIVATE = "asn_private.mmdb"; private final static String DEFAULT_DB_ASN_PRIVATE_V4 = "asn_private_v4.mmdb"; private final static String DEFAULT_DB_ASN_PRIVATE_V6 = "asn_private_v6.mmdb"; @@ -287,46 +289,36 @@ public class AsnLookup { public String asnLookup(String ip) { - String result = UNKNOW; - LocationResponse response = getResponse(ip); + LocationResponse response = getResponse(ip); - if (StringUtil.isNotEmpty(response)) { - result = StringUtil.isNotBlank(response.getAsn()) ? response.getAsn() : UNKNOW; + if (StringUtil.isEmpty(response) || response.isPrivateIP()) { + return UNKNOWN; } - return result; + return StringUtil.setDefaultIfEmpty(response.getAsn(), UNKNOWN).toString() ; + } public String asnLookupInfo(String ip) { - String result = UNKNOW; LocationResponse response = getResponse(ip); - - - if (StringUtil.isNotEmpty(response)) { - result = StringUtil.setDefaultIfEmpty(response.getOrganization(), UNKNOW) - +"," + StringUtil.setDefaultIfEmpty(response.getCountryCode(), UNKNOW); + if (StringUtil.isEmpty(response) || response.isPrivateIP()) { + return UNKNOWN; } - - return result; + return Joiner.on(SEPARATOR).useForNull("").join(response.getOrganization(), response.getCountryCode()); } public String asnLookupDetail(String ip) { - String result = UNKNOW; LocationResponse response = getResponse(ip); - if (StringUtil.isNotEmpty(response)) { - result = StringUtil.setDefaultIfEmpty(response.getAsn(), UNKNOW) + "\t" - + StringUtil.setDefaultIfEmpty(response.getOrganization(), UNKNOW) - +"," + StringUtil.setDefaultIfEmpty(response.getCountryCode(), UNKNOW); + if (StringUtil.isEmpty(response) || response.isPrivateIP()) { + return UNKNOWN; } - - - return result; + return Joiner.on(SEPARATOR).useForNull("").join(response.getAsn(), response.getOrganization(), response.getCountryCode()); } @@ -352,6 +344,7 @@ public class AsnLookup { private GalaxyDataBaseReader getAsnDataBaseReaderV4(String service) { + if (service.equalsIgnoreCase(ServiceEnum.PRIVATE.name())) { if (StringUtil.isNotEmpty(asnLocationPrivateReaderV4)) { return asnLocationPrivateReaderV4; @@ -368,6 +361,7 @@ public class AsnLookup { } private GalaxyDataBaseReader getAsnDataBaseReaderV6(String service) { + if (service.equalsIgnoreCase(ServiceEnum.PRIVATE.name())) { if (StringUtil.isNotEmpty(asnLocationPrivateReaderV6)) { return asnLocationPrivateReaderV6; diff --git a/src/main/java/com/zdjizhi/utils/DateUtil.java b/src/main/java/com/zdjizhi/utils/DateUtil.java deleted file mode 100644 index d35ff13..0000000 --- a/src/main/java/com/zdjizhi/utils/DateUtil.java +++ /dev/null @@ -1,470 +0,0 @@ -package com.zdjizhi.utils; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.time.format.DateTimeFormatter; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; -import java.util.List; -import java.util.Locale; -import java.util.TimeZone; - - -/** - * - * <p>时间/日期处理工具类</p> - * @author 中电积至有限公司 darnell - * @version 1.0 创建时间:2010-11-8 下午04:48:57 - * 该工具类较繁琐,功能单一并且有内存泄露及线程安全问题(SimpleDateFormat),日期处理请参见@see com.zdjizhi.utils.DateUtils - */ -@Deprecated -public final class DateUtil implements TimeConstants{ - - private static SimpleDateFormat simpleDateFormat = null; - - /** - *<p>Description:抑制默认的构造器,避免实例化对象 </p> - */ - private DateUtil() { - - } - - - - - - /** - * - * <p>获得当前日期,格式为默认yyyy-MM-dd.</p> - * <pre> - * DateUtil.getCurrentDate() = "2010-11-02" - * </pre> - * @see #getCurrentDate(String) - * @return String 返回标准日期格式(yyyy-MM-dd) - */ - public static String getCurrentDate() { - - simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD); - return simpleDateFormat.format(new Date()); - } - - /** - * - * <p> - * 获得当前时间. - * 如果pattern为空,将抛出{@link NullPointerException} - * </p> - * <pre> - * DateUtil.getCurrentDate(TimeConstants.YYYY_MM_DD) = "2010-11-02" - * DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss") = "2010-11-02 17:20:59" - * </pre> - * @param pattern 描述日期和时间格式. - * @return 当前日期 - * - */ - public static String getCurrentDate(String pattern) { - - if(StringUtil.isEmpty(pattern)){ - throw new NullPointerException("未规定时间格式."); - } - simpleDateFormat = new SimpleDateFormat(pattern); - return simpleDateFormat.format(new Date()); - } - - /** - * - * <p> 获得昨天时间.通过指定日期和时间格式,具体可参考本类提供的时间格式常量.</p> - * <pre> - * DateUtil.getDateOfYesterday(TimeConstants.YYYY_MM_DD_HH24_MM_SS) = "2010-11-04 16:59:13" - * </pre> - * @param pattern 描述日期和时间格式 - * @see #getFormatDate(Date, String) - * @return String 返回日期的字符串表现形式值. - */ - public static String getDateOfYesterday(String pattern) { - - Calendar calendar = Calendar.getInstance(); - calendar.add(Calendar.DAY_OF_MONTH, -1); - Date time = calendar.getTime(); - return getFormatDate(time,pattern ); - - } - - /** - * - * <p> - * 得到距离当天<code>offset</code>天数的某天日期,通过 <code>offset</code> 的值(+/-)来确定增减计算. - * </p> - * <pre> - * 当天日期: 2010-11-05 - * DateUtil.getSomeDate("yyyy-MM-dd", 2) = 2010-11-07 - * DateUtil.getSomeDate("yyyy-MM-dd", -2) = 2010-11-03 - * DateUtil.getSomeDate("yyyy-MM-dd", 0) = 2010-11-05 - * </pre> - * @param pattern 描述日期和时间格式 - * @param offset 描述增加\减少几日,为0时为当前日期. - * @see #getFormatDate(Date, String) - * @return 返回日期格式的字符串 - */ - - public static String getSomeDate(String pattern, int offset) { - - Calendar calendar = Calendar.getInstance(); - calendar.add(Calendar.DAY_OF_MONTH, offset); - Date time = calendar.getTime(); - - return getFormatDate(time,pattern); - - } - - /** - * - * <p>获得指定日期当月第一天</p> - * <pre> - * DateUtil.getFirstDayOfMonth(TimeConstants.YYYY_MM_DD_HH24_MM_SS, new Date()) = "2010-11-01 00:00:00" - * </pre> - * @param date 指定日期. - * @param pattern 描述日期和时间格式 - * @return 返回日期格式的字符串 - */ - public static String getFirstDayOfMonth(Date date, - String pattern) { - - if (StringUtil.isEmpty(pattern) || StringUtil.isEmpty(date)) { - throw new NullPointerException(); - } - - Calendar calendar = Calendar.getInstance(); - calendar.setTime(date); - - Integer minDateNum = calendar.getActualMinimum(Calendar.DAY_OF_MONTH); - - calendar.set(Calendar.DAY_OF_MONTH, minDateNum); - calendar.set(Calendar.HOUR, calendar.getActualMinimum(Calendar.HOUR)); - calendar.set(Calendar.AM_PM, Calendar.AM); - calendar.set(Calendar.MINUTE, calendar - .getActualMinimum(Calendar.MINUTE)); - calendar.set(Calendar.SECOND, calendar - .getActualMinimum(Calendar.SECOND)); - - return getFormatDate(calendar.getTime(), pattern ); - } - - /** - * - * <p>获得指定日期当月最后一天</p> - * <pre> - * DateUtil.getLastDayOfMonth(TimeConstants.YYYY_MM_DD_HH24_MM_SS, new Date()) = "2010-11-30 23:59:59" - * </pre> - * @param date 指定日期 - * @param pattern 描述日期和时间格式 - * @return 返回日期格式的字符串 - */ - public static String getLastDayOfMonth(Date date, - String pattern) { - - if (StringUtil.isEmpty(pattern) || StringUtil.isEmpty(date)) { - throw new NullPointerException(); - } - - Calendar c = Calendar.getInstance(); - c.setTime(date); - Integer maxDateNum = c.getActualMaximum(Calendar.DAY_OF_MONTH); - c.set(Calendar.DAY_OF_MONTH, maxDateNum); - c.set(Calendar.HOUR, c.getActualMaximum(Calendar.HOUR)); - c.set(Calendar.AM_PM, Calendar.PM); - c.set(Calendar.MINUTE, c.getActualMaximum(Calendar.MINUTE)); - c.set(Calendar.SECOND, c.getActualMaximum(Calendar.SECOND)); - return getFormatDate(c.getTime(),pattern); - } - - /** - * - * <p>获得指定日期的日.</p> - * @param date 日期值 - * @return 返回日期的日.如果{@link java.util.Date}为<code>null</code>,抛出异常. - * @throws NullPointerException - */ - public static String getDDFromDate(Date date) { - if (null==date) { - throw new NullPointerException("未存储日期对象"); - } - Calendar calendar = Calendar.getInstance(); - calendar.setTime(date); - - return calendar.get(Calendar.DATE)+""; - - } - - /** - * - * <p>获得指定日期的月.</p> - * @param date 指定日期 - * @return 返回日期的月.如果{@link java.util.Date}为<code>null</code>,抛出异常. - * @throws NullPointerException - */ - public static String getMMFromDate(Date date) { - - if (null==date) { - throw new NullPointerException(); - } - Calendar calendar = Calendar.getInstance(); - calendar.setTime(date); - calendar.add(Calendar.MONTH, 1); - - return calendar.get(Calendar.MONTH)+""; - - } - - - /** - * - * <p>获得指定日期的年.</p> - * @param date 指定日期 - * @return 返回日期的年.如果{@link java.util.Date}为<code>null</code>,抛出异常. - * @throws NullPointerException - */ - public static String getYYFromDate(Date date) { - - if (null==date) { - throw new NullPointerException(); - } - Calendar calendar = Calendar.getInstance(); - calendar.setTime(date); - - return calendar.get(Calendar.YEAR)+""; - - } - - - /** - * - * <p>返回两个日期之间的所有天数,放入集合{@link List}中.</p> - * <pre> - * DateUtil.getDateList(DateUtil.convertStringToDate(DateUtil.DATE_PATTERN_YYYY_MM_DD, - * "2010-10-22"),new Date()) = list - * </pre> - * @param begin 开始时间 - * @param end 结束时间 - * @return 返回两个日期相隔时间集合 - * @throws NullPointerException - */ - public static List<Date> getDateList(Date begin, Date end) { - - if (null == begin || null == end ) { - throw new NullPointerException(); - } - - List<Date> result = new ArrayList<Date>(); - Calendar calendar1 = Calendar.getInstance(); - Calendar calendar2 = Calendar.getInstance(); - calendar1.setTime(begin); - calendar2.setTime(end); - while (calendar1.before(calendar2)) { - result.add(calendar1.getTime()); - calendar1.add(Calendar.DAY_OF_MONTH, 1); - } - - return result; - - } - - /** - * - * <p>根据两个日期获得之间所有的工作日期返回一个日期数组 (工作日不包括星期六和星期日)</p> - * <pre> - * DateUtil.getWorkingdayList(DateUtil.convertStringToDate(TimeConstants.YYYY_MM_DD, - "2010-10-15"),new Date()) = list - * </pre> - * @param begin 开始日期 - * @param end 结束日期 - * @return 返回两个日期相隔时间集合 - * @throws NullPointerException - */ - public static List<Date> getWorkingdayList(Date begin, - Date end) { - if (null == begin || null == end ) { - throw new NullPointerException(); - } - List<Date> list = getDateList(begin, end); - List<Date> result = new ArrayList<Date>(); - Calendar calendar = Calendar.getInstance(); - - for (int i = 0; i < list.size(); i++) { - - calendar.setTime(list.get(i)); - if (calendar.get(Calendar.DAY_OF_WEEK) == 1 - || calendar.get(Calendar.DAY_OF_WEEK) == 7) { - continue; - } else { - result.add(list.get(i)); - } - - } - return result; - } - - /** - * - * <p>根据日期类型的数组获得数组内所有日期对应的星期X列表</p> - * @param list 日期类型的数组 - * @return 星期X列表 - */ - public static List<String> getListOfDayOfWeek(List<Date> list) { - - List<String> result = new ArrayList<String>(); - Calendar calendar = Calendar.getInstance(); - for (Date date:list) { - calendar.setTime(date); - switch (calendar.get(Calendar.DAY_OF_WEEK)) { - case 1: - result.add("星期天"); - break; - case 2: - result.add("星期一"); - break; - case 3: - result.add("星期二"); - break; - case 4: - result.add("星期三"); - break; - case 5: - result.add("星期四"); - break; - case 6: - result.add("星期五"); - break; - case 7: - result.add("星期六"); - break; - default: - break; - } - - } - return result; - } - - - /** - * - * <p>将日期,转换成10进制日期</p> - * <p> - * pattern需跟date格式相同,才可进行转换.转换后的长整型. - * </p> - * <pre> - * DateUtil.convertStringToTimestamp(TimeConstants.YYYY_MM_DD, "2010-10-11") = 1286726400 - * </pre> - * @param date 时间值 - * @param pattern 描述日期与格式 - * @throws NullPointerException - * @see #convertTimestampToString(long, String) - * @return 返回10进制时间值字符串 - */ - public static long convertStringToTimestamp(String date, - String pattern){ - - if(StringUtil.isEmpty(date) || StringUtil.isEmpty(pattern)) { - throw new NullPointerException("参数为NULL"); - } - - long time = 0L; - simpleDateFormat = new SimpleDateFormat(pattern); - try { - time = simpleDateFormat.parse(date).getTime() / 1000; - } catch (ParseException e) { - e.printStackTrace(); - } - return time; - - } - - - /** - * - * <p>将10进制,转换日期字符串类型.</p> - * <pre> - * DateUtil.convertTimestampToString(TimeConstants.YYYY_MM_DD, 1286726400l) = "2010-10-11 00:00:00" - * </pre> - * @param time10 时间戳值 - * @param pattern 描述日期与格式 - * @see #getFormatDate(Date, String) - * @see #convertStringToTimestamp(String, String) - * @return 返回日期字符串 - * @throws NullPointerException - */ - public static String convertTimestampToString( - long time10, String pattern) { - - if(StringUtil.isEmpty(pattern)){ - throw new NullPointerException("参数为NULL"); - } - Date date = new Date(); - date.setTime(time10 * 1000); - return getFormatDate(date,pattern); - } - - /** - * - * <p> - * 将日期转变为字符串的表现形式. - * </p> - * <p> - * 日期格式可以参考本类提供的日期常量,也可自己定义日期格式. - * 当<code>pattern</code> 或 <code>date</code> 为空时,抛出空指针异常. - * </p> - * <pre> - * DateUtil.getFormatDate("yyyyMMddHHmmss", new Date()) = "20101102174448" - * </pre> - * @param date 解析的日期值 - * @param pattern 描述日期与时间格式 - * @return 返回日期的字符串表现形式. - * @throws NullPointerException - */ - public static String getFormatDate(Date date , String pattern) { - - if(StringUtil.isEmpty(pattern) || StringUtil.isEmpty(date)){ - throw new NullPointerException("参数为NULL"); - } - simpleDateFormat = new SimpleDateFormat(pattern); - - return simpleDateFormat.format(date); - } - - - /** - * - * <p>通过字符串日期类型,转换为Date类型日期.</p> - * <p> - * pattern格式,需跟提供的日期字符串格式相统一,否则解析异常返回NULL值。 - * </P> - * <pre> - * DateUtil.convertStringToDate(TimeConstants.YYYY_MM_DD, "2010-09-11") = date类型日期 - * DateUtil.convertStringToDate(TimeConstants.YYYY_MM_DD_HH_MM, "2010-09-11") = NULL. - * </pre> - * @param date 字符串类型日期. - * @param pattern 描述日期与时间格式. - * @return 返回解析后的{@link java.util.Date }类型日期. - * @throws NullPointerException - */ - public static Date convertStringToDate(String date, - String pattern) { - - if (StringUtil.isEmpty(date) || StringUtil.isEmpty(pattern)) { - throw new NullPointerException(); - } - simpleDateFormat = new SimpleDateFormat(pattern); - Date parseDate = null; - - try { - parseDate = simpleDateFormat.parse(date); - } catch (ParseException e) { - e.printStackTrace(); - } - - return parseDate; - } - -} diff --git a/src/main/java/com/zdjizhi/utils/IpLookup.java b/src/main/java/com/zdjizhi/utils/IpLookup.java index bc8de36..cb82680 100644 --- a/src/main/java/com/zdjizhi/utils/IpLookup.java +++ b/src/main/java/com/zdjizhi/utils/IpLookup.java @@ -23,7 +23,6 @@ public final class IpLookup extends AbstractIpLookup{ PRIVATE, PUBLIC } public final static String DEFAULT_DATABASE_PATH = "dat"; - private final static String SEPRATOR = ","; private final static String DEFAULT_DB_IP_PUBLIC = "ip.mmdb"; private final static String DEFAULT_DB_IP_PUBLIC_V4 = "ip_v4.mmdb"; private final static String DEFAULT_DB_IP_PUBLIC_V6 = "ip_v6.mmdb"; @@ -33,18 +32,15 @@ public final class IpLookup extends AbstractIpLookup{ private AsnLookup asnLookup; - private static GalaxyDataBaseReader ipLocationPublicReader; private static GalaxyDataBaseReader ipLocationPublicReaderV4; private static GalaxyDataBaseReader ipLocationPublicReaderV6; - private static GalaxyDataBaseReader ipLocationPrivateReader; private static GalaxyDataBaseReader ipLocationPrivateReaderV4; private static GalaxyDataBaseReader ipLocationPrivateReaderV6; - synchronized void init(Builder builder) { try { @@ -183,7 +179,7 @@ public final class IpLookup extends AbstractIpLookup{ } catch (Exception e) { e.printStackTrace(); - logger.warn("Unknow mmdb dat file , please check your dat path!"); + logger.warn("Unknown mmdb dat file , please check your dat path!"); } @@ -378,100 +374,78 @@ public final class IpLookup extends AbstractIpLookup{ @Override public String countryLookup(String ip) { - String result = ""; LocationResponse response = getResponse(ip); - - if (StringUtil.isNotEmpty(response)) { - result = StringUtil.setDefaultIfEmpty(response.getCountry(), "").toString() ; + if (StringUtil.isEmpty(response)) { + return UNKNOWN; + } + if (response.isPrivateIP()) { + return PRIVATE_IP; } - return StringUtil.setDefaultIfEmpty(result, UNKNOW).toString(); + return StringUtil.setDefaultIfEmpty(response.getCountry(), UNKNOWN).toString() ; } @Override public String cityLookupDetail(String ip) { - String result = ""; LocationResponse response = getResponse(ip); - if (StringUtil.isNotEmpty(response)) { - result = Joiner.on(SEPRATOR).skipNulls().join(response.getCity(), response.getProvince(), response.getCountry()); + if (StringUtil.isEmpty(response)) { + return UNKNOWN; + } + if (response.isPrivateIP()) { + return PRIVATE_IP; } - return StringUtil.setDefaultIfEmpty(result, UNKNOW).toString(); + return Joiner.on(SEPARATOR).useForNull("").join(response.getCity(), response.getProvince(), response.getCountry()); + } @Override public String cityLookup(String ip) { - String result = ""; LocationResponse response = getResponse(ip); - if (StringUtil.isNotEmpty(response)) { - if(StringUtil.isNotBlank(response.getCity())) { - result = response.getCity(); - } + if (StringUtil.isEmpty(response)) { + return UNKNOWN; + } + if (response.isPrivateIP()) { + return PRIVATE_IP; } - return StringUtil.setDefaultIfEmpty(result, UNKNOW).toString(); + return StringUtil.setDefaultIfEmpty(response.getCity(), UNKNOWN).toString() ; } @Override public String provinceLookup(String ip) { - String result = ""; LocationResponse response = getResponse(ip); - if (StringUtil.isNotEmpty(response)) { - if (StringUtil.isNotBlank(response.getProvince())) { - result = response.getProvince(); - } + if (StringUtil.isEmpty(response)) { + return UNKNOWN; + } + if (response.isPrivateIP()) { + return PRIVATE_IP; } - return StringUtil.setDefaultIfEmpty(result, UNKNOW).toString(); + return StringUtil.setDefaultIfEmpty(response.getProvince(), UNKNOWN).toString() ; } @Override public String latLngLookup(String ip) { - String result = ""; - try { - LocationResponse response = getResponse(ip); - if (StringUtil.isNotEmpty(response) - && StringUtil.isNotBlank(response.getLatitude()) - && StringUtil.isNotBlank(response.getLongitude())) { - - - result = Joiner.on(SEPRATOR).skipNulls().join(response.getLatitude(), response.getLongitude()); - - } else { - result = Joiner.on(SEPRATOR).join(UNKNOW, UNKNOW); - } - - } catch (Exception e) { - - throw new IllegalArgumentException("ip address :" + ip +", parser error " + e); + LocationResponse response = getResponse(ip); + if (StringUtil.isEmpty(response) || response.isPrivateIP()) { + return UNKNOWN; } - return result; + return Joiner.on(SEPARATOR).skipNulls().join(response.getLatitude(), response.getLongitude()); } @Override public String cityLatLngLookup(String ip) { - String result = ""; - try { - - result = Joiner.on(SEPRATOR).skipNulls().join(cityLookup(ip), latLngLookup(ip)); - - } catch (Exception e) { - - throw new IllegalArgumentException("ip address :" + ip +", parser error " + e); - } - - - return StringUtil.setDefaultIfEmpty(result, UNKNOW).toString(); - + return Joiner.on(SEPARATOR).skipNulls().join(cityLookup(ip), latLngLookup(ip)); } @Override @@ -496,13 +470,11 @@ public final class IpLookup extends AbstractIpLookup{ private LocationResponse getResponse(String ip) { - LocationResponse response = null; - if (!IPUtil.isIP(ip) && !IPUtil.isIPv6Address(ip)) { throw new IllegalArgumentException("unknown ip format :" + ip ); } - response = new Context(new PrivateReader()).executeStragey(ip); + LocationResponse response = new Context(new PrivateReader()).executeStragey(ip); if (StringUtil.isEmpty(response)) { response = new Context(new PublicReader()).executeStragey(ip); @@ -572,6 +544,7 @@ public final class IpLookup extends AbstractIpLookup{ @Override public LocationResponse getResponse(String ip) { GalaxyDataBaseReader dataBaseReader = null; + LocationResponse response = null; try { InetAddress ipAddress = InetAddress.getByName(ip); @@ -582,7 +555,7 @@ public final class IpLookup extends AbstractIpLookup{ } if (StringUtil.isNotEmpty(dataBaseReader)) { - return dataBaseReader.location(ipAddress); + response = dataBaseReader.location(ipAddress); } } catch(AddressNotFoundException addressNotFoundException) { @@ -590,7 +563,7 @@ public final class IpLookup extends AbstractIpLookup{ } catch (Exception e) { throw new IllegalArgumentException("ip address :" + ip +", parser error " + e); } - return null; + return response; } } /** @@ -600,6 +573,7 @@ public final class IpLookup extends AbstractIpLookup{ @Override public LocationResponse getResponse(String ip) { GalaxyDataBaseReader dataBaseReader = null; + LocationResponse response = null; try { InetAddress ipAddress = InetAddress.getByName(ip); @@ -612,7 +586,7 @@ public final class IpLookup extends AbstractIpLookup{ } if (StringUtil.isNotEmpty(dataBaseReader)) { - return dataBaseReader.location(ipAddress); + response = dataBaseReader.location(ipAddress); } } catch(AddressNotFoundException addressNotFoundException) { @@ -620,7 +594,7 @@ public final class IpLookup extends AbstractIpLookup{ } catch (Exception e) { throw new IllegalArgumentException("ip address :" + ip +", parser error " + e); } - return null; + return response; } @@ -632,10 +606,10 @@ public final class IpLookup extends AbstractIpLookup{ class InnerReader implements Strategy { @Override public LocationResponse getResponse(String ip) { - LocationResponse response = null; - if (IPUtil.isIP(ip) && IPUtil.internalIp(ip)) { + LocationResponse response = new LocationResponse(); + if(IPUtil.isIP(ip) && IPUtil.internalIp(ip)) { response = new LocationResponse(); - response.setCountry("Private IP"); + response.setCountry(null); response.setCity(null); response.setProvince(null); response.setAsn(null); @@ -643,9 +617,10 @@ public final class IpLookup extends AbstractIpLookup{ response.setLatitude(null); response.setLongitude(null); response.setAreaCode(null); + response.setPrivateIP(true); return response; } else { - return null; + return response; } } diff --git a/src/test/java/com/zdjizhi/test/DateUtilTest.java b/src/test/java/com/zdjizhi/test/DateUtilTest.java index 88afd7e..f06de74 100644 --- a/src/test/java/com/zdjizhi/test/DateUtilTest.java +++ b/src/test/java/com/zdjizhi/test/DateUtilTest.java @@ -1,22 +1,14 @@ package com.zdjizhi.test; -import java.util.Arrays; import java.util.Collections; import java.util.Date; import java.util.List; -import java.util.concurrent.TimeUnit; -import com.google.common.base.Stopwatch; -import com.google.common.collect.Collections2; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; -import com.zdjizhi.utils.DateUtil; import com.zdjizhi.utils.DateUtils; import com.zdjizhi.utils.TimeConstants; import org.apache.log4j.Logger; -import org.joda.time.DurationFieldType; -import org.joda.time.Period; -import org.joda.time.PeriodType; import org.junit.Assert; import org.junit.Test; @@ -27,9 +19,7 @@ public class DateUtilTest { @Test public void testGetCurrentDate(){ - logger.info(DateUtil.getCurrentDate()); logger.info(DateUtils.getCurrentDate()); - logger.info(DateUtil.getCurrentDate(TimeConstants.YYYY_MM_DD_HH24_MM_SS)); logger.info(DateUtils.getCurrentDate(TimeConstants.YYYY_MM_DD_HH24_MM_SS)); @@ -37,26 +27,19 @@ public class DateUtilTest { @Test public void testGetSomeday(){ - logger.info(DateUtil.getSomeDate("yyyy-MM-dd", 2)); logger.info(DateUtils.getSomeDate("yyyy-MM-dd", 2)); - logger.info(DateUtil.getSomeDate("yyyy-MM-dd", -2)); logger.info(DateUtils.getSomeDate("yyyy-MM-dd", -2)); - logger.info(DateUtil.getSomeDate("yyyy-MM-dd", 0)); logger.info(DateUtils.getSomeDate("yyyy-MM-dd", 0)); } @Test public void testGetFormatDate(){ - logger.info(DateUtil.getDateOfYesterday(DateUtil.YYYY_MM_DD_HH24_MM_SS)); - logger.info(DateUtils.getDateOfYesterday(DateUtil.YYYY_MM_DD_HH24_MM_SS)); + logger.info(DateUtils.getDateOfYesterday(DateUtils.YYYY_MM_DD_HH24_MM_SS)); - logger.info(DateUtil.getFormatDate( new Date(),DateUtil.YYYYMMDDHH24MMSS)); - logger.info(DateUtils.getFormatDate( new Date(),DateUtil.YYYYMMDDHH24MMSS)); + logger.info(DateUtils.getFormatDate( new Date(),DateUtils.YYYYMMDDHH24MMSS)); } @Test public void testGetDateAsStringToTimestamp(){ - logger.info(DateUtil.convertStringToTimestamp( DateUtil.getCurrentDate(TimeConstants.YYYY_MM_DD_HH24_MM_SS),DateUtil.YYYY_MM_DD_HH24_MM_SS)); - logger.info(DateUtils.convertStringToTimestamp( DateUtils.getCurrentDate(TimeConstants.YYYY_MM_DD_HH24_MM_SS), DateUtils.YYYY_MM_DD_HH24_MM_SS)); @@ -67,9 +50,6 @@ public class DateUtilTest { @Test public void testConvertTimestampToString() { - logger.info(DateUtil.convertTimestampToString( 1536710400,DateUtil.YYYY_MM_DD_HH24_MM_SS)); - - logger.info(DateUtils.convertTimestampToString( 1536710400,DateUtils.YYYY_MM_DD_HH24_MM_SS)); @@ -82,9 +62,8 @@ public class DateUtilTest { @Test public void testGetDateAsStringToDate(){ try { - logger.info(DateUtil.convertStringToDate( "2010-09-11 11:12:23",DateUtil.YYYY_MM_DD_HH24_MM_SS)); - logger.info(DateUtils.convertStringToDate( "2010-09-11 11:12:23",DateUtil.YYYY_MM_DD_HH24_MM_SS)); + logger.info(DateUtils.convertStringToDate( "2010-09-11 11:12:23",DateUtils.YYYY_MM_DD_HH24_MM_SS)); } catch (Exception e) { e.printStackTrace(); } @@ -97,16 +76,10 @@ public class DateUtilTest { @Test public void testGetDateList(){ try { - List<Date> list = DateUtil.getDateList(DateUtil.convertStringToDate("2018-09-01", - DateUtil.YYYY_MM_DD),new Date()); - - for(Date date:list){ - logger.info(DateUtil.getFormatDate(date,DateUtil.YYYY_MM_DD )); - } - List<Date> lists = DateUtils.getEveryDay(DateUtil.convertStringToDate("2018-09-01", - DateUtil.YYYY_MM_DD),new Date()); + List<Date> lists = DateUtils.getEveryDay(DateUtils.convertStringToDate("2018-09-01", + DateUtils.YYYY_MM_DD),new Date()); for(Date date:lists){ logger.info(DateUtils.getFormatDate(date,DateUtils.YYYY_MM_DD )); @@ -121,21 +94,13 @@ public class DateUtilTest { @Test public void testGetDateListForWorkDay(){ try { - List<Date> list = DateUtil.getWorkingdayList(DateUtil.convertStringToDate("2018-09-01",DateUtil.YYYY_MM_DD - ),new Date()); - - - for(Date date:list){ - logger.info(DateUtil.getFormatDate(date,DateUtil.YYYY_MM_DD)); - } - - List<Date> lists = DateUtils.getWorkingdayList(DateUtil.convertStringToDate("2018-09-01",DateUtil.YYYY_MM_DD + List<Date> lists = DateUtils.getWorkingdayList(DateUtils.convertStringToDate("2018-09-01",DateUtils.YYYY_MM_DD ),new Date()); for(Date date:lists){ - logger.info(DateUtil.getFormatDate(date,DateUtil.YYYY_MM_DD)); + logger.info(DateUtils.getFormatDate(date,DateUtils.YYYY_MM_DD)); } } catch (Exception e) { @@ -149,8 +114,7 @@ public class DateUtilTest { @Test public void testGetFirstDayOfMonth(){ try { - logger.info(DateUtil.getFirstDayOfMonth(new Date(),DateUtil.YYYY_MM_DD_HH24_MM_SS )); - logger.info(DateUtils.getFirstDayOfMonth(new Date(),DateUtil.YYYY_MM_DD_HH24_MM_SS )); + logger.info(DateUtils.getFirstDayOfMonth(new Date(),DateUtils.YYYY_MM_DD_HH24_MM_SS )); } catch (Exception e) { e.printStackTrace(); } @@ -160,8 +124,7 @@ public class DateUtilTest { @Test public void testGetLastDayOfMonth(){ try { - logger.info(DateUtil.getLastDayOfMonth(new Date(), DateUtil.YYYY_MM_DD_HH24_MM_SS)); - logger.info(DateUtils.getLastDayOfMonth(new Date(), DateUtil.YYYY_MM_DD_HH24_MM_SS)); + logger.info(DateUtils.getLastDayOfMonth(new Date(), DateUtils.YYYY_MM_DD_HH24_MM_SS)); } catch (Exception e) { e.printStackTrace(); } @@ -173,7 +136,6 @@ public class DateUtilTest { @Test public void testGetDayFromDate(){ try { - logger.info(DateUtil.getDDFromDate(new Date())); logger.info(DateUtils.getDDFromDate(new Date())); } catch (Exception e) { e.printStackTrace(); @@ -186,7 +148,6 @@ public class DateUtilTest { @Test public void testGetMonthFromDate(){ try { - logger.info(DateUtil.getMMFromDate(new Date())); logger.info(DateUtils.getMMFromDate(new Date())); } catch (Exception e) { e.printStackTrace(); @@ -199,7 +160,6 @@ public class DateUtilTest { @Test public void testGetYearFromDate(){ try { - logger.info(DateUtil.getYYFromDate(new Date())); logger.info(DateUtils.getYYFromDate(new Date())); } catch (Exception e) { e.printStackTrace(); diff --git a/src/test/java/com/zdjizhi/test/FormatUtilTest.java b/src/test/java/com/zdjizhi/test/FormatUtilTest.java index 1c1fae3..0fa1d33 100644 --- a/src/test/java/com/zdjizhi/test/FormatUtilTest.java +++ b/src/test/java/com/zdjizhi/test/FormatUtilTest.java @@ -19,8 +19,7 @@ import java.util.List; import java.util.Set; public class FormatUtilTest { - - @Test + @Test public void testGetTopDomain() { Assert.assertEquals(FormatUtils.getTopPrivateDomain("https://pan.baidu.com/s/1wejv5ZceAL0NQGExvPQm-Q#list/path=%2F&parentPath=%2Fsharelink3477893581-1028235543414583"), "baidu.com"); diff --git a/src/test/java/com/zdjizhi/test/IpLookupTest.java b/src/test/java/com/zdjizhi/test/IpLookupTest.java index 8fa823f..530a2a1 100644 --- a/src/test/java/com/zdjizhi/test/IpLookupTest.java +++ b/src/test/java/com/zdjizhi/test/IpLookupTest.java @@ -6,13 +6,15 @@ import org.junit.Test; public class IpLookupTest { Logger logger = Logger.getLogger(IpLookupTest.class); + @Test - public void testIpCoutry() { + public void testIpLookup() { IpLookup ipLookup = new IpLookup.Builder(false) .loadDataFileV4("dat/ip_v4.mmdb") - .loadDataFilePrivateV4("dat/ip_private_v4.mmdb") - .loadDataFilePrivateV6("dat/ip_v6.mmdb") + //.loadDataFilePrivateV4("dat/ip_private_v4.mmdb") + .loadDataFileV6("dat/ip_v6.mmdb") + //.loadDataFilePrivateV6("dat/ip_private_v6.mmdb") .loadAsnDataFile("dat/asn_v4.mmdb") .loadAsnDataFileV6("dat/asn_v6.mmdb") .build(); @@ -23,17 +25,48 @@ public class IpLookupTest { logger.info(ipLookup.cityLookupDetail("192.168.50.21")); logger.info(ipLookup.cityLookup("120.221.155.223")); logger.info(ipLookup.countryLookup("2001:4860:4860::8888")); - logger.info(ipLookup.latLngLookup("119.253.39.18")); + logger.info(ipLookup.latLngLookup("120.221.155.223")); + logger.info(ipLookup.cityLookupDetail("120.221.155.223")); logger.info(ipLookup.cityLatLngLookup("120.221.155.223")); logger.info(ipLookup.asnLookup("120.221.155.223")); logger.info(ipLookup.asnLookup("203.187.160.135")); logger.info(ipLookup.asnLookup("116.128.133.102")); logger.info(ipLookup.asnLookupInfo("116.128.133.102")); - + logger.info(ipLookup.asnLookupDetail("116.128.133.102")); logger.info(ipLookup.asnLookup("2001:4860:4860::8888")); + logger.info(ipLookup.cityLookupDetail("202.106.0.20")); + + + } + + + @Test + public void testCountryLookup() { + + IpLookup ipLookup = new IpLookup.Builder(false) + .loadDataFileV4("dat/ip_v4.mmdb") + .loadDataFileV6("dat/ip_v6.mmdb") + .loadAsnDataFileV4("dat/asn_v4.mmdb") + .loadAsnDataFileV6("dat/asn_v6.mmdb") + .build(); + logger.info(ipLookup.cityLookupDetail("192.168.50.21")); + logger.info(ipLookup.cityLookup("120.221.155.223")); + logger.info(ipLookup.countryLookup("2001:4860:4860::8888")); + logger.info(ipLookup.latLngLookup("119.253.39.18")); + logger.info(ipLookup.latLngLookup("202.102.3.20")); + logger.info(ipLookup.cityLookupDetail("120.221.155.223")); + logger.info(ipLookup.cityLatLngLookup("120.221.155.223")); + logger.info(ipLookup.asnLookup("120.221.155.223")); + logger.info(ipLookup.asnLookup("203.187.160.135")); + logger.info(ipLookup.asnLookupDetail("116.128.133.102")); + logger.info(ipLookup.asnLookupInfo("116.128.133.102")); + logger.info(ipLookup.asnLookup("2001:4860:4860::8888")); } + + + } diff --git a/src/test/java/com/zdjizhi/test/S3Test.java b/src/test/java/com/zdjizhi/test/S3Test.java deleted file mode 100644 index cc3e5c2..0000000 --- a/src/test/java/com/zdjizhi/test/S3Test.java +++ /dev/null @@ -1,140 +0,0 @@ -package com.zdjizhi.test; - -import com.amazonaws.auth.AWSStaticCredentialsProvider; -import com.amazonaws.auth.BasicAWSCredentials; -import com.amazonaws.client.builder.AwsClientBuilder; -import com.amazonaws.services.s3.AmazonS3; -import com.amazonaws.services.s3.AmazonS3ClientBuilder; -import com.amazonaws.services.s3.model.ObjectMetadata; -import com.amazonaws.services.s3.model.PutObjectResult; -import com.amazonaws.services.s3.model.S3Object; -import com.amazonaws.services.s3.model.S3ObjectInputStream; -import com.google.common.collect.Maps; -import com.zdjizhi.utils.StringUtil; -import org.junit.Assert; -import org.junit.Test; - -import java.io.*; -import java.util.Map; - -public class S3Test { - @Test - public void testInit() { - String endpoint = "192.168.40.224:9098"; - String accessKey = "accessKey"; - String secretKey = "secretKey"; - AmazonS3 s3 = AmazonS3ClientBuilder.standard() - .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey))) - .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, "us-east-1")) - .withChunkedEncodingDisabled(true) - .build(); - - Assert.assertNotNull(s3); - - s3.shutdown(); - - } - - - @Test - public void testPutObject() { - String endpoint = "http://192.168.40.224:9098/hos"; - String accessKey = "accessKey"; - String secretKey = "secretKey"; - AmazonS3 s3 = AmazonS3ClientBuilder.standard() - .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey))) - .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, "us-east-1")) - .withChunkedEncodingDisabled(true) - .build(); - - - ObjectMetadata metadata = new ObjectMetadata(); - metadata.setContentType("txt/html"); - Map<String, String> userMap = Maps.newHashMap(); - - //userMap.put("x-hos-meta-message", "{\"name\":\"我是元数据\"}"); - // metadata.setUserMetadata(userMap); - metadata.setHeader("x-hos-meta-message", "{\"name\":\"我是元数据\"}"); - FileInputStream inputStream = null; - try { - inputStream = new FileInputStream(new File("/Users/darnell/Downloads/存储配额.pdf")); - PutObjectResult result = s3.putObject("file", "storage.pdf", inputStream, metadata); - - System.out.println(result.getMetadata().toString()); - Assert.assertNotNull(result.getContentMd5()); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } finally { - if (inputStream != null) { - try { - inputStream.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - - - s3.shutdown(); - - } - - - @Test - public void testGetObject() { - - String endpoint = "http://192.168.40.224:9098/hos"; - String accessKey = "accessKey"; - String secretKey = "secretKey"; - AmazonS3 s3 = AmazonS3ClientBuilder.standard() - .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey))) - .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, "us-east-1")) - .withChunkedEncodingDisabled(true) - .build(); - - S3Object object = s3.getObject("file", "storage.pdf"); - System.out.println(object.getObjectMetadata().getContentType()); - System.out.println(object.getObjectMetadata().getRawMetadataValue("x-hos-meta-message")); - - - InputStream in = null; - OutputStream out = null; - try { - - - try { - in = new BufferedInputStream(object.getObjectContent(), 1024); - out = new BufferedOutputStream(new FileOutputStream(new File("/Users/darnell/Downloads/" + StringUtil.createUUID() + object.getKey())),1024); - byte[] buffer = new byte[1024]; - - int length = -1; - while((length = in.read(buffer)) != -1) - { - out.write(buffer, 0, length); - } - - out.flush(); - } finally { - if (null != in) { - in.close(); - } - if (null != out) { - out.close(); - } - } - } catch (Exception e) { - e.printStackTrace(); - throw new RuntimeException("上传文件时出错!!!"); - } - - - s3.shutdown(); - - - - - - - } - -} |
