Entity

更新时间:
复制 MD 格式

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)

  • Constructs an entity from binary content. During construction, the binary content is not validated against the mimeType. An error occurs if the format is incorrect when the entity is used.

  • For more information about mimeType, see the subsequent sections in this topic.

  • Currently, only UTF-8 is supported for encoding.

Entity fromStringValue(String stringValue, String mimeType, String encoding)

  • Constructs an entity from a string. The stringValue is treated as serialized content, such as a JSON string. The supported formats vary depending on the mimeType. During construction, the binary content is not validated against the mimeType. An error occurs if the format is incorrect when the entity is used.

  • For more information about mimeType, see the subsequent sections in this topic.

  • Currently, only UTF-8 is supported for encoding.

Entity fromObjectValue(Object objectValue, String mimeType, String encoding)

  • Generates an entity from an object. The supported object types vary depending on the mimeType. For more information, see the subsequent sections in this topic. During construction, the object is transformed into binary content based on the mimeType. An error occurs if the format is incorrect.

  • Currently, only UTF-8 is supported for 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

  • For example: 123, -1.002

Corresponds to Integer, Double, or BigDecimal types, depending on the precision of the number.

boolean

  • For example: true

Boolean

string

  • For example: "abc"

String

null

  • For example: null

NullObject

array

  • For example: [], [1,2,3]

ArrayList

object

  • For example: {}, {"a":1,"b":[1,2,3]}

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.