Ejemplo n.º 1
0
        /// <summary>
        /// For webapplications always
        /// <ul>
        /// <li>convert IResources using the current context.</li>
        /// <li>use "web" as default resource protocol</li>
        /// <li>use <see cref="HybridContextStorage"/> as default threading storage</li>
        /// </ul>
        /// </summary>
        static WebSupportModule()
        {
            s_log = LogManager.GetLogger(typeof(WebSupportModule));
#if NET_2_0
            // required to enable accessing HttpContext.Request during IHttpModule.Init() in integrated mode
            ContextHideRequestResponse = null;
            try
            {
                fiHideRequestResponse = typeof(HttpContext).GetField("HideRequestResponse", BindingFlags.Instance | BindingFlags.NonPublic);
//                fiHideRequestResponse.SetValue(HttpContext.Current, false);
                ContextHideRequestResponse = (fiHideRequestResponse != null)?new SafeField(fiHideRequestResponse):null;
            }
            catch (SecurityException sec)
            {
                s_log.Warn(string.Format("failed reflecting field HttpContext.HideRequestResponse due to security restrictions {0}", sec));
            }
#endif

            // register additional resource handler
            ResourceHandlerRegistry.RegisterResourceHandler(WebUtils.DEFAULT_RESOURCE_PROTOCOL, typeof(WebResource));
            // replace default IResource converter
            TypeConverterRegistry.RegisterConverter(typeof(IResource),
                                                    new ResourceConverter(
                                                        new ConfigurableResourceLoader(WebUtils.DEFAULT_RESOURCE_PROTOCOL)));
            // default to hybrid thread storage implementation
            LogicalThreadContext.SetStorage(new HybridContextStorage());

            s_log.Debug("Set default resource protocol to 'web' and installed HttpContext-aware HybridContextStorage");
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Registers custom IResource implementations.  The supplied
 /// <paramref name="factory"/> is not used since IResourse implementations
 /// are registered with a global <see cref="Spring.Core.IO.ResourceHandlerRegistry"/>
 /// </summary>
 /// <param name="factory">
 /// The object factory.
 /// </param>
 /// <exception cref="Spring.Objects.ObjectsException">
 /// In case of errors.
 /// </exception>
 public override void PostProcessObjectFactory(
     IConfigurableListableObjectFactory factory)
 {
     if (resourceHandlers != null)
     {
         foreach (DictionaryEntry entry in resourceHandlers)
         {
             string protocolName = entry.Key.ToString();
             Type   type         = ResolveRequiredType(entry.Value, "value", "custom IResource implementation");
             ResourceHandlerRegistry.RegisterResourceHandler(protocolName, type);
         }
     }
 }
        /// <summary>
        /// Registers resource handlers that are specified in
        /// the <c>resources</c> config section with the <see cref="ResourceHandlerRegistry"/>.
        /// </summary>
        /// <param name="parent">
        /// The configuration settings in a corresponding parent
        /// configuration section. Ignored.
        /// </param>
        /// <param name="configContext">
        /// The configuration context when called from the ASP.NET
        /// configuration system. Otherwise, this parameter is reserved and
        /// is <see langword="null"/>.
        /// </param>
        /// <param name="section">
        /// The <see cref="System.Xml.XmlNode"/> for the section.
        /// </param>
        /// <returns>
        /// This method always returns <c>null</c>, because resource handlers are registered
        /// as a sideffect of its execution and there is no need to return anything.
        /// </returns>
        public object Create(object parent, object configContext, XmlNode section)
        {
            if (section != null)
            {
                XmlNodeList resourceHandlers = ((XmlElement)section).GetElementsByTagName(HandlerElement);

                foreach (XmlElement handler in resourceHandlers)
                {
                    string protocolName = GetRequiredAttributeValue(handler, ProtocolAttribute, section);
                    string typeName     = GetRequiredAttributeValue(handler, TypeAttribute, section);

                    ResourceHandlerRegistry.RegisterResourceHandler(protocolName, typeName);
                }
            }
            return(null);
        }
        private void CreateConfigurerAndTestNewProtcol(IDictionary resourceHandlers)
        {
            ResourceHandlerConfigurer resourceHandlerConfiguer = new ResourceHandlerConfigurer();

            resourceHandlerConfiguer.ResourceHandlers = resourceHandlers;

            resourceHandlerConfiguer.Order = 1;


            resourceHandlerConfiguer.PostProcessObjectFactory((IConfigurableListableObjectFactory)mocks.DynamicMock(typeof(IConfigurableListableObjectFactory)));

            //todo investigate mocking the typeregistry, for now ask the actual one for information.
            Assert.IsTrue(ResourceHandlerRegistry.IsHandlerRegistered("httpsss"),
                          "ResourceHandlerConfigurer did not register a protocol handler with the ResourceHandlerRegistry");

            Assert.IsTrue(ResourceHandlerRegistry.IsHandlerRegistered("httpsss"), "Custom IResource not registered.");
            Assert.AreEqual(1, resourceHandlerConfiguer.Order);
        }
Ejemplo n.º 5
0
 public ResourceRequestHandler(ResourceHandlerRegistry <CefResourceHandler> resourceHandlerRegistry, BridgeObjectRegistry bridgeObjectRegistry, IResourceRequestHandler?resourceRequestHandler)
 {
     this.logic = new ResourceRequestHandlerLogic <CefRequest, CefResponse, CefResourceHandler>(CefRequestAdapter.Instance, CefResponseAdapter.Instance, resourceHandlerRegistry, resourceRequestHandler);
     this.bridgeObjectRegistry = bridgeObjectRegistry;
 }
Ejemplo n.º 6
0
 public CefResourceRequestHandler(ResourceHandlerRegistry <IResourceHandler> resourceHandlerRegistry, IResourceRequestHandler resourceRequestHandler)
 {
     this.logic = new ResourceRequestHandlerLogic <IRequest, IResponse, IResourceHandler>(CefRequestAdapter.Instance, CefResponseAdapter.Instance, resourceHandlerRegistry, resourceRequestHandler);
 }
 public ResourceRequestHandlerFactoryLogic(IRequestAdapter <TRequest> requestAdapter, TResourceRequestHandler handler, ResourceHandlerRegistry <TResourceHandler> registry)
 {
     this.handler        = handler;
     this.registry       = registry;
     this.requestAdapter = requestAdapter;
 }
Ejemplo n.º 8
0
 void Application_Start(object sender, EventArgs e)
 {
     log4net.Config.XmlConfigurator.Configure();
     ResourceHandlerRegistry.RegisterResourceHandler("web", typeof(WebResource));
 }
Ejemplo n.º 9
0
 public SoundNotification(ResourceHandlerRegistry <IResourceHandler> registry)
 {
     this.registry = registry;
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Handles an HTTP request
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task <IResponse> HandleRequest(IRequest request)
        {
            try
            {
                // add access control header first
                request.Response.WithHeader("Access-Control-Allow-Origin", "*");

                Logger.Debug("Handling request to {0} {1}. Parsing resource descriptor from path...", request.Method, request.Path);

                // get the resource descriptor
                var resourceDescriptor = ResourceDescriptorHelper.GetResourceDescriptor(request.Path);
                if (resourceDescriptor == null)
                {
                    return(request.Response.WithStatus(HttpStatusCode.NotFound));
                }

                Logger.Debug("Parsing resource descriptor from path...");

                // if the resource type is not valid for this service, it's an unrecognized route
                if (!ResourceHandlerRegistry.IsSupported(resourceDescriptor.RootType))
                {
                    return(request.Response.WithStatus(HttpStatusCode.NotFound));
                }

                Logger.Debug("Successfully parsed resource descriptor for type {0} from path. Creating resource handler...",
                             resourceDescriptor.Type.FullName);

                // get handler for resource type
                var resourceHandler = ResourceHandlerRegistry.Get(resourceDescriptor.Type);
                if (resourceHandler == null)
                {
                    Logger.Error("Failed to created resource handler for type '{0}' even though its a supported type for this API.",
                                 resourceDescriptor.Type);
                    return(request.Response.WithStatus(HttpStatusCode.InternalServerError));
                }

                Logger.Debug("Successfully created resource handler of type {0} for resource type {1}. Processing {2} request...",
                             resourceHandler.GetType().FullName,
                             resourceDescriptor.Type.Name,
                             request.Method);

                // get or delete do not have a body - just use the route
                if (HttpMethods.IsGet(request.Method))
                {
                    await HandleGet(request, resourceHandler, resourceDescriptor);
                }
                else if (HttpMethods.IsDelete(request.Method))
                {
                    await HandleDelete(request, resourceHandler, resourceDescriptor);
                }
                else
                {
                    // read body of request as JSON
                    var resource = await ResourceSerializer.Deserialize(await request.ReadBodyAsText());

                    // ensure that the provided resource ID matches the ID from the route
                    // in the case of a POST, this should be null
                    if (resource.Id != null && resource.Id != resourceDescriptor.Url)
                    {
                        return(request.Response
                               .WithStatus(HttpStatusCode.BadRequest)
                               .WithPlainTextBody(
                                   $"Resource ID does not match ID in payload ('{resourceDescriptor.Id}' != '{resource.Id}'"));
                    }

                    // create or update based on the POST vs PUT
                    // if we have an ID for a POST or no ID for a PUT, the method is not supported for the route
                    if (HttpMethods.IsPost(request.Method) && resourceDescriptor.Id == null)
                    {
                        await HandlePost(request, resourceHandler, resourceDescriptor, resource);
                    }
                    else if (HttpMethods.IsPut(request.Method) && resourceDescriptor.Id != null)
                    {
                        await HandlePut(request, resourceHandler, resourceDescriptor, resource);
                    }
                    else
                    {
                        return(request.Response.WithStatus(HttpStatusCode.MethodNotAllowed));
                    }
                }

                return(request.Response);
            }
            catch (Exception e)
            {
                return(request.Response.WithStatus(HttpStatusCode.InternalServerError).WithPlainTextBody(e.ToString()));
            }
        }
 public ResourceRequestHandlerFactory(IResourceRequestHandler?resourceRequestHandler, ResourceHandlerRegistry <CefResourceHandler> resourceHandlerRegistry, BridgeObjectRegistry bridgeObjectRegistry)
 {
     this.logic = new ResourceRequestHandlerFactoryLogic <ResourceRequestHandler, CefResourceHandler, CefRequest>(CefRequestAdapter.Instance, new ResourceRequestHandler(resourceHandlerRegistry, bridgeObjectRegistry, resourceRequestHandler), resourceHandlerRegistry);
 }
Ejemplo n.º 12
0
 public CefResourceRequestHandlerFactory(IResourceRequestHandler resourceRequestHandler, ResourceHandlerRegistry <IResourceHandler> registry)
 {
     this.logic = new ResourceRequestHandlerFactoryLogic <CefResourceRequestHandler, IResourceHandler, IRequest>(CefRequestAdapter.Instance, new CefResourceRequestHandler(registry, resourceRequestHandler), registry);
 }