/// <summary> /// This method loads the XML report generated by the SIF into this workbook model. /// </summary> public void Load(string xml) { try { var rootElement = XElement.Parse(xml); // @TODO //XDocument d = new XDocument(rootElement); //d.Validate(XMLPartManager.Instance.getReportSchema(), null); LoadViolations(rootElement); ScanHelper.ScanSuccessful(); } catch (Exception e) { ScanHelper.ScanUnsuccessful(Resources.Error_FaultyResponse); Debug.WriteLine(e); } }
private void scanButton_Click(object sender, RibbonControlEventArgs e) { if (!AllowedToScan()) { return; } if (DataModel.Instance.CurrentWorkbook.PolicySettings.hasManualScans() || DataModel.Instance.CurrentWorkbook.Scenarios.Count > 0 || DataModel.Instance.CurrentWorkbook.Rules.Count > 0) { DataModel.Instance.CurrentWorkbook.Inspect(); } else { ScanHelper.ScanUnsuccessful(Resources.tl_Ribbon_MessageNoPolicies); } }
/// <summary> /// Handler when the Scan Button got clicked /// For example checking if scan is allowed and then statrting it /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void scanButton_Click(object sender, RibbonControlEventArgs e) { if (!AllowedToScan()) { return; } if (DataModel.Instance.CurrentWorkbook.PolicySettings.hasManualScans() || DataModel.Instance.CurrentWorkbook.Scenarios.Count > 0) { // SIFCore can't handle if it the documents is not saved. So if an inspection is started it is assured the file is saved somewhere. ScanHelper.SaveBefore(InspectionType.MANUAL); } else { ScanHelper.ScanUnsuccessful(Resources.tl_Ribbon_MessageNoPolicies); } }
/// <summary> /// This async method is called by the workbook model. it will be a silent running request /// </summary> internal async void doInspection(WorkbookModel workbook, string policyFile, string spreadsheetFile) { // initalize response string string responseString = null; // open policy and spreadsheet files save temporarily var policyStream = File.Open(policyFile, FileMode.Open); HttpContent policyContent = new StreamContent(policyStream); var spreadsheetStream = File.Open(spreadsheetFile, FileMode.Open); HttpContent spreadsheetContent = new StreamContent(spreadsheetStream); // Submit the form using HttpClient and // create form data as Multipart (enctype="multipart/form-data") using (var client = new HttpClient()) using (var formData = new MultipartFormDataContent()) { // Add the HttpContent objects to the form data // <input type="text" name="filename" /> formData.Add(policyContent, "policy", policyFile); formData.Add(spreadsheetContent, "spreadsheet", spreadsheetFile); // Actually invoke the request to the server // equivalent to (action="{url}" method="post") try { var response = client.PostAsync(Settings.Default.SifServerUrl + "/ooxml", formData).Result; if (response.IsSuccessStatusCode) { // get the responding xml as string responseString = await response.Content.ReadAsStringAsync(); var fileName = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + Path.DirectorySeparatorChar + "inspectionResponse.xml"; File.WriteAllText(fileName, responseString); } } catch (Exception) { ScanHelper.ScanUnsuccessful(Resources.Error_NoConnectionToServer); return; } } workbook.Load(responseString); }
/// <summary> /// Reads a String from the socket /// </summary> /// <param name="socket">Socket to recieve data from</param> /// <returns string> The string read from the socket </returns> public static string ReadString(this Socket socket) { try { byte[] buffer = new byte[8]; socket.Receive(buffer, 0, 8, SocketFlags.None); int stringLength = (int)BitConverter.ToInt64(buffer, 0); buffer = new byte[stringLength]; socket.Receive(buffer, 0, stringLength, SocketFlags.None); return(Encoding.UTF8.GetString(buffer).Trim()); } catch (OutOfMemoryException ex) { ScanHelper.ScanUnsuccessful(Resources.tl_OutOfMemory); return(string.Empty); } }
/// <summary> /// This method is the server run loop. /// </summary> protected void ServerRunLoop() { TcpListener currentTcpListener = TcpServer; try { // The server will keep running until its thread is aborted. while (true) { if (!File.Exists(Settings.Default.FrameworkPath + Path.DirectorySeparatorChar + "sif.jar")) { // Sif has not been installed correctly. MessageBox.Show(Resources.tl_Path_missing + Settings.Default.FrameworkPath + Path.DirectorySeparatorChar + "sif.jar\n\n" + Resources.tl_Path_install, Resources.tl_MessageBox_Error); } // Launch a new instance of the Spreadsheet Inspection Framework var startInfo = new ProcessStartInfo("cmd", "/q /c java -jar \"" + Settings.Default.FrameworkPath + Path.DirectorySeparatorChar + "sif.jar\" " + Settings.Default.SifOptions + " " + Instance.Port); startInfo.WindowStyle = ProcessWindowStyle.Hidden; Process.Start(startInfo); // Wait for the client to connect. var clientSocket = currentTcpListener.AcceptSocket(); #region Functionality while (IsSocketConnected(clientSocket)) { // Get the next inspection job from the inspection queue var currentJob = InspectionQueue.Take(); try { // At first, send the policy as generated Xml var writer = new StringWriter(); currentJob.PolicyXML.Save(writer); clientSocket.SendString(writer.GetStringBuilder().ToString()); // Read the report from the socket connection var report = clientSocket.ReadString(); // Let the inspection job know about the report currentJob.Finalize(report); } catch (OutOfMemoryException) { // Try to release as many resources as possible# ScanHelper.ScanUnsuccessful(Resources.tl_MemoryRestrictions + "\n" + Resources.tl_StartNewScan); _hadMemoryRestriction = true; currentJob.DeleteWorkbookFile(); foreach (InspectionJob job in InspectionQueue) { job.DeleteWorkbookFile(); } InspectionQueue.Dispose(); InspectionQueue = new BlockingCollection <InspectionJob>(); // restart the server loop throw new Exception(); } catch (Exception e) { if (!_hadMemoryRestriction) { ScanHelper.ScanUnsuccessful(); } Start(); } } #endregion } } catch (ExternalException) // Java is not on the path { if (Globals.Ribbons.Ribbon.scanButton != null && Globals.Ribbons.Ribbon != null && Globals.Ribbons != null) { ScanHelper.ScanUnsuccessful(Resources.tl_No_Java_Enviroment); } else { MessageBox.Show( Resources.tl_No_Java_Enviroment, Resources.tl_MessageBox_Error); } } catch (Exception e) { try { ScanHelper.ScanUnsuccessful(); } //Catch if Ribbon was never instantiated catch (NullReferenceException ex) { // Quietly swallow exception } // start will fork into a new thread Start(); // we will die in a short while, nothing to be done } finally { currentTcpListener.Stop(); } }