コード例 #1
0
 /// <summary>
 ///
 /// </summary>
 public void Dispose()         // Since this class is sealed, we don't need to implement the full dispose pattern.
 {
     if (_parser != null)
     {
         _parser.Dispose();
         _parser = null;
     }
 }
コード例 #2
0
 // Since this class is sealed, we don't need to implement the full dispose pattern.
 /// <summary>
 /// 
 /// </summary>
 public void Dispose()
 {
     if (_parser != null)
     {
         _parser.Dispose();
         _parser = null;
     }
 }
コード例 #3
0
        public AssembleDocumentResult AssembleDocument(Template template, TextReader answers, AssembleDocumentSettings settings, string logRef)
        {
            using (var client = new HttpClient())
            {
                AssembleDocumentResult adr = null;
                var stringContent = new StringContent(answers.ReadToEnd());
                var packageTemplateLocation = (PackageTemplateLocation)template.Location;
                var packageId = packageTemplateLocation.PackageID;

                OutputFormat of = ConvertFormat(settings.Format);

                var urlBuilder = new StringBuilder(string.Format(HostAddress + "/assemble/0/{0}/?templatename={1}&format={2}", packageId, HttpUtility.UrlEncode(template.FileName), of));

                if (settings.Settings != null)
                {
                    foreach (KeyValuePair<string, string> kv in settings.Settings)
                    {
                        urlBuilder.AppendFormat("&{0}={1}", kv.Key, kv.Value ?? "");
                    }
                }

                var result = client.PostAsync(urlBuilder.ToString(), stringContent).Result;

                var _parser = new MultipartMimeParser();

                HandleErrorStream(result, result.IsSuccessStatusCode);
                var streamResult = result.Content.ReadAsStreamAsync().Result;

                var outputDir = Path.GetTempPath();
                Directory.CreateDirectory(outputDir);
                using (var resultsStream = new MemoryStream())
                {
                    // Each part is written to a file whose name is specified in the content-disposition
                    // header, except for the AssemblyResult part, which has a file name of "meta0.xml",
                    // and is parsed into an AssemblyResult object.
                    _parser.WritePartsToStreams(
                        streamResult,
                        h =>
                        {
                            string fileName = GetFileNameFromHeaders(h);
                            if (fileName != null)
                            {
                                if (fileName.Equals("meta0.xml", StringComparison.OrdinalIgnoreCase))
                                {
                                    return resultsStream;
                                }

                                return new FileStream(Path.Combine(outputDir, fileName), FileMode.Create);
                            }
                            return Stream.Null;
                        },
                        (new ContentType(result.Content.Headers.ContentType.ToString())).Boundary);

                    if (resultsStream.Position > 0)
                    {
                        resultsStream.Position = 0;
                        var serializer = new XmlSerializer(typeof(AssemblyResult));
                        var asmResult = (AssemblyResult)serializer.Deserialize(resultsStream);
                        if (asmResult != null)
                            adr = Util.ConvertAssemblyResult(template, asmResult, settings.Format);
                    }
                }
                return adr;
            }
        }
コード例 #4
0
        public InterviewResult GetInterview(Template template, TextReader answers, InterviewSettings settings, IEnumerable<string> markedVariables, string logRef)
        {
            using (var client = new HttpClient())
            {
                var packageTemplateLocation = (PackageTemplateLocation)template.Location;
                var packageId = packageTemplateLocation.PackageID;

                var urlBuilder = new StringBuilder(string.Format(
                    HostAddress +
                    "/interview/0/{0}/?templatename={1}&format={2}&markedvariables{3}&tempimageurl={4}",
                    packageId, HttpUtility.UrlEncode(template.FileName), settings.Format,
                    markedVariables != null ? "=" + string.Join(",", settings.MarkedVariables) : null, settings.InterviewFilesUrl));

                foreach (KeyValuePair<string, string> kv in settings.Settings)
                {
                    urlBuilder.AppendFormat("&{0}={1}", kv.Key, kv.Value ?? "");
                }

                StringContent stringContent = answers == null ? new StringContent(String.Empty) : new StringContent(answers.ReadToEnd());

                HttpResponseMessage result = client.PostAsync(urlBuilder.ToString(), stringContent).Result;

                HandleErrorStream(result, result.IsSuccessStatusCode);
                var streamResult = result.Content.ReadAsStreamAsync().Result;
                if (!result.IsSuccessStatusCode)
                {
                    using (var streamReader = new StreamReader(streamResult))
                    {
                        string error = streamReader.ReadToEnd();
                        throw new Exception(error);
                    }
                }

                var parser = new MultipartMimeParser();
                var outputDir = Path.GetTempPath();
                Directory.CreateDirectory(outputDir);

                using (var resultsStream = new MemoryStream())
                {
                    // Each part is written to a file whose name is specified in the content-disposition
                    // header, except for the BinaryObject[] part, which has a file name of "meta0.xml",
                    // and is parsed into an BinaryObject[] object.
                    parser.WritePartsToStreams(
                        streamResult,
                        h =>
                        {
                            string fileName = GetFileNameFromHeaders(h);

                            if (!string.IsNullOrEmpty(fileName))
                            {
                                if (fileName.Equals("meta0.xml", StringComparison.OrdinalIgnoreCase))
                                {
                                    return resultsStream;
                                }
                                // The following stream will be closed by the parser
                                return new FileStream(Path.Combine(outputDir, fileName), FileMode.Create);
                            }
                            return Stream.Null;
                        },
                        (new ContentType(result.Content.Headers.ContentType.ToString())).Boundary);

                    if (resultsStream.Position > 0)
                    {
                        resultsStream.Position = 0;
                        var serializer = new XmlSerializer(typeof(BinaryObject[]));
                        var binObjects = (BinaryObject[])serializer.Deserialize(resultsStream);
                        var interviewContent = Util.ExtractString(binObjects[0]);
                        return new InterviewResult { HtmlFragment = interviewContent };
                    }
                    return null;
                }
            }
        }