Esempio n. 1
0
        public Request Body(object body)
        {
            ICodecManager codecManager = Session.Service.CodecManager;

            if (BodyContentType == null)
            {
                BodyContentType = Session.DefaultRequestMediaType;
            }

            if (body != null)
            {
                MediaTypeWriterRegistration writer = codecManager.GetWriter(body.GetType(), BodyContentType ?? MediaType.Wildcard);
                if (BodyContentType == null)
                {
                    BodyContentType = writer.MediaType;
                }
                BodyCodec = writer.Codec;
            }
            else if (BodyContentType == null)
            {
                BodyContentType = MediaType.ApplicationFormUrlEncoded;
            }

            if (BodyContentType.Matches("multipart/form-data"))
            {
                BodyBoundary = Guid.NewGuid().ToString();
            }

            BodyData = body;

            return(this);
        }
        private void VerifyPayload(byte[] payloadBytes, BodyContentType bodyContentType, bool forRequest)
        {
            using (MemoryStream stream = new MemoryStream(payloadBytes))
                using (StreamReader sr = new StreamReader(stream))
                {
                    string payload = sr.ReadToEnd();

                    string expectedPayload = null;
                    if (forRequest)
                    {
                        expectedPayload = Regex.Replace(
                            bodyContentType == BodyContentType.Textual
                        ? ExpectedRequestTextualPayload
                        : ExpectedRequestBinaryPayload,
                            "\"body\": \"__ENCODED_TOKEN_A__\"",
                            "\"body\":" + GetEncodedStringContent(bodyContentType, true),
                            RegexOptions.Multiline);
                    }
                    else
                    {
                        expectedPayload = Regex.Replace(
                            bodyContentType == BodyContentType.Textual
                        ? ExpectedResponseTextualPayload
                        : ExpectedResponseBinaryPayload,
                            "\"body\": \"__ENCODED_TOKEN_B__\"",
                            "\"body\":" + GetEncodedStringContent(bodyContentType, false),
                            RegexOptions.Multiline);
                    }

                    string expectedNormalizedPayload = GetNormalizedJsonMessage(expectedPayload);
                    string normalizedPayload         = GetNormalizedJsonMessage(payload);
                    Assert.True(expectedNormalizedPayload.Equals(normalizedPayload));
                }
        }
