Exemplo n.º 1
0
        private void LoadVariables()
        {
            foreach (var lineValues in FileUtil.GetFileLines(VARIABLES_FILE_NAME, VARIABLES_FILE_SEPARATOR))
            {
                if (lineValues.Length >= 2)
                {
                    var variableViewModel = new VariableViewModel()
                    {
                        Name  = lineValues[0],
                        Value = lineValues[1]
                    };

                    Variables.Add(variableViewModel);
                }
            }
        }
Exemplo n.º 2
0
        private void Validate()
        {
            try
            {
                var jsonObject = JObject.Parse(InputJson);

                if (jsonObject.HasValues)
                {
                    var envelopeViewModel = new EnvelopeViewModel();
                    envelopeViewModel.Json = InputJson;

                    if (envelopeViewModel.Envelope != null)
                    {
                        AddStatusMessage(string.Format("The input is a valid {0} JSON Envelope", envelopeViewModel.Envelope.GetType().Name));
                    }
                    else
                    {
                        AddStatusMessage("The input is a valid JSON document, but is not an Envelope", true);
                    }

                    var variables = InputJson.GetVariables();

                    foreach (var variable in variables)
                    {
                        if (!this.Variables.Any(v => v.Name.Equals(variable, StringComparison.OrdinalIgnoreCase)))
                        {
                            var variableViewModel = new VariableViewModel()
                            {
                                Name = variable
                            };

                            this.Variables.Add(variableViewModel);
                        }
                    }
                }
                else
                {
                    AddStatusMessage("The input is a invalid or empty JSON document", true);
                }
            }
            catch
            {
                AddStatusMessage("The input is a invalid JSON document", true);
            }
        }
Exemplo n.º 3
0
        public Task ProcessAsync(EnvelopeViewModel envelopeViewModel, SessionViewModel sessionViewModel)
        {
            if (envelopeViewModel == null)
            {
                throw new ArgumentNullException("envelopeViewModel");
            }

            if (sessionViewModel == null)
            {
                throw new ArgumentNullException("sessionViewModel");
            }

            var session = envelopeViewModel.Envelope as Session;

            if (session != null &&
                session.Id != Guid.Empty)
            {
                var sessionIdVariableViewModel = sessionViewModel
                    .Variables
                    .FirstOrDefault(v => v.Name.Equals("sessionId"));

                if (sessionIdVariableViewModel == null)
                {
                    sessionIdVariableViewModel = new VariableViewModel()
                    {
                        Name = "sessionId"
                    };

                    sessionViewModel.Variables.Add(sessionIdVariableViewModel);
                }

                sessionIdVariableViewModel.Value = session.Id.ToString();
            }

            return Task.FromResult<object>(null);
        }
