コード例 #1
0
        protected virtual void DoPublish(RC.IModel channel, Address replyTo, IMessage <byte[]> message)
        {
            var props = channel.CreateBasicProperties();

            MessagePropertiesConverter.FromMessageHeaders(message.Headers, props, EncodingUtils.GetEncoding(Encoding));
            channel.BasicPublish(replyTo.ExchangeName, replyTo.RoutingKey, MandatoryPublish, props, message.Payload);
        }
コード例 #2
0
        public object FromMessage(Message message, object conversionHint)
        {
            object content    = null;
            var    properties = message.MessageProperties;

            if (properties != null)
            {
                var contentType = properties.ContentType;
                if ((AssumeSupportedContentType &&
                     (contentType == null || contentType.Equals(MessageProperties.DEFAULT_CONTENT_TYPE))) ||
                    (contentType != null && contentType.Contains(SupportedContentType.Subtype)))
                {
                    var encoding = EncodingUtils.GetEncoding(properties.ContentEncoding);
                    if (encoding == null)
                    {
                        encoding = DefaultCharset;
                    }

                    content = DoFromMessage(message, conversionHint, properties, encoding);
                }
                else
                {
                    _logger?.LogWarning("Could not convert incoming message with content-type ["
                                        + contentType + "], '" + SupportedContentType.Subtype + "' keyword missing.");
                }
            }

            if (content == null)
            {
                content = message.Body;
            }

            return(content);
        }
コード例 #3
0
        protected override Message CreateMessage(object objectToConvert, MessageProperties messageProperties, Type genericType)
        {
            byte[] bytes;
            try
            {
                var jsonString = JsonConvert.SerializeObject(objectToConvert, Settings);
                bytes = DefaultCharset.GetBytes(jsonString);
            }
            catch (Exception e)
            {
                throw new MessageConversionException("Failed to convert Message content", e);
            }

            messageProperties.ContentType     = SupportedContentType.ToString();
            messageProperties.ContentEncoding = EncodingUtils.GetEncoding(DefaultCharset);
            messageProperties.ContentLength   = bytes.Length;

            // var type =  genericType == null ? objectToConvert.GetType() : genericType;
            // if (genericType != null && !type.isContainerType()
            //        && Modifier.isAbstract(type.getRawClass().getModifiers()))
            // {
            //    type = this.objectMapper.constructType(objectToConvert.getClass());
            // }
            // FromType(type, messageProperties);
            return(new Message(bytes, messageProperties));
        }
コード例 #4
0
        protected override Message CreateMessage(object payload, MessageProperties messageProperties)
        {
            byte[] bytes = null;
            if (payload is byte[])
            {
                bytes = (byte[])payload;
                messageProperties.ContentType = MessageProperties.CONTENT_TYPE_BYTES;
            }
            else if (payload is string)
            {
                try
                {
                    var enc = EncodingUtils.GetEncoding(DefaultCharset);
                    bytes = enc.GetBytes((string)payload);
                }
                catch (Exception e)
                {
                    throw new MessageConversionException("failed to convert to Message content", e);
                }

                messageProperties.ContentType     = MessageProperties.CONTENT_TYPE_TEXT_PLAIN;
                messageProperties.ContentEncoding = DefaultCharset;
            }

            if (bytes != null)
            {
                messageProperties.ContentLength = bytes.Length;
                return(new Message(bytes, messageProperties));
            }

            throw new ArgumentException("SimpleMessageConverter only supports String, and byte[] payloads, received: " + payload?.GetType().Name);
        }
