当前位置: 首页>>代码示例>>Java>>正文


Java CityResponse类代码示例

本文整理汇总了Java中com.maxmind.geoip2.model.CityResponse的典型用法代码示例。如果您正苦于以下问题:Java CityResponse类的具体用法?Java CityResponse怎么用?Java CityResponse使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


CityResponse类属于com.maxmind.geoip2.model包,在下文中一共展示了CityResponse类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getIPLocation

import com.maxmind.geoip2.model.CityResponse; //导入依赖的package包/类
/**
 * Returns a {@link Location} object with location information which may
 * not have very strictly accurate information.
 * 
 * @param ipStr         the IP Address for which a {@link Location} will be obtained.
 * @return
 * @throws Exception
 */
public static Location getIPLocation(String ipStr) throws Exception {
    System.out.println("gres = " + Locator.class.getClassLoader().getResource("GeoLite2-City.mmdb"));
    InputStream is = Locator.class.getClassLoader().getResourceAsStream("GeoLite2-City.mmdb");
    DatabaseReader reader = new DatabaseReader.Builder(is).build();
    CityResponse response = reader.city(InetAddress.getByName(ipStr));

    System.out.println("City " +response.getCity());
    System.out.println("ZIP Code " +response.getPostal().getCode());
    System.out.println("Country " +response.getCountry());
    System.out.println("Location " +response.getLocation());
    
    return new Location(response.getCity().toString(), response.getPostal().getCode(), 
        response.getCountry().toString(), response.getLocation().getTimeZone(), response.getLocation().getLatitude(), 
            response.getLocation().getLongitude(), response.getPostal().getConfidence(),
                response.getLocation().getAccuracyRadius(), response.getLocation().getPopulationDensity(),
                    response.getLocation().getAverageIncome());
}
 
开发者ID:fxpresso,项目名称:FXMaps,代码行数:26,代码来源:Locator.java

示例2: getGeoLocation

import com.maxmind.geoip2.model.CityResponse; //导入依赖的package包/类
String getGeoLocation(String remoteAddr) {
	try {
		CityResponse city = this.reader.city(InetAddress.getByName(remoteAddr));
		String cityName = city.getCity().getName();
		String countryName = city.getCountry().getName();
		if (cityName == null && countryName == null) {
			return null;
		}
		else if (cityName == null) {
			return countryName;
		}
		else if (countryName == null) {
			return cityName;
		}
		return cityName + ", " + countryName;
	}
	catch (Exception e) {
		return UNKNOWN;

	}
}
 
开发者ID:spring-projects,项目名称:spring-session,代码行数:22,代码来源:SessionDetailsFilter.java

示例3: getLocation

import com.maxmind.geoip2.model.CityResponse; //导入依赖的package包/类
/**
 * This method takes the ip and returns the MaxMind CityResponse object.
 *
 * @param ip the ip to lookup
 * @return the corresponding MaxMind CityResponse object or null if the viewtool is not inited.
 */
public CityResponse getLocation(String ip) {
    // Get the Location from the LookupService
    CityResponse response = null;
    if (inited) {
        try {
            InetAddress ipAddress = InetAddress.getByName(ip);
            response = cityLookup.city(ipAddress);
        } catch (Exception e) {
            Logger.error(this, "Could not get location from ip address: " + ip, e);
        }
    } else {
        Logger.info(this, "Attempt to Call getLocation and not inited");
    }

    return response;
}
 
开发者ID:aquent,项目名称:dotcms.plugins.geolocation,代码行数:23,代码来源:GeoIP.java

示例4: getCityCountry

import com.maxmind.geoip2.model.CityResponse; //导入依赖的package包/类
/**
 * 获取city库的城市地理信息
 *
 * @param ip
 * @return
 */
