public static async Task <RopResult <T, DomainEvent[]> > GetAsync <T>(string url) { try { using (var client = new HttpClient()) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var response = await client.GetAsync(url); if (!response.IsSuccessStatusCode) { throw new Exception(response.ReasonPhrase); } var v = await response.Content.ReadAsStringAsync(); return(RopResult <T, DomainEvent[]> .ReturnSuccess( (JsonConvert.DeserializeObject <T>(v), new[] { new DomainEvent($"HTTP_GET_SUCCESS", "HttpAdaptor", EventLevel.Info, $"GET { url }") }))); }; } catch (Exception ex) { return(RopResult <T, DomainEvent[]> .ReturnFailure( new[] { new DomainEvent($"HTTP_GET_FAILURE", "HttpAdaptor", EventLevel.Info, $"GET FAILURE with message {ex.Message}") })); } }
IHttpActionResult ToHttpResult(RopResult <IHttpActionResult, DomainMessage> result) { if (result.IsSuccess) { return(result.SuccessValue); } else { var first = result.FailureValues[0]; return(first.Match( () => this.BadRequestResponse("customerIsRequired"), () => this.BadRequestResponse("customerIdMustBePositive"), () => this.BadRequestResponse("firstNameIsRequired"), () => this.BadRequestResponse("firstNameMustNotBeMoreThan10Chars"), () => this.BadRequestResponse("lastNameIsRequired"), () => this.BadRequestResponse("lastNameMustNotBeMoreThan10Chars"), () => this.BadRequestResponse("emailIsRequired"), () => this.BadRequestResponse("emailMustNotBeMoreThan20Chars"), () => this.BadRequestResponse("emailMustContainAtSign"), (oldEmail, newEmail) => this.Ok("email changed"), () => this.NotFoundResponse("customerNotFound"), () => this.InternalServerErrorResponse("sqlCustomerIsInvalid"), () => this.InternalServerErrorResponse("databaseTimeout"), (error) => this.InternalServerErrorResponse("databaseError: " + error) )); } }
/// <summary> /// Reset the adapter. /// </summary> public override void Reset() { try { if (this.needDoCleanup) { this.RevertPublicFolder(LogonFlags.PublicFolder); foreach (KeyValuePair<int, OxcropsClient> oxcrop in this.oxcropsClient) { if (oxcrop.Key == 1 || (oxcrop.Key == 2 && this.isSecondSUTServerConnected)) { this.HardDeleteMessageAndSubfolders(oxcrop.Key); } } } foreach (KeyValuePair<int, OxcropsClient> oxcrop in this.oxcropsClient) { bool result = this.oxcropsClient[oxcrop.Key].Disconnect(); string comment = "Disconnecting server" + oxcrop.Key + " should be successful."; Site.Assert.IsTrue(result, comment); } } catch (SEHException e) { Site.Assume.Fail(e.Message); } finally { this.isImportMessageMoveROP = false; this.importMessageTimes = 0; this.isNonImportMessageChangeOperation = false; this.lastChangeMadeByServer = false; this.lastChangeMadeByClient = false; this.importFlag = ImportFlag.InvalidParameter; this.importPidTagChangeKeyValue = new byte[22]; this.previousGetBufferResult = RopResult.Success; this.needDoCleanup = true; this.isOrderByDeliveryTimeExtraFlagNotSet = false; AdapterHelper.ClearIndex(); this.isSecondSUTServerConnected = false; this.localIdOffSet = 0; this.localId = new byte[6]; this.serverReplicaGuid = new Guid(); this.localReplicaGuid = Guid.NewGuid(); this.handleContainer.Clear(); this.objectIdContainer.Clear(); this.streamBufferContainer.Clear(); this.icsStateContainer.Clear(); this.existNoPermissionFolder = false; this.cnsetRead.Clear(); this.cnsetSeen.Clear(); this.cnsetSeenFAI.Clear(); base.Reset(); } }
/// <summary> /// Downloads the next portion of a FastTransfer stream. /// </summary> /// <param name="serverId">A 32-bit signed integer represent the Identity of server.</param> /// <param name="downloadHandleIndex">A fast transfer stream object handle index. </param> /// <param name="bufferSize">Specifies the maximum amount of data to be output in the TransferBuffer.</param> /// <param name="transferBufferIndex">The index of data get from the fast transfer stream.</param> /// <param name="abstractFastTransferStream">Fast transfer stream.</param> /// <param name="transferDataSmallOrEqualToBufferSize">Variable to not if the transferData is small or equal to bufferSize</param> /// <returns>Indicate the result of this ROP operation.</returns> public RopResult FastTransferSourceGetBuffer(int serverId, int downloadHandleIndex, BufferSize bufferSize, out int transferBufferIndex, out AbstractFastTransferStream abstractFastTransferStream, out bool transferDataSmallOrEqualToBufferSize) { // Initialize ROP data. SyntacticalBase.AllPropList = null; RopResult returnValue = RopResult.InvalidParameter; this.totalTransferBufferList.Clear(); transferBufferIndex = -1; abstractFastTransferStream = new AbstractFastTransferStream(); transferDataSmallOrEqualToBufferSize = false; if (downloadHandleIndex < 0) { return returnValue; } if (bufferSize == BufferSize.Greater && !Common.IsRequirementEnabled(2625, this.Site)) { returnValue = RopResult.BufferTooSmall; return returnValue; } RopFastTransferSourceGetBufferResponse response = new RopFastTransferSourceGetBufferResponse(); uint sourceHandle = this.handleContainer[downloadHandleIndex]; uint downloadContextHandle = sourceHandle; // Construct ROP request. RopFastTransferSourceGetBufferRequest fastTransferSourceGetBufferRequest; fastTransferSourceGetBufferRequest.RopId = 0x4e; fastTransferSourceGetBufferRequest.LogonId = 0x00; fastTransferSourceGetBufferRequest.InputHandleIndex = 0x00; fastTransferSourceGetBufferRequest.BufferSize = (ushort)bufferSize; if (bufferSize != BufferSize.Normal) { fastTransferSourceGetBufferRequest.MaximumBufferSize = null; } else { fastTransferSourceGetBufferRequest.MaximumBufferSize = (ushort)bufferSize; } bool isRunIntoPartial = false; bool isRunIntoNoRoom = false; do { IDeserializable tempRopResponse = null; if ((tempRopResponse = this.Process(serverId, fastTransferSourceGetBufferRequest, downloadContextHandle)) != null) { // Send request and get response. response = (RopFastTransferSourceGetBufferResponse)tempRopResponse; byte[] transferBuffer = new byte[(int)response.TransferBufferSize]; if (response.ReturnValue == 0) { if (bufferSize != BufferSize.Normal) { transferDataSmallOrEqualToBufferSize = transferBuffer.Length <= (int)bufferSize; } for (int i = 0; i < (int)response.TransferBufferSize; i++) { transferBuffer[i] = response.TransferBuffer[i]; } this.totalTransferBufferList.Add(transferBuffer); if (response.TransferStatus == (ushort)TransferStatus.Partial) { isRunIntoPartial = true; } if (response.TransferStatus == (ushort)TransferStatus.NoRoom) { isRunIntoNoRoom = true; } } } else { break; } } while (response.TransferStatus != (ushort)TransferStatus.Done && (RopResult)response.ReturnValue == RopResult.Success); if (response.TransferBuffer == null) { returnValue = (RopResult)this.ropResult; } else { returnValue = (RopResult)response.ReturnValue; } bool isReachedBufferTooSmall = (bufferSize == BufferSize.Greater) && (returnValue == RopResult.BufferTooSmall); this.VerifyTransferStatus(isRunIntoPartial, isRunIntoNoRoom, isReachedBufferTooSmall); if (isReachedBufferTooSmall) { this.previousGetBufferResult = RopResult.BufferTooSmall; } if (response.TransferStatus == (ushort)TransferStatus.Done) { returnValue = RopResult.Success; int bufferlength = 0; foreach (byte[] blengt in this.totalTransferBufferList) { bufferlength += blengt.Length; } byte[] totalTransferBuffer = new byte[bufferlength]; int index = 0; foreach (byte[] buffer in this.totalTransferBufferList) { Array.Copy(buffer, 0, totalTransferBuffer, index, buffer.Length); index += buffer.Length; } byte[] requiredTransferBuffer = this.ProcessFXSourceGetBuffer(totalTransferBuffer); using (FastTransferStream fs = new FastTransferStream(requiredTransferBuffer, true)) { // Verify FastTransfer Stream this.VerifyFastTransferStream(fs, this.streamType); } abstractFastTransferStream = this.GenerateAbstractFastTransferStream(serverId, requiredTransferBuffer); if (bufferSize == BufferSize.Greater) { transferBufferIndex = -1; } else { transferBufferIndex = AdapterHelper.GetStreamBufferIndex(); } int k = this.streamBufferContainer.Count; foreach (byte[] subBuffer in this.totalTransferBufferList) { this.streamBufferContainer.Add(++k, subBuffer); } } if (response.RopId != 0x00) { // Verify ROP FastTransferSourceGetBuffer this.VerifyRopFastTransferSourceGetBuffer(response); } return returnValue; }