/**
  * Construct the AckDistributionEnvelope as an acknowledgment to the given
  * DistributionEnvelope.
  */
 public AckDistributionEnvelope(DistributionEnvelope d)
     : base()
 {
     Address[] a = new Address[1];
     a[0] = d.getSender();
     setTo(a);
     String id = null;
     String snd = null;
     try
     {
         Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
         id = config.AppSettings.Settings[AUDIT_ID_PROPERTY].Value;
         snd = config.AppSettings.Settings[SENDER_PROPERTY].Value;
     }
     catch (Exception e)
     {
         throw new DistributionEnvelopeException("SYST-0000", "Configuration manager exception", e.ToString());
     }
     Address sndr = new Address(snd);
     Identity[] auditId = new Identity[1];
     auditId[0] = new Identity(id);
     setAudit(auditId);
     setSender(sndr);
     setService(SERVICE);
     setTrackingId(d.getTrackingId());
     serviceRef = d.getService();
 }
        private DistributionEnvelope splitExtract(String s)
        {
            DistributionEnvelope d = new DistributionEnvelope();
            int ee = s.IndexOf(EXTRACT_END_DELIMITER);
            if (ee == -1)
            {
                throw new Exception("Failed DistributionEnvelope extract - envelope not found");
            }
            int ds = ee + EXTRACT_END_DELIMITER.Length;
            String env = s.Substring(ds);
            if ((env == null) || (env.Trim().Length == 0))
            {
                throw new Exception("Failed DistributionEnvelope extract - zero-length envelope");
            }
            d.setDistributionEnvelope(env);
            int es = s.IndexOf(EXTRACT_START_DELIMITER);
            if (es == -1)
            {
                throw new Exception("Failed DistributionEnvelope extract - extract not found");
            }
            es += EXTRACT_START_DELIMITER.Length;
            String extract = s.Substring(es, ee);

            Regex lineDelimiter = new Regex("\\!");
            Regex fieldDelimiter = new Regex("#");

            String[] lines = lineDelimiter.Split(extract);
            List<Address> addresses = new List<Address>();
            List<Identity> audit = new List<Identity>();

            foreach (String l in lines)
            {
                String[] fields = fieldDelimiter.Split(l);
                if (fields[0].Equals("R"))
                {
                    Address a = null;
                    if (fields.Length > 1)
                    {
                        a = (Address)makeEntity(true, fields);
                        d.setSender(a);
                    }
                    continue;
                }
                if (fields[0].Equals("S"))
                {
                    if (fields.Length == 2)
                    {
                        d.setService(fields[1]);
                    }
                    continue;
                }
                if (fields[0].Equals("T"))
                {
                    if (fields.Length == 2)
                    {
                        d.setTrackingId(fields[1]);
                    }
                    continue;
                }
                if (fields[0].Equals("A"))
                {
                    Address a = (Address)makeEntity(true, fields);
                    addresses.Add(a);
                    continue;
                }
                if (fields[0].Equals("I"))
                {
                    Identity i = (Identity)makeEntity(false, fields);
                    audit.Add(i);
                    continue;
                }
                if (fields[0].Equals("H"))
                {
                    if (fields.Length == 3)
                    {
                        d.addHandlingSpecification(fields[1], fields[2]);
                    }
                    else
                    {
                        d.addHandlingSpecification(fields[1], "");
                    }
                }
            }
            d.setTo(addresses.ToArray());
            d.setAudit(audit.ToArray());
            // Add any checkers we have for "all" and for this DE, also the adders and members
            // Dictionary<string,string[]> where key is service name, and array is list of class names
            if (checkClasses != null)
            {
                if (checkClasses.ContainsKey("all"))
                    makeCheckers(d, checkClasses["all"]);
                if (checkClasses.ContainsKey(d.getService()))
                    makeCheckers(d, checkClasses[d.getService()]);
            }
            return d;
        }
        /**
         * Implementation of the interface' "handle()" method. Determines the
         * file name and tries to write it. Note that this doesn't return any
         * ITK response because formally such a response requires a router. Providing
         * an ITK router here would put a dependency on the ITK code, in the TMS
         * adapter. An implementation that does do ITK responses will be found in
         * the router package.
         */
        public void handle(DistributionEnvelope d)
        {
            StringBuilder sb = new StringBuilder(spoolDirectory);
            sb.Append("\\");
            sb.Append(getLastServiceURIElement(d.getService()));
            sb.Append("_");
            sb.Append(getFileSafeMessageID(d.getTrackingId()));
            sb.Append(".message");
            string filename = sb.ToString();

            try
            {
                using (FileStream fs = new FileStream(filename, FileMode.Create))
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        d.parsePayloads();
                        sw.Write(d.getEnvelope());
                        sw.Flush();
                        sw.Close();
                    }
                }
            }
            catch (Exception e)
            {
                EventLog ev = new EventLog("Application");
                ev.Source = LOGSOURCE;
                StringBuilder sbe = new StringBuilder("Failed to save DistributionEnvelope ");
                sbe.Append(d.getTrackingId());
                sbe.Append(" service ");
                sbe.Append(d.getService());
                sbe.Append(" from ");
                sbe.Append(d.getSender().getUri());
                sbe.Append(". Reason: ");
                sbe.Append(e.ToString());
                ev.WriteEntry(sbe.ToString(), EventLogEntryType.Error);
            }
        }