/// <summary> /// Update Dropbox quota for Waveface /// </summary> /// <param name="quota"></param> /// <exception cref="Wammer.Station.Management.DropboxNotConnectedException"> /// Waveface has not connected to Dropbox /// </exception> /// <exception cref="Wammer.Station.Management.DropboxNotInstalledException"> /// Dropbox is not installed /// </exception> public static void UpdateDropbox(long quota) { try { if (!DropboxHelper.IsInstalled()) { throw new DropboxNotInstalledException("Dropbox is not installed"); } CloudResponse res = CloudServer.request <CloudResponse>( new WebClient(), StationMgmtURL + "cloudstorage/dropbox/update", new Dictionary <object, object> { { "quota", quota } }, true ); } catch (Cloud.WammerCloudException e) { ExtractApiRetMsg(e); throw; } }
public static void RespondFailure(HttpListenerResponse response, Exception e, int status) { CloudResponse json = new CloudResponse(status, DateTime.Now.ToUniversalTime(), -1, e.Message); RespondFailure(response, json); }
/// <summary> /// Connect Waveface to Dropbox /// </summary> /// <param name="quota"></param> /// <exception cref="Wammer.Station.Management.DropboxNoSyncFolderException"> /// Dropbox sync folder does not exist /// </exception> /// <exception cref="Wammer.Station.Management.WrongAccountException"> /// Link to inconsistent Dropbox account /// </exception> /// <exception cref="Wammer.Station.Management.DropboxNotInstalledException"> /// Dropbox is not installed /// </exception> public static void ConnectDropbox(long quota) { try { if (!DropboxHelper.IsInstalled()) { throw new DropboxNotInstalledException("Dropbox is not installed"); } string folder = DropboxHelper.GetSyncFolder(); CloudResponse res = CloudServer.request <CloudResponse>( new WebClient(), StationMgmtURL + "cloudstorage/dropbox/connect", new Dictionary <object, object> { { "quota", quota }, { "folder", folder } }, true ); } catch (Cloud.WammerCloudException e) { ExtractApiRetMsg(e); throw; } }
public void TestBypassException_pathWithQueryString() { using (HttpServer proxyServer = new HttpServer(80)) { proxyServer.AddHandler("/", new MyHandler("dummy")); // dummy BypassHttpHandler bypasser = new BypassHttpHandler("http://localhost:8080"); bypasser.AddExceptPrefix("/bypass/"); proxyServer.AddDefaultHandler(bypasser); proxyServer.Start(); WebClient agent = new WebClient(); try { agent.DownloadData("http://localhost:80/bypass?abc=123"); } catch (WebException e) { Assert.AreEqual(WebExceptionStatus.ProtocolError, e.Status); using (StreamReader r = new StreamReader(e.Response.GetResponseStream())) { string json = r.ReadToEnd(); CloudResponse resp = fastJSON.JSON.Instance.ToObject <CloudResponse>(json); Assert.AreEqual(-1, resp.api_ret_code); Assert.AreEqual(403, resp.status); Assert.AreEqual("Station does not support this REST API; only Cloud does", resp.api_ret_message); } return; } Assert.Fail("Expected exception is not thrown"); } }
public void TestGetAttachmentInfo_NoObjectId() { WebClient agent = new WebClient(); try { string output = agent.DownloadString("http://localhost:8080/api/get"); } catch (WebException e) { HttpWebResponse res = (HttpWebResponse)e.Response; Assert.AreEqual(HttpStatusCode.BadRequest, res.StatusCode); using (StreamReader r = new StreamReader(res.GetResponseStream())) { CloudResponse json = fastJSON.JSON.Instance.ToObject <CloudResponse>( r.ReadToEnd()); Assert.AreEqual(-1, json.api_ret_code); Assert.AreEqual((int)HttpStatusCode.BadRequest, json.status); Assert.AreEqual("missing parameter: object_id", json.api_ret_message); } return; } Assert.Fail("expected error is not thrown"); }
private void OnCloud_CloudResponse(Datagram datagram) { CloudResponse response = datagram.UnSerialData <CloudResponse>(); if (RunningDatas.RequestTable.TryGetValue(datagram.RequestID, out RequestSender value)) { value.RequestCallback(response); } }
public static void RespondFailure(HttpListenerResponse response, WammerStationException e, int status) { CloudResponse json = null; if (e.ErrorResponse != null) { json = e.ErrorResponse; } else { json = new CloudResponse(status, DateTime.Now.ToUniversalTime(), e.WammerError, e.Message); } RespondFailure(response, json); }
/// <summary> /// Disconnect Waveface from Dropbox /// </summary> public static void DisconnectDropbox() { try { CloudResponse res = CloudServer.request <CloudResponse>( new WebClient(), StationMgmtURL + "cloudstorage/dropbox/disconnect", new Dictionary <object, object>(), true ); } catch (Cloud.WammerCloudException e) { ExtractApiRetMsg(e); throw; } }
public static void RespondFailure(HttpListenerResponse response, CloudResponse json) { try { string resText = json.ToFastJSON(); response.StatusCode = json.status; response.ContentType = "application/json"; using (StreamWriter w = new StreamWriter(response.OutputStream)) { w.Write(resText); } } catch (Exception ex) { logger.Error("Unable to respond failure", ex); } }
internal DeviceInfo(CloudResponse response) : this(ResponseType.Cloud) { ServerVersion = response.ApiVersion; WurflLastUpdate = _baseDate.AddSeconds(response.MTime); Id = response.Id; foreach (var c in response.Capabilities) { Capabilities.Add(c.Key, c.Value); string value; if (!Capabilities.TryGetValue(c.Key, out value)) { Errors.Add("Exception", String.Format("The {0} capability is not currently available", c.Key)); } } foreach (var e in response.Errors) { Errors.Add(e.Key, e.Value); } }
private void OnCloudRequest(Datagram datagram) { CloudRequest request = datagram.UnSerialData <CloudRequest>(); CloudResponse response = new CloudResponse(); response.Files.AddRange ( InfoReader.ReadAll ( IKXTServer.DataConvert.GetString(datagram.Sender), request.Path ) ); datagram.DataType = DatagramType.Client; datagram.MessageType = CloudDatagramDefine.CloudResponse; byte[][] buffer = response.ToByteArrays(); for (int i = 0; i < buffer.Length; ++i) { datagram.Datas = buffer[i]; if (!Send(datagram.ToByteArray())) { Notify(IKXTServer.LogLevel.Error, "数据发送异常"); Close(); return; } } datagram.MessageType = CloudDatagramDefine.CloudResFinish; datagram.Datas = null; if (!Send(datagram.ToByteArray())) { Notify(IKXTServer.LogLevel.Error, "数据发送异常"); Close(); } }
public void HandleRequest(HttpListenerRequest origReq, HttpListenerResponse response) { logger.Debug("Forward to cloud: " + origReq.Url.AbsolutePath); try { if (HasNotAllowedPrefix(origReq.Url.AbsolutePath)) { CloudResponse json = new CloudResponse(403, -1, "Station does not support this REST API; only Cloud does"); HttpHelper.RespondFailure(response, json); } Uri targetUri = GetTargetUri(origReq); HttpWebRequest newReq = (HttpWebRequest)WebRequest.Create(targetUri); CopyRequestHeaders(origReq, newReq); if (origReq.HasEntityBody) { newReq.BeginGetRequestStream(RequestStreamGotten, new BypassContext(origReq, response, newReq)); } else { newReq.BeginGetResponse(ResponseGotten, new BypassContext(origReq, response, newReq)); } } catch (WebException e) { logger.Error("forward to cloud error", e); ReplyCloudError(response, e); } catch (Exception e) { logger.Error("Forward to cloud error", e); HttpHelper.RespondFailure(response, e, 400); } }
public void TestView_NoObjectID() { using (HttpServer server = new HttpServer(80)) { server.AddHandler("/v1/objects/view", new AttachmentViewHandler("sid")); server.Start(); HttpWebRequest req = (HttpWebRequest)WebRequest.Create( "http://localhost/v1/objects/view"); req.Method = "GET"; try { req.GetResponse(); } catch (WebException e) { Assert.AreEqual(WebExceptionStatus.ProtocolError, e.Status); HttpWebResponse response = (HttpWebResponse)e.Response; Assert.AreEqual("application/json", response.ContentType); using (StreamReader reader = new StreamReader(response.GetResponseStream())) { string responseText = reader.ReadToEnd(); CloudResponse res = fastJSON.JSON.Instance.ToObject <CloudResponse>(responseText); Assert.AreEqual(400, res.status); Assert.AreEqual(-1, res.api_ret_code); Assert.AreEqual("missing required param: object_id", res.api_ret_message); } return; } Assert.Fail("Expected failure is not thrown"); } }