Beispiel #1
0
        public void Execute(EncryptFileContext context, CancellationToken token)
        {
            var cspDunsPorts = clientDataAccess.ListCspDunsPort();

            foreach (var port in cspDunsPorts)
            {
                if (token.IsCancellationRequested)
                {
                    token.ThrowIfCancellationRequested();
                }

                if (!port.EncryptionEnabledFlag)
                {
                    continue;
                }

                logger.TraceFormat(
                    "Identified CSP DUNS Port {0} for LDC \"{1}\" \nDirectory In: \"{2}\" \nDirectory Out: \"{3}\" \n",
                    port.CspDunsPortId, port.LdcShortName, port.DirectoryIn, port.DirectoryOut);

                IPgpEncryption pgpEncryption;

                try
                {
                    pgpEncryption = InfrastructureFactory
                                    .CreatePgpEncryptor(port.PgpEncryptionKey, port.PgpSignatureKey, port.PgpPassphrase);
                }
                catch (IOException ex)
                {
                    logger.ErrorFormat(ex,
                                       "Unable to create PGP Encryption class for Csp Duns Port \"{0}\".", port.CspDunsPortId);
                    continue;
                }

                var identifier = port.TradingPartnerId.Substring(0, 3);

                var ldcId = port.LdcId ?? 0;
                if (ldcId == 0)
                {
                    var clientSearchPattern = string.Format("*{0}*", identifier);
                    EncryptFiles(pgpEncryption, context, clientSearchPattern, token);
                }
                else
                {
                    var fileExtension         = port.ProviderId == 2 ? ".x12" : ".txt";
                    var extendedSearchPattern = string.Format("*{0}*{1}*{2}", identifier, port.LdcShortName, fileExtension);
                    EncryptFiles(pgpEncryption, context, extendedSearchPattern, token);
                }
            }

            MoveFiles(context, "*", token);
        }
Beispiel #2
0
        public void EncryptFiles(ErcotFileContext context, CspDunsPortModel port, MarketFileModel[] models, CancellationToken token)
        {
            var pgpEncryption = InfrastructureFactory.CreatePgpEncryptor(port.PgpEncryptionKey, port.PgpSignatureKey, port.PgpPassphrase);
            var options       = new TransactionOptions {
                IsolationLevel = IsolationLevel.ReadCommitted
            };

            foreach (var marketFile in models)
            {
                if (token.IsCancellationRequested)
                {
                    token.ThrowIfCancellationRequested();
                }

                var sourcePath = Path.Combine(context.DirectoryDecrypted, marketFile.FileName);
                var sourceInfo = new FileInfo(sourcePath);
                if (!sourceInfo.Exists)
                {
                    logger.WarnFormat("Unable to encrypt file \"{0}\". File does not exist or has been deleted.",
                                      marketFile.FileName);

                    continue;
                }

                try
                {
                    using (var scope = new TransactionScope(TransactionScopeOption.Required, options))
                    {
                        var archivePath = Path.Combine(context.DirectoryArchive, DateTime.Now.ToString("yyyyMM"), marketFile.FileName);
                        sourceInfo.CopyTo(archivePath, true);

                        var targetPath = Path.Combine(context.DirectoryEncrypted, marketFile.FileName);
                        var targetName = string.Concat(targetPath, ".pgp");
                        pgpEncryption.EncryptFile(sourceInfo.FullName, targetName);

                        marketFile.Status      = MarketFileStatusOptions.Encrypted;
                        marketFile.ProcessDate = DateTime.Now;
                        marketDataAccess.UpdateMarketFile(marketFile);

                        scope.Complete();
                    }
                }
                catch (Exception ex)
                {
                    logger.ErrorFormat(ex, "Unknown error occurred while encrypting file \"{0}\".",
                                       marketFile.FileName);
                }
            }
        }
Beispiel #3
0
        public void Execute(DecryptFileContext context, CancellationToken token)
        {
            var cspDunsPorts = clientDataAccess.ListCspDunsPort();

            foreach (var port in cspDunsPorts)
            {
                if (token.IsCancellationRequested)
                {
                    token.ThrowIfCancellationRequested();
                }

                logger.TraceFormat(
                    "Identified CSP DUNS Port {0} for LDC \"{1}\" \nDirectory In: \"{2}\" \nDirectory Out: \"{3}\" \n",
                    port.CspDunsPortId, port.LdcShortName, port.DirectoryIn, port.DirectoryOut);

                IPgpEncryption pgpEncryption;

                try
                {
                    pgpEncryption = InfrastructureFactory
                                    .CreatePgpEncryptor(port.PgpEncryptionKey, port.PgpSignatureKey, port.PgpPassphrase);
                }
                catch (IOException ex)
                {
                    logger.ErrorFormat(ex,
                                       "Unable to create PGP Encryption class for Csp Duns Port \"{0}\".", port.CspDunsPortId);
                    continue;
                }

                var ldcId = port.LdcId ?? 0;
                if (ldcId == 0)
                {
                    var identifier          = port.TradingPartnerId.Substring(0, 3);
                    var clientSearchPattern = string.Format("*{0}*", identifier);
                    var dunsSearchPattern   = string.Format("*{0}*", port.Duns);

                    DecryptFiles(pgpEncryption, context, clientSearchPattern, token);
                    DecryptFiles(pgpEncryption, context, dunsSearchPattern, token);
                    continue;
                }

                var sourceDirectory = new DirectoryInfo(port.DirectoryIn);
                if (!sourceDirectory.Exists)
                {
                    logger.ErrorFormat("Encryption Directory \"{0}\" was not found or has been deleted.",
                                       sourceDirectory.FullName);
                    continue;
                }

                var sourceFiles = sourceDirectory.GetFiles();
                logger.TraceFormat("Identified {0} file(s) in directory \"{1}\" (all files).",
                                   sourceFiles.Length, sourceDirectory.FullName);

                if (sourceFiles.Length == 0)
                {
                    continue;
                }

                DecryptFiles(pgpEncryption, context, sourceFiles, token);
            }
        }