private void checkForError(String url, String status) throws ServiceUnavailableException { if (status.equals("OVER_QUERY_LIMIT")) { overQueryLimitCount++; log.warning("Google API is over query limit, count: " + overQueryLimitCount + ", url: " + url); throw new ServiceUnavailableException(getClass().getSimpleName(), url, status); } if (status.equals("REQUEST_DENIED")) { deniedCount++; log.warning("Google API access is denied, count: " + deniedCount + ", url: " + url); throw new ServiceUnavailableException(getClass().getSimpleName(), url, status); } }
private void checkCurrentlyOverloaded(String url, String result) throws ServiceUnavailableException { if (result.contains("limit") && (result.contains("overloaded") || result.contains("exceeded"))) { overQueryLimitCount++; log.warning("geonames API is over query limit, count: " + overQueryLimitCount + ", url: " + url); throw new ServiceUnavailableException(getClass().getSimpleName(), url, result); } if(result.contains("user does not exist")) { log.warning("geonames API reports user does not exist, url: " + url); throw new ServiceUnavailableException(getClass().getSimpleName(), url, result); } }
String addFile(File file) throws IOException { log.info(format("Adding file %s", file)); String fileUrl = rootUrl + FILE_URI; Post request = new Post(fileUrl, credentials); request.setAccept(APPLICATION_JSON); request.addString("name", file.getName()); request.addFile("file", file); String result = request.executeAsString(); if (request.isUnAuthorized()) throw new UnAuthorizedException("Not authorized to add file " + file, fileUrl); if (request.isForbidden()) throw new ForbiddenException("Forbidden to add file " + file, fileUrl); if (request.isPreconditionFailed()) throw new ServiceUnavailableException("File " + file + " is too large", fileUrl, result); if (!request.isSuccessful()) throw new IOException("POST on " + fileUrl + " with file " + file + " not successful: " + result); return request.getLocation(); }