<document>
  <header>
    <product>resin</product>
    <title>Hessian 1.0.2 Specification</title>
    <date>May 12, 2006</date>
    <description>
      <p>Hessian is a compact binary protocol for connecting web
      services.</p>

      <p>Because Hessian is a small protocol, J2ME devices like cell-phones can use
      it to connect to Resin servers.  Because it's powerful, it can be used
      for EJB services.</p>
    </description>
  </header>

  <body>
    <localtoc/>

<s1 title="Design Goals">

<p>The Hessian web services protocol was created as a
lightweight binary alternative to the XML-based web services protocols.
</p>

<p>The <a href="http://www.caucho.com/hessian">Hessian home page</a>
contains the latest information about Hessian.</p>

<p>Unlike older binary protocols, Hessian is both self-describing
and portable across languages.  The wire protocol for web
services should be invisible to application writers.  Wire protocols
should not require external schema or IDL.</p>

<p>Given the EJB environment, the Hessian protocol has the
following requirements:</p>

<ul>
<li>It must support XML as a first class object.
</li><li>It must not require external IDL or schema definitions; it should be
invisible to application writers.
</li><li>It must have sufficient power to serialize Java.
</li><li>It must have sufficient power to support EJB.
</li><li>It must allow non-Java clients to use web services.
</li><li>It must allow web services to deployed as a Servlet.
</li><li>It must be simple so it can be effectively tested.
</li><li>It must be as fast as possible.
</li><li>It should support transaction contexts.
</li></ul>

</s1>

<s1 title="Serialization">

<p>Hessian's object serialization has 9 primitive types:</p>

<ol>
<li><a href="#boolean">boolean</a>
</li><li>32-bit <a href="#int">int</a>
</li><li>64-bit <a href="#long">long</a>
</li><li>64-bit <a href="#double">double</a>
</li><li>64-bit <a href="#date">date</a>
</li><li>UTF8-encoded <a href="#string">string</a>
</li><li>UTF8-encoded <a href="#xml">xml</a>
</li><li>raw <a href="#binary">binary</a> data
</li><li><a href="#remote">remote</a> objects
</li></ol>

<p>It has 2 combining constructs:</p>
<ol>
<li><a href="#list">list</a> for lists and arrays
</li><li><a href="#map">map</a> for objects and hash tables.
</li></ol>

<p>Finally, it has 2 special contructs:</p>
<ol>
<li><a href="#null">null</a> for null values
</li><li><a href="#ref">ref</a> for shared and circular object references.
</li></ol>

<s2 title="null" type="defun">
<p>Null represents a null pointer.</p>

<p>The byte <var>'N'</var> represents the null pointer.</p>

<p><var>null</var> values are allowed in place of any <var>string</var>, <var>xml</var>,
<var>binary</var>, <var>list</var>, <var>map</var>, or <var>remote</var>.</p>

<example title="null">
null ::= N
</example>
</s2>

<s2 title="boolean" type="defun">
<p>The byte <var>'F'</var> represents false and the byte <var>'T'</var>
represents true.</p>

<def>
boolean ::= T
        ::= F
</def>

<example title="boolean true">
T
</example>

</s2>

<s2 title="int" type="defun">
<p>A 32-bit signed integer.  An integer is represented by the
byte <var>'I'</var> followed by the 4-bytes of the integer in big-endian
order</p>

<def>
int ::= I b32 b24 b16 b8
</def>

<example title="integer 300">
I x00 x00 x01 x2c
</example>

</s2>

<s2 title="long" type="defun">
<p>A 64-bit signed integer.  An long is represented by the
byte <var>'L'</var> followed by the 8-bytes of the integer in big-endian order
</p>

<def>
long ::= L b64 b56 b48 b40 b32 b24 b16 b8
</def>

<example title="long 300">
L x00 x00 x00 x00 x00 x00 x01 x2c
</example>

</s2>

<s2 title="double" type="defun">
<p>A 64-bit IEEE floating pointer number.</p>

<def>
double ::= D b64 b56 b48 b40 b32 b24 b16 b8
</def>

<example title="double 12.25">
D x40 x28 x80 x00 x00 x00 x00 x00
</example>

</s2>

<s2 title="date" type="defun">
<p>Date represented by a 64-bits long of milliseconds since the epoch.</p>

<def>
date ::= d b64 b56 b48 b40 b32 b24 b16 b8
</def>

<example title="2:51:31 May 8, 1998">
d x00 x00 x00 xd0 x4b x92 x84 xb8
</example>

</s2>

<s2 title="string" type="defun">
<p>A 16-bit unicode character string encoded in UTF-8.
Strings are encoded in chunks.  <var>'S'</var> represents the final chunk
and <var>'s'</var> represents any initial chunk.  Each chunk has a 16-bit
length value.</p>

<p>The length is the number of characters, which may be different than
the number of bytes.</p>

<def>
string ::= (s b16 b8 utf-8-data)* S b16 b8 utf-8-data
</def>

<example title="&quot;Hello&quot; string">
S x00 x05 hello
</example>
</s2>

<s2 title="xml" type="defun">
<p>An XML document encoded as a 16-bit unicode character
string encoded in UTF-8.
XML data is encoded in chunks.  <var>'X'</var> represents the final chunk
and <var>'x'</var> represents any initial chunk.</p>

<p>Each chunk has a 16-bit
length value.  The length is the number of characters, which may
be different than the number of bytes.</p>

<def>
xml ::= (x b16 b8 utf-8-data)* X b16 b8 utf-8-data
</def>

<example title="trivial XML document">
X x00 x10 &lt;top&gt;hello&lt;/top&gt;
</example>

<note>Because this document does not define the language mapping,
implementations are free to return a string when reading an <var>xml</var>
entity.</note>

</s2>

<s2 title="binary" type="defun">
<p>A binary value.</p>

<p>Binary data is encoded in chunks.  <var>'B'</var> represents the final chunk
and <var>'b'</var> represents any initial chunk.  Each chunk has a 16-bit
length value.</p>

<def>
binary ::= (b b16 b8 binary-data)* B b16 b8 binary-data
</def>

</s2>

