/// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="part">The original part.</param>
 /// <param name="stream">Stream to send.</param>
 /// <param name="destinationUrl">Fully-qualified destination URL.</param>
 /// <param name="mimeType">The mimeType</param>
 /// <param name="order">The order of this item for sending.</param>
 public FileTransmissionItem(ITransmissionPart part, Stream stream, Uri destinationUrl, string mimeType, int order) {
     this.part = part;
     if (stream.CanSeek) {
         stream.Seek(0, SeekOrigin.Begin);
     }
     this.stream = new TempFileStream();
     stream.CopyTo(this.stream);
     this.destinationUrl = destinationUrl;
     this.mimeType = mimeType;
     this.order = order;
 }
예제 #2
0
 /// <summary>
 /// Validate the ticket and optionally add schema info (defaults and PVSI)
 /// </summary>
 /// <param name="addSchemaInfo">True adds default elements, default attributes and schema info to the ticket.  False leaves this alone.  Default is true.</param>
 /// <param name="workAroundMSBug">True works around an issue in the .NET framework that causes validation to work improperly on schema types that inherit 
 /// from an abstract base class if the document is created via node authoring code instead of by Parse or Load methods.  Default is true.</param>
 /// <returns></returns>
 public bool Validate(bool addSchemaInfo = true, bool workAroundMSBug = true) {
     //todo: this method should probably return ResultOf.  ResultOf needs to be taken from connent and move to common to accomplish this properly.
     messages.Clear();
     ticket.Document.Validate(SchemaSet.Instance.Schemas, (o, e) => messages.Add(new ValidationMessage(o, e.Severity, e.Message)),addSchemaInfo);
     if (workAroundMSBug) {
         using (var tempFileSream = new TempFileStream()) {
             messages.Clear();
             ticket.Document.Save(tempFileSream);
             tempFileSream.Seek(0, SeekOrigin.Begin);
             var newDocument = XDocument.Load(tempFileSream);
             newDocument.Validate(SchemaSet.Instance.Schemas, (o, e) => messages.Add(new ValidationMessage(o, e.Severity, e.Message)),
                                  false);
         }
     }
     IsValid = messages.Where(m => m.ValidationMessageType == ValidationMessageType.Error).Count() == 0;
     Messages = new ReadOnlyCollection<ValidationMessage>(messages);
     HasValidatedAtLeastOnce = true;
     return IsValid.Value;
 }
예제 #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.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;
            }
        }
예제 #4
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="headers"></param>
        /// <param name="stream"></param>
        /// <param name="title">If title is null, the default log entry title will be used.</param>
        public TransmissionData(NameValueCollection headers, Stream stream, string title = null) {
            ParameterCheck.ParameterRequired(headers, "headers");
            ParameterCheck.ParameterRequired(stream, "stream");

            this.title = title;
            if (stream.CanSeek) {
                Stream = stream;
            }
            else {
                Stream = new TempFileStream();
                using (stream) {
                    stream.CopyTo(Stream);
                }
            }
            Stream.Seek(0, SeekOrigin.Begin);

            if (headers["content-type"] != null) {
                ContentType = headers["content-type"].NormalizeContentType();
            }
            else {
                ContentType = MimeTypeHelper.HtmlMimeType;
            }
            Headers = headers;
        }
예제 #5
0
 /// <summary>
 /// Creates a ticket from a template in an XDocument.
 /// </summary>
 /// <param name="document"></param>
 /// <returns></returns>
 public static GeneratedTicketTemplateSelectionBuilder CreateFromTemplate(XDocument document) {
     var stream = new TempFileStream();
     document.Save(stream);
     stream.Seek(0, SeekOrigin.Begin);
     return CreateFromTemplate(stream);
 }
        /// <summary>
        /// Gets the stream associated with the part.
        /// </summary>
        public Stream CopyOfStream() {
            var tempStream = new TempFileStream();
            Message.SaveHttpReady(tempStream);
            tempStream.Seek(0, SeekOrigin.Begin);
 
            return tempStream;
        }
