Exemplo n.º 1
0
        public static void Write(TextWriter w, Json json)
        {
            if (w == null || json == null)
            {
                return;
            }
            var s_json = json.ToCompactString();

            w.Write(s_json);
        }
Exemplo n.º 2
0
        public static void Write(Stream s, Json json)
        {
            if (s == null || json == null)
            {
                return;
            }
            var s_json = json.ToCompactString();
            var b_json = Encoding.UTF8.GetBytes(s_json);

            s.Write(b_json, 0, b_json.Length);
        }
Exemplo n.º 3
0
 public static void Write(TextWriter w, Json json)
 {
     if (w == null || json == null) return;
     var s_json = json.ToCompactString();
     w.Write(s_json);
 }
Exemplo n.º 4
0
 public static void Write(Stream s, Json json)
 {
     if (s == null || json == null) return;
     var s_json = json.ToCompactString();
     var b_json = Encoding.UTF8.GetBytes(s_json);
     s.Write(b_json, 0, b_json.Length);
 }
Exemplo n.º 5
0
        public static void Save(String uri, Json json, ICredentials credentials = null)
        {
            if (uri == null) return;

            var is_remote = uri.StartsWith("http://") || uri.StartsWith("https://");
            if (is_remote)
            {
                var req = (HttpWebRequest)WebRequest.Create(uri);
                req.Credentials = credentials ?? CredentialCache.DefaultCredentials;

                req.Method = json == null ? "DELETE" : "POST";
                if (json != null) new StreamWriter(req.GetRequestStream()).Write(json.ToCompactString());

                try
                {
                    var resp = (HttpWebResponse)req.GetResponse();
                    if (resp.StatusCode != HttpStatusCode.OK)
                    {
                        throw new Exception(String.Format("{4} for \"{1}\" has failed: {2}{0}{3}",
                            Environment.NewLine, uri, resp.StatusCode, resp.GetResponseStream().DumpToString(), req.Method));
                    }
                }
                catch (WebException wex)
                {
                    if (wex.Response != null)
                    {
                        var resp = (HttpWebResponse)wex.Response;
                        throw new Exception(String.Format("{4} for \"{1}\" has failed: {2}{0}{3}",
                            Environment.NewLine, uri, resp.StatusCode, resp.GetResponseStream().DumpToString(), wex, req.Method));
                    }
                    else
                    {
                        throw new Exception(String.Format("{3} for \"{1}\" has failed: {0}{2}",
                            Environment.NewLine, uri, wex, req.Method));
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(String.Format("{3} for \"{1}\" has failed: {0}{2}",
                        Environment.NewLine, uri, ex, req.Method));
                }
            }
            else
            {
                if (uri.StartsWith("file:///")) uri = uri.Slice("file:///".Length);

                var is_web = HttpContext.Current != null;
                var path = is_web && uri.StartsWith("~") ? HttpContext.Current.Server.MapPath(uri) : uri;
                path = Path.GetFullPath(path);
                if (!File.Exists(path)) throw new Exception(String.Format(
                    "{2} for \"{0}\" has failed: file \"{1}\" does not exist", uri, path, json == null ? "DELETE" : "WRITE"));

                var s_json = json == null ? null : json.ToCompactString();
                if (s_json == null) File.Delete(path);
                else File.WriteAllText(path, s_json);
            }
        }
Exemplo n.º 6
0
        public static void Save(String uri, Json json, ICredentials credentials = null)
        {
            if (uri == null)
            {
                return;
            }

            var is_remote = uri.StartsWith("http://") || uri.StartsWith("https://");

            if (is_remote)
            {
                var req = (HttpWebRequest)WebRequest.Create(uri);
                req.Credentials = credentials ?? CredentialCache.DefaultCredentials;

                req.Method = json == null ? "DELETE" : "POST";
                if (json != null)
                {
                    new StreamWriter(req.GetRequestStream()).Write(json.ToCompactString());
                }

                try
                {
                    var resp = (HttpWebResponse)req.GetResponse();
                    if (resp.StatusCode != HttpStatusCode.OK)
                    {
                        throw new Exception(String.Format("{4} for \"{1}\" has failed: {2}{0}{3}",
                                                          Environment.NewLine, uri, resp.StatusCode, resp.GetResponseStream().DumpToString(), req.Method));
                    }
                }
                catch (WebException wex)
                {
                    if (wex.Response != null)
                    {
                        var resp = (HttpWebResponse)wex.Response;
                        throw new Exception(String.Format("{4} for \"{1}\" has failed: {2}{0}{3}",
                                                          Environment.NewLine, uri, resp.StatusCode, resp.GetResponseStream().DumpToString(), wex, req.Method));
                    }
                    else
                    {
                        throw new Exception(String.Format("{3} for \"{1}\" has failed: {0}{2}",
                                                          Environment.NewLine, uri, wex, req.Method));
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(String.Format("{3} for \"{1}\" has failed: {0}{2}",
                                                      Environment.NewLine, uri, ex, req.Method));
                }
            }
            else
            {
                if (uri.StartsWith("file:///"))
                {
                    uri = uri.Slice("file:///".Length);
                }

                var is_web = HttpContext.Current != null;
                var path   = is_web && uri.StartsWith("~") ? HttpContext.Current.Server.MapPath(uri) : uri;
                path = Path.GetFullPath(path);
                if (!File.Exists(path))
                {
                    throw new Exception(String.Format(
                                            "{2} for \"{0}\" has failed: file \"{1}\" does not exist", uri, path, json == null ? "DELETE" : "WRITE"));
                }

                var s_json = json == null ? null : json.ToCompactString();
                if (s_json == null)
                {
                    File.Delete(path);
                }
                else
                {
                    File.WriteAllText(path, s_json);
                }
            }
        }
Exemplo n.º 7
0
        // todo. when outputting HTML, also inject the "$(document).ready(function(){window.log.server(<actual log>});"
        private void Complete(Object result = null)
        {
            if (result == null) Native.End();

            var result_ex = result as Exception;
            if (result_ex != null)
            {
                Native.Clear();

                // note. why there's no line info in the stack trace?
                // see http://stackoverflow.com/questions/2673623/iis-not-giving-line-numbers-in-stack-trace-even-though-pdb-present
                var message = (CallStack.Enabled ? result_ex.ToString() : result_ex.Message).ToHtml();
                var trace = (Debug.Enabled ? Log.Message : null).ToHtml();

                var is_json = Native.ContentType == "application/json";
                if (is_json)
                {
                    this["Content-Type"] = "application/json";
                    var lowlevel_result = new Json(new { success = true, result = message, trace = trace });
                    Write(Hints.Prettyprint ? lowlevel_result.ToPrettyString() : lowlevel_result.ToCompactString());
                    Native.End();
                }
                else
                {
                    this["Content-Type"] = "text/html";
                    // note. trace will be written by Gateway.cs
                    // note. no need to dump exception, since it's already included into trace
                    Native.End();
                }
            }

            var result_json = result as Json;
            if (result_json != null)
            {
                (this.Text() == null || this.Text().IsEmpty()).AssertTrue();
                this["Content-Type"] = Hints.Prettyprint ? "text/html" : "application/json";
                var lowlevel_result = new Json(new { success = true, result = result_json, trace = (Debug.Enabled ? Log.Message : null).ToHtml() });
                Write(Hints.Prettyprint ? lowlevel_result.ToPrettyString() : lowlevel_result.ToCompactString());
                Native.End();
            }

            var result_string = result as String;
            if (result_string != null)
            {
                // todo. if this is HTML, inject log into it and show via javascript upon hotkey press
                (this.Text() == null || this.Text().IsEmpty()).AssertTrue();
                Write(result_string);
                Native.End();
            }

            var result_bytes = result as byte[];
            if (result_bytes != null)
            {
                (this.Bytes() == null || this.Bytes().IsEmpty()).AssertTrue();
                BinaryWrite(result_bytes);
                Native.End();
            }

            var result_stream = result as Stream;
            if (result_stream != null)
            {
                (this.Bytes() == null || this.Bytes().IsEmpty()).AssertTrue();
                StreamWrite(result_stream);
                Native.End();
            }

            throw AssertionHelper.Fail();
        }