/// <summary> /// Initialize a test /// </summary> /// <param name="resource"></param> /// <response code="200">Test initialized.</response> public HttpResponseMessage Put(HttpRequestMessage request) { string nameValuePairs = request.Content.ReadAsStringAsync().GetAwaiter().GetResult(); Dictionary <string, string> resourceInfo = JsonSerializer.DeserializeDictionary(nameValuePairs); string resourceName = null; resourceInfo.TryGetValue("name", out resourceName); resource resource = resourceName == null ? null : new resource { name = resourceName }; Trace.WriteLine(String.Format("{0:T} - Received PUT request for resource {1}", DateTime.Now, String.IsNullOrWhiteSpace(resourceName) ? "null" : resourceName), this.GetType().Name); if (String.IsNullOrWhiteSpace(resourceName)) { Trace.WriteLine(String.Format("{0:T} - PUT response is BadRequest due to missing name", DateTime.Now), this.GetType().Name); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "resource data { name:'...' } not specified.")); } var correlationId = Guid.NewGuid(); try { var result = ResourceInvoker.DynamicInvokePut(resource); resourceResponse resourceResponse = new resourceResponse { id = correlationId, details = result.ToString() }; Trace.WriteLine(String.Format("{0:T} - PUT response for {1} is:{2}{3}", DateTime.Now, resourceName, Environment.NewLine, resourceResponse.ToString()), this.GetType().Name); // Directly return a json string to avoid use of MediaTypeFormatters HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new StringContent(resourceResponse.ToString()); response.Content.Headers.ContentType = new MediaTypeHeaderValue(JsonSerializer.JsonMediaType); return(response); } catch (Exception exception) { Trace.WriteLine(String.Format("{0:T} - Exception executing PUT for resource {1}{2}:{3}", DateTime.Now, resourceName, Environment.NewLine, exception.ToString()), this.GetType().Name); return(Request.CreateResponse(HttpStatusCode.InternalServerError, new resourceResponse { id = correlationId, details = exception.Message })); } }
/// <summary> /// Invoke the <c>Put</c> method of the <see cref="IResource"/> /// matching the name specified in the content name/value pairs. /// </summary> /// <param name="request">The incoming request containing the name/value pair content</param> /// <returns>The response to return to the caller.</returns> public HttpResponseMessage Put(string name) { var properties = this.BuildProperties(name); StringBuilder sb = new StringBuilder(); foreach (var pair in properties) { sb.AppendFormat("{0} {1} : {2}", Environment.NewLine, pair.Key, pair.Value); } string resourceName = null; if (!properties.TryGetValue(nameKeyName, out resourceName) || String.IsNullOrWhiteSpace(resourceName)) { string badRequestMessage = "PUT request content did not contain a resource name"; Trace.WriteLine(String.Format("{0:T} - {1}:{2}", DateTime.Now, badRequestMessage, sb.ToString()), this.GetType().Name); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, badRequestMessage)); } Trace.WriteLine(String.Format("{0:T} - PUT request received for resource name = '{1}', properties:{2}", DateTime.Now, String.IsNullOrWhiteSpace(resourceName) ? "null" : resourceName, sb.ToString()), this.GetType().Name); try { ResourceResponse result = ResourceInvoker.DynamicInvokePut(resourceName, properties); string contentString = JsonSerializer.SerializeDictionary(result.Properties); Trace.WriteLine(String.Format("{0:T} - PUT response for {1} is OK:{2}{3}", DateTime.Now, resourceName, Environment.NewLine, contentString), this.GetType().Name); // Directly return a json string to avoid use of MediaTypeFormatters HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new StringContent(contentString); response.Content.Headers.ContentType = new MediaTypeHeaderValue(JsonSerializer.JsonMediaType); return(BuildJsonContent(contentString)); } catch (Exception exception) { Trace.WriteLine(String.Format("{0:T} - Exception executing PUT for resource {1}{2}:{3}", DateTime.Now, resourceName, Environment.NewLine, exception.ToString()), this.GetType().Name); return(Request.CreateResponse(HttpStatusCode.InternalServerError, exception.ToString())); } }