Esempio n. 3
0
        protected void SetBody(object body)
        {
            ICodecManager codecManager = Session.Service.CodecManager;

            if (BodyContentType == null)
            {
                BodyContentType = Session.DefaultRequestMediaType;
            }

            if (body != null)
            {
                MediaTypeWriterRegistration writer = codecManager.GetWriter(body.GetType(), BodyContentType ?? MediaType.Wildcard);
                if (BodyContentType == null)
                {
                    BodyContentType = writer.MediaType;
                }
                if (BodyContentType.IsAnyWildcard)
                {
                    throw new InvalidOperationException($"Wildcard media type {BodyContentType} is not a valid content type.");
                }
                BodyCodec = writer.Codec;
            }
            else if (BodyContentType == null)
            {
                BodyContentType = MediaType.ApplicationFormUrlEncoded;
            }

            if (BodyContentType.Matches("multipart/form-data"))
            {
                BodyBoundary = Guid.NewGuid().ToString();
            }

            BodyData = body;
        }
        private void BodyContentTest(BodyContentType bodyContentType)
        {
            byte[] requestPayload = ClientWriteBatchRequest(bodyContentType);

            VerifyPayload(requestPayload, bodyContentType, /*forRequest*/ true);
            byte[] responsePayload = this.ServiceReadBatchRequestAndWriterBatchResponse(requestPayload, bodyContentType);
            VerifyPayload(responsePayload, bodyContentType, /*forRequest*/ false);
            this.ClientReadBatchResponse(responsePayload, bodyContentType);
        }
 public static string GetNotificationBodyContent(BodyContentType contentType)
 {
     switch (contentType)
     {
         case BodyContentType.PlainText:
             return getBodyContentByExtension("txt");
         case BodyContentType.Markdown:
             return getBodyContentByExtension("md");
         default:
             throw new ArgumentException("unknown content type");
     }
 }
        private byte[] ClientWriteBatchRequest(BodyContentType bodyContentType)
        {
            MemoryStream stream = new MemoryStream();

            IODataRequestMessage requestMessage = new InMemoryMessage {
                Stream = stream
            };

            requestMessage.SetHeader(ODataConstants.ContentTypeHeader, batchContentTypeApplicationJson);

            using (ODataMessageWriter messageWriter = new ODataMessageWriter(requestMessage))
            {
                ODataBatchWriter batchWriter = messageWriter.CreateODataBatchWriter();

                batchWriter.WriteStartBatch();

                // Write a change set with update operation.
                batchWriter.WriteStartChangeset();

                // Create an update operation in the change set.
                ODataBatchOperationRequestMessage updateOperationMessage = batchWriter.CreateOperationRequestMessage(
                    "PUT", new Uri(serviceDocumentUri + "MyBlob"), "1");

                // Set the content type with explicit character set so that the content string
                // is flushed into the operation message body stream without byte-order-mark.
                updateOperationMessage.SetHeader("CoNtEnt-TYPE", GetContentType(bodyContentType));

                // Use the message writer to write encoded string content.
                using (ODataMessageWriter operationMessageWriter = new ODataMessageWriter(updateOperationMessage))
                {
                    operationMessageWriter.WriteValue(GetEncodedContentObject(bodyContentType, /*forRequest*/ true));
                }

                batchWriter.WriteEndChangeset();

                // Write a query operation.
                ODataBatchOperationRequestMessage queryOperationMessage = batchWriter.CreateOperationRequestMessage(
                    "GET", new Uri(serviceDocumentUri + "MyBlob"), /*contentId*/ null);

                // Header modification on inner payload.
                queryOperationMessage.SetHeader("AcCePt", GetContentType(bodyContentType));

                batchWriter.WriteEndBatch();

                stream.Position = 0;
                return(stream.ToArray());
            }
        }
        private void ClientReadBatchResponse(byte[] responsePayload, BodyContentType bodyContentType)
        {
            IODataResponseMessage responseMessage = new InMemoryMessage()
            {
                Stream = new MemoryStream(responsePayload)
            };

            responseMessage.SetHeader(ODataConstants.ContentTypeHeader, batchContentTypeApplicationJson);
            using (ODataMessageReader messageReader = new ODataMessageReader(responseMessage, new ODataMessageReaderSettings(), null))
            {
                ODataBatchReader batchReader = messageReader.CreateODataBatchReader();
                while (batchReader.Read())
                {
                    switch (batchReader.State)
                    {
                    case ODataBatchReaderState.Operation:
                        // Encountered an operation (either top-level or in a change set)
                        ODataBatchOperationResponseMessage operationMessage = batchReader.CreateOperationResponseMessage();
                        if (operationMessage.StatusCode == 200)
                        {
                            using (Stream operationMessageBody = operationMessage.GetStream())
                            {
                                // Verify the bytes in the response body.
                                byte[] sampleBytes = bodyContentType == BodyContentType.Textual
                                        ? Encoding.UTF8.GetBytes("\"" + this.textualSampleStringB + "\"")
                                        : this.binarySampleBytesB;

                                Assert.Equal(operationMessageBody.Length, sampleBytes.Length);
                                foreach (byte samplebyte in sampleBytes)
                                {
                                    Assert.Equal(samplebyte, operationMessageBody.ReadByte());
                                }
                            }
                        }
                        else
                        {
                            Assert.True(201 == operationMessage.StatusCode);
                        }
                        break;
                    }
                }
            }
        }
        private string GetContentType(BodyContentType bodyContentType)
        {
            string result;

            switch (bodyContentType)
            {
            case BodyContentType.Textual:
                result = "text/plain; charset=utf-8";
                break;

            case BodyContentType.Binary:
                result = MimeConstants.MimeApplicationOctetStream + "; charset=utf-8";
                break;

            default:
                result = null;
                break;
            }

            return(result);
        }
        private object GetEncodedContentObject(BodyContentType bodyContentType, bool forRequest)
        {
            object result = null;

            switch (bodyContentType)
            {
            case BodyContentType.Textual:
                result = GetEncodedStringContent(bodyContentType, forRequest);
                break;

            case BodyContentType.Binary:
                // Create binary value object representing a string consisting of base64url-encoding characters
                result = Encoding.UTF8.GetBytes(GetEncodedStringContent(bodyContentType, forRequest));
                break;

            default:
                break;
            }

            return(result);
        }
