
com.github.libgraviton.workerbase.FileWorkerAbstract Maven / Gradle / Ivy
/**
* abstract base class for file workers providing convenience.
* it extends WorkerAbstract but provides more functions for /file api handling
*/
package com.github.libgraviton.workerbase;
import java.util.ArrayList;
import java.util.Iterator;
import com.fasterxml.jackson.jr.ob.JSON;
import com.github.libgraviton.workerbase.model.GravitonFile;
import com.github.libgraviton.workerbase.model.GravitonFileMetadata;
import com.github.libgraviton.workerbase.model.GravitonFileMetadataAction;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
/**
* @author List of contributors
*
* @link http://swisscom.ch
*/
public abstract class FileWorkerAbstract extends WorkerAbstract {
/**
* gets file metadata from backend as a GravitonFile object
*
* @param url the url of the object
* @throws Exception
*
* @return file instance
*/
public GravitonFile getGravitonFile(String url) throws Exception {
HttpResponse fileObj = Unirest.get(url).header("Accept", "application/json").asString();
return JSON.std.beanFrom(GravitonFile.class, fileObj.getBody());
}
/**
* checks if a certain action is present in the metadata.action array
*
* @param fileObj
* @param action
*
* @return true if yes, false if not
*/
public Boolean isActionCommandPresent(GravitonFile fileObj, String action) {
GravitonFileMetadata metadata = fileObj.getMetadata();
for (GravitonFileMetadataAction singleAction: metadata.getAction()) {
if (singleAction.getCommand() != null &&
singleAction.getCommand().equals(action)
) {
return true;
}
}
return false;
}
/**
* removes the action.0.command action from the /file resource, PUTs it to the backend removed
*
* @param documentUrl document url
* @param action action string to remove
* @throws Exception
*/
public void removeFileActionCommand(String documentUrl, String action) throws Exception {
// we will re-fetch again just to be sure.. this *really* should use PATCH ;-)
GravitonFile fileObj = this.getGravitonFile(documentUrl);
GravitonFileMetadata metadata = fileObj.getMetadata();
ArrayList metadataAction = metadata.getAction();
boolean hasBeenRemoved = false;
Iterator iter = metadataAction.iterator();
while (iter.hasNext()) {
GravitonFileMetadataAction singleAction = iter.next();
if (singleAction.getCommand() != null &&
singleAction.getCommand().equals(action)
) {
iter.remove();
hasBeenRemoved = true;
}
}
if (hasBeenRemoved) {
Unirest.put(documentUrl).header("Content-Type", "application/json").body(JSON.std.asString(fileObj)).asString();
System.out.println(" [*] Removed action property from " + documentUrl);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy