public String toString() { return super.toString() + "[serviceName=" + getServiceName() + ", serviceUrl=" + getServiceUrl() + "]"; } }
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); } }
public Category create(String name) throws IOException { if (name.contains("/") || name.contains(separator)) throw new ForbiddenException(format("Cannot have slashes in name %s", name), getHref()); File subDirectory = new File(directory, encodeFileName(name)); if (subDirectory.exists()) throw new DuplicateNameException(format("%s %s already exists", subDirectory.isDirectory() ? "Category" : "Route", name), subDirectory.getAbsolutePath()); if (!subDirectory.mkdir()) throw new IOException(format("Cannot create category %s", subDirectory)); return new LocalCategory(catalog, subDirectory); }
String addCategory(String categoryUrl, String name) throws IOException { log.info(format("Adding category %s to %s", name, categoryUrl)); Post request = new Post(rootUrl + CATEGORY_URI, credentials); request.setAccept(APPLICATION_JSON); request.addString("parent", categoryUrl); request.addString("name", name); String result = request.executeAsString(); if (request.isUnAuthorized()) throw new UnAuthorizedException("Not authorized to add category " + name, categoryUrl); if (request.isForbidden()) throw new ForbiddenException("Forbidden to add category " + name, categoryUrl); if (request.isBadRequest()) throw new NotFoundException("Category not found", categoryUrl); if (request.isPreconditionFailed()) throw new DuplicateNameException("Category " + name + " already exists", categoryUrl); if (!request.isSuccessful()) throw new IOException("POST on " + (rootUrl + CATEGORY_URI) + " with payload " + name + " not successful: " + result); return request.getLocation(); }
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(); }
void deleteFile(String fileUrl) throws IOException { log.info(format("Adding file %s", fileUrl)); Delete request = new Delete(fileUrl, credentials); String result = request.executeAsString(); if (request.isUnAuthorized()) throw new UnAuthorizedException("Not authorized to delete file", fileUrl); if (request.isForbidden()) throw new ForbiddenException("Forbidden to delete file", fileUrl); if (request.isNotFound()) throw new NotFoundException("File not found", fileUrl); if (request.isBadRequest()) throw new NotOwnerException("Not owner of file to delete", fileUrl); if (!request.isSuccessful()) throw new IOException("DELETE on " + fileUrl + " not successful: " + result); } }
void deleteUser(String userUrl) throws IOException { log.info("Deleting user " + userUrl); Delete request = new Delete(userUrl, credentials); request.setAccept(APPLICATION_JSON); String result = request.executeAsString(); if (request.isBadRequest()) throw new ForbiddenException("Not authorized to delete user", userUrl); if (!request.isSuccessful()) throw new IOException("DELETE on " + userUrl + " not successful: " + result); }
public String sendChecksums(DataSource dataSource, java.util.Map<FileAndChecksum, List<FileAndChecksum>> fileToFragments, String filterUrl) throws IOException { String xml = toXml(dataSource, fileToFragments, filterUrl); log.info(format("Sending checksums for %s filtered with %s:%n%s", fileToFragments, filterUrl, xml)); String dataSourcesUrl = getDataSourcesUrl(dataSource.getId()); Put request = new Put(dataSourcesUrl, credentials); request.setAccept(APPLICATION_JSON); request.addFile("file", xml.getBytes(StandardCharsets.UTF_8)); String result = request.executeAsString(); if (request.isUnAuthorized()) throw new UnAuthorizedException("Cannot send checksums", dataSourcesUrl); if (!request.isSuccessful()) throw new IOException("PUT on " + dataSourcesUrl + " for data source " + dataSource + " not successful: " + result); log.info(format("Sent checksum for %s filtered with %s with result:%n%s", fileToFragments, filterUrl, result)); return result; } }
public void update(Category parent, String description) throws IOException { File category = toFile(new URL(parent.getHref())); File newName = new File(category, encodeFileName(description)); if (newName.exists()) throw new DuplicateNameException(format("%s %s already exists", newName.isDirectory() ? "Category" : "Route", description), newName.getAbsolutePath()); if (!file.renameTo(newName)) throw new IOException(format("Cannot rename %s to %s", file, newName)); file = newName; }
void updateCategory(String categoryUrl, String parentUrl, String name) throws IOException { log.info(format("Updating category %s to parent %s and name %s", categoryUrl, parentUrl, name)); Put request = new Put(categoryUrl, credentials); request.setAccept(APPLICATION_JSON); request.addString("parent", parentUrl); request.addString("name", name); String result = request.executeAsString(); if (request.isUnAuthorized()) throw new UnAuthorizedException("Not authorized to update category " + name, categoryUrl); if (request.isForbidden()) throw new ForbiddenException("Forbidden to update category " + name, categoryUrl); if (request.isNotFound()) throw new NotFoundException("Category not found", categoryUrl); if (request.isBadRequest()) throw new NotOwnerException("Not owner of category to update", categoryUrl); if (request.isPreconditionFailed()) throw new DuplicateNameException("Category " + name + " already exists", categoryUrl); if (!request.isSuccessful()) throw new IOException("PUT on " + categoryUrl + " with payload " + parentUrl + "/" + name + " not successful: " + result); }
void deleteRoute(String routeUrl) throws IOException { log.info(format("Deleting route %s", routeUrl)); Delete request = new Delete(routeUrl, credentials); request.setAccept(APPLICATION_JSON); String result = request.executeAsString(); if (request.isUnAuthorized()) throw new UnAuthorizedException("Not authorized to delete route", routeUrl); if (request.isForbidden()) throw new ForbiddenException("Forbidden to delete route", routeUrl); if (request.isNotFound()) throw new NotFoundException("Route not found", routeUrl); if (request.isBadRequest()) throw new NotOwnerException("Not owner of route to delete", routeUrl); if (!request.isSuccessful()) throw new IOException("DELETE on " + routeUrl + " not successful: " + result); }
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); } }
public String addUser(String userName, String password, String firstName, String lastName, String email) throws IOException { log.info("Adding user " + userName + "," + firstName + "," + lastName + "," + email); Post request = new Post(apiUrl + USER_URI); request.setAccept(APPLICATION_JSON); request.addString("username", userName); request.addString("password", password); request.addString("first_name", firstName); request.addString("last_name", lastName); request.addString("email", email); String result = request.executeAsString(); if (request.isBadRequest()) throw new ForbiddenException("Cannot add user: " + result, apiUrl + USER_URI); if (request.isForbidden()) throw new ForbiddenException("Cannot add user: " + result, apiUrl + USER_URI); if (!request.isSuccessful()) throw new IOException("POST on " + (apiUrl + USER_URI) + " with payload " + userName + "," + firstName + "," + lastName + "," + email + " not successful: " + result); return request.getLocation(); }
public String sendErrorReport(String logOutput, String description, java.io.File file) throws IOException { log.fine("Sending error report with log \"" + logOutput + "\", description \"" + description + "\"" + (file != null ? ", file " + file.getAbsolutePath() : "")); Post request = new Post(getErrorReportUrl(), credentials); request.addString("log", logOutput); request.addString("description", description); if (file != null) request.addFile("file", file); String result = request.executeAsString(); if (request.isUnAuthorized()) throw new UnAuthorizedException("Cannot send error report " + (file != null ? ", file " + file.getAbsolutePath() : ""), getErrorReportUrl()); if (!request.isSuccessful()) throw new IOException("POST on " + getErrorReportUrl() + " with log " + logOutput.length() + " characters" + ", description \"" + description + "\", file " + file + " not successful: " + result); return request.getLocation(); }
String addRoute(String categoryUrl, String description, String localFile, String remoteUrl) throws IOException { log.info(format("Adding route %s to category %s with remote url %s", description, categoryUrl, remoteUrl)); Post request = new Post(rootUrl + ROUTE_URI, credentials); request.setAccept(APPLICATION_JSON); request.addString("category", categoryUrl); request.addString("description", description); if (localFile != null) request.addString("localFile", localFile); if (remoteUrl != null) request.addString("remoteUrl", remoteUrl); String result = request.executeAsString(); if (request.isUnAuthorized()) throw new UnAuthorizedException("Not authorized to add route " + description, categoryUrl); if (request.isForbidden()) throw new ForbiddenException("Forbidden to add route " + description, categoryUrl); if (request.isBadRequest()) throw new NotFoundException("Category not found", categoryUrl); if (request.isPreconditionFailed()) throw new DuplicateNameException("Route " + description + " already exists", categoryUrl); if (!request.isSuccessful()) throw new IOException("POST on " + (rootUrl + ROUTE_URI) + " with route " + description + "," + categoryUrl + "," + remoteUrl + " not successful: " + result); return request.getLocation(); }
void deleteCategory(String categoryUrl) throws IOException { log.info(format("Deleting category %s", categoryUrl)); Delete request = new Delete(categoryUrl, credentials); request.setAccept(APPLICATION_JSON); String result = request.executeAsString(); if (request.isUnAuthorized()) throw new UnAuthorizedException("Not authorized to delete category", categoryUrl); if (request.isForbidden()) throw new ForbiddenException("Forbidden to delete category", categoryUrl); if (request.isNotFound()) throw new NotFoundException("Category not found", categoryUrl); if (request.isBadRequest()) throw new NotOwnerException("Not owner of category to delete", categoryUrl); if (!request.isSuccessful()) throw new IOException("DELETE on " + categoryUrl + " not successful: " + result); }
void updateRoute(String routeUrl, String categoryUrl, String description, String localFile, String remoteUrl) throws IOException { log.info(format("Updating route %s to category %s and description %s with remote url %s", routeUrl, categoryUrl, description, remoteUrl)); Put request = new Put(routeUrl, credentials); request.setAccept(APPLICATION_JSON); request.addString("category", categoryUrl); request.addString("description", description); if (localFile != null) request.addString("localFile", localFile); if (remoteUrl != null) request.addString("remoteUrl", remoteUrl); String result = request.executeAsString(); if (request.isUnAuthorized()) throw new UnAuthorizedException("Not authorized to update route", routeUrl); if (request.isForbidden()) throw new ForbiddenException("Forbidden to update route", routeUrl); if (request.isNotFound()) throw new NotFoundException("Route not found", routeUrl); if (request.isBadRequest()) throw new NotOwnerException("Not owner of route to update", routeUrl); if (request.isPreconditionFailed()) throw new DuplicateNameException("Route " + description + " already exists", description); if (!request.isSuccessful()) throw new IOException("PUT on " + routeUrl + " with route " + description + "," + categoryUrl + "," + remoteUrl + " not successful: " + result); }