/// <summary> /// Processes the specified client. /// </summary> /// <param name="client">The client.</param> /// <param name="assetMode">The asset mode.</param> /// <param name="interval">The interval.</param> /// <param name="retries">The retries.</param> static void Process(OlsaPortTypeClient client, assetInitiationMode assetMode = assetInitiationMode.all, int interval = 1, int retries = 10) { log.InfoFormat("Starting Process. assetInitiationMode : {0}", assetMode.ToString()); //Best practice for OLSA AI cycle, on startup - send NULL Acknowledgement to cancel any existing pending requests AI_AcknowledgeAssetMetaData(client, null); //Call and get the XML data, send NULL acknowledgement var xmlResultsFile = DownloadMetadata(client, assetMetadataFormat.XMLX, assetMode, false, false, interval, retries); //Call and get the AICC data, send acknowledgement of the returned handled var aiccResultsFile = DownloadMetadata(client, assetMetadataFormat.AICC, assetMode, true, false, interval, retries); //Extract the downloaded ZIP metadata var xmlFolder = ExtractZip(xmlResultsFile); var aiccFolder = ExtractZip(aiccResultsFile); //Now perform AI processing List <CombinedAssetObject> combinedObjects = new List <CombinedAssetObject>(); //Loop thru the XML _ss_entitlements.xml from the AICC folder XmlTextReader reader = new XmlTextReader(string.Format("{0}\\{1}", aiccFolder, "_ss_entitlement_status.xml")); XPathDocument document = new XPathDocument(reader); XPathNavigator navigator = document.CreateNavigator(); XPathNavigator catalogNavigator = GetXpathNavigatorForCustomerCatalogXML(xmlFolder); //Get the ASSET nodes XPathNodeIterator nodeIterator = navigator.Select("//ASSET"); while (nodeIterator.MoveNext()) { if (nodeIterator.Current.HasAttributes) { //Extract the ID/STATUS var id = nodeIterator.Current.GetAttribute("ID", ""); var status = nodeIterator.Current.GetAttribute("STATUS", ""); //Generate a combine object using the XML and AICC data var result = GetCombinedObject(id, status, catalogNavigator, aiccFolder); combinedObjects.Add(result); } } DateTime now = DateTime.UtcNow; string jsonFilename = String.Format("JSONDATA-{0:D4}{1:D2}{2:D2}T{3:D2}{4:D2}{5:D2}Z.json", now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second); log.InfoFormat("Saving Results to JSON. File : {0}", jsonFilename); string jsonStr = JsonConvert.SerializeObject(combinedObjects); File.WriteAllText(jsonFilename, jsonStr); }
/// <summary> /// Initiates the asset meta data. /// </summary> /// <param name="client">The client.</param> /// <param name="metadataFormat">The metadata format.</param> /// <param name="initiationMode">The initiation mode.</param> /// <param name="onsuccessclose">if set to <c>true</c> [onsuccessclose].</param> /// <returns></returns> /// <exception cref="AIPOC.Exceptions.OlsaSecurityException"></exception> public static string AI_InitiateAssetMetaData(OlsaPortTypeClient client, assetMetadataFormat metadataFormat, assetInitiationMode initiationMode, bool onsuccessclose = false) { HandleResponse handleResponse = new HandleResponse(); try { log.InfoFormat("Sending AI_InitiateAssetMetadata Request. Format: {0} Mode: {1}", metadataFormat, initiationMode); InitiateAssetMetaDataRequest request = new InitiateAssetMetaDataRequest(); //Pull the OlsaAuthenticationBehviour so we can extract the customerid AuthenticationBehavior olsaCredentials = (AuthenticationBehavior)client.ChannelFactory.Endpoint.Behaviors.Where(p => p.GetType() == typeof(AuthenticationBehavior)).FirstOrDefault(); request.customerId = olsaCredentials.UserName; request.initiationMode = initiationMode; request.metadataFormat = metadataFormat; handleResponse = client.AI_InitiateAssetMetaData(request); } catch (WebException) { // This captures any Web Exepctions such as DNS lookup errors, HTTP status errors such as 404, proxy errors etc // See http://msdn.microsoft.com/en-us/library/48ww3ee9(VS.80).aspx throw; } catch (TimeoutException) { //This captures the WCF timeout exception throw; } //Olsa.GeneralFault exception will be thrown for issues like parameters invalid, user does could not be created etc catch (FaultException <Olsa.GeneralFault> ) { throw; } //WCF fault exception will be thrown for any other issues such as Security catch (FaultException fe) { if (fe.Message.ToLower(CultureInfo.InvariantCulture).Contains("the security token could not be authenticated or authorized")) { //The OLSA Credentials specified could not be authenticated //Check the values in the web.config are correct for OLSA.CustomerID and OLSA.SharedSecret - these are case sensitive //Check the time on the machine, the SOAP message is valid for 5 minutes. This means that if the time on the calling machine //is to slow OR to fast then the SOAP message will be invalid. throw new Exceptions.OlsaSecurityException(); } throw; } catch (CommunicationException) { throw; } catch (Exception) { //Any other type of exception, perhaps out of memory throw; } finally { if (client != null) { if (client.State == CommunicationState.Faulted) { client.Abort(); } if (onsuccessclose) { client.Close(); } } } return(handleResponse.handle); }
/// <summary> /// This calls the AI_InitiateAssetMetadata function, then the AI_PollForAssetMetadata, saves the results (metadata_<paramref name="metadataFormat" />.zip and extracts them. /// </summary> /// <param name="client">The OLSA client.</param> /// <param name="metadataFormat">The metadata format.</param> /// <param name="initiationMode">The initiation mode.</param> /// <param name="acknowledge">if set to <c>true</c> send an AI_AcknowledgeAssetMetadata</param> /// <param name="onsuccessclose">if set to <c>true</c> [onsuccessclose].</param> /// <param name="pollInterval">The poll interval.</param> /// <returns></returns> static FileInfo DownloadMetadata(OlsaPortTypeClient client, assetMetadataFormat metadataFormat, assetInitiationMode initiationMode, bool acknowledge = false, bool onsuccessclose = false, int pollInterval = 5, int pollRetries = 10) { DateTime now = DateTime.UtcNow; string _localFile = String.Format("METADATA-{0}-{1:D4}{2:D2}{3:D2}T{4:D2}{5:D2}{6:D2}Z.zip", metadataFormat, now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second); FileInfo localFile = null; Uri metadataDownloadUri; string metadataHandle = null; try { log.InfoFormat("DownloadMetadata. Format: {0} Mode: {1}", metadataFormat, initiationMode); metadataHandle = AI_InitiateAssetMetaData(client, metadataFormat, initiationMode, onsuccessclose); log.InfoFormat("Returned Handle: {0}", metadataHandle); } catch (FaultException <Olsa.RequestAlreadyInProgressFault> ) { throw; } catch (Exception ex) { log.Fatal("Issue while submitting request", ex); throw; } if (!string.IsNullOrEmpty(metadataHandle)) { //We have a report handle so poll every x minutes until report ready or for max y attempts int interval = (int)(pollInterval * 60 * 1000); int retries = pollRetries; log.InfoFormat("Starting Loop to check if data is ready. Interval: {0} minutes. Retries:{1}", pollInterval, retries); //Poll for the report try { metadataDownloadUri = CheckIfDataReady(client, metadataHandle, retries, interval); log.InfoFormat("Returned Url: {0}", metadataDownloadUri.OriginalString); } catch (Exception ex1) { log.Fatal("Issue while checking if data ready", ex1); throw; } log.InfoFormat("Starting Download of file. Saving to: {0}", _localFile); using (WebClient webClient = new WebClient()) { try { webClient.DownloadFile(metadataDownloadUri, _localFile); log.Info("Download Completed"); } catch (Exception ex2) { log.Fatal("Issue while downloading report", ex2); throw; } } if (client != null) { if (client.State == CommunicationState.Faulted) { client.Abort(); } } } if (acknowledge) { log.InfoFormat("Sending Acknowledgement. Handle: {0}", metadataHandle); AI_AcknowledgeAssetMetaData(client, metadataHandle); } else { //Send NULL Handle Ack log.Info("Sending Acknowledgement. Null Handle"); AI_AcknowledgeAssetMetaData(client, null); } localFile = new FileInfo(_localFile); return(localFile); }