/// <summary> /// Merges the message from the input stream based on the contentType provided /// </summary> /// <typeparam name="TBuilder">A type derived from IBuilderLite</typeparam> /// <param name="builder">An instance of a message builder</param> /// <param name="options">Options specific to reading this message and/or content type</param> /// <param name="contentType">The mime type of the input stream content</param> /// <param name="input">The stream to read the message from</param> /// <returns>The same builder instance that was supplied in the builder parameter</returns> public static TBuilder MergeFrom <TBuilder>( #if !NOEXTENSIONS this #endif TBuilder builder, MessageFormatOptions options, string contentType, Stream input) where TBuilder : IBuilderLite { ICodedInputStream codedInput = MessageFormatFactory.CreateInputStream(options, contentType, input); codedInput.ReadMessageStart(); builder.WeakMergeFrom(codedInput, options.ExtensionRegistry); codedInput.ReadMessageEnd(); return(builder); }
public SendMessageResult SendMessage(string chatRoomId, string token, string toUserId, string message, bool bold = false, bool italic = false, bool underline = false, string fontName = null, int?fontSize = null, string color = null) { var result = new SendMessageResult(); string userId = chatRoomStorage.GetUserIdByToken(token); if (userId == null) { result.Error = "Chat disconnected!"; return(result); } if (!chatRoomStorage.IsUserInRoom(chatRoomId, userId)) { result.Error = "Chat disconnected!"; return(result); } // TODO: Process message (trim, filter) if (!string.IsNullOrEmpty(color)) { color = Regex.Replace(color, @"[^\w\#]", String.Empty); // Strip dangerous input } var formatOptions = new MessageFormatOptions { Bold = bold, Italic = italic, Underline = underline, Color = color, FontName = fontName, }; if (fontSize.HasValue) { formatOptions.FontSize = fontSize.Value; } chatRoomStorage.AddMessage(chatRoomId, new Message { Content = WebUtility.HtmlEncode(message), FromUserId = userId, ToUserId = toUserId, MessageType = MessageTypeEnum.User, Timestamp = Miscellaneous.GetTimestamp(), FormatOptions = formatOptions }); return(result); }
/// <summary> /// Used to implement a service endpoint on an HTTP server. This works with services generated with the /// service_generator_type option set to IRPCDISPATCH. /// </summary> /// <param name="stub">The service execution stub</param> /// <param name="methodName">The name of the method being invoked</param> /// <param name="options">optional arguments for the format reader/writer</param> /// <param name="contentType">The mime type for the input stream</param> /// <param name="input">The input stream</param> /// <param name="responseType">The mime type for the output stream</param> /// <param name="output">The output stream</param> public static void HttpCallMethod( #if !NOEXTENSIONS this #endif IRpcServerStub stub, string methodName, MessageFormatOptions options, string contentType, Stream input, string responseType, Stream output) { ICodedInputStream codedInput = MessageFormatFactory.CreateInputStream(options, contentType, input); codedInput.ReadMessageStart(); IMessageLite response = stub.CallMethod(methodName, codedInput, options.ExtensionRegistry); codedInput.ReadMessageEnd(); WriteTo(response, options, responseType, output); }
public void TestWriteToCustomType() { var options = new MessageFormatOptions(); //Remove existing mime-type mappings options.MimeOutputTypes.Clear(); //Add our own options.MimeOutputTypes.Add("-custom-XML-mime-type-", XmlFormatWriter.CreateInstance); Assert.AreEqual(1, options.MimeOutputTypes.Count); MemoryStream ms = new MemoryStream(); Extensions.WriteTo(TestXmlMessage.CreateBuilder().SetText("a").SetNumber(1).Build(), options, "-custom-XML-mime-type-", ms); Assert.AreEqual("<root><text>a</text><number>1</number></root>", Encoding.UTF8.GetString(ms.ToArray(), 0, (int)ms.Length)); }
/// <summary> /// Writes the message instance to the stream using the content type provided /// </summary> /// <param name="message">An instance of a message</param> /// <param name="options">Options specific to writing this message and/or content type</param> /// <param name="contentType">The mime type of the content to be written</param> /// <param name="output">The stream to write the message to</param> public static void WriteTo( #if !NOEXTENSIONS this #endif IMessageLite message, MessageFormatOptions options, string contentType, Stream output) { ICodedOutputStream codedOutput = MessageFormatFactory.CreateOutputStream(options, contentType, output); // Output the appropriate message preamble codedOutput.WriteMessageStart(); // Write the message content to the output message.WriteTo(codedOutput); // Write the closing message fragment codedOutput.WriteMessageEnd(); codedOutput.Flush(); }
public void TestReadCustomMimeTypes() { var options = new MessageFormatOptions(); //Remove existing mime-type mappings options.MimeInputTypes.Clear(); //Add our own options.MimeInputTypes.Add("-custom-XML-mime-type-", XmlFormatReader.CreateInstance); Assert.AreEqual(1, options.MimeInputTypes.Count); Stream xmlStream = new MemoryStream(Encoding.UTF8.GetBytes( Extensions.ToXml(TestXmlMessage.CreateBuilder().SetText("a").SetNumber(1).Build()) )); TestXmlMessage msg = Extensions.MergeFrom(new TestXmlMessage.Builder(), options, "-custom-XML-mime-type-", xmlStream) .Build(); Assert.AreEqual("a", msg.Text); Assert.AreEqual(1, msg.Number); }
public void TestXmlWriterOptions() { TestXmlMessage message = TestXmlMessage.CreateBuilder().SetText("a").AddNumbers(1).AddNumbers(2).Build(); MessageFormatOptions options = new MessageFormatOptions() { XmlWriterOptions = XmlWriterOptions.OutputNestedArrays, XmlWriterRootElementName = "root-node" }; MemoryStream ms = new MemoryStream(); Extensions.WriteTo(message, options, "application/xml", ms); ms.Position = 0; TestXmlMessage.Builder builder = TestXmlMessage.CreateBuilder(); XmlFormatReader.CreateInstance(ms) .SetOptions(XmlReaderOptions.ReadNestedArrays) .Merge("root-node", builder); Assert.AreEqual("a", builder.Text); Assert.AreEqual(1, builder.NumbersList[0]); Assert.AreEqual(2, builder.NumbersList[1]); }
public void TestXmlReaderOptions() { MemoryStream ms = new MemoryStream(); XmlFormatWriter.CreateInstance(ms) .SetOptions(XmlWriterOptions.OutputNestedArrays) .WriteMessage("my-root-node", TestXmlMessage.CreateBuilder().SetText("a").AddNumbers(1).AddNumbers(2).Build()); ms.Position = 0; MessageFormatOptions options = new MessageFormatOptions() { XmlReaderOptions = XmlReaderOptions.ReadNestedArrays, XmlReaderRootElementName = "my-root-node" }; TestXmlMessage msg = Extensions.MergeFrom(new TestXmlMessage.Builder(), options, "application/xml", ms) .Build(); Assert.AreEqual("a", msg.Text); Assert.AreEqual(1, msg.NumbersList[0]); Assert.AreEqual(2, msg.NumbersList[1]); }