From 696a8ddbf64f92b6f3606bf637fd8387f5f25512 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Wed, 10 Apr 2019 16:15:00 +0200 Subject: [PATCH] general clean up, add file contents to getdocumentbyx methods --- JobRunner/JobRunner.vb | 50 ++++--- Jobs/App.config | 9 +- Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb | 1 - Jobs/Jobs.vbproj | 4 +- Jobs/packages.config | 1 + Message/Email.vb | 136 +++++++++++++++++- Message/Messaging.vbproj | 3 + Message/packages.config | 1 + Modules.Database/Firebird.vb | 15 +- SERVICES/DDEDM_NetworkService/EDMService.vb | 16 ++- SERVICES/DDEDM_NetworkService/IEDMService.vb | 2 + .../Results/BaseResult.vb | 2 +- 12 files changed, 196 insertions(+), 44 deletions(-) diff --git a/JobRunner/JobRunner.vb b/JobRunner/JobRunner.vb index 0b079680..0d94aefb 100644 --- a/JobRunner/JobRunner.vb +++ b/JobRunner/JobRunner.vb @@ -8,10 +8,10 @@ Public Class JobRunner Private WithEvents _workerThread As BackgroundWorker Private WithEvents _workerTimer As Timer - Private _interval As Long - Private _logConfig As LogConfig - Private _logger As Logger - Private _firebird As Firebird + Private ReadOnly _interval As Long + Private ReadOnly _logConfig As LogConfig + Private ReadOnly _logger As Logger + Private ReadOnly _firebird As Firebird Public Sub New(LogConfig As LogConfig, Firebird As Firebird, Interval As Long) _logConfig = LogConfig @@ -21,7 +21,7 @@ Public Class JobRunner _workerTimer = New Timer() _workerThread = New BackgroundWorker() With { - .WorkerReportsProgress = False, + .WorkerReportsProgress = True, .WorkerSupportsCancellation = True } End Sub @@ -34,6 +34,7 @@ Public Class JobRunner Public Sub [Stop]() Try + _logger.Debug("Stopping Background worker...") If _workerThread.IsBusy Then _workerThread.CancelAsync() _logger.Debug("Background Worker cancelled.") @@ -49,35 +50,32 @@ Public Class JobRunner If Not _workerThread.IsBusy Then _workerThread.RunWorkerAsync() Else - _logger.Warn("Background Worker is busy") + _logger.Warn("Background Worker is busy. Waiting for next interval.") End If End Sub Private Sub DoWork(sender As Object, e As DoWorkEventArgs) Handles _workerThread.DoWork - Try - Dim args As WorkerArgs = e.Argument - - _logger.Debug("Background worker running..") + _logger.Debug("Background worker running..") - ' TODO: WORK - - Dim oJob As New ADSyncJob(_logConfig, _firebird) - Dim oArgs As New ADSyncArgs() With { - .Enabled = My.Settings.JOB_ADSYNC_ENABLED, - .Interval = My.Settings.JOB_ADSYNC_INTERVAL, - .RootPath = My.Settings.JOB_ADSYNC_ROOT_PATH - } - If oJob.ShouldStart(oArgs) Then - oJob.Start(oArgs) - End If + Dim args As WorkerArgs = e.Argument + Dim oJob As New ADSyncJob(_logConfig, _firebird) + Dim oArgs As New ADSyncArgs() With { + .Enabled = My.Settings.JOB_ADSYNC_ENABLED, + .Interval = My.Settings.JOB_ADSYNC_INTERVAL, + .RootPath = My.Settings.JOB_ADSYNC_ROOT_PATH + } - Catch ex As Exception - _logger.Warn("Background worker failed!") - _logger.Error(ex) - End Try + If oJob.ShouldStart(oArgs) Then + oJob.Start(oArgs) + End If End Sub Private Sub WorkCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles _workerThread.RunWorkerCompleted - _logger.Debug("Background worker completed!") + If e.Error Is Nothing Then + _logger.Debug("Background worker completed!") + Else + _logger.Warn("Background worker failed!") + _logger.Error(e.Error) + End If End Sub End Class diff --git a/Jobs/App.config b/Jobs/App.config index 731f6de6..e2bb1a7e 100644 --- a/Jobs/App.config +++ b/Jobs/App.config @@ -1,6 +1,11 @@ - + - \ No newline at end of file + + + + + + \ No newline at end of file diff --git a/Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb b/Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb index 554a9523..8f8110e8 100644 --- a/Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb +++ b/Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb @@ -5,7 +5,6 @@ Imports System.Linq Imports System.Text.RegularExpressions Imports DigitalData.Modules.Database Imports DigitalData.Modules.Interfaces -Imports DigitalData.Modules.Jobs Imports DigitalData.Modules.Jobs.Exceptions Imports DigitalData.Modules.Logging Imports FirebirdSql.Data.FirebirdClient diff --git a/Jobs/Jobs.vbproj b/Jobs/Jobs.vbproj index 2ac0fe84..59acde28 100644 --- a/Jobs/Jobs.vbproj +++ b/Jobs/Jobs.vbproj @@ -101,7 +101,9 @@ - + + ..\packages\FirebirdSql.Data.FirebirdClient.6.4.0\lib\net452\FirebirdSql.Data.FirebirdClient.dll + ..\packages\NLog.4.5.11\lib\net45\NLog.dll diff --git a/Jobs/packages.config b/Jobs/packages.config index f89fa324..9204b8da 100644 --- a/Jobs/packages.config +++ b/Jobs/packages.config @@ -1,4 +1,5 @@  + \ No newline at end of file diff --git a/Message/Email.vb b/Message/Email.vb index 767a1d6a..e6135f3f 100644 --- a/Message/Email.vb +++ b/Message/Email.vb @@ -1,20 +1,148 @@ Imports Independentsoft.Email Imports Independentsoft.Email.Pop3 -Imports Independentsoft.Email.Smtp Imports Independentsoft.Email.Mime Imports Independentsoft.Email.Imap Imports DigitalData.Modules.Logging Imports System.Net.Mail Imports System.Net +Imports System.Reflection +Imports System.IO Public Class Email - Private _logger As Logging.Logger - Private _logConfig As LogConfig + Private ReadOnly _logger As Logging.Logger + Private ReadOnly _logConfig As LogConfig + Public Sub New(LogConfig As LogConfig) _logger = LogConfig.GetLogger() _logConfig = LogConfig - End Sub + + ''' + ''' Tests connection to a given IMAP Server by connecting and doing a simple message query. + ''' + ''' IP-Address or Domainname of Server + ''' IMAP-Port + ''' IMAP-Username + ''' IMAP-Password + ''' The folder to fetch messages from. Defaults to `Inbox` + ''' True if connection and query were successful. False otherwise. + Public Function TestIMAPLogin(Server As String, Port As Integer, Username As String, Password As String, Optional Folder As String = "Inbox") As Boolean + _logger.Debug("Testing Login to Server {0}:{1} with user {2}", Server, Port, Username) + + Try + _logger.Debug("Connecting...") + Using oClient As New S22.Imap.ImapClient(Server, Port, Username, Password, S22.Imap.AuthMethod.Login, True) + If Not oClient.Authed Then + _logger.Warn("Connected to server but authentication failed.") + Return False + End If + _logger.Debug("Connection successful") + + _logger.Debug("Fetching MessageIds..") + Dim oMessageIds As IEnumerable(Of UInteger) = oClient.Search(S22.Imap.SearchCondition.Unseen, Folder) + + _logger.Debug("Found {0} messages", oMessageIds.Count) + _logger.Debug("Fetching messages...") + + Dim oMessages As IEnumerable(Of MailMessage) = oClient.GetMessages(oMessageIds, False, Folder) + _logger.Debug("Messages fetched") + + oClient.Dispose() + + Return True + End Using + Catch ex As Exception + _logger.Error(ex) + Return False + End Try + End Function + + ''' + ''' Connects to an IMAP Server with the given credentials and + ''' fetches emails from the given folder. + ''' Results can be filtered with `SearchCondition` + ''' + ''' IP-Address or Domainname of Server + ''' IMAP-Port + ''' IMAP-Username + ''' IMAP-Password + ''' The folder to fetch messages from + ''' Filter the search command. Defaults to `All` + ''' A list of Independentsoft.Email.Mime.Message objects + Public Function FetchIMAPMessages(Server As String, Port As Integer, Username As String, Password As String, Folder As String, Optional SearchCondition As S22.Imap.SearchCondition = S22.Imap.SearchCondition.All) As List(Of Message) + Dim oMessages As New List(Of Message) + + _logger.Debug("Connecting to Server {0}:{1} with user {2}", Server, Port, Username) + + Try + _logger.Debug("Connecting...") + Using oClient As New S22.Imap.ImapClient(Server, Port, Username, Password, S22.Imap.AuthMethod.Login, True) + If Not oClient.Authed Then + _logger.Warn("Connected to server but authentication failed.") + Return Nothing + End If + _logger.Debug("Connection successful") + + _logger.Debug("Fetching MessageIds..") + Dim oMessageIds As IEnumerable(Of UInteger) = oClient.Search(S22.Imap.SearchCondition.Unseen, Folder) + + _logger.Debug("Found {0} messages", oMessageIds.Count) + _logger.Debug("Fetching messages...") + + ' Since this needs to return a list of IndependentSoft Message objects, + ' we 'convert' the .NET MailMessage objects that are fetched from the server + ' by writing them temporarily to disk as an eml file and then reading them back into a Message object. + ' This approach uses an unintended use of internal .NET APIs and may break in the future. + For Each oMessageId As UInteger In oMessageIds + Dim oMessage = oClient.GetMessage(oMessageId, False, Folder) + Dim oTempPath = Path.GetTempFileName() + Dim oResult = WriteMessageToFile(oMessage, oTempPath) + + Dim oMsg As New Message(oTempPath) + oMessages.Add(oMsg) + + Try + File.Delete(oTempPath) + Catch ex As Exception + _logger.Error(ex) + _logger.Warn("Temp file could not be deleted") + End Try + Next + + _logger.Debug("{0} Messages fetched", oMessages.Count) + End Using + + Return oMessages + Catch ex As Exception + _logger.Error(ex) + Return Nothing + End Try + End Function + + ''' + ''' Uses a private API from MailWriter to write a MailMessage to disk. + ''' May break in future versions of .NET + ''' + Public Function WriteMessageToFile(Message As MailMessage, Filename As String) As Boolean + Dim oAssembly As Assembly = GetType(Mail.SmtpClient).Assembly + Dim oMailWriterType As Type = oAssembly.[GetType]("System.Net.Mail.MailWriter") + + Try + Using oStream As New FileStream(Filename, FileMode.Create) + Dim oMailWriterConstructor As ConstructorInfo = oMailWriterType.GetConstructor( + BindingFlags.Instance Or BindingFlags.NonPublic, Nothing, New Type() {GetType(Stream)}, Nothing + ) + Dim oMailWriter As Object = oMailWriterConstructor.Invoke(New Object() {oStream}) + Dim sendMethod As MethodInfo = GetType(MailMessage).GetMethod("Send", BindingFlags.Instance Or BindingFlags.NonPublic) + sendMethod.Invoke(Message, BindingFlags.Instance Or BindingFlags.NonPublic, Nothing, {oMailWriter, True, True}, Nothing) + End Using + + Return True + Catch ex As Exception + Return Nothing + End Try + End Function + Public Function IMAP_COLLECT(INBOXNAME As String, MYMAIL_SERVER As String, MYMAIL_PORT As Integer, MYMAIL_USER As String, MYMAIL_USER_PW As String) Try Dim oMAIL_LIST As New ArrayList() diff --git a/Message/Messaging.vbproj b/Message/Messaging.vbproj index 8c9bb92e..a396cfbb 100644 --- a/Message/Messaging.vbproj +++ b/Message/Messaging.vbproj @@ -53,6 +53,9 @@ ..\packages\NLog.4.5.11\lib\net45\NLog.dll + + ..\packages\S22.Imap.3.6.0.0\lib\net40\S22.Imap.dll + diff --git a/Message/packages.config b/Message/packages.config index f89fa324..61e0c253 100644 --- a/Message/packages.config +++ b/Message/packages.config @@ -1,4 +1,5 @@  + \ No newline at end of file diff --git a/Modules.Database/Firebird.vb b/Modules.Database/Firebird.vb index 75ddb44d..1bc2b3b2 100644 --- a/Modules.Database/Firebird.vb +++ b/Modules.Database/Firebird.vb @@ -47,7 +47,7 @@ Imports DigitalData.Modules.Logging ''' Public Class Firebird Private _Logger As Logger - Private _MyLogger As LogConfig + Private _LogConfig As LogConfig Private _connectionServer As String Private _connectionDatabase As String Private _connectionUsername As String @@ -79,15 +79,16 @@ Public Class Firebird ''' ''' ''' - ''' - ''' - ''' - ''' + ''' The LogFactory containing the current log config. Used to instanciate the class logger for this and any dependent class + ''' The server where the database lives, for example 127.0.0.1 or dd-vmx09-vm03 + ''' The location of the Database in the format `127.0.0.1:E:\Path\To\Database.FDB` + ''' The user name to connect as + ''' The user's password ''' Public Sub New(LogConfig As LogConfig, Datasource As String, Database As String, User As String, Password As String) Try - _MyLogger = LogConfig - _Logger = _MyLogger.GetLogger() + _LogConfig = LogConfig + _Logger = _LogConfig.GetLogger() Dim oConnectionString = GetConnectionString(Datasource, Database, User, Password) _connectionServer = Datasource diff --git a/SERVICES/DDEDM_NetworkService/EDMService.vb b/SERVICES/DDEDM_NetworkService/EDMService.vb index 24350966..7863b874 100644 --- a/SERVICES/DDEDM_NetworkService/EDMService.vb +++ b/SERVICES/DDEDM_NetworkService/EDMService.vb @@ -254,7 +254,13 @@ Public Class EDMService oRow.Item("ORIGINAL_FILENAME") ) - Return New DocumentResult(oDocument) + TestFileExists(oDocument.ContainerId) + + Dim oContainerPath = GetContainerPath(oDocument.ContainerId) + Dim oContainer As FileContainer = FileContainer.Load(LogConfig, AppConfig.ContainerPassword, oContainerPath) + Dim oContents As Byte() = oContainer.GetFile().Contents + + Return New DocumentResult(oDocument, oContents) Catch ex As Exception Return New DocumentResult(ex.Message) End Try @@ -276,7 +282,13 @@ Public Class EDMService oRow.Item("ORIGINAL_FILENAME") ) - Return New DocumentResult(oDocument) + TestFileExists(oDocument.ContainerId) + + Dim oContainerPath = GetContainerPath(oDocument.ContainerId) + Dim oContainer As FileContainer = FileContainer.Load(LogConfig, AppConfig.ContainerPassword, oContainerPath) + Dim oContents As Byte() = oContainer.GetFile().Contents + + Return New DocumentResult(oDocument, oContents) Catch ex As Exception Return New DocumentResult(ex.Message) End Try diff --git a/SERVICES/DDEDM_NetworkService/IEDMService.vb b/SERVICES/DDEDM_NetworkService/IEDMService.vb index ac6bf76c..be7882e6 100644 --- a/SERVICES/DDEDM_NetworkService/IEDMService.vb +++ b/SERVICES/DDEDM_NetworkService/IEDMService.vb @@ -38,6 +38,8 @@ Interface IEDMService Function DeleteFile(DocObject As DocumentObject) As Boolean + + #End Region #Region "Utils" diff --git a/SERVICES/DDEDM_NetworkService/Results/BaseResult.vb b/SERVICES/DDEDM_NetworkService/Results/BaseResult.vb index 67ba3f4a..c2bff9b2 100644 --- a/SERVICES/DDEDM_NetworkService/Results/BaseResult.vb +++ b/SERVICES/DDEDM_NetworkService/Results/BaseResult.vb @@ -3,7 +3,7 @@ -Public Class BaseResult +Public MustInherit Class BaseResult Public Property OK As Boolean