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; }
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(); }
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); }
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 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); }