public WebProxyCredentials GetWebProxyCredentials() { WebProxyCredentials proxyCredentials = null; if (String.IsNullOrEmpty(this["ProxyCredentialToken"])) { proxyCredentials = new WebProxyCredentials(); } else { // Settings included an encrypted credential string so hopefully the ProxyPort and ProxyHost have also been provided // Note that ProxyBypassOnLocal and ProxyBypassList are optional int portNumber = 8080; Int32.TryParse(this["ProxyPort"], out portNumber); proxyCredentials = new WebProxyCredentials( this["ProxyCredentialToken"], this["ProxyHost"], portNumber, this["ProxyBypassOnLocal"], this["ProxyBypassList"]); } return(proxyCredentials); }
public DbDictionaryService() { Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory); _proxyCredentials = new WebProxyCredentials(); dbDictionaryFullFilePath = WebConfigurationManager.AppSettings["DbDictionaryFullFilePath"]; proxyCredentialToken = WebConfigurationManager.AppSettings["ProxyCredentialToken"]; proxyPort = WebConfigurationManager.AppSettings["ProxyPort"]; proxyHost = WebConfigurationManager.AppSettings["ProxyHost"]; if (!string.IsNullOrEmpty(proxyHost) || (!string.IsNullOrEmpty(proxyPort))) { _proxyCredentials.proxyHost = proxyHost; _proxyCredentials.proxyPort = Convert.ToInt32(proxyPort); } }
public Program() { //Adapter Service does all this... Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); file = new System.IO.StreamWriter(_fileName); NameValueCollection appSettings = System.Configuration.ConfigurationManager.AppSettings; ServiceSettings settings = new ServiceSettings(); settings.AppendSettings(appSettings); //This is how DataLayers should use settings... WebProxyCredentials webProxyCredentials = settings.GetWebProxyCredentials(); _widgetServiceClient = new WebHttpClient(settings["WidgetServiceUri"], webProxyCredentials.userName, webProxyCredentials.password, webProxyCredentials.domain, webProxyCredentials.proxyHost, webProxyCredentials.proxyPort); //In a DataLayer this is automatic and is not required _widgetServiceClient.AccessToken = settings["AccessToken"]; }
public static string Update(string baseUri, string sparql, WebCredentials targetCredentials, WebProxyCredentials proxyCredentials) { try { string message = String.Empty; string relativeUri = "?update=" + HttpUtility.UrlEncode(sparql); WebHttpClient webClient = new WebHttpClient(baseUri, targetCredentials.GetNetworkCredential(), proxyCredentials.GetWebProxy()); message = webClient.GetMessage(relativeUri); return(message); } catch (Exception exception) { _logger.Error("Error in Update: " + exception); throw exception; } }
public static SPARQLResults Query(string baseUri, string sparql, WebCredentials targetCredentials, WebProxyCredentials proxyCredentials) { try { SPARQLResults sparqlResults = null; string relativeUri = "?query=" + HttpUtility.UrlEncode(sparql); WebHttpClient webClient = new WebHttpClient(baseUri, targetCredentials.GetNetworkCredential(), proxyCredentials.GetWebProxy()); sparqlResults = webClient.Get <SPARQLResults>(relativeUri, false); return(sparqlResults); } catch (Exception exception) { _logger.Error("Error in Query: " + exception); throw exception; } }
public static void PostQueryAsMultipartMessage(string baseUri, string sparql, WebCredentials targetCredentials, WebProxyCredentials proxyCredentials) { try { string result = string.Empty; MultiPartMessage requestMessage = new MultiPartMessage { name = "update", type = MultipartMessageType.FormData, message = sparql, }; List <MultiPartMessage> requestMessages = new List <MultiPartMessage> { requestMessage }; WebHttpClient webClient = new WebHttpClient(baseUri, targetCredentials.GetNetworkCredential(), proxyCredentials.GetWebProxy()); string response = String.Empty; webClient.PostMultipartMessage("", requestMessages, ref response); } catch (Exception exception) { _logger.Error("Error in PostQueryAsMultipartMessage: " + exception); throw exception; } }
public Response Pull(string scope, string app, string graph, Request request) { Response response = new Response(); response.Level = StatusLevel.Success; Status status = new Status(); status.Messages = new Messages(); try { status.Identifier = String.Format("{0}.{1}", scope, app); InitializeScope(scope, app); if (_settings["ReadOnlyDataLayer"] != null && _settings["ReadOnlyDataLayer"].ToString().ToLower() == "true") { string message = "Can not perform post on read-only data layer of [" + scope + "." + app + "]."; _logger.Error(message); status.Level = StatusLevel.Error; status.Messages.Add(message); } else { InitializeDataLayer(); DateTime startTime = DateTime.Now; #region move this portion to dotNetRdfEngine? if (!request.ContainsKey("targetEndpointUri")) { throw new Exception("Target Endpoint Uri is required"); } string targetEndpointUri = request["targetEndpointUri"]; if (!request.ContainsKey("targetGraphBaseUri")) { throw new Exception("Target graph uri is required"); } string targetGraphBaseUri = request["targetGraphBaseUri"]; _settings["TargetGraphBaseUri"] = targetGraphBaseUri; if (targetGraphBaseUri.ToLower() == "[default graph]") { targetGraphBaseUri = String.Empty; } SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri(targetEndpointUri), targetGraphBaseUri); if (request.ContainsKey("targetCredentials")) { string targetCredentialsXML = request["targetCredentials"]; WebCredentials targetCredentials = Utility.Deserialize <WebCredentials>(targetCredentialsXML, true); if (targetCredentials.isEncrypted) { targetCredentials.Decrypt(); } endpoint.SetCredentials(targetCredentials.GetNetworkCredential().UserName, targetCredentials.GetNetworkCredential().Password, targetCredentials.GetNetworkCredential().Domain); } string proxyHost = _settings["ProxyHost"]; string proxyPort = _settings["ProxyPort"]; string proxyCredsToken = _settings["ProxyCredentialToken"]; if (!String.IsNullOrEmpty(proxyHost) && !String.IsNullOrEmpty(proxyPort) && !String.IsNullOrEmpty(proxyCredsToken)) { WebProxyCredentials proxyCreds = _settings.GetWebProxyCredentials(); endpoint.Proxy = proxyCreds.GetWebProxy() as WebProxy; endpoint.ProxyCredentials = proxyCreds.GetNetworkCredential(); } VDS.RDF.IGraph resultGraph = endpoint.QueryWithResultGraph("CONSTRUCT {?s ?p ?o} WHERE {?s ?p ?o}"); #endregion if (resultGraph != null && resultGraph.Triples.Count > 0) { // call RdfProjectionEngine to fill data objects from a given graph _projectionEngine = _kernel.Get <IProjectionLayer>("rdf"); System.Text.StringBuilder sb = new System.Text.StringBuilder(); TextWriter textWriter = new StringWriter(sb); VDS.RDF.Writing.RdfXmlWriter rdfWriter = new VDS.RDF.Writing.RdfXmlWriter(); rdfWriter.Save(resultGraph, textWriter); XDocument xDocument = XDocument.Parse(sb.ToString()); if (xDocument != null && xDocument.Root != null) { _logger.Debug(xDocument.Root.ToString()); _dataObjects = _projectionEngine.ToDataObjects(graph, ref xDocument); if (_dataObjects != null && _dataObjects.Count > 0) { status.Messages.Add("Query target endpoint completed successfully."); status.Messages.Add(String.Format("Number of data objects created [{0}].", _dataObjects.Count)); // post data objects to data layer response.Append(_dataLayer.Post(_dataObjects)); DateTime endTime = DateTime.Now; TimeSpan duration = endTime.Subtract(startTime); status.Messages.Add(String.Format("Execution time [{0}:{1}.{2}] minutes.", duration.Minutes, duration.Seconds, duration.Milliseconds)); } else { status.Messages.Add(string.Format("No data objects being created.")); } } else { throw new Exception("Facade document is empty."); } } else { throw new Exception("Facade graph is empty."); } } } catch (Exception ex) { _logger.Error("Error in Pull(): ", ex); status.Level = StatusLevel.Error; status.Messages.Add(string.Format("Error pulling graph: {0}", ex)); } response.Append(status); return(response); }
public Response Pull(string projectName, string applicationName, string graphName, Request request) { Status status = new Status(); status.Messages = new Messages(); try { status.Identifier = String.Format("{0}.{1}", projectName, applicationName); InitializeScope(projectName, applicationName); InitializeDataLayer(); DateTime startTime = DateTime.Now; if (!request.ContainsKey("targetEndpointUri")) { throw new Exception("Target Endpoint Uri is required"); } string targetEndpointUri = request["targetEndpointUri"]; if (!request.ContainsKey("targetGraphBaseUri")) { throw new Exception("Target graph uri is required"); } string targetGraphBaseUri = request["targetGraphBaseUri"]; _settings["TargetGraphBaseUri"] = targetGraphBaseUri; SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri(targetEndpointUri), targetGraphBaseUri); if (request.ContainsKey("targetCredentials")) { string targetCredentialsXML = request["targetCredentials"]; WebCredentials targetCredentials = Utility.Deserialize <WebCredentials>(targetCredentialsXML, true); if (targetCredentials.isEncrypted) { targetCredentials.Decrypt(); } endpoint.SetCredentials( targetCredentials.GetNetworkCredential().UserName, targetCredentials.GetNetworkCredential().Password, targetCredentials.GetNetworkCredential().Domain); } string proxyHost = _settings["ProxyHost"]; string proxyPort = _settings["ProxyPort"]; if (!String.IsNullOrEmpty(proxyHost) && !String.IsNullOrEmpty(proxyPort)) { WebProxy webProxy = new WebProxy(proxyHost, Int32.Parse(proxyPort)); WebProxyCredentials proxyCrendentials = _settings.GetWebProxyCredentials(); if (proxyCrendentials != null) { webProxy.Credentials = _settings.GetProxyCredential(); } endpoint.SetProxyCredentials(proxyCrendentials.userName, proxyCrendentials.password, proxyCrendentials.domain); endpoint.SetProxy(webProxy.Address); } String query = "CONSTRUCT {?s ?p ?o} WHERE {?s ?p ?o}"; VDS.RDF.IGraph graph = endpoint.QueryWithResultGraph(query); System.Text.StringBuilder sb = new System.Text.StringBuilder(); TextWriter textWriter = new StringWriter(sb); VDS.RDF.Writing.RdfXmlWriter rdfWriter = new VDS.RDF.Writing.RdfXmlWriter(); rdfWriter.Save(graph, textWriter); XDocument xDocument = XDocument.Parse(sb.ToString()); // call RdfProjectionEngine to fill data objects from a given graph _projectionEngine = _kernel.Get <IProjectionLayer>("rdf"); _dataObjects = _projectionEngine.ToDataObjects(graphName, ref xDocument); // post data objects to data layer _dataLayer.Post(_dataObjects); DateTime endTime = DateTime.Now; TimeSpan duration = endTime.Subtract(startTime); status.Messages.Add(string.Format("Graph [{0}] has been posted to legacy system successfully.", graphName)); status.Messages.Add(String.Format("Execution time [{0}:{1}.{2}] minutes.", duration.Minutes, duration.Seconds, duration.Milliseconds)); } catch (Exception ex) { _logger.Error("Error in Pull(): ", ex); status.Level = StatusLevel.Error; status.Messages.Add(string.Format("Error pulling graph: {0}", ex)); } _response.Append(status); return(_response); }