コード例 #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>
        /// 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
                }));
            }
        }
コード例 #3
0
        /// <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()));
            }
        }
コード例 #4
0
        /// <summary>
        /// Gets all resources available on the Bridge
        /// </summary>
        /// <returns></returns>
        public HttpResponseMessage Get()
        {
            IList <string> resources = ResourceInvoker.DynamicInvokeGetAllResources();
            var            items     = new List <KeyValuePair <string, string> >();

            foreach (var item in resources)
            {
                var name = item.Substring(item.LastIndexOf('.') + 1);
                var uri  = this.Request.RequestUri.GetLeftPart(UriPartial.Path) + "/" + item;
                items.Add(new KeyValuePair <string, string>(name, uri));
            }
            items.Sort((a, b) => string.Compare(a.Key, b.Key));

            string contentString = JsonSerializer.Serialize(items);
            var    response      = BuildJsonContent(contentString);

            return(response);
        }
コード例 #5
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.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 = "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 response = ResourceInvoker.DynamicInvokeGet(resourceName, properties);

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

                    var httpResponse = new HttpResponseMessage(HttpStatusCode.OK);
                    httpResponse.Content = new ByteArrayContent(response.RawResponse);
                    httpResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octect-stream");
                    return(httpResponse);
                }
                else
                {
                    string contentString = JsonSerializer.SerializeDictionary(response.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()));
            }
        }