/** * @param ipAddress IPv4 or IPv6 address to lookup. * @return A <T> object with the data for the IP address * @throws IOException if there is an error opening or reading from the file. * @throws AddressNotFoundException if the IP address is not in our database */ private <T> T get(InetAddress ipAddress, Class<T> cls, String type) throws IOException, AddressNotFoundException { String databaseType = this.getMetadata().getDatabaseType(); if (!databaseType.contains(type)) { String caller = Thread.currentThread().getStackTrace()[2] .getMethodName(); throw new UnsupportedOperationException( "Invalid attempt to open a " + databaseType + " database using the " + caller + " method"); } ObjectNode node = jsonNodeToObjectNode(reader.get(ipAddress)); // We throw the same exception as the web service when an IP is not in // the database if (node == null) { throw new AddressNotFoundException("The address " + ipAddress.getHostAddress() + " is not in the database."); } InjectableValues inject = new JsonInjector(locales, ipAddress.getHostAddress()); return this.om.reader(inject).treeToValue(node, cls); }
/** * @param ipAddress IPv4 or IPv6 address to lookup. * @return A <T> object with the data for the IP address * @throws IOException if there is an error opening or reading from the file. * @throws AddressNotFoundException if the IP address is not in our database */ private <T> T get(InetAddress ipAddress, Class<T> cls, String type) throws IOException, AddressNotFoundException { String databaseType = this.getMetadata().getDatabaseType(); if (!databaseType.contains(type)) { String caller = Thread.currentThread().getStackTrace()[2] .getMethodName(); throw new UnsupportedOperationException( "Invalid attempt to open a " + databaseType + " database using the " + caller + " method"); } ObjectNode node = jsonNodeToObjectNode(reader.get(ipAddress)); // We throw the same exception as the web service when an IP is not in // the database if (node == null) { throw new AddressNotFoundException("The address " + ipAddress.getHostAddress() + " is not in the database."); } InjectableValues inject = new JsonInjector(locales, ipAddress.getHostAddress()); return this.om.reader(inject).treeToValue(node, cls); }
private static void handleErrorWithJsonBody(Map<String, String> content, String body, int status, URL url) throws GeoIp2Exception, HttpException { String error = content.get("error"); String code = content.get("code"); if (error == null || code == null) { throw new HttpException( "Error response contains JSON but it does not specify code or error keys: " + body, status, url); } switch (code) { case "IP_ADDRESS_NOT_FOUND": case "IP_ADDRESS_RESERVED": throw new AddressNotFoundException(error); case "ACCOUNT_ID_REQUIRED": case "ACCOUNT_ID_UNKNOWN": case "AUTHORIZATION_INVALID": case "LICENSE_KEY_REQUIRED": case "USER_ID_REQUIRED": case "USER_ID_UNKNOWN": throw new AuthenticationException(error); case "INSUFFICIENT_FUNDS": case "OUT_OF_QUERIES": throw new OutOfQueriesException(error); case "PERMISSION_REQUIRED": throw new PermissionRequiredException(error); } // These should be fairly rare throw new InvalidRequestException(error, code, url); }
private static void handleErrorWithJsonBody(Map<String, String> content, String body, int status, URL url) throws GeoIp2Exception, HttpException { String error = content.get("error"); String code = content.get("code"); if (error == null || code == null) { throw new HttpException( "Error response contains JSON but it does not specify code or error keys: " + body, status, url); } switch (code) { case "IP_ADDRESS_NOT_FOUND": case "IP_ADDRESS_RESERVED": throw new AddressNotFoundException(error); case "ACCOUNT_ID_REQUIRED": case "ACCOUNT_ID_UNKNOWN": case "AUTHORIZATION_INVALID": case "LICENSE_KEY_REQUIRED": case "USER_ID_REQUIRED": case "USER_ID_UNKNOWN": throw new AuthenticationException(error); case "INSUFFICIENT_FUNDS": case "OUT_OF_QUERIES": throw new OutOfQueriesException(error); case "PERMISSION_REQUIRED": throw new PermissionRequiredException(error); } // These should be fairly rare throw new InvalidRequestException(error, code, url); }