private BoxItem mkdirItem(String path) throws InvalidTokenException {
Preconditions.checkNotNull(path, "Missing path.");
Logger.info("BoxClient.mkdir: path %s", path);
String parent = RuleUtils.getParent(path);
BoxItem parentItem = getItem(parent);
if (parentItem == null) {
Logger.warn("Parent folder doesn't exist, creating. Path: %s Parent: %s", path, parent);
parentItem = mkdirItem(parent);
if (parentItem == null) {
Logger.error("Could not create parent directory: %s", parent);
return null;
}
}
if (! parentItem.isFolder()) {
Logger.error("Cannot create directory because parent is not a directory. Parent path: %s Item: %s", parent, parentItem);
return null;
}
HttpResponse resp = req("/folders")
.body(new Gson().toJson(new BoxCreateFolderItem(RuleUtils.basename(path), parentItem)))
.post();
if (resp.success()) {
BoxItem folder = new Gson().fromJson(resp.getJson(), BoxItem.class);
invalidate(path);
Logger.info("Successfully created folder at path %s Folder: %s", path, folder);
return folder;
}