/// <summary>
        /// Factory Method to create CacheManifest from a properly formatted String.
        /// </summary>
        /// <param name="manifestString">Manifest Response string (i.e. cache manifest file as a string)</param>
        /// <returns>CacheManifest</returns>
        public static CacheManifest CreateFromString(string manifestString)
        {
            // validate parameter
            if (string.IsNullOrEmpty(manifestString))
            {
                throw new ArgumentNullException("manifestString");
            }

            // create CacheManifest object
            CacheManifest cacheManifest = new CacheManifest();

            // populate CacheManifest from string
            PopulateCacheManifest(cacheManifest, manifestString);

            return(cacheManifest);
        }
Exemple #2
0
        /// <summary>
        /// Adds the specified cache manifest.
        /// </summary>
        /// <param name="cacheManifest">The cache manifest.</param>
        public static void Add(CacheManifest cacheManifest)
        {
            if (cacheManifest == null)
            {
                throw new ArgumentNullException("cacheManifest");
            }

            lock (Map)
            {
                // if the cacheIndex key doesn't already exist in the map then add it.
                if (!Map.ContainsKey(cacheManifest.ManifestBaseUri))
                {
                    CacheIndex cacheIndex = CacheIndex.Create(cacheManifest);
                    Map.Add(cacheManifest.ManifestBaseUri, cacheIndex);
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Updates the index using the specified cache manifest.
        /// </summary>
        /// <param name="manifest">The cache manifest.</param>
        public void UpdateIndex(CacheManifest manifest)
        {
            if (manifest == null || manifest.Cache == null || manifest.Cache.Count == 0)
            {
                return;
            }

            StringComparer comparer = CacheIndexMap.IgnoreCase ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal;

            lock (syncLock)
            {
                // loop through each string in manifest cache list and create cache index item and add to list.
                foreach (string sCacheItem in manifest.Cache)
                {
                    CacheIndexItem cacheIndexItem = this.FirstOrDefault(item => comparer.Compare(item.RelativeUri, sCacheItem) == 0);

                    if (null == cacheIndexItem)
                    {
                        cacheIndexItem = new CacheIndexItem()
                        {
                            RelativeUri = sCacheItem,
                            PreFetch    = true
                        };

                        this.Add(cacheIndexItem, false);
                    }
                }
                // SerializeCacheIndex( this );

                //For all items in index where PreFetch is true, but does not exist in manifest, set PreFetch to false
                IEnumerable <CacheIndexItem> items2 = this.Where(item => item.PreFetch == true);
                foreach (var item in items2)
                {
                    if (!manifest.Cache.Any(cacheItem => comparer.Compare(cacheItem, item.RelativeUri) == 0))
                    {
                        item.PreFetch = false;
                    }
                }

                SerializeCacheIndex(this);
            }
        }
        /// <summary>
        /// Factory method to create CacheManifest based on URI string
        /// </summary>
        /// <param name="manifestUri">the manifest URI string.</param>
        /// <param name="headers">The headers for the request.</param>
        /// <returns>CacheManifest</returns>
        public static CacheManifest CreateFromUri(string manifestUri, IDictionary <string, string> headers)
        {
            // validate parameter
            if (string.IsNullOrEmpty(manifestUri))
            {
                throw new ArgumentNullException("manifestUri");
            }

            NetworkResponse networkResponse = Device.Network.Fetcher.Fetch(manifestUri, headers, 60000);

            if (networkResponse.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception(string.Format("Unable to download manifest file from server: uri: {0}  message: {1}", manifestUri, networkResponse.Message), networkResponse.Exception);
            }

            // create CacheManifest object from manifest string from response
            CacheManifest cacheManifest = CacheManifest.CreateFromString(networkResponse.ResponseString);

            cacheManifest.ManifestUri = new Uri(manifestUri);

            return(cacheManifest);
        }
Exemple #5
0
        // To-Do - make more efficient so 2 new statements aren't used
        /// <summary>
        /// Creates a new cache index using the specified cache manifest and cache path.
        /// </summary>
        /// <param name="cacheManifest">The cache manifest.</param>
        /// <param name="cachePath">The cache path.</param>
        /// <returns></returns>
        public static CacheIndex Create(CacheManifest cacheManifest, string cachePath)
        {
            // derive base URI from cache manifest.
            // base Uri = root folder of cache manifest file.

            if (cacheManifest == null)
            {
                throw new ArgumentNullException("cacheManifest cannot be null.");
            }

            string baseUriString = cacheManifest.ManifestBaseUri;

            CacheIndex cacheIndex = new CacheIndex(baseUriString, cachePath);

            // attempt to deserialize cache index.
            cacheIndex = DeserializeCacheIndex(cacheIndex.SerializeFile);

            if (cacheIndex == null)
            {
                cacheIndex = new CacheIndex(baseUriString, cachePath);
            }
            else
            {
                // To-Do: for some reason properties in this class aren't being serialized
                //        so for now, simply reapply base string, and use default values and SerializeFileName
                cacheIndex.BaseUri   = baseUriString;
                cacheIndex.CachePath = cachePath;
            }

            // since we're creating from CacheManifest, execute the update
            cacheIndex.UpdateIndex(cacheManifest);

            cacheIndex.PreFetchIndexEnabled = true;
            cacheIndex.CleanIndexEnabled    = true;

            return(cacheIndex);
        }
Exemple #6
0
 /// <summary>
 /// Creates a new cache index using the specified cache manifest.
 /// </summary>
 /// <param name="cacheManifest">The cache manifest.</param>
 /// <returns></returns>
 public static CacheIndex Create(CacheManifest cacheManifest)
 {
     return(Create(cacheManifest, Device.SessionDataPath));
 }
 /// <summary>
 /// Delegate to parse and add entry to fallback list
 /// </summary>
 /// <param name="cacheManifest"></param>
 /// <param name="value"></param>
 private static void AddCacheManifestFallbackItem(CacheManifest cacheManifest, string value)
 {
     cacheManifest.Fallback.Add(value);
 }
 /// <summary>
 /// Delegate to parse and add entry to Cache list
 /// </summary>
 /// <param name="cacheManifest"></param>
 /// <param name="value"></param>
 private static void AddCacheManifestCacheItem(CacheManifest cacheManifest, string value)
 {
     cacheManifest.Cache.Add(value);
 }
 /// <summary>
 /// Delegate to parse and add entry to Network list.
 /// </summary>
 /// <param name="cacheManifest"></param>
 /// <param name="value"></param>e-manifest are never cached.
 /// <remarks>Network list items in cache</remarks>
 private static void AddCacheManifestNetworkItem(CacheManifest cacheManifest, string value)
 {
     cacheManifest.Network.Add(value);
 }
        /// <summary>
        /// populates a CacheManifest object from a string of cache manifest data.
        /// </summary>
        /// <param name="cacheManifest"></param>
        /// <param name="manifest"></param>
        private static void PopulateCacheManifest(CacheManifest cacheManifest, string manifest)
        {
            if (string.IsNullOrEmpty(manifest))
            {
                throw new ArgumentNullException("manifest");
            }
            if (cacheManifest == null)
            {
                throw new ArgumentNullException("cacheManifest");
            }

            DelegateAddCacheManifestItem addCacheManifestItem;

            // Add code to validate manifest string

            // default mode to Cache
            addCacheManifestItem = AddCacheManifestCacheItem;

            // break string into lines array
            char[] delimiters = { '\r', '\n' };
            var    lines      = manifest.Split(delimiters).Where(line => !string.IsNullOrEmpty(line));

            var first = lines.First().Trim(Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble(), 0, Encoding.UTF8.GetPreamble().Length).ToCharArray());

            if (first.Trim() != "CACHE MANIFEST")
            {
                throw new Exception("Invalid Cache manifest");
            }

            // strip string of comment lines and empty lines
            foreach (String s in lines)
            {
                // skip first line of file string. (i.e "CACHE MANIFEST" )
                if (s == lines.First())
                {
                    continue;
                }

                string line = s.Trim();

                if (string.IsNullOrEmpty(line) || line.StartsWith("#"))
                {
                    continue;
                }

                switch (line.ToUpper())
                {
                case "NETWORK:":
                    addCacheManifestItem = AddCacheManifestNetworkItem;
                    continue;

                case "CACHE:":
                    addCacheManifestItem = AddCacheManifestCacheItem;
                    continue;

                case "FALLBACK:":
                    addCacheManifestItem = AddCacheManifestFallbackItem;
                    continue;
                }
                // add string to mode
                addCacheManifestItem(cacheManifest, line);
            }
        }