예제 #7
0
        /// <summary>
        /// Generate a tree for this template.
        /// </summary>
        /// <param name="vars">Name/value pairs.</param>
        /// <param name="makeIdsUnique">True to make ids in the file globally unique.  If false, IDs will be taken from the template.</param>
        /// <param name="jobId">The new JobID value.</param>
        /// <returns>The tree containing the generated JDF.</returns>
        public XDocument Generate(Dictionary<string, object> vars, string jobId = null, bool makeIdsUnique = true) {
            ParameterCheck.ParameterRequired(vars, "vars");

            XDocument tree = null;

            var buffStream = new TempFileStream();
            StreamWriter writer = new StreamWriter(buffStream);
            try
            {
                items.Generate(writer, vars);

                writer.Flush();

                buffStream.Seek(0, SeekOrigin.Begin);

                if (makeIdsUnique)
                {
                    tree = GenerateNewIds(buffStream, jobId);
                }
                else
                {
                    tree = XDocument.Load(buffStream);
                }
            }
            finally
            {
                writer.Close();
                buffStream.Close();
            }

            if (tree.XmlType() == XmlType.Jdf)
            {
                //the instance document is not a template so set Template to false in the root
                tree.Root.SetAttributeValue("Template", "false");
            }

            RemoveNullValueAttributes(tree);

            return tree;
        }
        /// <summary>
        /// Creates a transmission part based on an <see cref="XDocument"/>.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="doc"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <remarks>If the root is JDF, the part type registered for the JDF mime type is used.
        /// If the root is JMF, the part type registered for the JMF mime type is used.
        /// Otherwise, the part type registered for the generic xml mime type is used.</remarks>
        public ITransmissionPart CreateTransmissionPart(string name, XDocument doc, string id = null) {
            string mimeType = MimeTypeHelper.XmlMimeType;
            var xmlType = doc.XmlType();
            switch (xmlType) {
                case XmlType.Jdf:
                    mimeType = MimeTypeHelper.JdfMimeType;
                    break;
                case XmlType.Jmf:
                    mimeType = MimeTypeHelper.JmfMimeType;
                    break;
            }
            var transmissionPart = ConstructConfiguredTransmissionPart(mimeType);
            if (transmissionPart is IXmlTransmissionPart) {
                ((IXmlTransmissionPart)transmissionPart).InitalizeProperties(doc, name, id);
            }
            else {
                var tempStream = new TempFileStream();
                doc.Save(tempStream);
                transmissionPart.Initialize(name, tempStream, MimeTypeHelper.XmlMimeType, id);
            }

            return transmissionPart;
        }
        /// <summary>
        /// Creates a transmission part based on a Message.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="message"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public ITransmissionPart CreateTransmissionPart(string name, Message message, string id = null) {
            var transmissionPart = ConstructConfiguredTransmissionPart(MimeTypeHelper.JmfMimeType);
            if (transmissionPart is IMessageTransmissionPart) {
                ((IMessageTransmissionPart)transmissionPart).InitalizeProperties(message, name, id);
            }
            else {
                var tempStream = new TempFileStream();
                message.Save(tempStream);
                transmissionPart.Initialize(name, tempStream, MimeTypeHelper.JmfMimeType, id);
            }

            return transmissionPart;
        }
        /// <summary>
        /// Creates a transmission part based on a Ticket.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="ticket"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public ITransmissionPart CreateTransmissionPart(string name, Ticket ticket, string id = null) {
            var transmissionPart = ConstructConfiguredTransmissionPart(MimeTypeHelper.JdfMimeType);
            if (transmissionPart is ITicketTransmissionPart) {
                ((ITicketTransmissionPart)transmissionPart).InitalizeProperties(ticket, name, id);
            }
            else {
                var tempStream = new TempFileStream();
                ticket.Save(tempStream);
                transmissionPart.Initialize(name, tempStream, MimeTypeHelper.JdfMimeType, id);
            }

            return transmissionPart;
        }
        /// <summary>
        /// CopyOfStream
        /// </summary>
        /// <returns></returns>
        public Stream CopyOfStream() {
            var tempStream = new TempFileStream();
            stream.Seek(0, SeekOrigin.Begin);
            stream.CopyTo(tempStream);
            tempStream.Seek(0, SeekOrigin.Begin);
            stream.Seek(0, SeekOrigin.Begin);

            return tempStream;
        }
 /// <summary>
 /// Override this method to change behavior of dispose in child classes.
 /// </summary>
 /// <param name="isDisposing"></param>
 protected virtual void Dispose(bool isDisposing) {
     if (isDisposing) {
         if (stream != null) {
             stream.Dispose();
             stream = null;
         }
     }
 }