コード例 #5
0
        protected override IMessage CreateMessage(object payload, IMessageHeaders messageProperties, object conversionHint)
        {
            byte[] bytes    = null;
            var    accessor = RabbitHeaderAccessor.GetMutableAccessor(messageProperties);

            if (payload is byte[])
            {
                bytes = (byte[])payload;
                accessor.ContentType = MessageHeaders.CONTENT_TYPE_BYTES;
            }
            else if (payload is string)
            {
                try
                {
                    var enc = EncodingUtils.GetEncoding(DefaultCharset);
                    bytes = enc.GetBytes((string)payload);
                }
                catch (Exception e)
                {
                    throw new MessageConversionException("failed to convert to Message content", e);
                }

                accessor.ContentType     = MessageHeaders.CONTENT_TYPE_TEXT_PLAIN;
                accessor.ContentEncoding = DefaultCharset;
            }
            else if (payload.GetType().IsSerializable)
            {
                try
                {
                    var formatter = new BinaryFormatter();
                    var stream    = new MemoryStream(512);

                    // TODO: don't disable this warning! https://aka.ms/binaryformatter
#pragma warning disable SYSLIB0011 // Type or member is obsolete
                    formatter.Serialize(stream, payload);
#pragma warning restore SYSLIB0011 // Type or member is obsolete
                    bytes = stream.ToArray();
                    accessor.ContentType = MessageHeaders.CONTENT_TYPE_DOTNET_SERIALIZED_OBJECT;
                }
                catch (Exception e)
                {
                    throw new MessageConversionException("failed to convert serialized Message content", e);
                }
            }

            if (bytes == null)
            {
                throw new ArgumentException("SimpleMessageConverter only supports string, byte[] and serializable payloads, received: " + payload?.GetType().Name);
            }

            var message = Message.Create(bytes, messageProperties);
            accessor.ContentLength = bytes.Length;
            return(message);
        }
コード例 #6
0
        public override object FromMessage(Message message)
        {
            object content    = null;
            var    properties = message.MessageProperties;

            if (properties != null)
            {
                var contentType = properties.ContentType;
                if (contentType != null && contentType.StartsWith("text"))
                {
                    var encoding = properties.ContentEncoding;
                    if (encoding == null)
                    {
                        encoding = DefaultCharset;
                    }

                    try
                    {
                        var enc = EncodingUtils.GetEncoding(encoding);
                        content = enc.GetString(message.Body);
                    }
                    catch (Exception e)
                    {
                        throw new MessageConversionException(
                                  "failed to convert text-based Message content", e);
                    }
                }
                else if (contentType != null &&
                         contentType.Equals(MessageProperties.CONTENT_TYPE_SERIALIZED_OBJECT))
                {
                    throw new MessageConversionException("Content type: " + MessageProperties.CONTENT_TYPE_SERIALIZED_OBJECT + " unsupported");
                }
            }

            if (content == null)
            {
                content = message.Body;
            }

            return(content);
        }
コード例 #7
0
ファイル: HtmlClient.cs プロジェクト: jrsell/XHtmlKit
        public static async Task <HtmlTextReader> GetHtmlTextReaderAsync(this HttpContent content, Encoding defaultEncoding, bool detectEncoding)
        {
            // Try to get the stream's encoding from the Response Headers, or fall back on default.
            // We will also try to detect the encoding from the Byte Order Mark if there is no encoding supplied
            // by the headers. If both of these fail, the Parser should look for an encoding in the <meta> tags of
            // the html itself.
            Encoding encoding = defaultEncoding;

            // Try to detect the encoding from Http Headers
            bool gotEncodingFromHttpHeaders = false;

            if (detectEncoding)
            {
                var    contentHeaders = content.Headers;
                string charset        = (contentHeaders.ContentType != null) ? contentHeaders.ContentType.CharSet : null;
                encoding = EncodingUtils.GetEncoding(charset);
                gotEncodingFromHttpHeaders = encoding != null;
                encoding = (encoding == null ? defaultEncoding : encoding);
                System.Diagnostics.Debug.WriteLine("Detected encoding: charset: " + charset + ", got encoding from headers: " + gotEncodingFromHttpHeaders);
            }

            // Out of band encoding can be either passed in by clients, or found in the http headers...
            bool gotEncodingFromOutOfBandSource   = !detectEncoding || gotEncodingFromHttpHeaders;
            EncodingConfidence encodingConfidence = gotEncodingFromOutOfBandSource ? EncodingConfidence.Certain : EncodingConfidence.Tentative;

            // If encoding was NOT supplied out of band, then we will try to detect it from the stream's BOM
            bool tryToDetectEncodingFromByteOrderMark = (encodingConfidence == EncodingConfidence.Tentative);

            // Get the stream from the network
            Stream networkStream = await content.ReadAsStreamAsync().ConfigureAwait(false);

            // If we are still tentative about the encoding, pop the stream into a wrapper that let's us re-wind.
            Stream baseStream = (encodingConfidence == EncodingConfidence.Tentative) ? new HtmlStream(networkStream) : networkStream;

            // Return a HtmlTextReader with the encoding as detected so far...
            HtmlTextReader htmlReader = new HtmlTextReader(baseStream, encoding, encodingConfidence);

            return(htmlReader);
        }
