Ejemplo n.º 1
0
        /// <summary>
        /// Processing document received from the editing service
        /// </summary>
        /// <param name="jsonDocumentResponse">The resulting json from editing service</param>
        /// <param name="responseUri">Uri to the converted document</param>
        /// <returns>The percentage of completion of conversion</returns>
        private static int GetResponseUri(string jsonDocumentResponse, out string responseUri)
        {
            if (string.IsNullOrEmpty(jsonDocumentResponse))
            {
                throw new ArgumentException("Invalid param", "jsonDocumentResponse");
            }

            var responseFromService = JObject.Parse(jsonDocumentResponse);

            if (responseFromService == null)
            {
                throw new WebException("Invalid answer format");
            }

            var errorElement = responseFromService.Value <string>("error");

            if (!string.IsNullOrEmpty(errorElement))
            {
                DocumentServiceException.ProcessResponseError(errorElement);
            }

            var isEndConvert = responseFromService.Value <bool>("endConvert");

            int resultPercent;

            responseUri = string.Empty;
            if (isEndConvert)
            {
                responseUri   = responseFromService.Value <string>("fileUrl");
                resultPercent = 100;
            }
            else
            {
                resultPercent = responseFromService.Value <int>("percent");
                if (resultPercent >= 100)
                {
                    resultPercent = 99;
                }
            }

            return(resultPercent);
        }
Ejemplo n.º 2
0
        public static string DocbuilderRequest(
            string docbuilderUrl,
            string requestKey,
            string scriptUrl,
            bool isAsync,
            string signatureSecret,
            out Dictionary <string, string> urls)
        {
            if (string.IsNullOrEmpty(docbuilderUrl))
            {
                throw new ArgumentNullException("docbuilderUrl");
            }

            if (string.IsNullOrEmpty(requestKey) && string.IsNullOrEmpty(scriptUrl))
            {
                throw new ArgumentException("requestKey or inputScript is empty");
            }

            var request = (HttpWebRequest)WebRequest.Create(docbuilderUrl);

            request.Method      = "POST";
            request.ContentType = "application/json";
            request.Timeout     = Timeout;

            var body = new BuilderBody
            {
                Async = isAsync,
                Key   = requestKey,
                Url   = scriptUrl
            };

            if (!string.IsNullOrEmpty(signatureSecret))
            {
                var payload = new Dictionary <string, object>
                {
                    { "payload", body }
                };

                JsonWebToken.JsonSerializer = new JwtSerializer();
                var token = JsonWebToken.Encode(payload, signatureSecret, JwtHashAlgorithm.HS256);
                //todo: remove old scheme
                request.Headers.Add(FileUtility.SignatureHeader, "Bearer " + token);

                token      = JsonWebToken.Encode(body, signatureSecret, JwtHashAlgorithm.HS256);
                body.Token = token;
            }

            var bodyString = JsonConvert.SerializeObject(body);

            var bytes = Encoding.UTF8.GetBytes(bodyString ?? "");

            request.ContentLength = bytes.Length;
            using (var stream = request.GetRequestStream())
            {
                stream.Write(bytes, 0, bytes.Length);
            }

            // hack. http://ubuntuforums.org/showthread.php?t=1841740
            if (WorkContext.IsMono)
            {
                ServicePointManager.ServerCertificateValidationCallback += (s, ce, ca, p) => true;
            }

            string dataResponse = null;

            using (var response = (HttpWebResponse)request.GetResponse())
                using (var responseStream = response.GetResponseStream())
                {
                    if (responseStream != null)
                    {
                        using (var reader = new StreamReader(responseStream))
                        {
                            dataResponse = reader.ReadToEnd();
                        }
                    }
                }

            if (string.IsNullOrEmpty(dataResponse))
            {
                throw new Exception("Invalid response");
            }

            var responseFromService = JObject.Parse(dataResponse);

            if (responseFromService == null)
            {
                throw new Exception("Invalid answer format");
            }

            var errorElement = responseFromService.Value <string>("error");

            if (!string.IsNullOrEmpty(errorElement))
            {
                DocumentServiceException.ProcessResponseError(errorElement);
            }

            var isEnd = responseFromService.Value <bool>("end");

            urls = null;
            if (isEnd)
            {
                IDictionary <string, JToken> rates = (JObject)responseFromService["urls"];

                urls = rates.ToDictionary(pair => pair.Key, pair => pair.Value.ToString());
            }

            return(responseFromService.Value <string>("key"));
        }