Created
November 15, 2017 18:33
-
-
Save pmedcraft/21db391ad5236167b2bfe53f3702319a to your computer and use it in GitHub Desktop.
Uploads asset to WorldServer's Explorer (mounts) by posting to the "upload_assets" servlet.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Uploads assets to WorldServer's Explorer (mounts) by posting to the "upload_assets" servlet. | |
* | |
* @param wsBaseUrl | |
* @param token | |
* @param explorerFolder | |
* @param inputFile | |
* @throws IOException | |
*/ | |
private void uploadAssetsToExplorer(String wsBaseUrl, String token, String explorerFolder, File inputFile) throws IOException { | |
String[] folders = explorerFolder.split("/"); | |
Map<String, String> folderDetails = createFoldersInExplorer(wsBaseUrl, token, folders); | |
String postActionUrl = wsBaseUrl + "/ws-legacy/upload_assets?" + | |
"&aisCF=" + folderDetails.get("aisCF") + | |
"&aisSP=" + folderDetails.get("aisSP") + | |
"&token=" + token; | |
log.info("POST URL [" + postActionUrl + "]"); | |
HttpClient httpClient = HttpClientBuilder.create().build(); | |
HttpPost httpPostRequest = new HttpPost(postActionUrl); | |
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); | |
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); | |
builder.addBinaryBody("file", inputFile, ContentType.DEFAULT_BINARY, inputFile.getName()); | |
builder.addTextBody("node", folderDetails.get("node")); | |
builder.addTextBody("submittedBy", "upload"); // Standard value set by the Servlet in WorldServer | |
HttpEntity entity = builder.build(); | |
httpPostRequest.setEntity(entity); | |
HttpResponse response = httpClient.execute(httpPostRequest); | |
if (response.getStatusLine().getStatusCode() != 200) | |
throw new IOException("Error processing explorer file [" + inputFile.getName() + "]"); | |
log.info("Finished processing explorer entry [" + inputFile.getName() + "]"); | |
} | |
/** | |
* Creates the required folder structure specified by the folders parameter. The folders are created in | |
* WorldServer's Explorer (mounts) by posting to the "explorer_new_asset" servlet. If the folder structure is | |
* already present in the Explorer, then posting to the servlet has no effect. | |
* | |
* @param wsBaseUrl | |
* @param token | |
* @param folders | |
* @return a map containing folder details to help uploading files to them in WorldServer's Explorer (mounts). | |
* @throws IOException | |
*/ | |
private Map<String, String> createFoldersInExplorer(String wsBaseUrl, String token, String[] folders) | |
throws IOException { | |
Map<String, String> folderDetails = new HashMap<>(); | |
String aisCF, aisSP, node; | |
aisCF = aisSP = node = ""; | |
for (int i=1; i < folders.length; i++) { | |
aisSP = "%2F" + folders[i - 1]; | |
node += "/" + folders[i - 1]; | |
String postActionUrl = wsBaseUrl + "/ws-legacy/explorer_new_asset?" + | |
"&aisCF=" + aisCF + | |
"&aisSP=" + aisSP + | |
"&token=" + token; | |
HttpClient httpClient = HttpClientBuilder.create().build(); | |
HttpPost httpPostRequest = new HttpPost(postActionUrl); | |
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); | |
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); | |
builder.addTextBody("type", "1"); // This type value defines that we are creating a folder | |
builder.addTextBody("name", folders[i]); | |
builder.addTextBody("node", node); | |
builder.addTextBody("submittedBy", "ok"); // Standard value set by the Servlet in WorldServer | |
HttpEntity entity = builder.build(); | |
httpPostRequest.setEntity(entity); | |
HttpResponse response = httpClient.execute(httpPostRequest); | |
if (response.getStatusLine().getStatusCode() != 200) | |
throw new IOException("Error processing folder structure [" + Arrays.toString(folders) + "]"); | |
if (i + 1 < folders.length) { | |
aisCF += "%2F" + folders[i - 1]; | |
} | |
else { | |
aisCF += "%2F" + folders[i - 1]; | |
aisSP = "%2F" + folders[i]; | |
node += "/" + folders[i]; | |
} | |
} | |
// The following details are required for the 'uploadAssetsToExplorer' method to continue | |
folderDetails.put("aisCF", aisCF); | |
folderDetails.put("aisSP", aisSP); | |
folderDetails.put("node", node); | |
return folderDetails; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment