throws EncodingException
{
try {
final String contentType = message.getContentType ();
if (!HttpgQueueConnectorProxy.expectedContentType.equals (contentType)) {
throw (new EncodingException ("invalid content type"));
}
final String contentEncoding = message.getContentEncoding ();
if (!HttpgQueueConnectorProxy.expectedContentEncoding.equals (contentEncoding)) {
throw (new EncodingException ("invalid content encoding"));
}
final byte[] rawBytes = message.getData ();
if (rawBytes.length < 4) {
throw (new EncodingException ("invalid message length"));
}
final DataInputStream rawStream = new DataInputStream (new ByteArrayInputStream (rawBytes));
final int metadataLength = rawStream.readInt ();
if (metadataLength > rawBytes.length) {
throw (new EncodingException ("invalid metadata length"));
}
final byte[] metadataBytes = new byte[metadataLength];
rawStream.readFully (metadataBytes);
final JSONObject metadata;
try {
metadata = SerDesUtils.jsonToRawObject (metadataBytes, Charsets.UTF_8);
} catch (final JSONException exception) {
throw (new EncodingException ("invalid metadata"));
}
final int version;
try {
version = metadata.getInt ("version");
} catch (final JSONException exception) {
throw (new EncodingException ("invalid metadata version", exception));
}
if (version != 1) {
throw (new EncodingException (String.format ("unexpected metadata version `%d`", Integer.valueOf (version))));
}
final String callbackIdentifier;
final String callbackExchange;
final String callbackRoutingKey;
try {
callbackIdentifier = metadata.getString ("callback-identifier");
callbackExchange = metadata.getString ("callback-exchange");
callbackRoutingKey = metadata.getString ("callback-routing-key");
} catch (final JSONException exception) {
throw (new EncodingException ("invalid callback metadata", exception));
}
final String httpVersion;
final String httpMethod;
final String httpPath;
final ImmutableMap<String, String> httpHeaders;
final String httpBodyEncoding;
try {
httpVersion = metadata.getString ("http-version");
httpMethod = metadata.getString ("http-method");
httpPath = metadata.getString ("http-uri");
final ImmutableMap.Builder<String, String> httpHeadersBuilder = ImmutableMap.<String, String>builder ();
final JSONObject httpHeadersRaw = metadata.getJSONObject ("http-headers");
final Iterator<?> httpHeadersIterator = httpHeadersRaw.keys ();
while (httpHeadersIterator.hasNext ()) {
final String httpHeaderName = ((String) httpHeadersIterator.next ());
final String httpHeaderValue = httpHeadersRaw.getString (httpHeaderName);
httpHeadersBuilder.put (httpHeaderName.toLowerCase (), httpHeaderValue);
}
httpHeaders = httpHeadersBuilder.build ();
httpBodyEncoding = metadata.getString ("http-body");
} catch (final JSONException exception) {
throw (new EncodingException ("invalid http metadata", exception));
}
final byte[] httpBodyBytes;
if (httpBodyEncoding.equals ("empty")) {
httpBodyBytes = null;
} else if (httpBodyEncoding.equals ("following")) {
final int bodyLength = rawStream.readInt ();
if (bodyLength != (rawBytes.length - metadataLength)) {
throw (new EncodingException ("invalid body length"));
}
httpBodyBytes = new byte[bodyLength];
rawStream.readFully (httpBodyBytes);
} else if (httpBodyEncoding.equals ("embedded")) {
try {
httpBodyBytes = metadata.getString ("http-body-content").getBytes ();
} catch (final JSONException exception) {
throw (new EncodingException ("invalid http body", exception));
}
} else {
throw (new EncodingException (String.format ("invalid body encoding `%s`", httpBodyEncoding)));
}
final EncodingMetadata bodyEncodingMetadata = new EncodingMetadata (httpHeaders.get ("content-type"), httpHeaders.get ("content-encoding"));
final TRequestBody httpBody;
if (httpBodyBytes != null) {
try {
httpBody = this.requestBodyEncoder.decode (httpBodyBytes, bodyEncodingMetadata);
} catch (final EncodingException exception) {
throw (new EncodingException ("invalid body", exception));
}
} else {
httpBody = null;
}
final DeliveryToken token = new DeliveryToken (this, message.getDelivery (), callbackExchange, callbackRoutingKey, callbackIdentifier);
final HttpgRequestMessage<TRequestBody> request = HttpgRequestMessage.create (httpVersion, httpMethod, httpPath, httpHeaders, httpBody, token);
return (request);
} catch (final EncodingException exception) {
throw (exception);
} catch (final Throwable exception) {
throw (new EncodingException ("unexpected exception", exception));
}
}