示例#1
0
        public override void DidReceiveScriptMessage(WKUserContentController userContentController, WKScriptMessage message)
        {
            SystemLogger.Log(SystemLogger.Module.PLATFORM, "# IPhoneWKScriptMessageHandler - DidReceiveScriptMessage");

            try {
                NSDictionary body = message.Body as NSDictionary;

                string requestUrl = null;
                string query      = null;

                string serviceName = "undefined";
                string methodName  = "undefined";
                Object service     = null;

                if (body.ContainsKey(new NSString("uri")))
                {
                    requestUrl = (body ["uri"]).ToString();

                    // get service path
                    if (requestUrl != null)
                    {
                        if (requestUrl.StartsWith(IPhoneServiceLocator.APPVERSE_SERVICE_URI))
                        {
                            string commandParams = requestUrl.Substring(IPhoneServiceLocator.APPVERSE_SERVICE_URI.Length);
                            //SystemLogger.Log (SystemLogger.Module.PLATFORM, "# IPhoneWKScriptMessageHandler - commandParams: " + commandParams);
                            string[] commandParamsArray = commandParams.Split(new char[] { '/' });
                            serviceName = commandParamsArray [0];
                            methodName  = commandParamsArray [1];
                            //SystemLogger.Log (SystemLogger.Module.PLATFORM, "# IPhoneWKScriptMessageHandler - serviceName: " + serviceName);
                            //SystemLogger.Log (SystemLogger.Module.PLATFORM, "# IPhoneWKScriptMessageHandler - methodName: " + methodName);
                            service = serviceLocator.GetService(serviceName);
                        }
                    }
                }

                if (body.ContainsKey(new NSString("query")))
                {
                    query = (body ["query"]).ToString();
                    //SystemLogger.Log (SystemLogger.Module.PLATFORM, "# IPhoneWKScriptMessageHandler - query: " + query);
                }

                SystemLogger.Log(SystemLogger.Module.PLATFORM, "# IPhoneWKScriptMessageHandler - sending Async POST result for service: " + serviceName + ", and method: " + methodName);

                serviceURIHandler.ProcessAsyncPOSTResult(serviceInvocationManager, service, methodName, query);
            } catch (Exception ex) {
                SystemLogger.Log(SystemLogger.Module.PLATFORM, "# IPhoneWKScriptMessageHandler - exception handling WKScriptMessage. Exception message: " + ex.Message);
            }
        }
        public override void StartLoading()
        {
            //SystemLogger.Log (SystemLogger.Module.PLATFORM, "# IPhoneNSUrlProtocol MANAGED SERVICE - Start Loading Content - HttpMethod: " + Request.HttpMethod);

            NSMutableUrlRequest newRequest = (NSMutableUrlRequest)Request.MutableCopy();               // change to mutable in case we need to change something
            string requestUrl = null;

            if (newRequest != null && newRequest.Url != null)
            {
                requestUrl = newRequest.Url.ToString();
            }
            SystemLogger.Log(SystemLogger.Module.PLATFORM, "# IPhoneNSUrlProtocol - Start Loading Content - Request URl: " + requestUrl);

            /* TO BE REMOVED - 5.0.6 [AMOB-30]
             * if (Request.HttpMethod == "OPTIONS")
             * {
             *      //In case of an OPTIONS, we allow the access to the origin of the petition
             *      var vlsOrigin = Request.Headers["ORIGIN"];
             *      //SystemLogger.Log (SystemLogger.Module.PLATFORM, "# IPhoneNSUrlProtocol - StartLoading: vlsOrigin: " + vlsOrigin);
             *
             *      NSMutableDictionary headers = new NSMutableDictionary ();
             *      NSHttpUrlResponse Response = new NSHttpUrlResponse (Request.Url, 403, "1.1", headers);
             *
             *      // if (vlsOrigin!=null && vlsOrigin.ToString() == String.Format(IPhoneServiceLocator.INTERNAL_SERVER_URL, IPhoneServiceLocator.CurrentDelegate.GetListeningPort()) ) {
             *      if (vlsOrigin!=null && vlsOrigin.ToString() == IPhoneServiceLocator.APPVERSE_HOST ) {
             *              headers.Add (new NSString ("Access-Control-Allow-Origin"), vlsOrigin);
             *              headers.Add (new NSString ("Access-Control-Allow-Methods"), new NSString ("POST"));
             *              headers.Add (new NSString ("Access-Control-Allow-Headers"), new NSString ("accept, content-type"));
             *              headers.Add (new NSString ("Access-Control-Max-Age"), new NSString ("1728000"));
             *
             *              Response = new NSHttpUrlResponse (Request.Url, 200, "1.1", headers);
             *      } else {
             *              SystemLogger.Log (SystemLogger.Module.PLATFORM, "# IPhoneNSUrlProtocol - StartLoading: WARNING - origin not allowed: " + vlsOrigin);
             *      }
             *
             *      Client.ReceivedResponse(this, Response, NSUrlCacheStoragePolicy.NotAllowed);
             *
             * } else
             */

            if (requestUrl != null)
            {
                if (Request.HttpMethod == "POST")
                {
                    string serviceName = "undefined";
                    string methodName  = "undefined";
                    Object service     = null;

                    if (requestUrl.StartsWith(IPhoneServiceLocator.APPVERSE_SERVICE_URI))
                    {
                        SystemLogger.Log(SystemLogger.Module.PLATFORM, "# IPhoneNSUrlProtocol MANAGED SERVICE");
                        // get service path
                        string commandParams = requestUrl.Substring(IPhoneServiceLocator.APPVERSE_SERVICE_URI.Length);
                        //SystemLogger.Log (SystemLogger.Module.PLATFORM, "# IPhoneNSUrlProtocol - commandParams: " + commandParams);
                        string[] commandParamsArray = commandParams.Split(new char[] { '/' });
                        serviceName = commandParamsArray [0];
                        methodName  = commandParamsArray [1];
                        //SystemLogger.Log (SystemLogger.Module.PLATFORM, "# IPhoneNSUrlProtocol - serviceName: " + serviceName + ", methodName: " + methodName);
                        service = serviceLocator.GetService(serviceName);

                        String query = null;
                        if (newRequest.Body != null)
                        {
                            SystemLogger.Log(SystemLogger.Module.PLATFORM,
                                             "# IPhoneNSUrlProtocol - query data [ONLY FOR TESTING PURPOSES, PLEASE REMOVE THIS TRACE FROM LOG] request length: " + newRequest.Body.Length);

                            query = NSString.FromData(newRequest.Body, NSStringEncoding.UTF8);
                        }

                        SystemLogger.Log(SystemLogger.Module.PLATFORM, "# IPhoneNSUrlProtocol - sending Async POST result for service: " + serviceName + ", and method: " + methodName);
                        serviceURIHandler.ProcessAsyncPOSTResult(serviceInvocationManager, service, methodName, query);

                        NSData        nsDataResponse = NSData.FromString("MANAGED SERVICE (data returned using callback or global listener)");
                        NSUrlResponse response       = new NSUrlResponse(newRequest.Url, "text/html", (nint)nsDataResponse.Length, "utf-8");
                        SystemLogger.Log(SystemLogger.Module.PLATFORM, "# IPhoneNSUrlProtocol MANAGED SERVICE - Service executed (data will be returned using callback or global listener)");
                        Client.ReceivedResponse(this, response, NSUrlCacheStoragePolicy.NotAllowed);
                        Client.DataLoaded(this, nsDataResponse);
                    }
                }
                else if (Request.HttpMethod == "GET")
                {
                    if (requestUrl.StartsWith(IPhoneServiceLocator.APPVERSE_RESOURCE_URI))
                    {
                        SystemLogger.Log(SystemLogger.Module.PLATFORM, "# IPhoneNSUrlProtocol MANAGED WEB RESOURCE");

                        String resourcePath   = newRequest.Url.Path;
                        String mimeType       = resourceURIHandler.GetWebResourceMimeType(resourcePath, true);
                        NSData nsDataResponse = resourceURIHandler.ProcessWebResource(resourcePath);
                        if (nsDataResponse == null)
                        {
                            SystemLogger.Log(SystemLogger.Module.PLATFORM, "# IPhoneNSUrlProtocol MANAGED WEB RESOURCE no data bytes - " + requestUrl);
                            nsDataResponse = NSData.FromString("MANAGED WEB RESOURCE : no data bytes");
                        }
                        NSUrlResponse response = new NSUrlResponse(newRequest.Url, mimeType, (nint)nsDataResponse.Length, "utf-8");
                        Client.ReceivedResponse(this, response, NSUrlCacheStoragePolicy.NotAllowed);
                        Client.DataLoaded(this, nsDataResponse);
                    }
                    else if (requestUrl.StartsWith(IPhoneServiceLocator.APPVERSE_DOCUMENT_URI))
                    {
                        SystemLogger.Log(SystemLogger.Module.PLATFORM, "# IPhoneNSUrlProtocol MANAGED DOCUMENT RESOURCE");

                        String resourcePath   = newRequest.Url.Path.Substring(IPhoneServiceLocator.APPVERSE_DOCUMENT_PATH.Length);
                        String mimeType       = resourceURIHandler.GetWebResourceMimeType(resourcePath.ToLower(), false);
                        NSData nsDataResponse = resourceURIHandler.ProcessDocumentResource(resourcePath);
                        if (nsDataResponse == null)
                        {
                            SystemLogger.Log(SystemLogger.Module.PLATFORM, "# IPhoneNSUrlProtocol MANAGED DOCUMENT RESOURCE no data bytes - " + requestUrl);
                            nsDataResponse = NSData.FromString("MANAGED DOCUMENT RESOURCE : no data bytes");
                        }
                        NSUrlResponse response = new NSUrlResponse(newRequest.Url, mimeType, (nint)nsDataResponse.Length, "utf-8");
                        Client.ReceivedResponse(this, response, NSUrlCacheStoragePolicy.NotAllowed);
                        Client.DataLoaded(this, nsDataResponse);
                    }
                }
            }

            Client.FinishedLoading(this);
        }