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 checkForUpdate(String routeConverterVersion, String routeConverterBits, long startCount, String javaVersion, String javaBits, String osName, String osVersion, String osArch, long startTime) throws IOException { log.fine("Checking for update for version " + routeConverterVersion); Post request = new Post(rootUrl + UPDATE_CHECK_URI, credentials); request.addString("id", valueOf(startTime)); request.addString("javaBits", javaBits); request.addString("javaVersion", javaVersion); request.addString("locale", getDefault().getLanguage()); request.addString("osArch", osArch); request.addString("osName", osName); request.addString("osVersion", osVersion); request.addString("rcStartCount", Long.toString(startCount)); request.addString("rcVersion", routeConverterVersion); request.addString("rcBits", routeConverterBits); return request.executeAsString().replace("\"", ""); }
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(); }
private String addUris(DataSource dataSource, Collection<String> uris) throws IOException { String xml = toXml(dataSource, uris, type); log.info(format("Adding URIs:%n%s", xml)); String dataSourcesUrl = getDataSourcesUrl(); Post request = new Post(dataSourcesUrl, getCredentials()); request.addFile("file", xml.getBytes(StandardCharsets.UTF_8)); request.setAccept(APPLICATION_JSON); request.setSocketTimeout(SOCKET_TIMEOUT); String result = null; try { result = request.executeAsString(); log.info(format("Added URIs with result:%n%s", result)); addCount += uris.size(); } catch(Exception e) { log.severe(format("Cannot add URIs: %s", e)); } return result; }
private String updateUris(DatasourceType dataSourceType) throws IOException { String xml = toXml(dataSourceType); log.info(format("Updating URIs:%n%s", xml)); String dataSourcesUrl = getDataSourcesUrl(); Post request = new Post(dataSourcesUrl, getCredentials()); request.addFile("file", xml.getBytes(StandardCharsets.UTF_8)); request.setAccept(APPLICATION_JSON); request.setSocketTimeout(SOCKET_TIMEOUT); String result = null; try { result = request.executeAsString(); log.info(format("Updated URIs with result:%n%s", result)); updateCount += getDownloadableCount(dataSourceType); } catch(Exception e) { log.severe(format("Cannot update URIs: %s", e)); } return result; }
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(); }
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(); }