예제 #1
0
        /// <summary>
        /// Gets resource information
        /// </summary>
        /// <param name="name">name of the resource</param>
        public HttpResponseMessage Get(string name)
        {
            Trace.WriteLine(String.Format("{0:T} - Received GET request for resource {1}",
                                          DateTime.Now, String.IsNullOrWhiteSpace(name) ? "null" : name),
                            this.GetType().Name);

            if (string.IsNullOrWhiteSpace(name))
            {
                Trace.WriteLine(String.Format("{0:T} - GET response is BadRequest due to missing name", DateTime.Now),
                                this.GetType().Name);
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "name not specified."));
            }

            try
            {
                var result = ResourceInvoker.DynamicInvokeGet(name);

                Trace.WriteLine(String.Format("{0:T} - GET response for resource {1} is:{2}{3}",
                                              DateTime.Now, name, Environment.NewLine, result),
                                this.GetType().Name);

                return(Request.CreateResponse(HttpStatusCode.OK, result));
            }
            catch (Exception exception)
            {
                Trace.WriteLine(String.Format("{0:T} - Exception executing GET for resource {1}{2}:{3}",
                                              DateTime.Now, name, Environment.NewLine, exception.ToString()),
                                this.GetType().Name);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, new resourceResponse
                {
                    id = Guid.NewGuid(),
                    details = exception.Message
                }));
            }
        }
예제 #2
0
        /// <summary>
        /// Invoke the <c>Get</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 Get(string name)
        {
            var properties = this.BuildProperites(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("name", out resourceName) || String.IsNullOrWhiteSpace(resourceName))
            {
                string badRequestMessage = "GET 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} - GET request received for resource name = '{1}', properties:{2}",
                                          DateTime.Now,
                                          String.IsNullOrWhiteSpace(resourceName) ? "null" : resourceName,
                                          sb.ToString()),
                            this.GetType().Name);

            try
            {
                ResourceResponse result        = ResourceInvoker.DynamicInvokeGet(resourceName, properties);
                string           contentString = JsonSerializer.SerializeDictionary(result.Properties);

                Trace.WriteLine(String.Format("{0:T} - GET response for {1} is OK:{2}{3}",
                                              DateTime.Now, resourceName, Environment.NewLine, contentString),
                                this.GetType().Name);

                return(BuildJsonContent(contentString));
            }
            catch (Exception exception)
            {
                Trace.WriteLine(String.Format("{0:T} - Exception executing GET for resource {1}{2}:{3}",
                                              DateTime.Now, resourceName, Environment.NewLine, exception.ToString()),
                                this.GetType().Name);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, exception.ToString()));
            }
        }