Introduction
An entity is an encapsulation of binary data.
Fully qualified name: com.alibaba.bwif.script.core.Entity
Structure
Property | Type | Description |
blob | byte[] | The raw binary data. |
mimeType | String | The Multipurpose Internet Mail Extensions (MIME) type distinguishes different formats, such as application/json and text/xml. The DPI engine processes binary data based on the MIME type. |
encoding | String | The encoding method, such as UTF-8. |
Construction methods
Construction can be performed automatically by the DPI engine, or manually by a developer writing a script in the stream designer.
Automatic construction: At runtime, the DPI engine constructs an entity based on the node configuration. For example, in the output message of an HTTP node, the engine constructs an entity with the corresponding MIME type based on the content that is returned by the peer interface.
Script construction: The following construction methods are available.
Method | Description |
Entity fromBytesValue(byte[] blobValue, String mimeType, String encoding) |
|
Entity fromStringValue(String stringValue, String mimeType, String encoding) |
|
Entity fromObjectValue(Object objectValue, String mimeType, String encoding) |
|
Methods
Method | Description |
String getEncoding() | Gets the encoding of the current entity object. |
String getMimeType() | Gets the MIME type of the current entity object. |
byte[] getBlob() | Gets the raw binary content of the current entity object. |
Integer getBlobSize() | Gets the length of the raw binary content of the current entity object in bytes. |
Object getObjectValue() | Automatically deserializes the binary content into a basic Java type based on the MIME type for easy use. The type and structure of the resulting object depend on the MIME type. |
String getStringValue() | Encodes the binary content based on the specified encoding and returns the result. |
MIME types
The supported MIME types include the following:
Format | Supported MIME type names |
json | application/json, text/json |
x-www-form-urlencoded | x-www-form-urlencoded |
JSON format
When you use the getObjectValue() and fromObjectValue() methods, types in the JSON format are transformed into object types in the script. The DPI engine supports the following transformations:
JSON type | Object type in script |
number
| Corresponds to Integer, Double, or BigDecimal types, depending on the precision of the number. |
boolean
| Boolean |
string
| String |
null
| NullObject |
array
| ArrayList |
object
| HashMap |
If the getObjectValue() and fromObjectValue() methods do not meet your needs, you can use the getStringValue() and fromStringValue() methods. You can also use the built-in JSON processing utility class to directly handle the serialized string.
Example
For an example of how to process JSON data using the getObjectValue() and fromObjectValue() methods, see HTTP API Reply in Quick Start.
x-www-form-urlencoded format
When you call an API using an HTTP node, you can use this type of entity if the peer application requires the HTTP request body to be in the x-www-form-urlencoded format.
The getObjectValue() and fromObjectValue() methods support the HashMap type. The following example shows how to use these methods:
HashMap<String,Object> m = new HashMap()
m.put("a","1")
m.put("b","2")
m.put("c","'aa'")
m.put("d","Chinese")
Entity e = Entity.fromObjectValue(m,"application/x-www-form-urlencoded","UTF-8")The generated output is: a=1&b=2&c=%27aa%27&d=%E4%B8%AD%E6%96%87.