Exemplo n.º 4
0
        private async Task OpenTransportAsync()
        {
            await ExecuteAsync(async() =>
            {
                AddStatusMessage("Connecting...");

                var timeoutCancellationToken = _operationTimeout.ToCancellationToken();

                X509Certificate2 clientCertificate = null;

                if (!string.IsNullOrWhiteSpace(ClientCertificateThumbprint))
                {
                    ClientCertificateThumbprint = ClientCertificateThumbprint
                                                  .Replace(" ", "")
                                                  .Replace("‎", "");

                    var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);

                    try
                    {
                        store.Open(OpenFlags.ReadOnly);

                        var certificates = store.Certificates.Find(X509FindType.FindByThumbprint, ClientCertificateThumbprint, false);
                        if (certificates.Count > 0)
                        {
                            clientCertificate = certificates[0];

                            var identity = clientCertificate.GetIdentity();

                            if (identity != null)
                            {
                                var fromVariableViewModel = this.Variables.FirstOrDefault(v => v.Name.Equals("from", StringComparison.OrdinalIgnoreCase));

                                if (fromVariableViewModel == null)
                                {
                                    fromVariableViewModel = new VariableViewModel()
                                    {
                                        Name = "from"
                                    };

                                    this.Variables.Add(fromVariableViewModel);
                                }

                                fromVariableViewModel.Value = identity.ToString();
                            }
                        }
                        else
                        {
                            AddStatusMessage("The specified certificate was not found", true);
                        }
                    }
                    finally
                    {
                        store.Close();
                    }
                }

                if (_hostUri.Scheme == WebSocketTransportListener.UriSchemeWebSocket ||
                    _hostUri.Scheme == WebSocketTransportListener.UriSchemeWebSocketSecure)
                {
                    Transport = new ClientWebSocketTransport(
                        new JsonNetSerializer(),
                        this);
                }
                else
                {
                    TcpClient = new TcpClientAdapter(new TcpClient());
                    Transport = new TcpTransport(
                        TcpClient,
                        new JsonNetSerializer(),
                        _hostUri.Host,
                        clientCertificate,
                        traceWriter: this);
                }

                await Transport.OpenAsync(_hostUri, timeoutCancellationToken);

                _connectionCts = new CancellationTokenSource();

                var dispatcher = Dispatcher.CurrentDispatcher;

                _receiveTask = ReceiveAsync(
                    Transport,
                    (e) => ReceiveEnvelopeAsync(e, dispatcher),
                    _connectionCts.Token)
                               .WithCancellation(_connectionCts.Token)
                               .ContinueWith(t =>
                {
                    IsConnected = false;

                    if (t.Exception != null)
                    {
                        AddStatusMessage(string.Format("Disconnected with errors: {0}", t.Exception.InnerException.Message.RemoveCrLf()), true);
                    }
                    else
                    {
                        AddStatusMessage("Disconnected");
                    }
                }, TaskScheduler.FromCurrentSynchronizationContext());

                IsConnected  = true;
                CanSendAsRaw = true;

                AddStatusMessage("Connected");
            });
        }
Exemplo n.º 5
0
        private void LoadVariables()
        {
            foreach (var lineValues in FileUtil.GetFileLines(VARIABLES_FILE_NAME, VARIABLES_FILE_SEPARATOR))
            {
                if (lineValues.Length >= 2)
                {
                    var variableViewModel = new VariableViewModel()
                    {
                        Name = lineValues[0],
                        Value = lineValues[1]
                    };

                    this.Variables.Add(variableViewModel);
                }
            }
        }
Exemplo n.º 6
0
        private void Validate()
        {
            try
            {
                var jsonObject = JsonObject.ParseJson(InputJson);

                if (jsonObject.Any())
                {
                    var envelopeViewModel = new EnvelopeViewModel();
                    envelopeViewModel.Json = InputJson;

                    if (envelopeViewModel.Envelope != null)
                    {
                        AddStatusMessage(string.Format("The input is a valid {0} JSON Envelope", envelopeViewModel.Envelope.GetType().Name));
                    }
                    else
                    {
                        AddStatusMessage("The input is a valid JSON document, but is not an Envelope", true);
                    }

                    var variables = InputJson.GetVariables();

                    foreach (var variable in variables)
                    {
                        if (!this.Variables.Any(v => v.Name.Equals(variable, StringComparison.OrdinalIgnoreCase)))
                        {
                            var variableViewModel = new VariableViewModel()
                            {
                                Name = variable
                            };

                            this.Variables.Add(variableViewModel);
                        }
                    }
                }
                else
                {
                    AddStatusMessage("The input is a invalid or empty JSON document", true);
                }
            }
            catch (ArgumentException)
            {
                AddStatusMessage("The input is a invalid JSON document", true);
            }
        }
