public override void ProcessRequest(ICommunicationContext ctx) { // Read post data which is expected to be JSON string args; using(var reader = new StreamReader(ctx.Request.InputStream, ctx.Request.ContentEncoding)) { args = reader.ReadToEnd(); } var className = GetClassName(ctx); var methodName = GetMethodName(ctx); var dataListID = GetDataListID(ctx); var workspaceID = GetWorkspaceID(ctx); dynamic result; try { Guid workspaceGuid; Guid.TryParse(workspaceID, out workspaceGuid); Guid dataListGuid; Guid.TryParse(dataListID, out dataListGuid); // // NOTE: result.ToString() MUST return JSON // // Ensure we execute as the correct user ;) var userPrinciple = ctx.Request.User; if(userPrinciple != null) { Thread.CurrentPrincipal = userPrinciple; Dev2Logger.Log.Info("WEB EXECUTION USER CONTEXT [ " + userPrinciple.Identity.Name + " ]"); } result = _serviceInvoker.Invoke(className, methodName, args, workspaceGuid, dataListGuid); } catch(Exception ex) { result = new ValidationResult { ErrorMessage = ex.Message }; } ctx.Send(new StringResponseWriter(result.ToString(), ContentTypes.Json)); }
public override void ProcessRequest(ICommunicationContext ctx) { // Read post data which is expected to be JSON string args; using(var reader = new StreamReader(ctx.Request.InputStream, ctx.Request.ContentEncoding)) { args = reader.ReadToEnd(); } var className = GetClassName(ctx); var methodName = GetMethodName(ctx); var dataListID = GetDataListID(ctx); var workspaceID = GetWorkspaceID(ctx); dynamic result = null; var userPrinciple = ctx.Request.User; Thread.CurrentPrincipal = userPrinciple; try { Guid workspaceGuid; Guid.TryParse(workspaceID, out workspaceGuid); Guid dataListGuid; Guid.TryParse(dataListID, out dataListGuid); Thread.CurrentPrincipal = userPrinciple; Dev2Logger.Log.Info("WEB EXECUTION USER CONTEXT [ " + userPrinciple.Identity.Name + " ]"); Common.Utilities.PerformActionInsideImpersonatedContext(userPrinciple, () => { result = _serviceInvoker.Invoke(className, methodName, args, workspaceGuid, dataListGuid); }); } catch(Exception ex) { result = new ValidationResult { ErrorMessage = ex.Message }; } if(result != null) { ctx.Send(new StringResponseWriter(result.ToString(), ContentTypes.Json)); } }
void Verify_TestEmailAccount(bool isTestResultValid, bool hasFromAccount) { //------------Setup for test-------------------------- const string ExpectedUri = AppLocalhost + "/wwwroot/sources/Service/EmailSources/Test"; const string TestToAddress = "*****@*****.**"; const string TestFromAccount = "*****@*****.**"; const string TestFromPassword = "******"; var result = new ValidationResult { IsValid = isTestResultValid }; if (!isTestResultValid) { result.ErrorMessage = "Unable to connect to SMTP server"; } var emailSource = new EmailSource { ResourceID = Guid.NewGuid(), ResourceName = "EmailTest", UserName = "******", Password = "******", }; var modelItem = CreateModelItem(); modelItem.SetProperty("SelectedEmailSource", emailSource); modelItem.SetProperty("To", TestToAddress); var expectedSource = new EmailSource(emailSource.ToXml()) { TestToAddress = TestToAddress }; if (hasFromAccount) { modelItem.SetProperty("FromAccount", TestFromAccount); modelItem.SetProperty("Password", TestFromPassword); expectedSource.UserName = TestFromAccount; expectedSource.Password = TestFromPassword; expectedSource.TestFromAddress = TestFromAccount; } else { expectedSource.TestFromAddress = emailSource.UserName; } var expectedPostData = expectedSource.ToString(); string postData = null; var webRequestInvoker = new Mock<IWebRequestInvoker>(); webRequestInvoker.Setup(w => w.ExecuteRequest("POST", ExpectedUri, It.IsAny<string>(), null, It.IsAny<Action<string>>())) .Callback((string method, string url, string data, List<Tuple<string, string>> headers, Action<string> asyncCallback) => { postData = data; asyncCallback(new Dev2JsonSerializer().Serialize(result)); }).Returns(string.Empty) .Verifiable(); var viewModel = CreateViewModel(new List<EmailSource> { emailSource }, modelItem); viewModel.WebRequestInvoker = webRequestInvoker.Object; Assert.IsTrue(viewModel.CanTestEmailAccount); //------------Execute Test--------------------------- viewModel.TestEmailAccountCommand.Execute(null); //------------Assert Results------------------------- webRequestInvoker.Verify(w => w.ExecuteRequest("POST", ExpectedUri, It.IsAny<string>(), null, It.IsAny<Action<string>>())); Assert.IsNotNull(postData); Assert.AreEqual(expectedPostData, postData); Assert.IsTrue(viewModel.CanTestEmailAccount); if (isTestResultValid) { Assert.IsNull(viewModel.Errors); } else { Assert.IsNotNull(viewModel.Errors); Assert.AreEqual(result.ErrorMessage, viewModel.Errors[0].Message); Assert.IsFalse(viewModel.IsFromAccountFocused); viewModel.Errors[0].Do(); Assert.IsTrue(viewModel.IsFromAccountFocused); } }
ValidationResult CanConnectToServer(Dev2.Data.ServiceModel.Connection connection) { var result = new ValidationResult { ErrorFields = new ArrayList(new[] { "address" }), }; try { // Validate URI, ports, etc... // ReSharper disable ObjectCreationAsStatement new Uri(connection.Address); // ReSharper restore ObjectCreationAsStatement var connectResult = ConnectToServer(connection); if(!string.IsNullOrEmpty(connectResult)) { if(connectResult.Contains("FatalError")) { var error = XElement.Parse(connectResult); result.IsValid = false; result.ErrorMessage = string.Join(" - ", error.Nodes().Cast<XElement>().Select(n => n.Value)); } } } catch(Exception ex) { var hex = ex.InnerException as HttpClientException; if(hex != null) { result.IsValid = false; // This we know how to handle this result.ErrorMessage = "Connection Error : " + hex.Response.ReasonPhrase; return result; } result.IsValid = false; // get something more relevant ;) if(ex.Message == "One or more errors occurred." && ex.InnerException != null) { result.ErrorMessage = "Connection Error : " + ex.InnerException.Message; } else { var msg = ex.Message; if(msg.IndexOf("Connection Error : ", StringComparison.Ordinal) >= 0 || msg.IndexOf("Invalid URI:", StringComparison.Ordinal) >= 0) { result.ErrorMessage = ex.Message; } else { result.ErrorMessage = "Connection Error : " + ex.Message; } } } return result; }
// POST: Service/Connections/Test public ValidationResult Test(string args, Guid workspaceID, Guid dataListID) { var result = new ValidationResult { IsValid = false, ErrorMessage = "Unknown connection type." }; try { var connection = JsonConvert.DeserializeObject<Dev2.Data.ServiceModel.Connection>(args); switch(connection.ResourceType) { case ResourceType.Server: result = CanConnectToServer(connection); break; } } catch(Exception ex) { RaiseError(ex); result.ErrorMessage = ex.Message; } return result; }