static AmqpMessage GetAmqpResponse(AmqpMessage requestMessage, AmqpResponseStatusCode statusCode, string statusDescription) { AmqpMessage response = AmqpMessage.Create(); response.Properties.CorrelationId = requestMessage.Properties.MessageId; response.ApplicationProperties = new ApplicationProperties(); response.ApplicationProperties.Map[CbsConstants.PutToken.StatusCode] = (int)statusCode; response.ApplicationProperties.Map[CbsConstants.PutToken.StatusDescription] = statusDescription; return(response); }
public static AmqpResponseStatusCode GetResponseStatusCode(this AmqpMessage responseMessage) { AmqpResponseStatusCode responseStatusCode = AmqpResponseStatusCode.Unused; object statusCodeValue = responseMessage?.ApplicationProperties.Map[ManagementConstants.Response.StatusCode]; if (statusCodeValue is int && Enum.IsDefined(typeof(AmqpResponseStatusCode), statusCodeValue)) { responseStatusCode = (AmqpResponseStatusCode)statusCodeValue; } return(responseStatusCode); }
public void ThrowIfErrorResponseThrowsOnFailure(AmqpResponseStatusCode statusCode) { var description = "This is a test description."; var resourceName = "TestHub"; using var response = AmqpMessage.Create(); response.ApplicationProperties = new ApplicationProperties(); response.ApplicationProperties.Map[AmqpResponse.StatusCode] = (int)statusCode; response.ApplicationProperties.Map[AmqpResponse.StatusDescription] = description; Assert.That(() => AmqpError.ThrowIfErrorResponse(response, resourceName), Throws.Exception); }
Task SendResponseAsync(AmqpMessage requestMessage, AmqpResponseStatusCode statusCode, string statusDescription) { try { AmqpMessage response = GetAmqpResponse(requestMessage, statusCode, statusDescription); return(this.sendingLink?.SendMessageAsync(response, this.GetDeliveryTag(), AmqpConstants.NullBinary, AmqpConstants.DefaultTimeout) ?? Task.CompletedTask); } catch (Exception e) { Events.ErrorSendingResponse(e); return(Task.CompletedTask); } }
public static AmqpSymbol GetResponseErrorCondition(AmqpMessage response, AmqpResponseStatusCode statusCode) { object condition = response.ApplicationProperties.Map[ManagementConstants.Response.ErrorCondition]; if (condition != null) { return((AmqpSymbol)condition); } // Most of the time we should have an error condition foreach (var kvp in ConditionToStatusMap) { if (kvp.Value == statusCode) { return(kvp.Key); } } return(AmqpErrorCode.InternalError); }
public void CreateExceptionForResponseWitStatus(AmqpResponseStatusCode statusCode, Type exceptionType) { var description = "This is a test description."; var resourceName = "TestHub"; using var response = AmqpMessage.Create(); response.ApplicationProperties = new ApplicationProperties(); response.ApplicationProperties.Map[AmqpResponse.StatusCode] = (int)statusCode; response.ApplicationProperties.Map[AmqpResponse.StatusDescription] = description; var exception = AmqpError.CreateExceptionForResponse(response, resourceName); Assert.That(exception, Is.Not.Null, "An exception should have been created"); Assert.That(exception, Is.TypeOf(exceptionType), "The exception should be the proper type"); Assert.That(exception.Message, Is.SupersetOf(description), "The exception message should contain the description"); if (exception is EventHubsException) { Assert.That(((EventHubsException)exception).ResourceName, Is.EqualTo(resourceName), "The exception should report the proper resource"); } }
static Exception ConvertToException(int statusCode, string statusDescription) { Exception exception; if (Enum.IsDefined(typeof(AmqpResponseStatusCode), statusCode)) { AmqpResponseStatusCode amqpResponseStatusCode = (AmqpResponseStatusCode)statusCode; switch (amqpResponseStatusCode) { case AmqpResponseStatusCode.BadRequest: exception = new AmqpException(AmqpErrorCode.InvalidField, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); break; case AmqpResponseStatusCode.NotFound: exception = new AmqpException(AmqpErrorCode.NotFound, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); break; case AmqpResponseStatusCode.Forbidden: exception = new AmqpException(AmqpErrorCode.TransferLimitExceeded, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); break; case AmqpResponseStatusCode.Unauthorized: exception = new AmqpException(AmqpErrorCode.UnauthorizedAccess, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); break; default: exception = new AmqpException(AmqpErrorCode.InvalidField, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); break; } } else { exception = new AmqpException(AmqpErrorCode.InvalidField, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); } return(exception); }
public static Exception ToMessagingContractException(this AmqpMessage responseMessage, AmqpResponseStatusCode statusCode) { AmqpSymbol errorCondition = AmqpExceptionHelper.GetResponseErrorCondition(responseMessage, statusCode); var statusDescription = responseMessage.ApplicationProperties.Map[ManagementConstants.Response.StatusDescription] as string ?? errorCondition.Value; Exception exception = AmqpExceptionHelper.ToMessagingContractException(errorCondition.Value, statusDescription); return(exception); }
/// <summary> /// Determines whether the given AMQP status code value should be considered a successful /// request. /// </summary> /// /// <param name="statusCode">The status code value to consider.</param> /// /// <returns><c>true</c> if the request should be considered successful; otherwise, <c>false</c>.</returns> /// public static bool IsSuccessStatus(AmqpResponseStatusCode statusCode) => ((statusCode == AmqpResponseStatusCode.OK) || (statusCode == AmqpResponseStatusCode.Accepted));
/// <summary> /// Sends a security token for a resource for later access. /// </summary> /// <param name="tokenProvider">The provider for issuing security tokens.</param> /// <param name="namespaceAddress">The namespace (or tenant) name.</param> /// <param name="audience">The audience. In most cases it is the same as resource.</param> /// <param name="resource">The resource to access.</param> /// <param name="requiredClaims">The required claims to access the resource.</param> /// <param name="timeout">The operation timeout.</param> /// <returns></returns> public async Task <DateTime> SendTokenAsync(ICbsTokenProvider tokenProvider, Uri namespaceAddress, string audience, string resource, string[] requiredClaims, TimeSpan timeout) { if (this.connection.IsClosing()) { throw new OperationCanceledException("Connection is closing or closed."); } CbsToken token = await tokenProvider.GetTokenAsync(namespaceAddress, resource, requiredClaims).ConfigureAwait(false); string tokenType = token.TokenType; if (tokenType == null) { throw new NotSupportedException(AmqpResources.AmqpUnsupportedTokenType); } RequestResponseAmqpLink requestResponseLink; if (!this.linkFactory.TryGetOpenedObject(out requestResponseLink)) { requestResponseLink = await this.linkFactory.GetOrCreateAsync(timeout).ConfigureAwait(false); } AmqpValue value = new AmqpValue(); value.Value = token.TokenValue; AmqpMessage putTokenRequest = AmqpMessage.Create(value); putTokenRequest.ApplicationProperties.Map[CbsConstants.Operation] = CbsConstants.PutToken.OperationValue; putTokenRequest.ApplicationProperties.Map[CbsConstants.PutToken.Type] = tokenType; putTokenRequest.ApplicationProperties.Map[CbsConstants.PutToken.Audience] = audience; putTokenRequest.ApplicationProperties.Map[CbsConstants.PutToken.Expiration] = token.ExpiresAtUtc; AmqpMessage putTokenResponse = await requestResponseLink.RequestAsync(putTokenRequest, timeout).ConfigureAwait(false); int statusCode = (int)putTokenResponse.ApplicationProperties.Map[CbsConstants.PutToken.StatusCode]; string statusDescription = (string)putTokenResponse.ApplicationProperties.Map[CbsConstants.PutToken.StatusDescription]; if (statusCode == (int)AmqpResponseStatusCode.Accepted || statusCode == (int)AmqpResponseStatusCode.OK) { return(token.ExpiresAtUtc); } Exception exception; AmqpResponseStatusCode amqpResponseStatusCode = (AmqpResponseStatusCode)statusCode; switch (amqpResponseStatusCode) { case AmqpResponseStatusCode.BadRequest: exception = new AmqpException(AmqpErrorCode.InvalidField, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); break; case AmqpResponseStatusCode.NotFound: exception = new AmqpException(AmqpErrorCode.NotFound, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); break; case AmqpResponseStatusCode.Forbidden: exception = new AmqpException(AmqpErrorCode.TransferLimitExceeded, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); break; case AmqpResponseStatusCode.Unauthorized: exception = new AmqpException(AmqpErrorCode.UnauthorizedAccess, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); break; default: exception = new AmqpException(AmqpErrorCode.InvalidField, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); break; } throw exception; }