コード例 #8
0
        public override object FromMessage(IMessage from, Type targetType, object convertionsHint)
        {
            if (from is not IMessage <byte[]> message)
            {
                throw new MessageConversionException("Failed to convert non byte[] Message content" + from.GetType());
            }

            object content    = null;
            var    properties = message.Headers;

            if (properties != null)
            {
                var contentType = properties.ContentType();
                if (contentType != null && contentType.StartsWith("text"))
                {
                    var encoding = properties.ContentEncoding();
                    if (encoding == null)
                    {
                        encoding = DefaultCharset;
                    }

                    try
                    {
                        var enc = EncodingUtils.GetEncoding(encoding);
                        content = enc.GetString(message.Payload);
                    }
                    catch (Exception e)
                    {
                        throw new MessageConversionException(
                                  "failed to convert text-based Message content", e);
                    }
                }
                else if (contentType != null &&
                         contentType.Equals(MessageHeaders.CONTENT_TYPE_DOTNET_SERIALIZED_OBJECT))
                {
                    try
                    {
                        var formatter = new BinaryFormatter();
                        var stream    = new MemoryStream(message.Payload);

                        // TODO: don't disable this warning! https://aka.ms/binaryformatter
#pragma warning disable SYSLIB0011 // Type or member is obsolete
#pragma warning disable S5773      // Types allowed to be deserialized should be restricted
                        content = formatter.Deserialize(stream);
#pragma warning restore S5773      // Types allowed to be deserialized should be restricted
#pragma warning restore SYSLIB0011 // Type or member is obsolete
                    }
                    catch (Exception e)
                    {
                        throw new MessageConversionException("failed to convert serialized Message content", e);
                    }
                }
                else if (contentType != null &&
                         contentType.Equals(MessageHeaders.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT))
                {
                    throw new MessageConversionException("Content type: " + MessageHeaders.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT + " unsupported");
                }
            }

            if (content == null)
            {
                _logger?.LogDebug("FromMessage() returning message payload unchanged");
                content = message.Payload;
            }

            return(content);
        }
コード例 #9
0
        public override object FromMessage(IMessage from, Type targetType, object convertionsHint)
        {
            IMessage <byte[]> message = from as IMessage <byte[]>;

            if (message == null)
            {
                throw new MessageConversionException("Failed to convert non byte[] Message content" + from.GetType());
            }

            object content    = null;
            var    properties = message.Headers;

            if (properties != null)
            {
                var contentType = properties.ContentType();
                if (contentType != null && contentType.StartsWith("text"))
                {
                    var encoding = properties.ContentEncoding();
                    if (encoding == null)
                    {
                        encoding = DefaultCharset;
                    }

                    try
                    {
                        var enc = EncodingUtils.GetEncoding(encoding);
                        content = enc.GetString(message.Payload);
                    }
                    catch (Exception e)
                    {
                        throw new MessageConversionException(
                                  "failed to convert text-based Message content", e);
                    }
                }
                else if (contentType != null &&
                         contentType.Equals(MessageHeaders.CONTENT_TYPE_DOTNET_SERIALIZED_OBJECT))
                {
                    try
                    {
                        var formatter = new BinaryFormatter();
                        var stream    = new MemoryStream(message.Payload);
                        content = formatter.Deserialize(stream);
                    }
                    catch (Exception e)
                    {
                        throw new MessageConversionException("failed to convert serialized Message content", e);
                    }
                }
                else if (contentType != null &&
                         contentType.Equals(MessageHeaders.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT))
                {
                    throw new MessageConversionException("Content type: " + MessageHeaders.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT + " unsupported");
                }
            }

            if (content == null)
            {
                _logger?.LogDebug("FromMessage() returning message payload unchanged");
                content = message.Payload;
            }

            return(content);
        }
コード例 #10
0
 protected virtual void DoPublish(IModel channel, Address replyTo, Message message)
 {
     channel.BasicPublish(
         replyTo.ExchangeName,
         replyTo.RoutingKey,
         MandatoryPublish,
         MessagePropertiesConverter.FromMessageProperties(message.MessageProperties, channel.CreateBasicProperties(), EncodingUtils.GetEncoding(Encoding)),
         message.Body);
 }