Ejemplo n.º 1
0
        /// <summary>
        /// Transmit data to the given URI.
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="partsToSend"></param>
        /// <returns>A result that includes parsed JMF results if available.</returns>
        public IJmfResult Transmit(Uri uri, ITransmissionPartCollection partsToSend)
        {
            ParameterCheck.ParameterRequired(uri, "uri");
            ParameterCheck.ParameterRequired(partsToSend, "partsToSend");
            if (partsToSend.Count == 0)
            {
                throw new PreconditionException(Messages.HttpTransmitter_Transmit_AtLeastOneTransmissionPartIsRequired);
            }
            if (uri.IsFile || uri.Scheme.ToLower() != "http")
            {
                throw new PreconditionException(Messages.HttpTransmitter_Transmit_RequiresHttpUrl);
            }

            try {
                var encodingResult = encodingfactory.GetEncodingForTransmissionParts(partsToSend).Encode(partsToSend);
                transmissionLogger.Log(new TransmissionData(encodingResult.Stream, encodingResult.ContentType, "Request"));

                var request = httpWebRequestFactory.Create(uri, encodingResult.ContentType);
                using (var outStream = request.GetRequestStream()) {
                    encodingResult.Stream.CopyTo(outStream);
                }

                var response = (HttpWebResponse)request.GetResponse();
                try {
                    var contentType = response.ContentType.NormalizeContentType();

                    var responseStream = new TempFileStream();
                    response.GetResponseStream().CopyTo(responseStream);
                    transmissionLogger.Log(new TransmissionData(responseStream, contentType, "Response"));

                    var responseParts = encodingfactory.GetEncodingForMimeType(contentType).Decode("httpContent", responseStream,
                                                                                                   contentType);
                    return(new JmfResult(responseParts));
                }
                finally {
                    response.Close();
                }
            }
            catch (Exception err) {
                logger.Error(string.Format(Messages.HttpTransmitter_Transmit_HttpTransmitter_UnexpectedException, uri), err);
                throw;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Prepare a collection of files for transmission.
        /// </summary>
        /// <param name="parts">The transmission parts.</param>
        /// <param name="transmissionFactory">Transmission factory that is needed but not used.</param>
        /// <param name="encodingfactory">The encodingfactory</param>
        /// <param name="transmissionLogger">The transmissionLogger</param>
        /// <returns>A collection of file transmission items in order of sending.</returns>
        /// <remarks>
        /// <para>
        /// Path variables supported :
        /// </para>
        /// <para>
        /// ${Root} = urlBase attribute of the FileTransmitterEncoder configuration.
        /// </para>
        /// <para>
        /// ${Guid} = A GUID directory.
        /// </para>
        /// <para>
        /// ${JobId} = A JobId directory.  JobId used is from the first JDF found in the parts.
        /// </para>
        /// <para>
        /// ${JobKey} = A JobKey directory.  JobKey used is from the first JDF found in the parts (tree.Key).
        /// </para>
        /// </remarks>
        public virtual List <FileTransmissionItem> PrepareTransmission(ITransmissionPartCollection parts,
                                                                       ITransmissionPartFactory transmissionFactory,
                                                                       IEncodingFactory encodingfactory,
                                                                       ITransmissionLogger transmissionLogger)
        {
            var items = new List <FileTransmissionItem>();

            try {
                if (UseMime)
                {
                    MimeEncoding encoding = new MimeEncoding(transmissionFactory);
                    var          encoded  = encoding.Encode(parts);
                    using (var mimeResult = encoded.Stream) {
                        //TODO determine better way to provide the part.
                        items.Add(new FileTransmissionItem(null, mimeResult, new Uri(Path.Combine(LocalPath, Guid.NewGuid().ToString() + ".mim")), encoded.ContentType, 0));
                    }
                }
                else
                {
                    if (JmfFolderInfo == null && JdfFolderInfo == null && AttachmentFolderInfo == null)
                    {
                        throw new JdfException(
                                  string.Format(FluentJdf.Resources.Messages.TheConfigurationOfTheFileTransmissionEncoderId0IsInvaild,
                                                Id));
                    }
                    if (JmfFolderInfo != null && (JdfFolderInfo == null || AttachmentFolderInfo == null))
                    {
                        throw new JdfException(
                                  string.Format(FluentJdf.Resources.Messages.TheConfigurationOfTheFileTransmissionEncoderId0IsInvaildJMFNoJDF,
                                                Id));
                    }
                    if (JdfFolderInfo != null && AttachmentFolderInfo == null)
                    {
                        throw new JdfException(
                                  string.Format(FluentJdf.Resources.Messages.TheConfigurationOfTheFileTransmissionEncoderId0IsInvaildJDFNoAttach,
                                                Id));
                    }

                    Guid   transmissionGuid = Guid.NewGuid();
                    string jobId            = "JDF_JobIdUnknown";
                    string jobKey           = "JDF_JobKeyUnknown";

                    var jdfTransmissionPart = parts.TicketPart;

                    //pass over parts to get the job id and key
                    if (jdfTransmissionPart != null)
                    {
                        //TODO is this the best way to obtain the ticket? We know it must exist since we are on the correct mime item.
                        var ticket = parts.Ticket;

                        if (ticket.Root.GetJobId().Length > 0)
                        {
                            jobId = "JDF_" + ticket.Root.GetJobId();
                        }
                        if (ticket.Root.GetId().Length > 0)
                        {
                            jobKey = "JDF_" + ticket.Root.GetId();
                        }
                    }

                    var itemsToProcess = new List <KeyValuePair <ITransmissionPart, string> >();

                    //pass over parts to generate destination file names and mapping
                    var urlMapping = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
                    foreach (ITransmissionPart part in parts)
                    {
                        FileTransmitterFolderInfo folder;
                        string extension = null;
                        if (part.MimeType == MimeTypeHelper.JdfMimeType)
                        {
                            folder    = JdfFolderInfo;
                            extension = ".jdf";
                        }
                        else if (part.MimeType == MimeTypeHelper.JmfMimeType)
                        {
                            folder    = JmfFolderInfo;
                            extension = ".jmf";
                        }
                        else
                        {
                            folder    = AttachmentFolderInfo;
                            extension = MimeTypeHelper.MimeTypeExtension(part.MimeType);
                        }

                        string fileName = part.Id.ToString() + extension;

                        if (folder != null)
                        {
                            var    newFileName   = Path.Combine(ExpandFolder(folder.DestinationFolder, transmissionGuid, jobId, jobKey), fileName);
                            string referencePath = Path.Combine(ExpandFolder(folder.ReferenceFolder, transmissionGuid, jobId, jobKey), fileName);
                            urlMapping.Add("cid:" + part.Id.ToLower(), referencePath);
                            itemsToProcess.Add(new KeyValuePair <ITransmissionPart, string>(part, newFileName));
                        }
                    }

                    //fixup urls and add to the collection of files to send
                    foreach (var processPart in itemsToProcess)
                    {
                        FileTransmitterFolderInfo folder;

                        var part = processPart.Key;
                        var file = processPart.Value;

                        if (part.MimeType == MimeTypeHelper.JdfMimeType)
                        {
                            folder = JdfFolderInfo;
                        }
                        else if (part.MimeType == MimeTypeHelper.JmfMimeType)
                        {
                            folder = JmfFolderInfo;
                        }
                        else
                        {
                            folder = AttachmentFolderInfo;
                        }

                        //TODO is this the best way to obtain the ticket? We know it must exist since we are on the correct mime item.
                        if (part.MimeType == MimeTypeHelper.JdfMimeType)
                        {
                            FileSpecUrlMangler.MapFileSpecUrls(parts.Ticket, urlMapping, true);
                            FileSpecUrlMangler.MapPreviewUrls(parts.Ticket, urlMapping, true);
                        }

                        //TODO is this the best way to obtain the message? We know it must exist since we are on the correct mime item.
                        if (part.MimeType == MimeTypeHelper.JmfMimeType)
                        {
                            MapMessageUrls(parts.Message, urlMapping);
                        }

                        if (folder != null)
                        {
                            if (!folder.Suppress)
                            {
                                var encodingResult = encodingfactory.GetEncodingForMimeType(part.MimeType).Encode(part);
                                encodingResult.Stream.Seek(0, SeekOrigin.Begin);
                                transmissionLogger.Log(new TransmissionData(encodingResult.Stream, encodingResult.ContentType, "Request"));
                                items.Add(new FileTransmissionItem(part, encodingResult.Stream, new Uri(file), part.MimeType, folder.Order));
                            }
                        }
                    }
                }
            }
            catch (Exception err) {
                throw new JdfException(string.Format(FluentJdf.Resources.Messages.ErrorOccuredWhileTryingToEncodeTransmissionMessageIs,
                                                     err.Message), err);
            }
            return(items);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Transmit data to the given URI.
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="partsToSend"></param>
        /// <returns>A result that includes parsed JMF results if available.</returns>
        public IJmfResult Transmit(Uri uri, ITransmissionPartCollection partsToSend)
        {
            ParameterCheck.ParameterRequired(uri, "uri");
            ParameterCheck.ParameterRequired(partsToSend, "partsToSend");
            if (partsToSend.Count == 0)
            {
                throw new PreconditionException(Messages.FileTransmitter_Transmit_AtLeastOneTransmissionPartIsRequired);
            }
            if (!uri.IsFile && !uri.IsUnc)
            {
                throw new PreconditionException(Messages.FileTransmitter_Transmit_RequiresHttpUrl);
            }

            var transmissionEncoder = GetFileTransmitterEncoder(uri);

            if (transmissionEncoder != null)
            {
                FileTransmitterEncoder actualEncoder;

                if (transmissionEncoder.FolderInfo.Count == 0)
                {
                    actualEncoder = FileTransmitterEncoder.BuildDefaultFolderInfoCollection(transmissionEncoder);
                }
                else
                {
                    actualEncoder = transmissionEncoder;
                }

                List <FileTransmissionItem> results = null;
                try {
                    results = actualEncoder.PrepareTransmission(partsToSend, transmissionPartFactory, encodingfactory, transmissionLogger);

                    foreach (var item in results.OrderBy(item => item.Order))
                    {
                        if (item.Stream.CanSeek)
                        {
                            item.Stream.Seek(0, SeekOrigin.Begin);
                        }
                        var fileInfo = new FileInfo(item.DestinationUri.LocalPath);
                        DirectoryAndFileHelper.EnsureFolderExists(fileInfo.Directory, logger);
                        DirectoryAndFileHelper.SaveStreamToFile(item.Stream, fileInfo, false, logger);
                    }
                }
                catch (Exception err) {
                    logger.Error(string.Format(Messages.HttpTransmitter_Transmit_HttpTransmitter_UnexpectedException, uri), err);
                    throw;
                }
                finally {
                    if (results != null)
                    {
                        try {
                            foreach (var item in results)
                            {
                                item.Dispose();
                            }
                        }
                        finally {
                            //do nothing
                        }
                    }
                }

                return(new FileTransmissionJmfResult());
            }
            else
            {
                //This code is no longer valid
                //TODO delete this code since mime is no longer the default.
                var fileInfo = new FileInfo(uri.LocalPath);
                DirectoryAndFileHelper.EnsureFolderExists(fileInfo.Directory, logger);
                try {
                    var encodingResult = encodingfactory.GetEncodingForTransmissionParts(partsToSend).Encode(partsToSend);
                    transmissionLogger.Log(new TransmissionData(encodingResult.Stream, encodingResult.ContentType, "Request"));

                    if (encodingResult.Stream.CanSeek)
                    {
                        encodingResult.Stream.Seek(0, SeekOrigin.Begin);
                    }

                    DirectoryAndFileHelper.SaveStreamToFile(encodingResult.Stream, fileInfo, false, logger);

                    return(new FileTransmissionJmfResult());
                }
                catch (Exception err) {
                    logger.Error(string.Format(Messages.HttpTransmitter_Transmit_HttpTransmitter_UnexpectedException, uri), err);
                    throw;
                }
            }
        }
        /// <summary>
        /// Prepare a collection of files for transmission.
        /// </summary>
        /// <param name="parts">The transmission parts.</param>
        /// <param name="transmissionFactory">Transmission factory that is needed but not used.</param>
        /// <param name="encodingfactory">The encodingfactory</param>
        /// <param name="transmissionLogger">The transmissionLogger</param>
        /// <returns>A collection of file transmission items in order of sending.</returns>
        /// <remarks>
        /// <para>
        /// Path variables supported :
        /// </para>
        /// <para>
        /// ${Root} = urlBase attribute of the FileTransmitterEncoder configuration.
        /// </para>
        /// <para>
        /// ${Guid} = A GUID directory.
        /// </para>
        /// <para>
        /// ${JobId} = A JobId directory.  JobId used is from the first JDF found in the parts.
        /// </para>
        /// <para>
        /// ${JobKey} = A JobKey directory.  JobKey used is from the first JDF found in the parts (tree.Key).
        /// </para>
        /// </remarks>
        public virtual List<FileTransmissionItem> PrepareTransmission(ITransmissionPartCollection parts,
                                                                        ITransmissionPartFactory transmissionFactory,
                                                                        IEncodingFactory encodingfactory,
                                                                        ITransmissionLogger transmissionLogger) {
            var items = new List<FileTransmissionItem>();
            try {
                if (UseMime) {
                    MimeEncoding encoding = new MimeEncoding(transmissionFactory);
                    var encoded = encoding.Encode(parts);
                    using (var mimeResult = encoded.Stream) {
                        //TODO determine better way to provide the part.
                        items.Add(new FileTransmissionItem(null, mimeResult, new Uri(Path.Combine(LocalPath, Guid.NewGuid().ToString() + ".mim")), encoded.ContentType, 0));
                    }
                }
                else {
                    if (JmfFolderInfo == null && JdfFolderInfo == null && AttachmentFolderInfo == null) {
                        throw new JdfException(
                            string.Format(FluentJdf.Resources.Messages.TheConfigurationOfTheFileTransmissionEncoderId0IsInvaild,
                            Id));
                    }
                    if (JmfFolderInfo != null && (JdfFolderInfo == null || AttachmentFolderInfo == null)) {
                        throw new JdfException(
                            string.Format(FluentJdf.Resources.Messages.TheConfigurationOfTheFileTransmissionEncoderId0IsInvaildJMFNoJDF,
                            Id));
                    }
                    if (JdfFolderInfo != null && AttachmentFolderInfo == null) {
                        throw new JdfException(
                            string.Format(FluentJdf.Resources.Messages.TheConfigurationOfTheFileTransmissionEncoderId0IsInvaildJDFNoAttach,
                            Id));
                    }

                    Guid transmissionGuid = Guid.NewGuid();
                    string jobId = "JDF_JobIdUnknown";
                    string jobKey = "JDF_JobKeyUnknown";

                    var jdfTransmissionPart = parts.TicketPart;

                    //pass over parts to get the job id and key
                    if (jdfTransmissionPart != null) {

                        //TODO is this the best way to obtain the ticket? We know it must exist since we are on the correct mime item.
                        var ticket = parts.Ticket;

                        if (ticket.Root.GetJobId().Length > 0) {
                            jobId = "JDF_" + ticket.Root.GetJobId();
                        }
                        if (ticket.Root.GetId().Length > 0) {
                            jobKey = "JDF_" + ticket.Root.GetId();
                        }
                    }

                    var itemsToProcess = new List<KeyValuePair<ITransmissionPart, string>>();

                    //pass over parts to generate destination file names and mapping
                    var urlMapping = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
                    foreach (ITransmissionPart part in parts) {
                        FileTransmitterFolderInfo folder;
                        string extension = null;
                        if (part.MimeType == MimeTypeHelper.JdfMimeType) {
                            folder = JdfFolderInfo;
                            extension = ".jdf";
                        }
                        else if (part.MimeType == MimeTypeHelper.JmfMimeType) {
                            folder = JmfFolderInfo;
                            extension = ".jmf";
                        }
                        else {
                            folder = AttachmentFolderInfo;
                            extension = MimeTypeHelper.MimeTypeExtension(part.MimeType);
                        }

                        string fileName = part.Id.ToString() + extension;

                        if (folder != null) {
                            var newFileName = Path.Combine(ExpandFolder(folder.DestinationFolder, transmissionGuid, jobId, jobKey), fileName);
                            string referencePath = Path.Combine(ExpandFolder(folder.ReferenceFolder, transmissionGuid, jobId, jobKey), fileName);
                            urlMapping.Add("cid:" + part.Id.ToLower(), referencePath);
                            itemsToProcess.Add(new KeyValuePair<ITransmissionPart, string>(part, newFileName));
                        }
                    }

                    //fixup urls and add to the collection of files to send
                    foreach (var processPart in itemsToProcess) {
                        FileTransmitterFolderInfo folder;

                        var part = processPart.Key;
                        var file = processPart.Value;

                        if (part.MimeType == MimeTypeHelper.JdfMimeType) {
                            folder = JdfFolderInfo;
                        }
                        else if (part.MimeType == MimeTypeHelper.JmfMimeType) {
                            folder = JmfFolderInfo;
                        }
                        else {
                            folder = AttachmentFolderInfo;
                        }

                        //TODO is this the best way to obtain the ticket? We know it must exist since we are on the correct mime item.
                        if (part.MimeType == MimeTypeHelper.JdfMimeType) {
                            FileSpecUrlMangler.MapFileSpecUrls(parts.Ticket, urlMapping, true);
                            FileSpecUrlMangler.MapPreviewUrls(parts.Ticket, urlMapping, true);
                        }

                        //TODO is this the best way to obtain the message? We know it must exist since we are on the correct mime item.
                        if (part.MimeType == MimeTypeHelper.JmfMimeType) {
                            MapMessageUrls(parts.Message, urlMapping);
                        }

                        if (folder != null) {
                            if (!folder.Suppress) {
                                var encodingResult = encodingfactory.GetEncodingForMimeType(part.MimeType).Encode(part);
                                encodingResult.Stream.Seek(0, SeekOrigin.Begin);
                                transmissionLogger.Log(new TransmissionData(encodingResult.Stream, encodingResult.ContentType, "Request"));
                                items.Add(new FileTransmissionItem(part, encodingResult.Stream, new Uri(file), part.MimeType, folder.Order));
                            }
                        }
                    }
                }
            }
            catch (Exception err) {
                throw new JdfException(string.Format(FluentJdf.Resources.Messages.ErrorOccuredWhileTryingToEncodeTransmissionMessageIs,
                    err.Message), err);
            }
            return items;
        }