示例#1
0
 private static void WriteScript(Assembly assembly,
                                 ScriptResourceInfo resourceInfo, ScriptResourceInfo releaseResourceInfo,
                                 ResourceManager resourceManager, ResourceSet neutralSet,
                                 ResourceManager releaseResourceManager, ResourceSet releaseNeutralSet,
                                 bool zip, StringBuilder output)
 {
     using (StreamReader reader = new StreamReader(
                assembly.GetManifestResourceStream(resourceInfo.ScriptName), true)) {
         if (resourceInfo.IsDebug)
         {
             // Output version information
             AssemblyName assemblyName = assembly.GetName();
             output.AppendLine("// Name:        " + resourceInfo.ScriptName);
             output.AppendLine("// Assembly:    " + assemblyName.Name);
             output.AppendLine("// Version:     " + assemblyName.Version.ToString());
             output.AppendLine("// FileVersion: " + AssemblyUtil.GetAssemblyFileVersion(assembly));
         }
         if (resourceInfo.PerformSubstitution)
         {
             CopyScriptToStringBuilderWithSubstitution(
                 reader.ReadToEnd(), assembly, zip, output);
         }
         else
         {
             output.Append(reader.ReadToEnd());
         }
         WriteResourceToStringBuilder(resourceInfo, releaseResourceInfo,
                                      resourceManager, neutralSet,
                                      releaseResourceManager, releaseNeutralSet,
                                      output);
     }
 }
示例#2
0
        private string GetUrlForCdn(ScriptManager scriptManager, string resourceName, Assembly assembly, bool hasDebugResource)
        {
            // if EnableCdn, then url always comes from mapping.Cdn[Debug]Path or WRA.CdnPath, if available.
            // first see if the script description mapping has a cdn path defined
            bool isDebuggingEnabled = IsDebuggingEnabled(scriptManager);
            bool isAssemblyResource = !String.IsNullOrEmpty(resourceName);
            bool secureConnection   = scriptManager.IsSecureConnection;

            isDebuggingEnabled = isDebuggingEnabled && (hasDebugResource || !isAssemblyResource);
            string cdnPath = isDebuggingEnabled ?
                             (secureConnection ? ScriptInfo.CdnDebugPathSecureConnection : ScriptInfo.CdnDebugPath) :
                             (secureConnection ? ScriptInfo.CdnPathSecureConnection : ScriptInfo.CdnPath);

            // then see if the WebResourceAttribute for the resource has one
            // EXCEPT when the ScriptInfo has a cdnpath but it wasn't selected due to this being a secure connection
            // and it does not support secure connections. Avoid having the HTTP cdn path come from the mapping and the
            // HTTPS path come from the WRA.
            if (isAssemblyResource && String.IsNullOrEmpty(cdnPath) && String.IsNullOrEmpty(isDebuggingEnabled ? ScriptInfo.CdnDebugPath : ScriptInfo.CdnPath))
            {
                ScriptResourceInfo scriptResourceInfo = ScriptResourceInfo.GetInstance(assembly, resourceName);
                if (scriptResourceInfo != null)
                {
                    cdnPath = secureConnection ? scriptResourceInfo.CdnPathSecureConnection : scriptResourceInfo.CdnPath;
                }
            }
            return(String.IsNullOrEmpty(cdnPath) ? null : ClientUrlResolver.ResolveClientUrl(AddCultureName(scriptManager, cdnPath)));
        }
