VMware Aria Automation offers a mime attachment class, to describe mime attachments in the context of the JavaScript runtime environment. The Multipurpose Internet Mail Extensions (MIME) is a standard that extends the format of messages to support text in character sets other than ASCII. To use the methods of the this class outside of Aria Automation, a library is available here to emulate it.
Mock-up of Mime Attachment Class
This mock-up of the mime attachment class bases on the same JavaScript engine used in Aria Automation, the
Mozilla Rhino engine. To use this file classes it is necessary to use the Rhino engine, because their constructor and method access Java classes directly. This ensures us a consistent basis for simulation and operation.
To use this library in your code it is necessary to add e.g.
load("mimeattachment.class.js");
at the beginning of the program. After that, all functions can be used seamlessly just like in Aria Automation.
You can download the source from
my GitHub account.
/**
* Mock-up of the MimeAttachment class from VMware Aria Automation.
* Describe a mime attachment.
*
* @author Stefan Schnell <mail@stefan-schnell.de>
* @license MIT
* @version 0.2.1
*
* Hint: This mock-up works only with the Mozilla Rhino JavaScript
* engine.
*
* Checked with Rhino engines version 1.7R4, 1.7.14 and 1.7.15
*/
var MimeAttachment = function(file) {
if (
typeof file !== "undefined" &&
file !== null &&
String(file).trim() !== ""
) {
this.mimeType = "";
try {
this.name = System.extractFileName(file);
var path = java.nio.file.FileSystems.getDefault().getPath("", file);
this.buffer = java.nio.file.Files.readAllBytes(path);
this.content = String(java.lang.String(this.buffer));
} catch (exception) {
System.error(exception);
}
}
};
MimeAttachment.prototype = {
/**
* Returns the class name.<br>
* Hint: This method is a standard.
*
* @function getClassName
* @returns {string}
*
* @example
* var mimeAttachment = new MimeAttachment();
* var result = mimeAttachment.getClassName();
* System.log(result);
*/
getClassName : function() {
return "MimeAttachment";
},
/**
* Write the content of the mime attachment to file.
*
* @function write
* @param {string} directory - Directory where to store the file.
* @param {string} filename - Optional filename<br>
* (if null, it uses the mime attachment name)
* @returns {File}
*/
write : function(directory, filename) {
var context;
try {
if (
typeof directory === "undefined" ||
directory === null ||
String(directory).trim() === ""
) {
throw new Error("directory argument can not be undefined or null");
}
if (
typeof filename === "undefined" ||
filename === null ||
String(filename).trim() === ""
) {
filename = this.name;
}
var fullPath = System.appendToPath(String(directory), String(filename));
var contextFactory = org.mozilla.javascript.ContextFactory();
context = contextFactory.getGlobal().enterContext();
var path = java.nio.file.Paths.get(fullPath);
java.nio.file.Files.deleteIfExists(path);
java.nio.file.Files.createFile(path);
var outputStream = java.nio.file.Files.newOutputStream(path);
var className = "";
try {
className = this.buffer.getClassName();
} catch (exception) {
className = this.buffer.getClass().getName();
}
if ( className === "ByteBuffer" || className === "[B") {
outputStream.write(this.buffer._byteBuffer);
} else {
outputStream.write(this.buffer);
}
outputStream.close();
} catch (exception) {
System.log(exception);
} finally {
context.exit();
}
}
};
|