// Send all queued messages if connected to the room. private void maybeDrainQueue() { lock (sendQueue) { if (appRTCSignalingParameters == null) { return; } try { foreach (string msg in sendQueue) { Log.Debug("console", msg); URLConnection connection = (new URL(appRTCSignalingParameters.gaeBaseHref + appRTCSignalingParameters.postMessageUrl)).OpenConnection(); connection.DoOutput = true; connection.OutputStream.Write(msg.GetBytes("UTF-8"), 0, msg.Length - 1); if (!connection.GetHeaderField(null).StartsWith("HTTP/1.1 200 ")) { throw new IOException("Non-200 response to POST: " + connection.GetHeaderField(null) + " for msg: " + msg); } } } catch (IOException e) { throw new Exception("Error", e); } sendQueue.Clear(); } }
public Response <Bitmap> Load(Uri uri, bool localCacheOnly) { if (Build.VERSION.SdkInt >= BuildVersionCodes.IceCreamSandwich) { InstallCacheIfNeeded(m_Context); } URLConnection connection = OpenConnection(uri); connection.UseCaches = true; if (localCacheOnly) { connection.SetRequestProperty("Cache-Control", "only-if-cached,max-age=" + int.MaxValue); } var httpConnection = connection as HttpURLConnection; if (httpConnection != null) { int responseCode = (int)httpConnection.ResponseCode; if (responseCode >= 300) { httpConnection.Disconnect(); throw new ResponseException(responseCode + " " + httpConnection.ResponseMessage); } } long contentLength = connection.GetHeaderFieldInt("Content-Length", -1); bool fromCache = ParseResponseSourceHeader(connection.GetHeaderField(ResponseSource)); return(new Response <Bitmap>(connection.InputStream, fromCache, contentLength)); }
/// <exception cref="System.IO.IOException"></exception> public virtual void TestDocWithAttachment() { string inlineTextString = "Inline text string created by cblite functional test"; Send("PUT", "/db", Status.Created, null); IDictionary <string, object> attachment = new Dictionary <string, object>(); attachment["content_type"] = "text/plain"; attachment["data"] = "SW5saW5lIHRleHQgc3RyaW5nIGNyZWF0ZWQgYnkgY2JsaXRlIGZ1bmN0aW9uYWwgdGVzdA==" ; IDictionary <string, object> attachments = new Dictionary <string, object>(); attachments["inline.txt"] = attachment; IDictionary <string, object> docWithAttachment = new Dictionary <string, object>(); docWithAttachment["_id"] = "docWithAttachment"; docWithAttachment["text"] = inlineTextString; docWithAttachment["_attachments"] = attachments; IDictionary <string, object> result = (IDictionary <string, object>)SendBody("PUT", "/db/docWithAttachment", docWithAttachment, Status.Created, null); result = (IDictionary <string, object>)Send("GET", "/db/docWithAttachment", Status .Ok, null); IDictionary <string, object> attachmentsResult = (IDictionary <string, object>)result ["_attachments"]; IDictionary <string, object> attachmentResult = (IDictionary <string, object>)attachmentsResult .Get("inline.txt"); // there should be either a content_type or content-type field. //https://github.com/couchbase/couchbase-lite-android-core/issues/12 //content_type becomes null for attachments in responses, should be as set in Content-Type string contentTypeField = (string)attachmentResult["content_type"]; NUnit.Framework.Assert.IsTrue(attachmentResult.ContainsKey("content_type")); NUnit.Framework.Assert.IsNotNull(contentTypeField); URLConnection conn = SendRequest("GET", "/db/docWithAttachment/inline.txt", null, null); string contentType = conn.GetHeaderField("Content-Type"); NUnit.Framework.Assert.IsNotNull(contentType); NUnit.Framework.Assert.IsTrue(contentType.Contains("text/plain")); StringWriter writer = new StringWriter(); IOUtils.Copy(conn.GetInputStream(), writer, "UTF-8"); string responseString = writer.ToString(); NUnit.Framework.Assert.IsTrue(responseString.Contains(inlineTextString)); }
public virtual void TestViews() { Send("PUT", "/db", Status.Created, null); IDictionary <string, object> result; IDictionary <string, object> doc1 = new Dictionary <string, object>(); doc1["message"] = "hello"; result = (IDictionary <string, object>)SendBody("PUT", "/db/doc1", doc1, Status.Created , null); string revID = (string)result["rev"]; IDictionary <string, object> doc3 = new Dictionary <string, object>(); doc3["message"] = "bonjour"; result = (IDictionary <string, object>)SendBody("PUT", "/db/doc3", doc3, Status.Created , null); string revID3 = (string)result["rev"]; IDictionary <string, object> doc2 = new Dictionary <string, object>(); doc2["message"] = "guten tag"; result = (IDictionary <string, object>)SendBody("PUT", "/db/doc2", doc2, Status.Created , null); string revID2 = (string)result["rev"]; Database db = manager.GetDatabase("db"); View view = db.GetView("design/view"); view.SetMapAndReduce(new _Mapper_372(), null, "1"); // Build up our expected result IDictionary <string, object> row1 = new Dictionary <string, object>(); row1["id"] = "doc1"; row1["key"] = "hello"; IDictionary <string, object> row2 = new Dictionary <string, object>(); row2["id"] = "doc2"; row2["key"] = "guten tag"; IDictionary <string, object> row3 = new Dictionary <string, object>(); row3["id"] = "doc3"; row3["key"] = "bonjour"; IList <IDictionary <string, object> > expectedRows = new AList <IDictionary <string, object > >(); expectedRows.AddItem(row3); expectedRows.AddItem(row2); expectedRows.AddItem(row1); IDictionary <string, object> expectedResult = new Dictionary <string, object>(); expectedResult["offset"] = 0; expectedResult["total_rows"] = 3; expectedResult["rows"] = expectedRows; // Query the view and check the result: Send("GET", "/db/_design/design/_view/view", Status.Ok, expectedResult); // Check the ETag: URLConnection conn = SendRequest("GET", "/db/_design/design/_view/view", null, null ); string etag = conn.GetHeaderField("Etag"); NUnit.Framework.Assert.AreEqual(string.Format("\"%d\"", view.GetLastSequenceIndexed ()), etag); // Try a conditional GET: IDictionary <string, string> headers = new Dictionary <string, string>(); headers["If-None-Match"] = etag; conn = SendRequest("GET", "/db/_design/design/_view/view", headers, null); NUnit.Framework.Assert.AreEqual(Status.NotModified, conn.GetResponseCode()); // Update the database: IDictionary <string, object> doc4 = new Dictionary <string, object>(); doc4["message"] = "aloha"; result = (IDictionary <string, object>)SendBody("PUT", "/db/doc4", doc4, Status.Created , null); // Try a conditional GET: conn = SendRequest("GET", "/db/_design/design/_view/view", headers, null); NUnit.Framework.Assert.AreEqual(Status.Ok, conn.GetResponseCode()); result = (IDictionary <string, object>)ParseJSONResponse(conn); NUnit.Framework.Assert.AreEqual(4, result["total_rows"]); }
/// <exception cref="System.IO.IOException"></exception> public virtual void TestDocWithAttachment() { string inlineTextString = "Inline text string created by cblite functional test"; Send("PUT", "/db", Status.Created, null); IDictionary <string, object> attachment = new Dictionary <string, object>(); attachment.Put("content_type", "text/plain"); attachment.Put("data", "SW5saW5lIHRleHQgc3RyaW5nIGNyZWF0ZWQgYnkgY2JsaXRlIGZ1bmN0aW9uYWwgdGVzdA==" ); IDictionary <string, object> attachments = new Dictionary <string, object>(); attachments.Put("inline.txt", attachment); IDictionary <string, object> docWithAttachment = new Dictionary <string, object>(); docWithAttachment.Put("_id", "docWithAttachment"); docWithAttachment.Put("text", inlineTextString); docWithAttachment.Put("_attachments", attachments); IDictionary <string, object> result = (IDictionary <string, object>)SendBody("PUT", "/db/docWithAttachment", docWithAttachment, Status.Created, null); IDictionary expChanges = new Dictionary <string, IDictionary <string, object> >(); IList changesResults = new ArrayList(); IDictionary docChanges = new Dictionary <string, object>(); docChanges.Put("id", "docWithAttachment"); docChanges.Put("seq", 1); IList lChanges = new AList <IDictionary <string, object> >(); Hashtable mChanges = new Dictionary <string, object>(); mChanges.Put("rev", result.Get("rev")); lChanges.AddItem(mChanges); docChanges.Put("changes", lChanges); changesResults.AddItem(docChanges); expChanges.Put("results", changesResults); expChanges.Put("last_seq", 1); Send("GET", "/db/_changes?feed=normal&heartbeat=300000&style=all_docs", Status.Ok , expChanges); result = (IDictionary <string, object>)Send("GET", "/db/docWithAttachment", Status .Ok, null); IDictionary <string, object> attachmentsResult = (IDictionary <string, object>)result .Get("_attachments"); IDictionary <string, object> attachmentResult = (IDictionary <string, object>)attachmentsResult .Get("inline.txt"); // there should be either a content_type or content-type field. //https://github.com/couchbase/couchbase-lite-android-core/issues/12 //content_type becomes null for attachments in responses, should be as set in Content-Type string contentTypeField = (string)attachmentResult.Get("content_type"); NUnit.Framework.Assert.IsTrue(attachmentResult.ContainsKey("content_type")); NUnit.Framework.Assert.IsNotNull(contentTypeField); URLConnection conn = SendRequest("GET", "/db/docWithAttachment/inline.txt", null, null); string contentType = conn.GetHeaderField("Content-Type"); NUnit.Framework.Assert.IsNotNull(contentType); NUnit.Framework.Assert.IsTrue(contentType.Contains("text/plain")); StringWriter writer = new StringWriter(); IOUtils.Copy(conn.GetInputStream(), writer, "UTF-8"); string responseString = writer.ToString(); NUnit.Framework.Assert.IsTrue(responseString.Contains(inlineTextString)); }