LOGGER.debug("Address not fount in DB " + ne.getMessage()); } catch(Exception e) { throw new ServiceException(e);
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 HttpGet getResponse(URL url) throws GeoIp2Exception, IOException { Credentials credentials = new UsernamePasswordCredentials(Integer.toString(accountId), licenseKey); HttpGet request; try { request = new HttpGet(url.toURI()); } catch (URISyntaxException e) { throw new GeoIp2Exception("Error parsing request URL", e); } try { request.addHeader(new BasicScheme().authenticate(credentials, request, null)); } catch (org.apache.http.auth.AuthenticationException e) { throw new AuthenticationException("Error setting up request authentication", e); } request.addHeader("Accept", "application/json"); return request; }
private <T> T handleResponse(CloseableHttpResponse response, URL url, Class<T> cls) throws GeoIp2Exception, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 400 && status < 500) { this.handle4xxStatus(response, url); } else if (status >= 500 && status < 600) { throw new HttpException("Received a server error (" + status + ") for " + url, status, url); } else if (status != 200) { throw new HttpException("Received an unexpected HTTP status (" + status + ") for " + url, status, url); } InjectableValues inject = new JsonInjector(locales, null); HttpEntity entity = response.getEntity(); try { return mapper.readerFor(cls).with(inject).readValue(entity.getContent()); } catch (IOException e) { throw new GeoIp2Exception( "Received a 200 response but could not decode it as JSON", e); } finally { EntityUtils.consume(entity); } }
private URL createUri(String service, InetAddress ipAddress) throws GeoIp2Exception { try { return new URIBuilder() .setScheme(useHttps ? "https" : "http") .setHost(host) .setPort(this.port) .setPath("/geoip/v2.1/" + service + "/" + (ipAddress == null ? "me" : ipAddress.getHostAddress())) .build().toURL(); } catch (MalformedURLException e) { throw new GeoIp2Exception("Malformed service URL", e); } catch (URISyntaxException e) { throw new GeoIp2Exception("Syntax error creating service URL", e); } }
private void handle4xxStatus(HttpResponse response, URL url) throws GeoIp2Exception, IOException { HttpEntity entity = response.getEntity(); int status = response.getStatusLine().getStatusCode(); if (entity.getContentLength() == 0L) { throw new HttpException("Received a " + status + " error for " + url + " with no body", status, url); } String body = EntityUtils.toString(entity, "UTF-8"); try { Map<String, String> content = mapper.readValue(body, new TypeReference<HashMap<String, String>>() { }); handleErrorWithJsonBody(content, body, status, url); } catch (HttpException e) { throw e; } catch (IOException e) { throw new HttpException("Received a " + status + " error for " + url + " but it did not include the expected JSON body: " + body, status, url); } }
/** * @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); }
rp.setRemoteAddrLocale(new Locale("", country.getIsoCode())); } catch (AddressNotFoundException e) { logger.debug("Address Not Found: {}", e.getMessage()); logger.trace("", e);
private HttpGet getResponse(URL url) throws GeoIp2Exception, IOException { Credentials credentials = new UsernamePasswordCredentials(Integer.toString(accountId), licenseKey); HttpGet request; try { request = new HttpGet(url.toURI()); } catch (URISyntaxException e) { throw new GeoIp2Exception("Error parsing request URL", e); } try { request.addHeader(new BasicScheme().authenticate(credentials, request, null)); } catch (org.apache.http.auth.AuthenticationException e) { throw new AuthenticationException("Error setting up request authentication", e); } request.addHeader("Accept", "application/json"); return request; }
private <T> T handleResponse(CloseableHttpResponse response, URL url, Class<T> cls) throws GeoIp2Exception, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 400 && status < 500) { this.handle4xxStatus(response, url); } else if (status >= 500 && status < 600) { throw new HttpException("Received a server error (" + status + ") for " + url, status, url); } else if (status != 200) { throw new HttpException("Received an unexpected HTTP status (" + status + ") for " + url, status, url); } InjectableValues inject = new JsonInjector(locales, null); HttpEntity entity = response.getEntity(); try { return mapper.readerFor(cls).with(inject).readValue(entity.getContent()); } catch (IOException e) { throw new GeoIp2Exception( "Received a 200 response but could not decode it as JSON", e); } finally { EntityUtils.consume(entity); } }
private URL createUri(String service, InetAddress ipAddress) throws GeoIp2Exception { try { return new URIBuilder() .setScheme(useHttps ? "https" : "http") .setHost(host) .setPort(this.port) .setPath("/geoip/v2.1/" + service + "/" + (ipAddress == null ? "me" : ipAddress.getHostAddress())) .build().toURL(); } catch (MalformedURLException e) { throw new GeoIp2Exception("Malformed service URL", e); } catch (URISyntaxException e) { throw new GeoIp2Exception("Syntax error creating service URL", e); } }
private void handle4xxStatus(HttpResponse response, URL url) throws GeoIp2Exception, IOException { HttpEntity entity = response.getEntity(); int status = response.getStatusLine().getStatusCode(); if (entity.getContentLength() == 0L) { throw new HttpException("Received a " + status + " error for " + url + " with no body", status, url); } String body = EntityUtils.toString(entity, "UTF-8"); try { Map<String, String> content = mapper.readValue(body, new TypeReference<HashMap<String, String>>() { }); handleErrorWithJsonBody(content, body, status, url); } catch (HttpException e) { throw e; } catch (IOException e) { throw new HttpException("Received a " + status + " error for " + url + " but it did not include the expected JSON body: " + body, status, url); } }
/** * @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); }