/// <summary> /// Gets the content of an WsMtomBodyPart. /// </summary> /// <param name="contentID">A string containing a unique content identifier.</param> /// <param name="bodyParts">A WsMtomBodyParts collection.</param> /// <returns>A byte array containing the body part content.</returns> public byte[] GetBodyPartContent(string contentID, WsMtomBodyParts bodyParts) { WsMtomBodyPart bodyPart = null; // XmlReader will convert ':' and '/' to their respective HEX values (%3A, %2F) // for attributes values. So we need to switch them back in order for the compare to work // We may want to change the XML parser to handle this in a more generic way int idx = contentID.IndexOf("%"); while (idx != -1) { if (contentID[idx + 1] == '3' && contentID[idx + 2] == 'A') { contentID = contentID.Substring(0, idx) + ":" + contentID.Substring(idx + 3); } else if (contentID[idx + 1] == '2' && contentID[idx + 2] == 'F') { contentID = contentID.Substring(0, idx) + "/" + contentID.Substring(idx + 3); } idx = contentID.IndexOf("%", idx); } if ((bodyPart = bodyParts["<" + contentID.Substring(4) + ">"]) != null) { return(bodyPart.Content.ToArray()); } return(null); }
/// <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); }
// 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); }
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); }