Esempio n. 10
0
        // Token: 0x06001AB6 RID: 6838 RVA: 0x00064D7C File Offset: 0x00062F7C
        private List <BodyContentAttributedValue> GetNotesFromActiveDirectory(ADObjectId adObjectId)
        {
            PersonId personId = this.IsAdPersonLinkedInMailbox(adObjectId);

            if (personId != null)
            {
                return(this.GetNotesFromStore(personId));
            }
            ADRecipient adrecipient = this.adRecipientSession.FindByObjectGuid(adObjectId.ObjectGuid);
            List <BodyContentAttributedValue> list = new List <BodyContentAttributedValue>();

            if (adrecipient != null)
            {
                IADOrgPerson iadorgPerson = adrecipient as IADOrgPerson;
                if (iadorgPerson != null && !string.IsNullOrWhiteSpace(iadorgPerson.Notes))
                {
                    BodyContentType bodyContentType = new BodyContentType();
                    bodyContentType.BodyType = BodyType.Text;
                    if (iadorgPerson.Notes.Length > this.maxBytesToFetch)
                    {
                        bodyContentType.Value       = iadorgPerson.Notes.Substring(0, this.maxBytesToFetch);
                        bodyContentType.IsTruncated = true;
                    }
                    else
                    {
                        bodyContentType.Value       = iadorgPerson.Notes;
                        bodyContentType.IsTruncated = false;
                    }
                    BodyContentAttributedValue item = new BodyContentAttributedValue(bodyContentType, new string[]
                    {
                        WellKnownNetworkNames.GAL
                    });
                    list.Add(item);
                }
            }
            return(list);
        }
        // Token: 0x06001AD2 RID: 6866 RVA: 0x00065C5C File Offset: 0x00063E5C
        private static void PostUploadMessage(string groupAddress, string userAddress, string userDisplayName, BaseItemId referenceItemId, string fileName, string contentUrl, string providerType, string endpointUrl, string sessionId)
        {
            BodyContentType bodyContentType = new BodyContentType();

            bodyContentType.Value = string.Format(Strings.ModernGroupAttachmentUploadNoticeBody, fileName, userDisplayName);
            ReferenceAttachmentType referenceAttachmentType = new ReferenceAttachmentType();

            referenceAttachmentType.AttachLongPathName  = contentUrl;
            referenceAttachmentType.ProviderEndpointUrl = endpointUrl;
            referenceAttachmentType.ProviderType        = providerType;
            referenceAttachmentType.Name = fileName;
            ReplyToItemType replyToItemType = new ReplyToItemType();

            replyToItemType.NewBodyContent  = bodyContentType;
            replyToItemType.Attachments     = new AttachmentType[1];
            replyToItemType.Attachments[0]  = referenceAttachmentType;
            replyToItemType.ReferenceItemId = referenceItemId;
            PostModernGroupItemJsonRequest postModernGroupItemJsonRequest = new PostModernGroupItemJsonRequest();

            postModernGroupItemJsonRequest.Body       = new PostModernGroupItemRequest();
            postModernGroupItemJsonRequest.Body.Items = new NonEmptyArrayOfAllItemsType();
            postModernGroupItemJsonRequest.Body.Items.Add(replyToItemType);
            postModernGroupItemJsonRequest.Body.ModernGroupEmailAddress = new EmailAddressWrapper();
            postModernGroupItemJsonRequest.Body.ModernGroupEmailAddress.EmailAddress = groupAddress;
            postModernGroupItemJsonRequest.Body.ModernGroupEmailAddress.MailboxType  = MailboxHelper.MailboxTypeType.GroupMailbox.ToString();
            OWAService owaservice = new OWAService();

            GetWacAttachmentInfo.PostUploadMessageAsyncState postUploadMessageAsyncState = new GetWacAttachmentInfo.PostUploadMessageAsyncState();
            postUploadMessageAsyncState.MailboxSmtpAddress = groupAddress;
            postUploadMessageAsyncState.LogonSmtpAddress   = userAddress;
            postUploadMessageAsyncState.OwaService         = owaservice;
            postUploadMessageAsyncState.SessionId          = sessionId;
            IAsyncResult asyncResult = owaservice.BeginPostModernGroupItem(postModernGroupItemJsonRequest, null, null);

            asyncResult.AsyncWaitHandle.WaitOne();
            PostModernGroupItemResponse body = owaservice.EndPostModernGroupItem(asyncResult).Body;
        }
        private string GetEncodedStringContent(BodyContentType bodyContentType, bool forRequest)
        {
            string result = null;

            switch (bodyContentType)
            {
            case BodyContentType.Textual:
                string text = JsonLightUtils.GetJsonEncodedString(forRequest ? this.textualSampleStringA : this.textualSampleStringB);
                result = string.Format(CultureInfo.InvariantCulture, "\"{0}\"", text);
                break;

            case BodyContentType.Binary:
                byte[] bytes = forRequest ? this.binarySampleBytesA : this.binarySampleBytesB;
                // Beginning double quote and ending double quote are needed for Json string representation of
                // base64url-encoded data.
                result = string.Format(CultureInfo.InvariantCulture, "\"{0}\"", JsonLightUtils.GetBase64UrlEncodedString(bytes));
                break;

            default:
                break;
            }

            return(result);
        }
        private byte[] ServiceReadBatchRequestAndWriterBatchResponse(byte[] requestPayload, BodyContentType bodyContentType)
        {
            IODataRequestMessage requestMessage = new InMemoryMessage()
            {
                Stream = new MemoryStream(requestPayload)
            };

            requestMessage.SetHeader(ODataConstants.ContentTypeHeader, batchContentTypeApplicationJson);

            using (ODataMessageReader messageReader = new ODataMessageReader(requestMessage, new ODataMessageReaderSettings(), null))
            {
                MemoryStream responseStream = new MemoryStream();

                IODataResponseMessage responseMessage = new InMemoryMessage {
                    Stream = responseStream
                };

                // Client is expected to receive the response message in the same format as that is used in the request sent.
                responseMessage.SetHeader(ODataConstants.ContentTypeHeader, batchContentTypeApplicationJson);
                ODataMessageWriter messageWriter = new ODataMessageWriter(responseMessage);
                ODataBatchWriter   batchWriter   = messageWriter.CreateODataBatchWriter();
                batchWriter.WriteStartBatch();

                ODataBatchReader batchReader = messageReader.CreateODataBatchReader();
                while (batchReader.Read())
                {
                    switch (batchReader.State)
                    {
                    case ODataBatchReaderState.Operation:

                        ODataBatchOperationRequestMessage operationMessage = batchReader.CreateOperationRequestMessage();

                        if (operationMessage.Method == "PUT")
                        {
                            using (Stream operationMessageBodyStream = operationMessage.GetStream())
                            {
                                // Verify the bytes in the request operation stream.
                                byte[] sampleBytes = bodyContentType == BodyContentType.Textual
                                        ? Encoding.UTF8.GetBytes("\"" + this.textualSampleStringA + "\"")
                                        : binarySampleBytesA;
                                Assert.Equal(operationMessageBodyStream.Length, sampleBytes.Length);
                                foreach (byte samplebyte in sampleBytes)
                                {
                                    Assert.Equal(samplebyte, operationMessageBodyStream.ReadByte());
                                }
                            }

                            // Create the response.
                            ODataBatchOperationResponseMessage operationResponse = batchWriter.CreateOperationResponseMessage(operationMessage.ContentId);
                            operationResponse.StatusCode = 201;
                            operationResponse.SetHeader("CoNtEnT-TyPe", "application/json;odata.metadata=none");
                        }
                        else if (operationMessage.Method == "GET")
                        {
                            ODataBatchOperationResponseMessage operationResponse = batchWriter.CreateOperationResponseMessage(operationMessage.ContentId);
                            operationResponse.StatusCode = 200;
                            operationResponse.SetHeader("CoNtEnT-TyPe", GetContentType(bodyContentType));
                            ODataMessageWriterSettings settings = new ODataMessageWriterSettings();
                            settings.SetServiceDocumentUri(new Uri(serviceDocumentUri));
                            using (ODataMessageWriter operationMessageWriter = new ODataMessageWriter(operationResponse, settings, null))
                            {
                                operationMessageWriter.WriteValue(GetEncodedContentObject(bodyContentType, false));
                            }
                        }

                        break;

                    case ODataBatchReaderState.ChangesetStart:
                        batchWriter.WriteStartChangeset();
                        break;

                    case ODataBatchReaderState.ChangesetEnd:
                        batchWriter.WriteEndChangeset();
                        break;
                    }
                }

                batchWriter.WriteEndBatch();
                responseStream.Position = 0;
                return(responseStream.ToArray());
            }
        }
Esempio n. 14
0
 public BodyContent(BodyContentType contentType, string content)
 {
     Id          = Guid.NewGuid();
     ContentType = contentType;
     Content     = content;
 }