<s2 title="list" type="defun">
<p>An ordered list, like an array.  All lists have a type string,
a length, a list of objects, and a trailing 'z'.
The type string may be an arbitrary UTF-8 string understood
by the service (often a Java class name, but this isn't required.)
The length may be -1 to indicate that the list is variable length.
</p>

<def>
list ::= V <var>type</var>? <var>length</var>? <var>object</var>* z
</def>

<p>Each <var>list</var> item is added to the reference list to handle
shared and circular elements.  See the
<var>ref</var> element.</p>

<p>Any parser expecting a <var>list</var> must also accept a <var>null</var>
or a shared <var>ref</var>.</p>

<results title="serialization of a Java int[] = {0, 1}">
V t x00 x04 [int
  l x00 x00 x00 x02
  I x00 x00 x00 x00
  I x00 x00 x00 x01
  z
</results>

<results title="anonymous variable-length list = {0, &quot;foobar&quot;}">
V I x00 x00 x00 x00
  S x00 x06 foobar
  z
</results>

<note>The valid values of <var>type</var> are not specified in this
document and may depend on the specific application.  For example, a
Java EJB server which exposes an Hessian interface can use the <var>type</var>
information to instantiate the specific array type.
On the other hand, a Perl server would likely ignore the contents of <var>type</var>
entirely and create a generic array.
</note>

</s2>

<s2 title="map" type="defun">
<p>Represents serialized objects and Maps. The <var>type</var>
element describes the type of the map.  Objects are represented by a
map from field names to their values and <var>type</var> is the class
of the object itself.</p>

<def>
map ::= M t b16 b8 type-string (<var>object</var>, <var>object</var>)* z
</def>

<p>The <var>type</var> may be empty, i.e. a zero length.  The parser is
responsible for choosing a type if one is not specified.
For objects, unrecognized keys will be ignored.</p>

<p>Each <var>map</var> is added to the reference list.  Any time the
parser expects a <var>map</var>, it must also be able to support a
<var>null</var> or a <var>ref</var>.</p>

<example title="Serialization of a Java Object">
public class Car implements Serializable {
  String model = "Beetle";
  String color = "aquamarine";
  int mileage = 65536;
}
</example>
<results>
M t x00 x13 com.caucho.test.Car
  S x00 x05 model
  S x00 x06 Beetle

  S x00 x05 color
  S x00 x0a aquamarine

  S x00 x07 mileage
  I x00 x01 x00 x00
  z
</results>

<example title="A sparse array">
map = new HashMap();
map.put(new Integer(1), "fee");
map.put(new Integer(16), "fie");
map.put(new Integer(256), "foe");
</example>
<results>
M I x00 x00 x00 x01
  S x00 x03 fee

  I x00 x00 x00 x10
  S x00 x03 fie

  I x00 x00 x01 x00
  S x00 x03 foe

  z
</results>

<note>The <var>type</var> is chosen by the service.  Often it may be the
Java classname describing the service.</note>

</s2>

<s2 title="ref" type="defun">
<p>An integer referring to a previous <var>list</var> or <var>map</var>
instance.  As each <var>list</var> or <var>map</var> is read from the
input stream, it is assigned the integer position in the stream,
i.e. the first <var>list</var> or <var>map</var> is '0', the next is '1', etc.
A later <var>ref</var> can then use the previous object.  Writers are not
required to generate <var>refs</var>, but parsers must be able to recognize them.
</p>

<def>
ref ::= R b32 b24 b16 b8
</def>

<p><var>ref</var> can refer to incompletely-read items.  For example, a
circular linked-list will refer to the first link before the entire list
has been read.</p>

<p>A possible implementation would add each <var>map</var> and <var>list</var> to an
array as it's read.  The <var>ref</var> will return the corresponding
object from the array.  To support circular structures, the
implementation would store the <var>map</var> or <var>list</var> immediately,
before filling in the object's contents.</p>

<p>Each &lt;list&gt; or &lt;array&gt; is stored into an array as it is
parsed.  &lt;ref&gt; selects one of the stored objects.  The first
object is numbered '0'.</p>

<example title="circular list">
list = new LinkedList();
list.head = 1;
list.tail = list;
</example>
<results>
M t x00 x0a LinkedList
  S x00 x04 head
  I x00 x00 x00 x01
  S x00 x04 tail
  R x00 x00 x00 x00
  z
</results>

<note><var>ref</var> only refers to <var>list</var> and <var>map</var> elements.
<var>string</var> and <var>binary</var>, in particular, will only share
references if they're wrapped in a <var>list</var> or <var>map</var>.</note>

</s2>

<s2 title="remote" type="defun">
<p>A reference to a remote object.  The remote has a
<var>type</var> and a utf-8 string representing the object's URL.</p>

<def>
remote ::= r t b16 b8 type-name S b16 b8 url
</def>

<results title="EJB Session Reference">
r t x00 x0c test.TestObj
  S x00 x24 http://slytherin/ejbhome?id=69Xm8-zW
</results>

</s2>

</s1>

<s1 title="Call">

<p>A Hessian call invokes a method on an object with an argument
list.  The object is specified by the container, e.g. for a HTTP
request, it's the HTTP URL.  The arguments are
specified by Hessian serialization.</p>

<def>
call ::= c x01 x00 <var>header</var>* m b16 b8 method-string (<var>object</var>)* z
</def>

<example title="obj.add2(2,3) call">
c x01 x00
  m x00 x04 add2
  I x00 x00 x00 x02
  I x00 x00 x00 x03
  z
</example>

<example title="obj.add2(2,3) reply">
r x01 x00
  I x00 x00 x00 x05
  z
</example>

<s2 title="Object Naming (non-normative)">

<p>
URLs are flexible
enough to encode object instances as well as simple
static service locations.  The URL uniquely identifies the
Hessian object.  Thus, Hessian can support
object-oriented services, e.g. naming services, entity beans, or
session beans, specified by the URL without requiring extra method parameters
or headers.</p>

<p>Object naming may use the query string convention that "?id=XXX"
names the object "XXX" in the given service.  This convention is recommented,
but not required.</p>

<p>For example, a stock quote service might have a
factory interface like http://foo.com/stock and object instances
like http://foo.com?id=PEET.  The factory interface would return valid
object references through the factory methods.</p>

</s2>

<s2 title="Object naming (non-normative)">

<p>As an example, the following format is used for EJB:</p>

<def>
http://hostname/hessian<var>/ejb-name</var>?id=<var>object-id</var>
</def>

<p><var>http://hostname/hessian</var> identifies the EJB container.
In Resin-EJB, this will refer to the EJB Servlet. 
"/hessian" is the servlet prefix (url-pattern.)  HTTP is just used as
an example; Hessian does not require the use of HTTP.
</p>

<p><var>/ejb-name</var>, the path info of the request, identifies the EJB name,
specifically the home interface.  EJB containers can contain several
entity and session beans, each with its own EJB home.  The <var>ejb-name</var>
corresponds to the ejb-name in the deployment descriptor.
</p>

<p><var>object-id</var> identifies the specific object.  For entity beans, the
object-id encodes the primary key.  For session beans, the object-id encodes
a unique session identifier.  Home interfaces have no ";ejbid=..." portion.
</p>

<example title="Example Entity Home Identifier">
http://localhost/hessian/my-entity-bean
</example>

<example title="Example Entity Bean Identifier">
http://localhost/hessian/my-entity-bean?ejbid=slytherin
</example>

<example title="Example Session Home Identifier">
http://localhost/hessian/my-session-bean
</example>

<example title="Example Session Bean Identifier">
http://localhost/hessian/my-session-bean?ejbid=M9Zs1Zm
</example>

</s2>

<s2 title="Methods and Overloading">

<p>Method names must be unique.  Two styles of overloading are
supported: overloading by number of argumetns and overloading
by argument types.  Overloading is permitted by
encoding the argument types in the method names.  The types of
the actual arguments must not be used to select the methods.</p>

<p>Method names beginning with <var>_hessian_</var> are reserved.</p>

<p>Servers should accept calls with either the mangled method name
or the unmangled method name.  Clients should send the mangled method name.</p>

<note>See the Java binding for a possible overloading scheme.</note>

<example>add(int a, int b)</example>
<results>add_int_int</results>
<example>add(double a, double b)</example>
<results>add_double_double</results>
<example>add(shopping.Cart cart, shopping.Item item)</example>
<results>add_shopping.Cart_shopping.Item</results>

</s2>

<s2 title="Arguments">

<p>Arguments immediately follow the method in positional order.
Argument values use Hessian's serialization.</p>

<p>All arguments share references, i.e. the reference list
starts with the first argument and continues for all other arguments.
This lets two arguments share values.</p>

<example title="remote.eq(bean, bean)">
bean = new qa.Bean("foo", 13);

System.out.println(remote.eq(bean, bean));
</example>
<results>
c x01 x00
  m x00 x02 eq
  M t x00 x07 qa.Bean
    S x00 x03 foo
    I x00 x00 x00 x0d
    z
  R x00 x00 x00 x00
  z
</results>

<p>The number and type of arguments are fixed by the remote method.
Variable length arguments are forbidden.  Implementations
may take advantage of the expected type to improve performance.</p>

</s2>

<s2 title="Headers">

<p>Headers are (string, object) pairs that preceed the arguments.</p>

<p>The value of the header can be any serialized object.</p>

<p>For example, a request might include a transaction context in a
header.</p>

<results title="Call with Distributed Transaction Context">
c x01 x00
  H x00 x0b transaction
  r t x00 x28 com.caucho.hessian.xa.TransactionManager
    S x00 x23 http://hostname/xa?ejbid=01b8e19a77
  m x00 x05 debug
  I x00 x03 x01 xcb
  z
</results>

</s2>

<s2 title="Versioning">

<p>The call and response tags include a major and minor byte.  The
current version is 1.0.</p>

</s2>

</s1>

<s1 title="Reply">

<def>
valid-reply ::= r x01 x00 <var>header</var>* <var>object</var> z
fault-reply ::= r x01 x00 <var>header</var>* <var>fault</var> z
</def>

<s2 title="Value">
  
<p>A successful reply returns a single value and possibly
some header information.  </p>
  
<example title="integer 5 result">
r x01 x00
  I x00 x00 x00 x05
  z
</example>

</s2>
<s2 title="Faults">
<p>Failed calls return a <var>fault</var>.</p>

<p>Each fault has a number of informative fields, expressed like
&lt;map&gt; entries.  The defined fields are <var>code</var>, <var>message</var>,
and <var>detail</var>. <var>code</var> is one of a short list of strings
defined below.  <var>message</var> is a user-readable message.
<var>detail</var> is an object representing the exception.  In Java,
<var>detail</var> will be a serialized exception.</p>

<results title="Remote Call throws FileNotFoundException">
r x01 x00
  f
  S x00 x04 code
  S x00 x10 ServiceException

  S x00 x07 message
  S x00 x0e File Not Found

  S x00 x06 detail
  M t x00 x1d java.io.FileNotFoundException
    z
  z
</results>

<deftable>
<tr><td>ProtocolException</td><td>The Hessian request has some sort of
syntactic error.
</td></tr><tr><td>NoSuchObjectException</td><td>The requested object does not exist.
</td></tr><tr><td>NoSuchMethodException</td><td>The requested method does not exist.
</td></tr><tr><td>RequireHeaderException</td><td>A required header was not understood
by the server.
</td></tr><tr><td>ServiceException</td><td>The called method threw an exception.
</td></tr></deftable>

</s2>

</s1>

<s1 title="Metadata (non-normative)">

<p>Metadata is handled by special method calls, methods beginning
with <var>_hessian_</var>.</p>

<p><var>_hessian_getAttribute(String&#160;key)</var> returns a string.
The following attributes are predefined by this spec:</p>

<deftable>
<tr><th>attribute</th><th>meaning
</th></tr><tr><td>java.api.class</td><td>Java interface for this URL
</td></tr><tr><td>java.home.class</td><td>Java interface for this service
</td></tr><tr><td>java.object.class</td><td>Java interface for a service object
</td></tr><tr><td>java.ejb.primary.key.class</td><td>Java EJB primary key class
</td></tr></deftable>

<p>"java.api.class" returns the client proxy's Java API class
for the current URL.  "java.home.class" returns the API class for the
factory URL, i.e. without any "?id=XXX" query string.  "java.object.class"
returns the API class for object instances.</p>

<p>In the case of services with no object instances, i.e.
non-factory services, all three attributes will return the
same class name.</p>

<s2 title="Obsolete meta data properties">

<deftable>
<tr><th>attribute</th><th>meaning
</th></tr><tr><td>home-class</td><td>Java class for the home interface.
</td></tr><tr><td>remote-class</td><td>Java class for the object interface.
</td></tr><tr><td>primary-key-class</td><td>Java class for the primary key.
</td></tr></deftable>

</s2>

</s1>

<s1 title="Micro Hessian">

<p>A "Micro Hessian" implementation may omit support for the "double" type.
</p>

</s1>

<s1 title="Formal Definitions">

<def>
<var>top</var>     ::= <var>call</var>
        ::= <var>reply</var>

<var>call</var>    ::= c x01 x00 <var>header</var>* <var>method</var> <var>object</var>* z

<var>reply</var>   ::= r x01 x00 <var>header</var>* <var>object</var> z
        ::= r x01 x00 <var>header</var>* <var>fault</var> z

<var>object</var>  ::= <var>null</var>
        ::= <var>boolean</var>
        ::= <var>int</var>
        ::= <var>long</var>
        ::= <var>double</var>
        ::= <var>date</var>
        ::= <var>string</var>
        ::= <var>xml</var>
        ::= <var>binary</var>
        ::= <var>remote</var>
        ::= <var>ref</var>
        ::= <var>list</var>
        ::= <var>map</var>

<var>header</var>  ::= H b16 b8 header-string <var>object</var>
<var>method</var>  ::= m b16 b8 method-string

<var>fault</var>   ::= f (<var>object</var> <var>object</var>)* z

<var>list</var>    ::= V <var>type</var>? <var>length</var>? <var>object</var>* z
<var>map</var>     ::= M <var>type</var>? (<var>object</var> <var>object</var>)* z
<var>remote</var>  ::= r <var>type</var>? <var>string</var>

<var>type</var>    ::= t b16 b8 type-string
<var>length</var>  ::= l b32 b24 b16 b8

<var>null</var>    ::= N
<var>boolean</var> ::= T
        ::= F
<var>int</var>     ::= I b32 b24 b16 b8
<var>long</var>    ::= L b64 b56 b48 b40 b32 b24 b16 b8
<var>double</var>  ::= D b64 b56 b48 b40 b32 b24 b16 b8
<var>date</var>    ::= d b64 b56 b48 b40 b32 b24 b16 b8
<var>string</var>  ::= (s b16 b8 string-data)* S b16 b8 string-data
<var>xml</var>     ::= (x b16 b8 xml-data)* X b16 b8 xml-data
<var>binary</var>  ::= (b b16 b8 binary-data)* B b16 b8 binary-data
<var>ref</var>     ::= R b32 b24 b16 b8
</def>

</s1>

<s1 title="Copyright and Licensing">

<p>
© Copyright 2000-2004 Caucho Technology, Inc. All Rights Reserved.
</p>

<p>Any party may implement this protocol for any purpose without royalty
or license fee, provided that the implementation conforms to this
specification.   Caucho Technology reserves the right to create a
test suite, freely available without royalty or license fee, to
validate implementation conformance.  The limited permissions granted
herein are perpetual and may not be revoked by Caucho Technology or
its successors or assigns.</p>

<p>This document and translations of it may be copied and furnished to
others, and derivative works that comment on or otherwise explain it
or assist in its implementation may be prepared, copied, published and
distributed, in whole or in part, without restriction of any kind,
provided that the above copyright notice and these paragraphs are
included on all such copies and derivative works.</p>

<p>This document and the information contained herein is provided on an
"AS IS" basis and CAUCHO TECHNOLOGY DISCLAIMS ALL WARRANTIES, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF
THE INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED
WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.</p>

</s1>

<s1 title="Changes">

<s2 title="changes in 1.0.2">

<ul>
<li>Clarified that length of XML and strings is in characters (Petr Gladkikh)
</li></ul>
</s2>

<s2 title="changes in 1.0">

<ul>
<li>Removed unidirectional messages.
</li></ul>
</s2>

<s2 title="changes in V3">

<ul>
<li>Added unidirectional messages
</li><li>Removed 'v' from reply
</li><li>changed length code to 'l'
</li><li>made <var>type</var> and <var>length</var> optional
</li></ul>

</s2>

<s2 title="changes in V2">

<ul>
<li>EJB naming: clarified examples especially for session beans (John Mitchell)
</li><li>Formal definitions: clarified grammar and added missing object (John Mitchell)
</li><li>Formal definitions: initial binary should use 'b' (John Mitchell)
</li></ul>

</s2>

</s1>
</body></document>
