private void SetUp(IFlowData data) { var host = Host; var protocol = Protocol; bool supportsPromises; if (string.IsNullOrEmpty(host)) { // Try and get the request host name so it can be used to request // the Json refresh in the JavaScript code. data.TryGetEvidence(Constants.EVIDENCE_HOST_KEY, out host); } if (string.IsNullOrEmpty(protocol)) { // Try and get the request protocol so it can be used to request // the JSON refresh in the JavaScript code. data.TryGetEvidence(Constants.EVIDENCE_PROTOCOL, out protocol); } // Couldn't get protocol from anywhere if (string.IsNullOrEmpty(protocol)) { protocol = Constants.DEFAULT_PROTOCOL; } // If device detection is in the Pipeline then we can check // if the client's browser supports promises. // This can be used to customize the JavaScript response. try { var promise = data.GetAs <IAspectPropertyValue <string> >("Promise"); supportsPromises = promise != null && promise.HasValue && promise.Value == "Full"; } catch (PipelineDataException) { supportsPromises = false; } catch (InvalidCastException) { supportsPromises = false; } catch (KeyNotFoundException) { supportsPromises = false; } // Get the JSON include to embed into the JavaScript include. string jsonObject = string.Empty; try { jsonObject = data.Get <IJsonBuilderElementData>().Json; } catch (KeyNotFoundException ex) { throw new PipelineConfigurationException( Messages.ExceptionJsonBuilderNotRun, ex); } // Generate any required parameters for the JSON request. List <string> parameters = new List <string>(); // Any query parameters from this request that were ingested by // the Pipeline are added to the request URL that will appear // in the JavaScript. var queryEvidence = data .GetEvidence() .AsDictionary() .Where(e => e.Key.StartsWith(Core.Constants.EVIDENCE_QUERY_PREFIX, StringComparison.OrdinalIgnoreCase)) .Select(k => { var dotPos = k.Key.IndexOf(Core.Constants.EVIDENCE_SEPERATOR, StringComparison.OrdinalIgnoreCase); return($"{WebUtility.UrlEncode(k.Key.Remove(0, dotPos + 1))}" + $"={WebUtility.UrlEncode(k.Value.ToString())}"); }); parameters.AddRange(queryEvidence); string queryParams = string.Join("&", parameters); string endpoint = Endpoint; Uri url = null; if (string.IsNullOrWhiteSpace(protocol) == false && string.IsNullOrWhiteSpace(host) == false && string.IsNullOrWhiteSpace(endpoint) == false) { var endpointHasSlash = endpoint[0] == '/'; var hostHasSlash = host[host.Length - 1] == '/'; // if there is no slash between host and endpoint then add one. if (endpointHasSlash == false && hostHasSlash == false) { endpoint = $"/{endpoint}"; } // if there are two slashes between host and endpoint then remove one. else if (endpointHasSlash == true && hostHasSlash == true) { endpoint = endpoint.Substring(1); } url = new Uri($"{protocol}://{host}{endpoint}" + (String.IsNullOrEmpty(queryParams) ? "" : $"?{queryParams}")); } // With the gathered resources, build a new JavaScriptResource. BuildJavaScript(data, jsonObject, supportsPromises, url); }
private void SetUp(IFlowData data) { var host = Host; var protocol = Protocol; bool supportsPromises = false; bool supportsFetch = false; if (string.IsNullOrEmpty(host) && // Try and get the request host name so it can be used to request // the Json refresh in the JavaScript code. data.TryGetEvidence(Constants.EVIDENCE_HOST_KEY, out object hostObj)) { host = hostObj?.ToString() ?? String.Empty; } if (string.IsNullOrEmpty(protocol) && // Try and get the request protocol so it can be used to request // the JSON refresh in the JavaScript code. data.TryGetEvidence(Core.Constants.EVIDENCE_PROTOCOL, out object protocolObj)) { protocol = protocolObj?.ToString() ?? String.Empty; } // Couldn't get protocol from anywhere if (string.IsNullOrEmpty(protocol)) { protocol = Constants.DEFAULT_PROTOCOL; } // If device detection is in the Pipeline then we can check // if the client's browser supports promises. // This can be used to customize the JavaScript response. if (_promisePropertyAvailable) { // Execute this action if one of our expected // exceptions occurs. Action promisesNotAvailable = () => { // Short-circuit future calls, so we don't keep checking // for this property. _promisePropertyAvailable = false; supportsPromises = false; }; try { var promise = data.GetAs <IAspectPropertyValue <string> >("Promise"); supportsPromises = promise != null && promise.HasValue && promise.Value == "Full"; } catch (PropertyMissingException) { promisesNotAvailable(); } catch (PipelineDataException) { promisesNotAvailable(); } catch (InvalidCastException) { promisesNotAvailable(); } catch (KeyNotFoundException) { promisesNotAvailable(); } } // If device detection is in the Pipeline then we can check // if the client's browser supports fetch. // This can be used to customize the JavaScript response. if (_fetchPropertyAvailable) { // Execute this action if one of our expected // exceptions occurs. Action fetchNotAvailable = () => { // Short-circuit future calls, so we don't keep checking // for this property. _fetchPropertyAvailable = false; supportsFetch = false; }; try { var fetch = data.GetAs <IAspectPropertyValue <bool> >("Fetch"); supportsFetch = fetch != null && fetch.HasValue && fetch.Value; } catch (PropertyMissingException) { fetchNotAvailable(); } catch (PipelineDataException) { fetchNotAvailable(); } catch (InvalidCastException) { fetchNotAvailable(); } catch (KeyNotFoundException) { fetchNotAvailable(); } } // Get the JSON include to embed into the JavaScript include. string jsonObject = string.Empty; try { jsonObject = data.Get <IJsonBuilderElementData>().Json; } catch (KeyNotFoundException ex) { throw new PipelineConfigurationException( Messages.ExceptionJsonBuilderNotRun, ex); } var parameters = GetParameters(data); var paramsObject = JsonConvert.SerializeObject(parameters); var sessionId = GetSessionId(data); var sequence = GetSequence(data); string endpoint = Endpoint; Uri url = null; // Check the call-back URL is formatted correctly. if (string.IsNullOrWhiteSpace(protocol) == false && string.IsNullOrWhiteSpace(host) == false && string.IsNullOrWhiteSpace(endpoint) == false) { var endpointHasSlash = endpoint[0] == '/'; var hostHasSlash = host[host.Length - 1] == '/'; // if there is no slash between host and endpoint then add one. if (endpointHasSlash == false && hostHasSlash == false) { endpoint = $"/{endpoint}"; } // if there are two slashes between host and endpoint then remove one. else if (endpointHasSlash == true && hostHasSlash == true) { endpoint = endpoint.Substring(1); } url = new Uri($"{protocol}://{host}{endpoint}"); } // With the gathered resources, build a new JavaScriptResource. BuildJavaScript(data, jsonObject, sessionId, sequence, supportsPromises, supportsFetch, url, paramsObject); }