/// <summary> /// Removes the first occurrence of a specific MtomBodyPart oject from the collection. /// </summary> /// <param name="bodyPart">The MtomBodyPart object to remove from the collection. The value can be null.</param> public void Remove(WsMtomBodyPart bodyPart) { lock (m_threadLock) { m_partsList.Remove(bodyPart); } }
/// <summary> /// Adds an MtomodyPart object to the beginning of the collection. /// </summary> /// <param name="value">The MtomBodyPart object to be added to the beginning of the collection. /// The value can be null. /// </param> public void AddStart(WsMtomBodyPart value) { lock (m_threadLock) { m_partsList.Insert(0, value); } }
/// <summary> /// Adds an MtomodyPart object to the end of the collection. /// </summary> /// <param name="value">The MtomBodyPart object to be added to the end of the collection. /// The value can be null. /// </param> /// <returns>The index at which the MtomBodyPart has been added.</returns> public int Add(WsMtomBodyPart value) { lock (m_threadLock) { return(m_partsList.Add(value)); } }
public MFTestResults MtomTest1_WsMtomBodyPart() { /// <summary> /// 1. Gets and verifies each of the properties of a WsMtomBodyPart object /// 2. Sets and re-verifies all properties /// </summary> /// bool testResult = true; try { WsMtomBodyPart testWMBP = new WsMtomBodyPart(); Log.Comment("ContentID"); if (testWMBP.ContentID != null) if (testWMBP.ContentID.GetType() != Type.GetType("System.String")) throw new Exception("ContentID wrong type"); testWMBP.ContentID = "test datum 1"; if (testWMBP.ContentID.GetType() != Type.GetType("System.String")) throw new Exception("ContentID wrong type after set to new"); if (testWMBP.ContentID != "test datum 1") throw new Exception("ContentID wrong data after set to new"); Log.Comment("ContentTransferEncoding"); if (testWMBP.ContentTransferEncoding != null) if (testWMBP.ContentTransferEncoding.GetType() != Type.GetType("System.String")) throw new Exception("ContentTransferEncoding wrong type"); testWMBP.ContentTransferEncoding = "test datum 2"; if (testWMBP.ContentTransferEncoding.GetType() != Type.GetType("System.String")) throw new Exception("ContentTransferEncoding wrong type after set to new"); if (testWMBP.ContentTransferEncoding != "test datum 2") throw new Exception("ContentTransferEncoding wrong data after set to new"); Log.Comment("ContentType"); if (testWMBP.ContentType != null) if (testWMBP.ContentType.GetType() != Type.GetType("System.String")) throw new Exception("ContentType wrong type"); testWMBP.ContentType = "test datum 3"; if (testWMBP.ContentType.GetType() != Type.GetType("System.String")) throw new Exception("ContentType wrong type after set to new"); if (testWMBP.ContentType != "test datum 3") throw new Exception("ContentType wrong data after set to new"); Log.Comment("Content"); if (testWMBP.Content != null) if (testWMBP.Content.GetType() != Type.GetType("System.IO.MemoryStream")) throw new Exception("Content wrong type"); testWMBP.Content = new System.IO.MemoryStream(); if (testWMBP.Content.GetType() != Type.GetType("System.IO.MemoryStream")) throw new Exception("Content wrong type"); } catch (Exception e) { testResult = false; Log.Comment("Incorrect exception caught: " + e.Message); } return (testResult ? MFTestResults.Pass : MFTestResults.Fail); }
/// <summary> /// Parse the mtom message. Validate and store body parts /// </summary> /// <param name="boundary">A string contining the Mtom boundary element.</param> /// <returns>byte arrary containing the root body part message</returns> public byte[] ParseMessage(string boundary) { if (m_buffer == null) throw new ArgumentNullException(); //"BodyParts", "Must use WsMtom(byte[] buffer) constructor with ParseMessage."); if (boundary == null) throw new ArgumentNullException(); // "boudary", "must not be null"); byte[] soapMessage = null; bool soapPartFound = false; string readBuffer = null; // Set the boundary element m_bodyParts.Boundary = boundary; // Read to first boundary sentinal while ((readBuffer = ReadLine()) != null) { // Make sure there's nothing but blank lines upto first boundary if (readBuffer.Length == 0) continue; // If a boundary line is found break if (readBuffer == "--" + boundary) break; else throw new System.Xml.XmlException(); // "Invalid Mtom pre-boundary characters."); } // Read to the first Mtom header line while ((readBuffer = ReadLine()) != null && readBuffer.Length == 0); // Read Mtom header while (m_curPos < m_length) { WsMtomBodyPart bodyPart = new WsMtomBodyPart(); byte[] bodyPartContent = null; // Read the body part header while (readBuffer.Length != 0) { // Parse the body part header int idx = readBuffer.IndexOf(':'); if(idx != -1) { string headerEntry = readBuffer.Substring(0, idx); string headerValue = readBuffer.Substring(idx+1); switch (headerEntry) { case HttpKnownHeaderNames.ContentType: bodyPart.ContentType = headerValue.Trim(' '); break; case HttpKnownHeaderNames.ContentTransferEncoding: bodyPart.ContentTransferEncoding = headerValue.Trim(' '); break; case HttpKnownHeaderNames.ContentID: bodyPart.ContentID = headerValue.Trim(' '); break; default: throw new System.Xml.XmlException(); // "Invalid Mtom header value \"" + readBuffer + "\""); } } // Read the next header line. readBuffer = ReadLine(); } // Make sure all of the body part header entries are valid if (bodyPart.ContentID == null) throw new ArgumentException(); // "Mtom Content-ID header missing."); if (bodyPart.ContentType == null) throw new ArgumentException(); // "Mtom Content-Type header missing."); if (bodyPart.ContentTransferEncoding == null) throw new ArgumentException(); // "Mtom Content-Transfer-Encoding header missing."); // Only support binary encoding if (bodyPart.ContentTransferEncoding != "binary" && bodyPart.ContentTransferEncoding != "8bit") throw new ArgumentException(); // "Mtom Content-Transfer-Encoding must be \"binary\""); // Read the Mtom body parts if ((bodyPartContent = ReadBinaryBodyPart(boundary)) == null) throw new ArgumentException(); // "Malformed Mtom body part. A valid end boundar is missing."); // If this is the soap message convert the soap message to a byte[] // and store for return if (!soapPartFound) { if (bodyPart.ContentType.ToLower().IndexOf("type=\"application/soap+xml\"") != -1) { soapMessage = bodyPartContent; soapPartFound = true; } else throw new ArgumentException(); // "First Mtom body part must contain the soap message."); } // Store the body part memory stream bodyPart.Content = new MemoryStream(bodyPartContent); // Store the bodyPart in the bodyParts collection m_bodyParts.Add(bodyPart); // If at least two characters don't follow the boundary it is an error. // A mid boundary marker will be followed by \r\n. An end boundary will be // followed by --\r\n. if (m_length - m_curPos > 2) { // Check to see if this was the end boundary. if (m_buffer[m_curPos + 1] == '-' && m_buffer[m_curPos + 2] == '-') break; // If not eat the \r\n characters. else if (m_buffer[m_curPos + 1] == '\r' && m_buffer[m_curPos + 2] == '\n') m_curPos += 3; else throw new ArgumentException(); // "Invalid Mtom boundary element. Expected cr-lf after boundary."); } else throw new ArgumentException(); // "Invalid Mtom boundary element. Expected end boundary trailer or cr-lf."); // read the next header line or blank line readBuffer = ReadLine(); } // Return the soap message body part return soapMessage; }
/// <summary> /// Removes the first occurrence of a specific MtomBodyPart oject from the collection. /// </summary> /// <param name="bodyPart">The MtomBodyPart object to remove from the collection. The value can be null.</param> public void Remove(WsMtomBodyPart bodyPart) { lock (m_threadLock) { m_partsList.Remove(bodyPart); } }
/// <summary> /// Adds an MtomodyPart object to the beginning of the collection. /// </summary> /// <param name="value">The MtomBodyPart object to be added to the beginning of the collection. /// The value can be null. /// </param> public void AddStart(WsMtomBodyPart value) { lock(m_threadLock) { m_partsList.Insert(0, value); } }
/// <summary> /// Adds an MtomodyPart object to the end of the collection. /// </summary> /// <param name="value">The MtomBodyPart object to be added to the end of the collection. /// The value can be null. /// </param> /// <returns>The index at which the MtomBodyPart has been added.</returns> public int Add(WsMtomBodyPart value) { lock (m_threadLock) { return m_partsList.Add(value); } }
/// <summary> /// Builds an WsMtomBodyPart object. /// </summary> /// <param name="content">A byte array containing the binary body part content.</param> /// <param name="contentID">A string containing a unique content identifier.</param> /// <returns>An complete WsMtomBodyPart.</returns> public WsMtomBodyPart CreateNewBodyPart(byte[] content, string contentID) { WsMtomBodyPart bodyPart = new WsMtomBodyPart(); bodyPart.Content = new MemoryStream(content); bodyPart.ContentID = contentID; bodyPart.ContentTransferEncoding = "binary"; bodyPart.ContentType = "application/xop+xml;charset=UTF-8;type=\"application/soap+xml\""; return bodyPart; }
/// <summary> /// Parse the mtom message. Validate and store body parts /// </summary> /// <param name="boundary">A string contining the Mtom boundary element.</param> /// <returns>byte arrary containing the root body part message</returns> public byte[] ParseMessage(string boundary) { if (m_buffer == null) { throw new ArgumentNullException(); //"BodyParts", "Must use WsMtom(byte[] buffer) constructor with ParseMessage."); } if (boundary == null) { throw new ArgumentNullException(); // "boudary", "must not be null"); } byte[] soapMessage = null; bool soapPartFound = false; string readBuffer = null; // Set the boundary element m_bodyParts.Boundary = boundary; // Read to first boundary sentinal while ((readBuffer = ReadLine()) != null) { // Make sure there's nothing but blank lines upto first boundary if (readBuffer.Length == 0) { continue; } // If a boundary line is found break if (readBuffer == "--" + boundary) { break; } else { throw new System.Xml.XmlException(); // "Invalid Mtom pre-boundary characters."); } } // Read to the first Mtom header line while ((readBuffer = ReadLine()) != null && readBuffer.Length == 0) { ; } // Read Mtom header while (m_curPos < m_length) { WsMtomBodyPart bodyPart = new WsMtomBodyPart(); byte[] bodyPartContent = null; // Read the body part header while (readBuffer.Length != 0) { // Parse the body part header int idx = readBuffer.IndexOf(':'); if (idx != -1) { string headerEntry = readBuffer.Substring(0, idx); string headerValue = readBuffer.Substring(idx + 1); switch (headerEntry) { case HttpKnownHeaderNames.ContentType: bodyPart.ContentType = headerValue.Trim(' '); break; case HttpKnownHeaderNames.ContentTransferEncoding: bodyPart.ContentTransferEncoding = headerValue.Trim(' '); break; case HttpKnownHeaderNames.ContentID: bodyPart.ContentID = headerValue.Trim(' '); break; default: throw new System.Xml.XmlException(); // "Invalid Mtom header value \"" + readBuffer + "\""); } } // Read the next header line. readBuffer = ReadLine(); } // Make sure all of the body part header entries are valid if (bodyPart.ContentID == null) { throw new ArgumentException(); // "Mtom Content-ID header missing."); } if (bodyPart.ContentType == null) { throw new ArgumentException(); // "Mtom Content-Type header missing."); } if (bodyPart.ContentTransferEncoding == null) { throw new ArgumentException(); // "Mtom Content-Transfer-Encoding header missing."); } // Only support binary encoding if (bodyPart.ContentTransferEncoding != "binary" && bodyPart.ContentTransferEncoding != "8bit") { throw new ArgumentException(); // "Mtom Content-Transfer-Encoding must be \"binary\""); } // Read the Mtom body parts if ((bodyPartContent = ReadBinaryBodyPart(boundary)) == null) { throw new ArgumentException(); // "Malformed Mtom body part. A valid end boundar is missing."); } // If this is the soap message convert the soap message to a byte[] // and store for return if (!soapPartFound) { if (bodyPart.ContentType.ToLower().IndexOf("type=\"application/soap+xml\"") != -1) { soapMessage = bodyPartContent; soapPartFound = true; } else { throw new ArgumentException(); // "First Mtom body part must contain the soap message."); } } // Store the body part memory stream bodyPart.Content = new MemoryStream(bodyPartContent); // Store the bodyPart in the bodyParts collection m_bodyParts.Add(bodyPart); // If at least two characters don't follow the boundary it is an error. // A mid boundary marker will be followed by \r\n. An end boundary will be // followed by --\r\n. if (m_length - m_curPos > 2) { // Check to see if this was the end boundary. if (m_buffer[m_curPos + 1] == '-' && m_buffer[m_curPos + 2] == '-') { break; } // If not eat the \r\n characters. else if (m_buffer[m_curPos + 1] == '\r' && m_buffer[m_curPos + 2] == '\n') { m_curPos += 3; } else { throw new ArgumentException(); // "Invalid Mtom boundary element. Expected cr-lf after boundary."); } } else { throw new ArgumentException(); // "Invalid Mtom boundary element. Expected end boundary trailer or cr-lf."); } // read the next header line or blank line readBuffer = ReadLine(); } // Return the soap message body part return(soapMessage); }
// TwoWayAttachmentResp: Construct response message for TwoWayAttachmentRequest public WsMessage TwoWayAttachmentResp(WsWsaHeader header) { // Create xmlWriter object MemoryStream soapStream = new MemoryStream(); XmlWriter xmlWriter = XmlWriter.Create(soapStream); // Write processing instructions and root element xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'"); xmlWriter.WriteStartElement("soap", "Envelope", "http://www.w3.org/2003/05/soap-envelope"); // Write namespaces ProtocolVersion10 ver = new ProtocolVersion10(); xmlWriter.WriteAttributeString("xmlns", "wsa", null, ver.AddressingNamespace); xmlWriter.WriteAttributeString("xmlns", "xop", null, WsWellKnownUri.XopNamespaceUri); xmlWriter.WriteAttributeString("xmlns", "att", null, "http://schemas.example.org/AttachmentService"); // Write header xmlWriter.WriteStartElement("soap", "Header", null); xmlWriter.WriteStartElement("wsa", "To", null); xmlWriter.WriteString("http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous"); xmlWriter.WriteEndElement(); // End To xmlWriter.WriteStartElement("wsa", "Action", null); xmlWriter.WriteString("http://schemas.example.org/AttachmentService/TwoWayAttachmentResponse"); xmlWriter.WriteEndElement(); // End Action xmlWriter.WriteStartElement("wsa", "RelatesTo", null); xmlWriter.WriteString(header.MessageID); xmlWriter.WriteEndElement(); // End RelatesTo xmlWriter.WriteStartElement("wsa", "MessageID", null); xmlWriter.WriteString("urn:uuid:" + Guid.NewGuid().ToString()); xmlWriter.WriteEndElement(); // End To xmlWriter.WriteEndElement(); // End Header // write body xmlWriter.WriteStartElement("soap", "Body", null); xmlWriter.WriteStartElement("att", "TwoWayAttachmentResponse", null); xmlWriter.WriteStartElement("att", "Param", null); xmlWriter.WriteStartElement("xop", "Include", null); xmlWriter.WriteAttributeString("href", "cid:0@body"); xmlWriter.WriteString(""); xmlWriter.WriteEndElement(); // End Include xmlWriter.WriteEndElement(); // End Param xmlWriter.WriteEndElement(); // End TwoWayAttachmentResponse xmlWriter.WriteEndElement(); // End Body xmlWriter.WriteEndElement(); // End Envelope // Create return buffer and close writer xmlWriter.Flush(); xmlWriter.Close(); WsMessage msg = new WsMessage(null, soapStream.ToArray(), WsPrefix.Wse); // Clear the hosted service's MTOM body parts collection and build return body parts collection // Create and store soap body part (As per spec soap envelope must be first body part) WsMtomBodyPart bodyPart = new WsMtomBodyPart(); // Create and store attachment body part bodyPart = new WsMtomBodyPart(); HelpIcon testIcon = new HelpIcon(); bodyPart.Content = testIcon.Data; bodyPart.ContentID = "<0@body>"; bodyPart.ContentTransferEncoding = "binary"; bodyPart.ContentType = "application/octet-stream"; msg.BodyParts.Add(bodyPart); // Set the response type so the HTTP processor knows we are sending MTOM //MessageType = WsMessageType.Mtom; // We are returning the actual response in the hosted service's body parts, so return null return msg; }