public bool Validate(string nameOrID) { Guid simulationId = Guid.Empty; bool isGuid = Guid.TryParse(nameOrID, out simulationId); bool all = SimulationQueryParameters.GetRecurse(false); ISimulationProducerContract contract = null; try { contract = ProducerSimulationContract.Get(nameOrID); } catch (Exception ex) { string msg = String.Format("Failed to Get Simulation {0}: {1} {2}", nameOrID, ex.Message, ex.StackTrace.ToString()); if (ex.InnerException != null) { msg += String.Format(" InnerException: {0} {1}", ex.InnerException.Message, ex.InnerException.StackTrace.ToString()); } Debug.WriteLine(msg, this.GetType().Name); var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, msg ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.InternalServerError); } return(isGuid ? contract.Validate() : contract.ValidateAll()); }
/// <summary> /// POST: Creates a new simulation /// </summary> /// <param name="appname">Application name</param> /// <returns>URI of New simulation</returns> public string CreateSimulation(string appname) { string name = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters["simulation"]; if (string.IsNullOrEmpty(name)) { var detail = new ErrorDetail( ErrorDetail.Codes.QUERYPARAM_ERROR, "Required parameter \"simulation\": Provide unique name for factory method." ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } ISimulationProducerContract contract = null; try { contract = ProducerSimulationContract.Create(name, name, appname); } catch (Exception ex) { string msg = String.Format("Failed to Create Simulation {0}: {1} {2}", name, ex.Message, ex.StackTrace.ToString()); Debug.WriteLine(msg, this.GetType().Name); var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, msg ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.InternalServerError); } var uri = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri; return(new Uri(uri, String.Format("../simulation/{0}", name)).ToString()); }
/* * * /// <summary> * /// * /// </summary> * /// <param name="sessionID"></param> * /// <returns></returns> * public int KillSession(string sessionID) * { * ISessionProducerContract contract = new AspenSessionProducerContract(); * int total = 0; * * try * { * total += contract.Cancel(new Guid(sessionID)); * total += contract.Terminate(new Guid(sessionID)); * } * catch (AuthorizationError ex) * { * var detail = new ErrorDetail( * ErrorDetail.Codes.AUTHORIZATION_ERROR, * ex.Message * ); * throw new WebFaultException<ErrorDetail>(detail, System.Net.HttpStatusCode.Unauthorized); * } * return total; * } */ /// <summary> /// Append new Jobs to session, in 'Create' state. /// </summary> /// <param name="sessionID"></param> /// <param name="data">list of job requests [ {"Simulation":.., "Input":..., "Initialize":..., "Reset":..., } ]</param> /// <returns></returns> public List <int> AppendJobs(string sessionID, System.IO.Stream data) { Debug.WriteLine(String.Format("append jobs to session {0}", sessionID), this.GetType().Name); Object name = ""; Object inputs = ""; Object initialize = false; Object reset = true; Object visible = false; Object jobId = ""; Object consumerId = ""; StreamReader sr = new StreamReader(data); string json = sr.ReadToEnd(); List <Dictionary <string, Object> > jobsInputList = Newtonsoft.Json.JsonConvert.DeserializeObject <List <Dictionary <string, Object> > >(json); List <Dictionary <String, Object> > jobDictList = new List <Dictionary <string, object> >(); List <string> simulationList = new List <string>(); List <bool> initializeList = new List <bool>(); List <bool> resetList = new List <bool>(); List <bool> visibleList = new List <bool>(); List <Guid> jobIdList = new List <Guid>(); List <Guid> consumerIdList = new List <Guid>(); foreach (var inputData in jobsInputList) { if (!inputData.TryGetValue("Simulation", out name)) { Debug.WriteLine("Missing simulation name", this.GetType().Name); var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, "Key Simulation is required" ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } if (!inputData.TryGetValue("Input", out inputs)) { Debug.WriteLine("Missing input data", this.GetType().Name); throw new ArgumentException("No Input data name"); } if (!inputData.TryGetValue("Initialize", out initialize)) { initialize = false; } if (!inputData.TryGetValue("Reset", out reset)) { reset = true; } if (!inputData.TryGetValue("Visible", out visible)) { visible = false; } if (inputData.TryGetValue("Guid", out jobId)) { try { jobIdList.Add(Guid.Parse((string)jobId)); } catch (InvalidCastException) { var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, "Key jobId is required to be a string/GUID" ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } catch (FormatException) { var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, String.Format("Key jobId {0} is required to be a formatted as a GUID", jobId) ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } } else { jobIdList.Add(Guid.Empty); } if (inputData.TryGetValue("ConsumerId", out consumerId)) { try { consumerIdList.Add(Guid.Parse((string)consumerId)); } catch (InvalidCastException) { var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, "Key ConsumerId is required to be a string/GUID" ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } catch (FormatException) { var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, String.Format("Key ConsumerId {0} is required to be a formatted as a GUID", consumerId) ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } } else { consumerIdList.Add(Guid.Empty); } Newtonsoft.Json.Linq.JObject inputsD = null; try { inputsD = (Newtonsoft.Json.Linq.JObject)inputs; } catch (InvalidCastException) { var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, "Key Input is required to be a dictionary" ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } Dictionary <String, Object> dd = new Dictionary <string, object>(); foreach (var v in inputsD) { //Debug.WriteLine("VALUE: " + v); dd[v.Key] = v.Value; } jobDictList.Add(dd); simulationList.Add(String.Format("{0}", name)); try { Debug.WriteLine("initialize String: " + initialize.ToString(), "SessionResource.AppendJobs"); Debug.WriteLine("initialize Type: " + initialize.GetType().ToString(), "SessionResource.AppendJobs"); initializeList.Add(Convert.ToBoolean(initialize)); } catch (InvalidCastException e) { Debug.WriteLine("Error: " + e.Message, "SessionResource.AppendJobs"); Debug.WriteLine("StackTrace: " + e.StackTrace, "SessionResource.AppendJobs"); var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, "Key initialize is required to be a boolean" ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } try { resetList.Add(Convert.ToBoolean(reset)); } catch (InvalidCastException) { var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, "Key Reset is required to be a boolean" ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } try { visibleList.Add(Convert.ToBoolean(visible)); } catch (InvalidCastException) { var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, "Key Visible is required to be a boolean" ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } } List <int> jobList = new List <int>(); // Formatting complete for (int i = 0; i < jobDictList.Count; i++) { ISimulationProducerContract contract = null; string simulationName = simulationList[i]; Dictionary <String, Object> dd = jobDictList[i]; try { contract = ProducerSimulationContract.Get(simulationName); } catch (Exception ex) { string msg = String.Format("Failed to get Simulation {0}: {1}", name, ex.StackTrace.ToString()); Debug.WriteLine(msg, this.GetType().Name); var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, msg ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } if (contract == null) { string msg = String.Format("Failed to create new job {0}: No Simulation {1}", i, simulationName); var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, msg ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.NotFound); } IJobProducerContract jobContract; try { jobContract = contract.NewJob(new Guid(sessionID), initializeList[i], resetList[i], visibleList[i], jobIdList[i], consumerIdList[i]); jobContract.Process.Input = dd; } catch (Exception ex) { string msg = String.Format("Failed to create new job {0}: {1} {2}", i, ex, ex.StackTrace.ToString()); Debug.WriteLine(msg, this.GetType().Name); var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, msg ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } jobList.Add(jobContract.Id); } return(jobList); }
/// <summary> /// PUT: Updates staged input file /// </summary> /// <param name="name">Simulation name</param> /// <param name="inputName">input file name</param> /// <param name="data">new contents</param> /// <returns>Boolean success</returns> public bool UpdateStagedInputFile(string nameOrID, string inputName, Stream data) { Debug.WriteLine("UpdateStagedInputFile: " + nameOrID, this.GetType().Name); if (data == null) { string msg = String.Format("Simulation Resource {0} Data with input '{1}' is empty", nameOrID, inputName); Debug.WriteLine(msg, this.GetType().Name); var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, msg ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest); } //Simulation sim = GetSimulation(name); //OutgoingWebResponseContext ctx = WebOperationContext.Current.OutgoingResponse; //var content_type = WebOperationContext.Current.IncomingRequest.ContentType; var content_type = this.OutgoingWebResponseContext_ContentType; byte[] bytes; using (var ms = new MemoryStream()) { data.CopyTo(ms); bytes = ms.ToArray(); } //StreamReader sr = new StreamReader(data); //string s = sr.ReadToEnd(); //byte[] bytes = System.Text.Encoding.ASCII.GetBytes(s); Debug.WriteLine("content_type " + content_type, this.GetType().Name); // TODO: Validate Backup File var contract = ProducerSimulationContract.Get(nameOrID); if (contract == null) { string msg = String.Format("Simulation Resource {0} Does not Exist", nameOrID); Debug.WriteLine(msg, this.GetType().Name); var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, msg ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.NotFound); } bool success = false; try { success = contract.UpdateInput(inputName, bytes, content_type); } catch (ArgumentException ex) { string msg = String.Format("Failed to Update Simulation {0} input {1}: {2}", nameOrID, inputName, ex.Message); Debug.WriteLine(msg, this.GetType().Name); var detail = new ErrorDetail( ErrorDetail.Codes.AUTHORIZATION_ERROR, msg ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.Unauthorized); } catch (Exception ex) { string msg = String.Format("Failed to Update Simulation {0} input {1}: {2}, {3}. ", nameOrID, inputName, ex.Message, ex.StackTrace); Debug.WriteLine(msg, this.GetType().Name); if (ex.InnerException != null) { Debug.WriteLine("InnerException: " + ex.InnerException.Message, this.GetType().Name); Debug.WriteLine(ex.InnerException.StackTrace, this.GetType().Name); msg += "InnerException: " + ex.InnerException.Message; msg += " StackTrace: " + ex.InnerException.StackTrace; } var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, msg ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.InternalServerError); } Debug.WriteLine("Success: " + success, this.GetType().Name); //sim = GetSimulation(name); //ctx.ContentType = content_type; //ctx.StatusCode = System.Net.HttpStatusCode.OK; this.OutgoingWebResponseContext_ContentType = content_type; this.OutgoingWebResponseContext_StatusCode = System.Net.HttpStatusCode.OK; //return new MemoryStream(bytes); return(success); }
/// <summary> /// PUT: Update Simulation /// </summary> /// <param name="name">Simulation name</param> /// <param name="sim">Simulation Resource Representation</param> /// <returns>Updated Simulation</returns> public Simulation UpdateSimulation(string nameOrID, Simulation sim) { Guid simulationId = Guid.Empty; bool isGuid = Guid.TryParse(nameOrID, out simulationId); ISimulationProducerContract contract = null; if (sim == null || sim.Name == null) { Debug.WriteLine(String.Format("Sim Name: {0}, ID: {1}, app: {2}", sim.Name, sim.Id.ToString(), sim.Application) , this.GetType().Name); string msg = String.Format("No Application specified in Simulation {0}", nameOrID); Debug.WriteLine(msg, this.GetType().Name); var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, msg ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.InternalServerError); } if (!isGuid && !nameOrID.Equals(sim.Name)) { string msg = String.Format("No URL Resource Name {0} doesn't match Simulation.Name {1}", nameOrID, sim.Name); Debug.WriteLine(msg, this.GetType().Name); var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, msg ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.InternalServerError); } string appname = sim.Application; try { contract = ProducerSimulationContract.Create(nameOrID, sim.Name, appname); } catch (Exception ex) { string msg = String.Format("Failed to Create Simulation {0}: {1} {2}", nameOrID, ex.Message, ex.StackTrace.ToString()); if (ex.InnerException != null) { msg += String.Format(" InnerException: {0} {1}", ex.InnerException.Message, ex.InnerException.StackTrace.ToString()); } Debug.WriteLine(msg, this.GetType().Name); var detail = new ErrorDetail( ErrorDetail.Codes.DATAFORMAT_ERROR, msg ); throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.InternalServerError); } sim = DataMarshal.GetSimulation(nameOrID, isGuid); return(sim); }