public static JSONResponse Status(JSONRequest request) { JSONResponse response = new JSONResponse(); response.Body = new Dictionary<string, object>(); response.Body["running"] = true; Dictionary<string,object> server = new Dictionary<string,object>(); server["name"] = "Twin"; server["version"] = Assembly.GetExecutingAssembly().GetName().Version.ToString(); response.Body["server"] = server; SessionFactory factory = GetFactory(request); List<Dictionary<string, object>> configurations = new List<Dictionary<string, object>>(); foreach(Configuration config in factory.Configurations) { Dictionary<string, object> serial = new Dictionary<string, object>(); serial["id"] = config.identifier; serial["capabilities"] = config.Capabilities; serial["path"] = config.appPath; if (config.arguments != null) serial["arguments"] = config.arguments; configurations.Add(serial); } response.Body["configurations"] = configurations; List<Dictionary<string, object>> sessions = new List<Dictionary<string, object>>(); foreach (KeyValuePair<Guid, Session> session in factory.GetSessions()) { Dictionary<string, object> serial = new Dictionary<string, object>(); serial["id"] = session.Key.ToString(); serial["configuration"] = session.Value.Configuration.identifier; serial["processId"] = session.Value.Process.Id; sessions.Add(serial); } response.Body["sessions"] = sessions; return response; }
JSONResponse GetExceptionResponse(Exception e) { Logger.Current.Trace("Building response for thrown exception"); Logger.Current.Trace(e); JSONResponse response = new JSONResponse(); response.StatusCode = 500; // TODO: should this be here? breaks layering ResponseStatus responseCode = ResponseStatus.UnknownError; // unknown if (e is TwinException) { responseCode = ((TwinException)e).ResponseStatus; } if (e is ElementNotAvailableException) { responseCode = ResponseStatus.NoSuchElement; } Dictionary <string, object> wrapper = new Dictionary <string, object>(); wrapper["status"] = (int)responseCode; wrapper["value"] = EncodeException(e); response.Body = wrapper; return(response); }
public static JSONResponse Create(JSONRequest request) { if(!request.Body.ContainsKey("desiredCapabilities")) throw new ArgumentException("desiredCapabilities not provided"); Dictionary<string, object> sessionSetupInfo = request.Body.ContainsKey("sessionSetup") ? (Dictionary<string, object>)request.Body["sessionSetup"] : null; Session session = GetFactory(request).Create(flatten((Dictionary<string,object>)request.Body["desiredCapabilities"]), sessionSetupInfo); JSONResponse response = new JSONResponse(); response.StatusCode = 303; response.Location = "/session/" + session.ToString(); return response; }
public override void Respond(ParsedRequest request) { JSONRequest jreq = new JSONRequest(request); JSONResponse jres = null; try { jres = Respond(jreq); } catch (Exception e) { if (e is HttpException) { throw e; } jres = GetExceptionResponse(e); } WriteJSONResponse(jres, request.Request.Response); }
public override JSONResponse Respond(JSONRequest request) { SessionRequest sessionRequest = new SessionRequest(request); object value = Respond(sessionRequest); if (value is JSONResponse) { return((JSONResponse)value); } JSONResponse response = new JSONResponse(); response.Body = new Dictionary <string, object>(); response.Body["sessionId"] = sessionRequest.Session.ToString(); response.Body["status"] = (int)ResponseStatus.Success; response.Body["value"] = value; return(response); }
void WriteJSONResponse(JSONResponse response, IResponse http) { http.StatusCode = response.StatusCode; if (response.Location != null) { http.Headers["Location"] = http.URL(response.Location); } if (response.Options != null) { http.Headers["Allow"] = string.Join(",", response.Options); } if (response.Body != null) { using (TextWriter writer = http.OpenWriter("application/json")) { // Ideally we'd stream the object for perf reasons. // however during dev, if the serialiser hits an unrecognised object we want the stacktrace to be sent to the client // this can't happen if data has already been written. So for now, convert to a string in memory, then write when done. // JSON.Write(response.Body, writer); writer.Write(JSON.ToString(response.Body, 4)); } } }
public static object GetValueOptions(ElementRequest request) { JSONResponse response = new JSONResponse(); if (request.Target.IsValueReadOnly()) { response.Options = new string[] { "GET", "POST", "OPTIONS" }; } else { response.Options = new string[] { "GET", "OPTIONS" }; } return response; }
public static JSONResponse CreateResponse(Session session, ResponseStatus status, object value) { JSONResponse response = new JSONResponse(); response.Body = new Dictionary<string, object>(); response.Body["sessionId"] = session == null ? null : session.ToString(); response.Body["status"] = (int)status; response.Body["value"] = value; return response; }
public override JSONResponse Respond(JSONRequest request) { SessionRequest sessionRequest = new SessionRequest(request); object value = Respond(sessionRequest); if (value is JSONResponse) return (JSONResponse)value; JSONResponse response = new JSONResponse(); response.Body = new Dictionary<string, object>(); response.Body["sessionId"] = sessionRequest.Session.ToString(); response.Body["status"] = (int)ResponseStatus.Success; response.Body["value"] = value; return response; }
void WriteJSONResponse(JSONResponse response, IResponse http) { http.StatusCode = response.StatusCode; if (response.Location != null) http.Headers["Location"] = http.URL(response.Location); if (response.Options != null) http.Headers["Allow"] = string.Join(",", response.Options); if(response.Body != null) using(TextWriter writer = http.OpenWriter("application/json")) { // Ideally we'd stream the object for perf reasons. // however during dev, if the serialiser hits an unrecognised object we want the stacktrace to be sent to the client // this can't happen if data has already been written. So for now, convert to a string in memory, then write when done. // JSON.Write(response.Body, writer); writer.Write(JSON.ToString(response.Body, 4)); } }
JSONResponse GetExceptionResponse(Exception e) { Logger.Current.Trace("Building response for thrown exception"); Logger.Current.Trace(e); JSONResponse response = new JSONResponse(); response.StatusCode = 500; // TODO: should this be here? breaks layering ResponseStatus responseCode = ResponseStatus.UnknownError; // unknown if (e is TwinException) responseCode = ((TwinException)e).ResponseStatus; if (e is ElementNotAvailableException) responseCode = ResponseStatus.NoSuchElement; Dictionary<string, object> wrapper = new Dictionary<string, object>(); wrapper["status"] = (int)responseCode; wrapper["value"] = EncodeException(e); response.Body = wrapper; return response; }