public Country getCityCountry(String ip) {
    try {
        // "128.101.101.101"
        InetAddress ipAddress = InetAddress.getByName(ip);
        CityResponse response = cityReader.city(ipAddress);
        Country country = response.getCountry();
        return country;
    } catch (IOException | GeoIp2Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
开发者ID:zerosoft,项目名称:CodeBroker,代码行数:19,代码来源:GeoIPService.java

示例5: createDocFromCityService

import com.maxmind.geoip2.model.CityResponse; //导入依赖的package包/类
@SuppressWarnings("unused")
public static NutchDocument createDocFromCityService(String serverIp,
    NutchDocument doc, WebServiceClient client) throws UnknownHostException,
    IOException, GeoIp2Exception {
  CityResponse response = client.city(InetAddress.getByName(serverIp));
  return doc;
}
 
开发者ID:jorcox,项目名称:GeoCrawler,代码行数:8,代码来源:GeoIPDocumentCreator.java

示例6: decorate

import com.maxmind.geoip2.model.CityResponse; //导入依赖的package包/类
public JsonEvent decorate(JsonEvent event) {
    if (!event.has(sourceField)) {
        return event;
    }
    String ip = event.valueAsString(sourceField);
    Optional<CityResponse> locationOptional = locationOf(ip);
    if (!locationOptional.isPresent()) {
        return event;
    }

    CityResponse location = locationOptional.get();

    ObjectNode geoIpNode = Json.OBJECT_MAPPER.createObjectNode();

    // Wrapper objects are never null but actual values can be null
    put(geoIpNode, "country_code2", location.getCountry().getIsoCode());
    put(geoIpNode, "country_code3", location.getCountry().getIsoCode());
    put(geoIpNode, "country_name", location.getCountry().getName());
    put(geoIpNode, "continent_code", location.getContinent().getCode());
    put(geoIpNode, "continent_name", location.getContinent().getName());
    put(geoIpNode, "city_name", location.getCity().getName());
    put(geoIpNode, "timezone", location.getLocation().getTimeZone());
    put(geoIpNode, "latitude", location.getLocation().getLatitude().doubleValue());
    put(geoIpNode, "longitude", location.getLocation().getLongitude().doubleValue());

    geoIpNode.set("location", Json.createArrayNode (
           location.getLocation().getLongitude().doubleValue(),
           location.getLocation().getLatitude().doubleValue()));

    event.unsafe().set(targetField, geoIpNode);
    return event;
}
 
开发者ID:sonyxperiadev,项目名称:lumber-mill,代码行数:33,代码来源:GeoIP.java

示例7: hasLocations

import com.maxmind.geoip2.model.CityResponse; //导入依赖的package包/类
private boolean hasLocations(CityResponse location) {
    if (location == null || (location.getLocation().getLatitude() == null
            || location.getLocation().getLongitude() == null)) {
        return false;
    }
    return true;
}
 
开发者ID:sonyxperiadev,项目名称:lumber-mill,代码行数:8,代码来源:GeoIP.java

示例8: getCity

import com.maxmind.geoip2.model.CityResponse; //导入依赖的package包/类
String getCity(CityResponse cityResponse, InetAddress ipAddress) {

        String city = cityResponse.getCity().getName();
        if (city == null) {
            city = ipAddress.getHostAddress();
        }

        return city;
    }
 
开发者ID:githublemming,项目名称:CloudTrailViewer,代码行数:10,代码来源:GeoService.java

示例9: retrieveCityFromIp

import com.maxmind.geoip2.model.CityResponse; //导入依赖的package包/类
@Test
public void retrieveCityFromIp() throws IOException {

    InetAddress ipAddress = InetAddress.getByName(IP_ADDRESS);

    try {
        CityResponse cityResponse = geoService.getCityResponse(ipAddress);
        String city = geoService.getCity(cityResponse, ipAddress);

        assertEquals(city, CITY);

    } catch (GeoIp2Exception e) {
        e.printStackTrace();
    }
}
 
开发者ID:githublemming,项目名称:CloudTrailViewer,代码行数:16,代码来源:GeoServicesTests.java

示例10: retrieveCountryFromIp

import com.maxmind.geoip2.model.CityResponse; //导入依赖的package包/类
@Test
public void retrieveCountryFromIp() throws IOException {

    InetAddress ipAddress = InetAddress.getByName(IP_ADDRESS);

    try {
        CityResponse cityResponse = geoService.getCityResponse(ipAddress);
        String country = geoService.getCountry(cityResponse);

        assertEquals(country, COUNTRY);

    } catch (GeoIp2Exception e) {
        e.printStackTrace();
    }
}
 
开发者ID:githublemming,项目名称:CloudTrailViewer,代码行数:16,代码来源:GeoServicesTests.java

示例11: retrieveContinentFromIp

import com.maxmind.geoip2.model.CityResponse; //导入依赖的package包/类
@Test
public void retrieveContinentFromIp() throws IOException {

    InetAddress ipAddress = InetAddress.getByName(IP_ADDRESS);

    try {
        CityResponse cityResponse = geoService.getCityResponse(ipAddress);
        String continent = geoService.getContinent(cityResponse);

        assertEquals(continent, CONTINENT);

    } catch (GeoIp2Exception e) {
        e.printStackTrace();
    }
}
 
开发者ID:githublemming,项目名称:CloudTrailViewer,代码行数:16,代码来源:GeoServicesTests.java

示例12: handle

import com.maxmind.geoip2.model.CityResponse; //导入依赖的package包/类
@Override
public void handle(String line) {
	Matcher matcher = TailService.this.accessLogPattern.matcher(line);

	if (!matcher.matches()) {
		// System.out.println(line);
		return;
	}

	String ip = matcher.group(1);
	if (!"-".equals(ip) && !"127.0.0.1".equals(ip)) {
		CityResponse cr = lookupCity(ip);
		if (cr != null) {
			Access access = new Access();
			access.setIp(ip);
			access.setDate(Instant.now().toEpochMilli());
			access.setCity(cr.getCity().getName());
			access.setCountry(cr.getCountry().getName());

			String userAgent = matcher.group(9);
			ReadableUserAgent ua = TailService.this.parser.parse(userAgent);
			if (ua != null && ua.getFamily() != UserAgentFamily.UNKNOWN) {
				String uaString = ua.getName() + " "
						+ ua.getVersionNumber().toVersionString();
				uaString += "; " + ua.getOperatingSystem().getName();
				uaString += "; " + ua.getFamily();
				uaString += "; " + ua.getTypeName();
				uaString += "; " + ua.getProducer();

				access.setMessage(matcher.group(4) + "; " + uaString);
			}
			else {
				access.setMessage(null);
			}
			access.setLl(new Double[] { cr.getLocation().getLatitude(),
					cr.getLocation().getLongitude() });

			TailService.this.eventMessenger.sendToAll("/queue/geoip", access);
		}
	}
}
 
开发者ID:ralscha,项目名称:wampspring-demos,代码行数:42,代码来源:TailService.java

示例13: populateGeoData

import com.maxmind.geoip2.model.CityResponse; //导入依赖的package包/类
void populateGeoData(AwsData data) {

        if (reader == null) {
            return;
        }

        String ip = "";

        if (data instanceof Event) {
            ip = ((Event)data).getSourceIPAddress();

        } else if (data instanceof ElbLog) {
            ip = ((ElbLog)data).getClientAddress();

        } else if (data instanceof VpcFlowLog) {
            ip = ((VpcFlowLog)data).getSrcaddr();

        } else {
            return;
        }

        if (!isIp(ip)) {
            return;
        }

        InetAddress ipAddress;
        CityResponse response;
        try {
            ipAddress = InetAddress.getByName(ip);
            response = getCityResponse(ipAddress);

        } catch (IOException | GeoIp2Exception e) {
            return;
        }

        String latLng = getLatLong(response.getLocation());

        data.setContinent(getContinent(response));
        data.setCountry(getCountry(response));
        data.setCity(getCity(response, ipAddress));
        data.setLatLng(latLng);
    }
 
开发者ID:githublemming,项目名称:CloudTrailViewer,代码行数:43,代码来源:GeoService.java

示例14: getCityResponse

import com.maxmind.geoip2.model.CityResponse; //导入依赖的package包/类
CityResponse getCityResponse(InetAddress ipAddress) throws GeoIp2Exception, IOException {
    return reader.city(ipAddress);
}
 
开发者ID:githublemming,项目名称:CloudTrailViewer,代码行数:4,代码来源:GeoService.java

示例15: getCountry

import com.maxmind.geoip2.model.CityResponse; //导入依赖的package包/类
String getCountry(CityResponse cityResponse) {
    return cityResponse.getCountry().getName();
}
 
开发者ID:githublemming,项目名称:CloudTrailViewer,代码行数:4,代码来源:GeoService.java


注:本文中的com.maxmind.geoip2.model.CityResponse类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。