private static void UnmarshallResult(XmlUnmarshallerContext context, SendMessageBatchResponse response) { int originalDepth = context.CurrentDepth; int targetDepth = originalDepth + 1; if (context.IsStartOfDocument) targetDepth += 2; while (context.ReadAtDepth(originalDepth)) { if (context.IsStartElement || context.IsAttribute) { if (context.TestExpression("BatchResultErrorEntry", targetDepth)) { var unmarshaller = BatchResultErrorEntryUnmarshaller.Instance; var item = unmarshaller.Unmarshall(context); response.Failed.Add(item); continue; } if (context.TestExpression("SendMessageBatchResultEntry", targetDepth)) { var unmarshaller = SendMessageBatchResultEntryUnmarshaller.Instance; var item = unmarshaller.Unmarshall(context); response.Successful.Add(item); continue; } } } return; }
/// <summary> /// Unmarshaller the response from the service to the response class. /// </summary> /// <param name="context"></param> /// <returns></returns> public override AmazonWebServiceResponse Unmarshall(XmlUnmarshallerContext context) { SendMessageBatchResponse response = new SendMessageBatchResponse(); context.Read(); int targetDepth = context.CurrentDepth; while (context.ReadAtDepth(targetDepth)) { if (context.IsStartElement) { if(context.TestExpression("SendMessageBatchResult", 2)) { UnmarshallResult(context, response); continue; } if (context.TestExpression("ResponseMetadata", 2)) { response.ResponseMetadata = ResponseMetadataUnmarshaller.Instance.Unmarshall(context); } } } return response; }
private static void UnmarshallResult(XmlUnmarshallerContext context,SendMessageBatchResponse response) { int originalDepth = context.CurrentDepth; int targetDepth = originalDepth + 1; if (context.IsStartOfDocument) targetDepth += 2; while (context.Read()) { if (context.IsStartElement || context.IsAttribute) { if (context.TestExpression("SendMessageBatchResultEntry", targetDepth)) { response.Successful.Add(SendMessageBatchResultEntryUnmarshaller.GetInstance().Unmarshall(context)); continue; } if (context.TestExpression("BatchResultErrorEntry", targetDepth)) { response.Failed.Add(BatchResultErrorEntryUnmarshaller.GetInstance().Unmarshall(context)); continue; } } else if (context.IsEndElement && context.CurrentDepth < originalDepth) { return; } } return; }
private static void GenerateErrorsAndThrow(SendMessageBatchResponse[] response) { var failed = response.SelectMany(r => r.Failed); var failedMessages = String.Join("\n", failed.Select(f => String.Format("Code:{0}, Id:{1}, Error:{2}", f.Code, f.Id, f.Message))); var errorMessage = "There were 1 or more errors when sending messages on commit." + failedMessages; var successMessages = response.SelectMany(r => r.Successful).ToList(); if (successMessages.Any()) errorMessage += "\n These message went through the loophole:\n" + String.Join("\n", successMessages.Select(s => " Id: " + s.Id + " MessageId:" + s.MessageId)); throw new ApplicationException(errorMessage); }
public static void ValidateSendMessageBatch(SendMessageBatchRequest request, SendMessageBatchResponse response) { if (response != null && response.SendMessageBatchResult != null && response.SendMessageBatchResult.SendMessageBatchResultEntry != null && response.SendMessageBatchResult.SendMessageBatchResultEntry.Count > 0) { Dictionary<string, string> requestMessages = request.Entries.ToDictionary(entry => entry.Id, entry => entry.MessageBody, StringComparer.Ordinal); List<SendMessageBatchResultEntry> resultEntries = response.SendMessageBatchResult.SendMessageBatchResultEntry; foreach (SendMessageBatchResultEntry entry in resultEntries) { string body = requestMessages[entry.Id]; string md5 = entry.MD5OfMessageBody; string id = entry.MessageId; AmazonSQSUtil.ValidateMD5(body, id, md5); } } }
private static void ValidateSendMessageBatch(SendMessageBatchRequest request, SendMessageBatchResponse response) { if (response != null && response.Successful != null && response.Successful.Count > 0) { Dictionary<string, SendMessageBatchRequestEntry> requestMessages = request.Entries.ToDictionary(entry => entry.Id, StringComparer.Ordinal); List<SendMessageBatchResultEntry> resultEntries = response.Successful; foreach (SendMessageBatchResultEntry entry in resultEntries) { var message = requestMessages[entry.Id]; string id = entry.MessageId; var body = message.MessageBody; var bodyMd5 = entry.MD5OfMessageBody; ValidateMD5(body, id, bodyMd5); var attributes = message.MessageAttributes; var attributesMd5 = entry.MD5OfMessageAttributes; if (attributes != null && attributes.Count > 0 && !string.IsNullOrEmpty(attributesMd5)) ValidateMD5(attributes, id, attributesMd5); } } }
public SendMessageBatchResponse SendMessageBatch(SendMessageBatchRequest request) { if (request.Entries == null || request.Entries.Count <= 0) { throw new EmptyBatchRequestException("No entires in request"); } if (request.Entries.Count > SqsQueueDefinition.MaxBatchSendItems) { throw new TooManyEntriesInBatchRequestException("Count of [{0}] exceeds limit of [{1}]".Fmt(request.Entries.Count, SqsQueueDefinition.MaxBatchSendItems)); } var q = GetQueue(request.QueueUrl); var response = new SendMessageBatchResponse { Failed = new List<BatchResultErrorEntry>(), Successful = new List<SendMessageBatchResultEntry>() }; var entryIds = new HashSet<string>(); foreach (var entry in request.Entries) { string id = null; BatchResultErrorEntry batchError = null; try { if (entryIds.Contains(entry.Id)) throw new BatchEntryIdsNotDistinctException("Duplicate Id of [{0}]".Fmt(entry.Id)); entryIds.Add(entry.Id); id = q.Send(new SendMessageRequest { MessageBody = entry.MessageBody, QueueUrl = q.QueueDefinition.QueueUrl }); } catch (ReceiptHandleIsInvalidException rhex) { batchError = new BatchResultErrorEntry { Id = entry.Id, Message = rhex.Message, Code = rhex.ErrorCode }; } if (id == null) { var entryToQueue = batchError ?? new BatchResultErrorEntry { Id = entry.Id, Message = "FakeSendError", Code = "789" }; response.Failed.Add(entryToQueue); } else { response.Successful.Add(new SendMessageBatchResultEntry { Id = entry.Id, MessageId = id }); } } return response; }