Esempio n. 1
0
        public static object DynamicInvokePut(resource resource)
        {
            if (String.IsNullOrEmpty(resource.name))
            {
                throw new ArgumentNullException("resource.name");
            }

            // Disallow concurrent resource instantation or configuration changes
            lock (ConfigController.BridgeLock)
            {
                AppDomain appDomain;
                if (String.IsNullOrWhiteSpace(ConfigController.CurrentAppDomainName))
                {
                    throw new InvalidOperationException("The Bridge resource folder has not been configured.");
                }
                if (!TypeCache.AppDomains.TryGetValue(ConfigController.CurrentAppDomainName, out appDomain))
                {
                    throw new ArgumentException("Resource not found", "resource");
                }

                Type loaderType = typeof(AssemblyLoader);
                var  loader     =
                    (AssemblyLoader)appDomain.CreateInstanceFromAndUnwrap(
                        loaderType.Assembly.Location,
                        loaderType.FullName);

                ResourceRequestContext context = new ResourceRequestContext
                {
                    BridgeConfiguration = ConfigController.BridgeConfiguration
                };
                return(loader.IResourceCall(resource.name, "Put", new object[] { context }));
            }
        }
Esempio n. 2
0
        public static object DynamicInvokePut(resource resource)
        {
            if (String.IsNullOrEmpty(resource.name))
            {
                throw new ArgumentNullException("resource.name");
            }

            // Disallow concurrent resource instantation or configuration changes
            lock (ConfigController.BridgeLock)
            {
                AppDomain appDomain;
                if (String.IsNullOrWhiteSpace(ConfigController.CurrentAppDomainName))
                {
                    throw new InvalidOperationException("The Bridge resource folder has not been configured.");
                }
                if (!TypeCache.AppDomains.TryGetValue(ConfigController.CurrentAppDomainName, out appDomain))
                {
                    throw new ArgumentException("Resource not found", "resource");
                }

                Type loaderType = typeof(AssemblyLoader);
                var loader =
                    (AssemblyLoader)appDomain.CreateInstanceFromAndUnwrap(
                        loaderType.Assembly.Location,
                        loaderType.FullName);

                ResourceRequestContext context = new ResourceRequestContext
                {
                    BridgeConfiguration = ConfigController.BridgeConfiguration
                };
                return loader.IResourceCall(resource.name, "Put", new object[] { context });
            }
        }
Esempio n. 3
0
        public static object DynamicInvokePut(resource resource)
        {
            if (String.IsNullOrEmpty(resource.name))
            {
                throw new ArgumentNullException("resource.name");
            }

            AppDomain appDomain;
            if (!TypeCache.AppDomains.TryGetValue(ConfigController.CurrentAppDomainName, out appDomain))
            {
                throw new ArgumentException("Resource not found");
            }

            Type loaderType = typeof(AssemblyLoader);
            var loader =
                (AssemblyLoader)appDomain.CreateInstanceFromAndUnwrap(
                    loaderType.Assembly.Location,
                    loaderType.FullName);

            ResourceRequestContext context = new ResourceRequestContext
            {
                BridgeConfiguration = ConfigController.BridgeConfiguration
            };
            return loader.IResourceCall(resource.name, "Put", new object[] { context });
        }
Esempio n. 4
0
        /// <summary>
        /// Initialize a test
        /// </summary>
        /// <param name="resource"></param>
        /// <response code="200">Test initialized.</response>
        public HttpResponseMessage Put(resource resource)
        {
            if (resource == null)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "resource data { name:'...' } not specified.");
            }

            var correlationId = Guid.NewGuid();

            try
            {
                Debug.WriteLine("Received request to create resource \n" + resource);

                var result = ResourceInvoker.DynamicInvokePut(resource);

                return Request.CreateResponse(HttpStatusCode.OK, new resourceResponse
                {
                    id = correlationId,
                    details = result.ToString()
                });
            }
            catch (Exception exception)
            {
                Debug.WriteLine("Exception when trying to create resource : " + resource.name + " : " + exception.Message);
                return Request.CreateResponse(HttpStatusCode.InternalServerError, new resourceResponse
                {
                    id = correlationId,
                    details = exception.Message
                });
            }
        }
Esempio n. 5
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
                }));
            }
        }