示例#3
0
            public ScriptEffectiveInfo(ScriptReference scriptReference)
            {
                ScriptResourceDefinition definition =
                    ScriptManager.ScriptResourceMapping.GetDefinition(scriptReference);
                string   name     = scriptReference.Name;
                string   path     = scriptReference.Path;
                Assembly assembly = scriptReference.GetAssembly();

                if (definition != null)
                {
                    if (String.IsNullOrEmpty(path))
                    {
                        // only when the SR has no path, the mapping's path and debug path, if any, apply
                        path       = definition.Path;
                        _debugPath = definition.DebugPath;
                    }
                    name                          = definition.ResourceName;
                    assembly                      = definition.ResourceAssembly;
                    _cdnPath                      = definition.CdnPath;
                    _cdnDebugPath                 = definition.CdnDebugPath;
                    _cdnPathSecureConnection      = definition.CdnPathSecureConnection;
                    _cdnDebugPathSecureConnection = definition.CdnDebugPathSecureConnection;
                    LoadSuccessExpression         = definition.LoadSuccessExpression;
                }
                else if ((assembly == null) && !String.IsNullOrEmpty(name))
                {
                    // name is set and there is no mapping, default to SWE for assembly
                    assembly = AssemblyCache.SystemWebExtensions;
                }
                _resourceName = name;
                _assembly     = assembly;
                _path         = path;

                if (assembly != null && !String.IsNullOrEmpty(name) && String.IsNullOrEmpty(LoadSuccessExpression))
                {
                    var scriptResourceInfo = ScriptResourceInfo.GetInstance(assembly, name);
                    if (scriptResourceInfo != null)
                    {
                        LoadSuccessExpression = scriptResourceInfo.LoadSuccessExpression;
                    }
                }
            }
示例#4
0
 private static void WriteResourceToStringBuilder(
     ScriptResourceInfo resourceInfo,
     ScriptResourceInfo releaseResourceInfo,
     ResourceManager resourceManager,
     ResourceSet neutralSet,
     ResourceManager releaseResourceManager,
     ResourceSet releaseNeutralSet,
     StringBuilder builder)
 {
     if ((resourceManager != null) || (releaseResourceManager != null))
     {
         string typeName = resourceInfo.TypeName;
         if (String.IsNullOrEmpty(typeName))
         {
             typeName = releaseResourceInfo.TypeName;
         }
         WriteResources(builder, typeName, resourceManager, neutralSet,
                        releaseResourceManager, releaseNeutralSet, resourceInfo.IsDebug);
     }
 }
        private static void WriteScript(Assembly assembly,
            ScriptResourceInfo resourceInfo, ScriptResourceInfo releaseResourceInfo,
            ResourceManager resourceManager, ResourceSet neutralSet,
            ResourceManager releaseResourceManager, ResourceSet releaseNeutralSet,
            bool zip, StringBuilder output) {

            using (StreamReader reader = new StreamReader(
                assembly.GetManifestResourceStream(resourceInfo.ScriptName), true)) {

                if (resourceInfo.IsDebug) {
                    // Output version information
                    AssemblyName assemblyName = assembly.GetName();
                    output.AppendLine("// Name:        " + resourceInfo.ScriptName);
                    output.AppendLine("// Assembly:    " + assemblyName.Name);
                    output.AppendLine("// Version:     " + assemblyName.Version.ToString());
                    output.AppendLine("// FileVersion: " + AssemblyUtil.GetAssemblyFileVersion(assembly));
                }
                if (resourceInfo.PerformSubstitution) {
                    CopyScriptToStringBuilderWithSubstitution(
                        reader.ReadToEnd(), assembly, zip, output);
                }
                else {
                    output.Append(reader.ReadToEnd());
                }
                WriteResourceToStringBuilder(resourceInfo, releaseResourceInfo, 
                    resourceManager, neutralSet, 
                    releaseResourceManager, releaseNeutralSet, 
                    output);
            }
        }
        private static void WriteResourceToStringBuilder(
            ScriptResourceInfo resourceInfo, 
            ScriptResourceInfo releaseResourceInfo, 
            ResourceManager resourceManager, 
            ResourceSet neutralSet, 
            ResourceManager releaseResourceManager, 
            ResourceSet releaseNeutralSet, 
            StringBuilder builder) {

            if ((resourceManager != null) || (releaseResourceManager != null)) {
                string typeName = resourceInfo.TypeName;
                if (String.IsNullOrEmpty(typeName)) {
                    typeName = releaseResourceInfo.TypeName;
                }
                WriteResources(builder, typeName, resourceManager, neutralSet,
                    releaseResourceManager, releaseNeutralSet, resourceInfo.IsDebug);
            }
        }
        public static ScriptResourceInfo GetInstance(Assembly assembly, string resourceName)
        {
            // The first time this API is called, check for attributes that point to the same script
            if (!_duplicateScriptAttributesChecked.Contains(assembly))
            {
                Dictionary <string, bool> scripts = new Dictionary <string, bool>();
                foreach (ScriptResourceAttribute attr
                         in assembly.GetCustomAttributes(typeof(ScriptResourceAttribute), false))
                {
                    string scriptName = attr.ScriptName;
                    if (scripts.ContainsKey(scriptName))
                    {
                        throw new InvalidOperationException(
                                  String.Format(CultureInfo.CurrentCulture,
                                                AtlasWeb.ScriptResourceHandler_DuplicateScriptResources,
                                                scriptName, assembly.GetName()));
                    }
                    scripts.Add(scriptName, true);
                }

                _duplicateScriptAttributesChecked[assembly] = true;
            }
            Tuple <Assembly, string> cacheKey     = new Tuple <Assembly, string>(assembly, resourceName);
            ScriptResourceInfo       resourceInfo = (ScriptResourceInfo)_scriptCache[cacheKey];

            if (resourceInfo == null)
            {
                WebResourceAttribute    webResourceAttribute    = null;
                ScriptResourceAttribute scriptResourceAttribute = null;
                // look for a WebResourceAttribute with that name
                object[] attrs = assembly.GetCustomAttributes(typeof(WebResourceAttribute), false);
                foreach (WebResourceAttribute wra in attrs)
                {
                    if (String.Equals(wra.WebResource, resourceName, StringComparison.Ordinal))
                    {
                        webResourceAttribute = wra;
                        break;
                    }
                }
                if (webResourceAttribute != null)
                {
                    // look for a script resource attribute with that name
                    attrs = assembly.GetCustomAttributes(typeof(ScriptResourceAttribute), false);
                    foreach (ScriptResourceAttribute sra in attrs)
                    {
                        if (String.Equals(sra.ScriptName, resourceName, StringComparison.Ordinal))
                        {
                            scriptResourceAttribute = sra;
                            break;
                        }
                    }
                    resourceInfo = new ScriptResourceInfo(webResourceAttribute, scriptResourceAttribute, assembly);
                }
                else
                {
                    resourceInfo = ScriptResourceInfo.Empty;
                }
                // Cache the results so we don't have to do this again
                _scriptCache[cacheKey] = resourceInfo;
            }
            return(resourceInfo);
        }
