Exemple #1
0
        public void CanWriteMultilineBase64()
        {
            const string text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

            var output = new StringBuilder();

            using (var contentStream = new MemoryStream(Encoding.UTF8.GetBytes(text)))
                using (var attachment = new XRoadAttachment(contentStream))
                    using (var writer = new StringWriter(output))
                    {
                        attachment.WriteAsBase64(writer);
                        writer.Flush();
                    }

            var encodedOutput = output.ToString();

            var lines = encodedOutput.Split(new [] { "\r\n" }, StringSplitOptions.None);

            Assert.Equal(9, lines.Length);
            Assert.Equal("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdCwg", lines[0]);
            Assert.Equal("c2VkIGRvIGVpdXNtb2QgdGVtcG9yIGluY2lkaWR1bnQgdXQgbGFib3JlIGV0IGRvbG9yZSBtYWdu", lines[1]);
            Assert.Equal("YSBhbGlxdWEuIFV0IGVuaW0gYWQgbWluaW0gdmVuaWFtLCBxdWlzIG5vc3RydWQgZXhlcmNpdGF0", lines[2]);
            Assert.Equal("aW9uIHVsbGFtY28gbGFib3JpcyBuaXNpIHV0IGFsaXF1aXAgZXggZWEgY29tbW9kbyBjb25zZXF1", lines[3]);
            Assert.Equal("YXQuIER1aXMgYXV0ZSBpcnVyZSBkb2xvciBpbiByZXByZWhlbmRlcml0IGluIHZvbHVwdGF0ZSB2", lines[4]);
            Assert.Equal("ZWxpdCBlc3NlIGNpbGx1bSBkb2xvcmUgZXUgZnVnaWF0IG51bGxhIHBhcmlhdHVyLiBFeGNlcHRl", lines[5]);
            Assert.Equal("dXIgc2ludCBvY2NhZWNhdCBjdXBpZGF0YXQgbm9uIHByb2lkZW50LCBzdW50IGluIGN1bHBhIHF1", lines[6]);
            Assert.Equal("aSBvZmZpY2lhIGRlc2VydW50IG1vbGxpdCBhbmltIGlkIGVzdCBsYWJvcnVtLg==", lines[7]);
            Assert.Equal("", lines[8]);

            var encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(text));

            Assert.NotEqual(encoded, encodedOutput);
            Assert.Equal(encoded, encodedOutput.Replace("\r\n", ""));
        }
Exemple #2
0
        public override void Serialize(XmlWriter writer, IXmlTemplateNode templateNode, object value, ContentDefinition content, XRoadMessage message)
        {
            var attachment = new XRoadAttachment((Stream)value);

            message.AllAttachments.Add(attachment);

            if (message.BinaryMode == BinaryMode.Attachment)
            {
                if (!(content.Particle is RequestDefinition))
                {
                    message.Style.WriteExplicitType(writer, encodedTypeName);
                }

                writer.WriteAttributeString("href", $"cid:{attachment.ContentID}");
                return;
            }

            if (!(content.Particle is RequestDefinition))
            {
                message.Style.WriteExplicitType(writer, Definition.Name);
            }

            attachment.IsMultipartContent = false;
            attachment.WriteAsBase64(writer);
        }
Exemple #3
0
 public void CanWriteContentWithAttachments()
 {
     using (var x = new XRoadAttachment(new MemoryStream(new byte[] { 1, 2, 3, 4 })))
         using (var y = new XRoadAttachment(new MemoryStream(new byte[] { 5, 6, 7, 8, 9 })))
         {
             var contentLength = WriteContent("test2", new[] { x, y });
             Assert.Equal(562L, contentLength);
         }
 }
        public override object Deserialize(XmlReader reader, IXmlTemplateNode templateNode, ContentDefinition content, XRoadMessage message)
        {
            var contentID = reader.GetAttribute("href");

            if (string.IsNullOrWhiteSpace(contentID))
            {
                if (message.IsMultipartContainer)
                {
                    throw new InvalidQueryException("Missing `href` attribute to multipart content.");
                }

                var tempAttachment = new XRoadAttachment(new MemoryStream())
                {
                    IsMultipartContent = false
                };
                message.AllAttachments.Add(tempAttachment);

                if (reader.IsEmptyElement)
                {
                    return(MoveNextAndReturn(reader, tempAttachment.ContentStream));
                }

                reader.Read();

                const int bufferSize = 1000;

                int bytesRead;
                var buffer = new byte[bufferSize];

                while ((bytesRead = reader.ReadContentAsBase64(buffer, 0, bufferSize)) > 0)
                {
                    tempAttachment.ContentStream.Write(buffer, 0, bytesRead);
                }

                return(tempAttachment.ContentStream);
            }

            var attachment = message.GetAttachment(contentID.Substring(4));

            if (attachment == null)
            {
                throw new InvalidQueryException($"MIME multipart message does not contain message part with ID `{contentID}`.");
            }

            if (reader.IsEmptyElement)
            {
                return(MoveNextAndReturn(reader, attachment.ContentStream));
            }

            reader.ReadToEndElement();

            return(attachment.ContentStream);
        }
Exemple #5
0
        public void Serialize(XmlWriter writer, IXmlTemplateNode templateNode, object value, ContentDefinition content, XRoadMessage message)
        {
            var attachment = new XRoadAttachment((Stream)value);

            message.AllAttachments.Add(attachment);

            message.Style.WriteType(writer, Definition, content);

            writer.WriteStartElement(PrefixConstants.XOP, "Include", NamespaceConstants.XOP);

            writer.WriteAttributeString("href", $"cid:{attachment.ContentID}");

            writer.WriteEndElement();
        }
Exemple #6
0
        public void CanWriteEmptyBase64()
        {
            var output = new StringBuilder();

            using (var contentStream = new MemoryStream())
                using (var attachment = new XRoadAttachment(contentStream))
                    using (var writer = new StringWriter(output))
                    {
                        attachment.WriteAsBase64(writer);
                        writer.Flush();
                    }

            Assert.Equal("\r\n", output.ToString());
        }
Exemple #7
0
        public void CanWriteBase64()
        {
            var output = new StringBuilder();

            using (var contentStream = new MemoryStream(Encoding.UTF8.GetBytes("ABC")))
                using (var attachment = new XRoadAttachment(contentStream))
                    using (var writer = new StringWriter(output))
                    {
                        attachment.WriteAsBase64(writer);
                        writer.Flush();
                    }

            Assert.Equal("QUJD\r\n", output.ToString());
        }
        public void Serialize(XmlWriter writer, IXmlTemplateNode templateNode, object value, ContentDefinition content, XRoadMessage message)
        {
            var attachment = new XRoadAttachment((Stream)value);

            message.AllAttachments.Add(attachment);

            if (!(content.Particle is RequestDefinition))
            {
                message.Style.WriteExplicitType(writer, Definition.Name);
            }

            writer.WriteStartElement(PrefixConstants.XOP, "Include", NamespaceConstants.XOP);
            //writer.WriteAttributeString(PrefixConstants.XMIME, "contentType", NamespaceConstants.XMIME, "application/octet-stream");

            writer.WriteAttributeString("href", $"cid:{attachment.ContentID}");

            writer.WriteEndElement();
        }