Package eu.mosaic_cloud.platform.core.utils

Examples of eu.mosaic_cloud.platform.core.utils.EncodingException


      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));
    }
  }
View Full Code Here


      final byte[] httpBodyBytes;
      try {
        final EncodeOutcome outcome = this.responseBodyEncoder.encode (response.body, httpBodyEncodingMetadata);
        httpBodyBytes = outcome.data;
      } catch (final EncodingException exception) {
        throw (new EncodingException ("invalid body", exception));
      }
      metadata.put ("version", 1);
      metadata.put ("callback-identifier", token.callbackIdentifier);
      metadata.put ("http-version", response.version);
      metadata.put ("http-code", response.status);
      metadata.put ("http-status", "mosaic-http-response");
      metadata.put ("http-headers", httpHeaders);
      metadata.put ("http-body", "following");
      final byte[] metadataBytes;
      try {
        metadataBytes = SerDesUtils.toJsonBytes (metadata, Charsets.UTF_8);
      } catch (final JSONException exception) {
        throw (new EncodingException ("invalid metadata"));
      }
      final int messageLength = metadataBytes.length + httpBodyBytes.length + 8;
      final ByteArrayOutputStream rawBytesStream = new ByteArrayOutputStream (messageLength);
      final DataOutputStream rawStream = new DataOutputStream (rawBytesStream);
      rawStream.writeInt (metadataBytes.length);
      rawStream.write (metadataBytes);
      rawStream.writeInt (httpBodyBytes.length);
      rawStream.write (httpBodyBytes);
      final byte[] rawData = rawBytesStream.toByteArray ();
      final AmqpOutboundMessage rawMessage = new AmqpOutboundMessage (token.callbackExchange, token.callbackRoutingKey, rawData, HttpgQueueConnectorProxy.expectedContentEncoding, HttpgQueueConnectorProxy.expectedContentType);
      return (rawMessage);
    } catch (final EncodingException exception) {
      throw (exception);
    } catch (final Throwable exception) {
      throw (new EncodingException ("unexpected exception", exception));
    }
  }
View Full Code Here

TOP

Related Classes of eu.mosaic_cloud.platform.core.utils.EncodingException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.