Exemplo n.º 7
0
        private async Task OpenTransportAsync()
        {
            await ExecuteAsync(async () =>
                {
                    AddStatusMessage("Connecting...");

                    var timeoutCancellationToken = _operationTimeout.ToCancellationToken();

                    X509Certificate2 clientCertificate = null;

                    if (!string.IsNullOrWhiteSpace(ClientCertificateThumbprint))
                    {
                        ClientCertificateThumbprint = ClientCertificateThumbprint
                            .Replace(" ", "")
                            .Replace("‎", "");

                        var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);

                        try
                        {
                            store.Open(OpenFlags.ReadOnly);

                            var certificates = store.Certificates.Find(X509FindType.FindByThumbprint, ClientCertificateThumbprint, false);
                            if (certificates.Count > 0)
                            {
                                clientCertificate = certificates[0];

                                var identity = clientCertificate.GetIdentity();

                                if (identity != null)
                                {
                                    var fromVariableViewModel = this.Variables.FirstOrDefault(v => v.Name.Equals("from", StringComparison.OrdinalIgnoreCase));

                                    if (fromVariableViewModel == null)
                                    {
                                        fromVariableViewModel = new VariableViewModel()
                                        {
                                            Name = "from"
                                        };

                                        this.Variables.Add(fromVariableViewModel);
                                    }

                                    fromVariableViewModel.Value = identity.ToString();
                                }

                            }
                            else
                            {
                                AddStatusMessage("The specified certificate was not found", true);
                            }
                        }
                        finally
                        {
                            store.Close();
                        }                        
                    }

                    TcpClient = new TcpClientAdapter(new TcpClient());

                    Transport = new TcpTransport(
                        TcpClient,
                        new EnvelopeSerializer(),
                        _hostUri.Host,
                        clientCertificate: clientCertificate,
                        traceWriter: this);

                    await Transport.OpenAsync(_hostUri, timeoutCancellationToken);

                    _connectionCts = new CancellationTokenSource();

                    var dispatcher = Dispatcher.CurrentDispatcher;
                    
                    _receiveTask = ReceiveAsync(
                        Transport,
                        (e) => ReceiveEnvelopeAsync(e, dispatcher),
                        _connectionCts.Token)
                    .ContinueWith(t => 
                    {
                        IsConnected = false;

                        if (t.Exception != null)
                        {
                            AddStatusMessage(string.Format("Disconnected with errors: {0}", t.Exception.InnerException.Message.RemoveCrLf()), true);
                        }
                        else
                        {
                            AddStatusMessage("Disconnected");
                        }

                    }, TaskScheduler.FromCurrentSynchronizationContext());

                    IsConnected = true;
                    CanSendAsRaw = true;

                    AddStatusMessage("Connected");

                });
        }
        private async Task SendAsync(object parameter)
        {
            var times = 0;

            var repeatCountVariable = Variables.FirstOrDefault(v => v.Name == "repeatCount");

            if (repeatCountVariable == null)
            {
                repeatCountVariable = new VariableViewModel()
                {
                    Name = "repeatCount"
                };
                Variables.Add(repeatCountVariable);
            }

            do
            {
                repeatCountVariable.Value = (times + 1).ToString();

                await ExecuteAsync(async() =>
                {
                    AddStatusMessage("Sending...");

                    var inputJson = parameter.ToString();

                    if (ParseBeforeSend)
                    {
                        inputJson = ParseInput(inputJson, Variables);
                    }

                    var timeoutCancellationToken = _operationTimeout.ToCancellationToken();

                    var envelopeViewModel       = new EnvelopeViewModel(false);
                    envelopeViewModel.Json      = inputJson;
                    var envelope                = envelopeViewModel.Envelope;
                    envelopeViewModel.Direction = DataOperation.Send;

                    if (SendAsRaw)
                    {
                        envelopeViewModel.IsRaw = true;
                        var stream        = TcpClient.GetStream();
                        var envelopeBytes = Encoding.UTF8.GetBytes(envelopeViewModel.Json);
                        await stream.WriteAsync(envelopeBytes, 0, envelopeBytes.Length, timeoutCancellationToken);
                    }
                    else
                    {
                        await Transport.SendAsync(envelope, timeoutCancellationToken);
                        envelopeViewModel.IndentJson();
                    }

                    Envelopes.Add(envelopeViewModel);

                    if (ClearAfterSent)
                    {
                        InputJson = string.Empty;
                    }

                    AddStatusMessage(string.Format("{0} envelope sent", envelope.GetType().Name));
                });
            } while (Repeat && RepeatTimes > ++times);
        }