示例#8
0
        internal static string GetScriptFromWebResourceInternal(
            Assembly assembly, string resourceName, CultureInfo culture,
            bool zip, out string contentType)
        {
            ScriptResourceInfo resourceInfo        = ScriptResourceInfo.GetInstance(assembly, resourceName);
            ScriptResourceInfo releaseResourceInfo = null;

            if (resourceName.EndsWith(".debug.js", StringComparison.OrdinalIgnoreCase))
            {
                // This is a debug script, we'll need to merge the debug resource
                // with the release one.
                string releaseResourceName = resourceName.Substring(0, resourceName.Length - 9) + ".js";
                releaseResourceInfo = ScriptResourceInfo.GetInstance(assembly, releaseResourceName);
            }
            if ((resourceInfo == ScriptResourceInfo.Empty) &&
                ((releaseResourceInfo == null) || (releaseResourceInfo == ScriptResourceInfo.Empty)))
            {
                throw new HttpException(AtlasWeb.ScriptResourceHandler_InvalidRequest);
            }

            ResourceManager resourceManager        = null;
            ResourceSet     neutralSet             = null;
            ResourceManager releaseResourceManager = null;
            ResourceSet     releaseNeutralSet      = null;
            CultureInfo     previousCulture        = Thread.CurrentThread.CurrentUICulture;

            try {
                Thread.CurrentThread.CurrentUICulture = culture;

                if (!String.IsNullOrEmpty(resourceInfo.ScriptResourceName))
                {
                    resourceManager = GetResourceManager(resourceInfo.ScriptResourceName, assembly);
                    // The following may throw MissingManifestResourceException
                    neutralSet = resourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, true);
                }
                if ((releaseResourceInfo != null) &&
                    !String.IsNullOrEmpty(releaseResourceInfo.ScriptResourceName))
                {
                    releaseResourceManager = GetResourceManager(releaseResourceInfo.ScriptResourceName, assembly);
                    releaseNeutralSet      = releaseResourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, true);
                }
                if ((releaseResourceInfo != null) &&
                    !String.IsNullOrEmpty(releaseResourceInfo.ScriptResourceName) &&
                    !String.IsNullOrEmpty(resourceInfo.ScriptResourceName) &&
                    (releaseResourceInfo.TypeName != resourceInfo.TypeName))
                {
                    throw new HttpException(String.Format(
                                                CultureInfo.CurrentCulture,
                                                AtlasWeb.ScriptResourceHandler_TypeNameMismatch,
                                                releaseResourceInfo.ScriptResourceName));
                }

                StringBuilder builder = new StringBuilder();
                WriteScript(assembly,
                            resourceInfo, releaseResourceInfo,
                            resourceManager, neutralSet,
                            releaseResourceManager, releaseNeutralSet,
                            zip, builder);
                contentType = resourceInfo.ContentType;
                return(builder.ToString());
            }
            finally {
                Thread.CurrentThread.CurrentUICulture = previousCulture;

                if (releaseNeutralSet != null)
                {
                    releaseNeutralSet.Dispose();
                }
                if (neutralSet != null)
                {
                    neutralSet.Dispose();
                }
            }
        }
        public static ScriptResourceInfo GetInstance(Assembly assembly, string resourceName) {
            // The first time this API is called, check for attributes that point to the same script
            if (!_duplicateScriptAttributesChecked.Contains(assembly)) {
                Dictionary<string, bool> scripts = new Dictionary<string, bool>();
                foreach (ScriptResourceAttribute attr
                    in assembly.GetCustomAttributes(typeof(ScriptResourceAttribute), false)) {

                    string scriptName = attr.ScriptName;
                    if (scripts.ContainsKey(scriptName)) {
                        throw new InvalidOperationException(
                            String.Format(CultureInfo.CurrentCulture,
                                  AtlasWeb.ScriptResourceHandler_DuplicateScriptResources,
                                  scriptName, assembly.GetName()));
                    }
                    scripts.Add(scriptName, true);
                }

                _duplicateScriptAttributesChecked[assembly] = true;
            }
            Tuple<Assembly, string> cacheKey = new Tuple<Assembly, string>(assembly, resourceName);
            ScriptResourceInfo resourceInfo = (ScriptResourceInfo)_scriptCache[cacheKey];
            if (resourceInfo == null) {
                WebResourceAttribute webResourceAttribute = null;
                ScriptResourceAttribute scriptResourceAttribute = null;
                // look for a WebResourceAttribute with that name
                object[] attrs = assembly.GetCustomAttributes(typeof(WebResourceAttribute), false);
                foreach (WebResourceAttribute wra in attrs) {
                    if (String.Equals(wra.WebResource, resourceName, StringComparison.Ordinal)) {
                        webResourceAttribute = wra;
                        break;
                    }
                }
                if (webResourceAttribute != null) {
                    // look for a script resource attribute with that name
                    attrs = assembly.GetCustomAttributes(typeof(ScriptResourceAttribute), false);
                    foreach (ScriptResourceAttribute sra in attrs) {
                        if (String.Equals(sra.ScriptName, resourceName, StringComparison.Ordinal)) {
                            scriptResourceAttribute = sra;
                            break;
                        }
                    }
                    resourceInfo = new ScriptResourceInfo(webResourceAttribute, scriptResourceAttribute, assembly);
                }
                else {
                    resourceInfo = ScriptResourceInfo.Empty;
                }
                // Cache the results so we don't have to do this again
                _scriptCache[cacheKey] = resourceInfo;
            }
            return resourceInfo;
        }