// Register an assembly as an application module, which makes its compiled web pages
        // and embedded resources available
        internal void Register(ApplicationPart applicationPart, Func<object> registerPageAction) {
            // Throw if this assembly has been registered
            if (_applicationParts.ContainsKey(applicationPart.Assembly)) {
                throw new InvalidOperationException(
                    String.Format(CultureInfo.CurrentCulture,
                    WebPageResources.ApplicationPart_ModuleAlreadyRegistered, applicationPart.Assembly));
            }

            // Throw if the virtual path is already in use
            if (_registeredVirtualPaths.ContainsKey(applicationPart.RootVirtualPath)) {
                throw new InvalidOperationException(
                    String.Format(CultureInfo.CurrentCulture,
                    WebPageResources.ApplicationPart_ModuleAlreadyRegisteredForVirtualPath, applicationPart.RootVirtualPath));
            }

            // REVIEW: Should we register the app-part after we scan the assembly for webpages?
            // Add the part to the list
            if (_applicationParts.TryAdd(applicationPart.Assembly, applicationPart)) {
                // We don't really care about the value
                _registeredVirtualPaths.TryAdd(applicationPart.RootVirtualPath, true);

                // Get all of the web page types
                var webPageTypes = from type in applicationPart.Assembly.GetTypes()
                                   where type.IsSubclassOf(_webPageType)
                                   select type;

                // Register each of page with the plan9
                foreach (Type webPageType in webPageTypes) {
                    RegisterWebPage(applicationPart, webPageType, registerPageAction);
                }
            }
        }
Beispiel #2
0
 public static Exception PublishingFailedBecauseVariableValueWasNotPresent(
     ApplicationPart part,
     Component component,
     string variableName) =>
 new PublishingException(
     $"Could not publish application part {part.Name}. No variable with name ${variableName} " +
     $"was found");
Beispiel #3
0
        /// <summary>
        /// Create the view engine.
        /// </summary>
        public static void Init()
        {
            // Create new precompiled view engine
            var engine = new PrecompiledMvcEngine(typeof(ManagerModule).Assembly)
            {
                UsePhysicalViewsIfNewer = true
            };

            engine.PartialViewLocationFormats = engine.PartialViewLocationFormats.Union(ExtensionsFolder).ToArray();
            var standard = new RazorViewEngine();

            standard.PartialViewLocationFormats = standard.PartialViewLocationFormats.Union(ExtensionsFolder).ToArray();

            ViewEngines.Engines.Insert(0, standard);
            ViewEngines.Engines.Insert(1, engine);

            VirtualPathFactoryManager.RegisterVirtualPathFactory(engine);

            // Register the manager area
            var manager = new ManagerRegistration();
            var context = new AreaRegistrationContext(manager.AreaName, RouteTable.Routes);

            manager.RegisterArea(context);

            // Register custom model binders
            RegisterBinders();

            // Register json deserialization for post data
            ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());

            // Register application part
            ApplicationPart.Register(new ApplicationPart(typeof(ManagerModule).Assembly, "~/"));
        }
Beispiel #4
0
 internal static void RegisterAdminModule()
 {
     // Add a admin module as an application module (precompiled)
     ApplicationPart.Register(
         new ApplicationPart(typeof(SiteAdmin).Assembly, AdminVirtualPath)
         );
 }
        private void RegisterWebPage(
            ApplicationPart module,
            Type webPageType,
            Func <object> registerPageAction
            )
        {
            var virtualPathAttribute = webPageType
                                       .GetCustomAttributes(typeof(PageVirtualPathAttribute), false)
                                       .Cast <PageVirtualPathAttribute>()
                                       .SingleOrDefault();

            // Ignore it if it doesn't have a PageVirtualPathAttribute
            if (virtualPathAttribute == null)
            {
                return;
            }

            // Get the path of the page relative to the module root
            string virtualPath = GetRootRelativeVirtualPath(
                module.RootVirtualPath,
                virtualPathAttribute.VirtualPath
                );

            // Create a factory for the page type
            Func <object> pageFactory = registerPageAction ?? NewTypeInstance(webPageType);

            // Register a page factory for it
            _virtualPathFactory.RegisterPath(virtualPath, pageFactory);
        }
Beispiel #6
0
 public static Exception ClaimFailedBecauseVariableValueWasNotPresent(
     ApplicationPart part,
     string environment,
     string variableName)
 => new ClaimVersionFailedException(
     $"Could not claim application part {part.Name}. No value for variable ${variableName} " +
     $"in env {environment}");
Beispiel #7
0
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            // Get the package name and static resource path from the route
            string partName = (string)requestContext.RouteData.GetRequiredString("module");

            // Try to find an application module by this name
            ApplicationPart module = _partRegistry[partName];

            // Throw an exception if we can't find the module by name
            if (module == null)
            {
                throw new InvalidOperationException(
                          String.Format(
                              CultureInfo.CurrentCulture,
                              WebPageResources.ApplicationPart_ModuleCannotBeFound,
                              partName
                              )
                          );
            }

            // Get the resource path
            string path = (string)requestContext.RouteData.GetRequiredString("path");

            // Return the resource handler for this module and path
            return(new ResourceHandler(module, path));
        }
Beispiel #8
0
        public void RemoveControllers(string pluginId)
        {
            ApplicationPart last = _applicationPartManager.ApplicationParts.First(m => m.Name == pluginId);

            _applicationPartManager.ApplicationParts.Remove(last);

            ResetControllActions();
        }
Beispiel #9
0
        public void DisableModule(string moduleName)
        {
            ApplicationPart last = _partManager.ApplicationParts.First(p => p.Name == moduleName);

            _partManager.ApplicationParts.Remove(last);

            ResetControllActions();
        }
Beispiel #10
0
        private static CompiledRazorAssemblyPart GetViewsPartOrNull(ApplicationPartManager partManager,
                                                                    ApplicationPart assemblyPart)
        {
            var viewsAssemblyName = assemblyPart.Name + ".Views";

            return(partManager
                   .ApplicationParts
                   .OfType <CompiledRazorAssemblyPart>()
                   .FirstOrDefault(p => p.Name == viewsAssemblyName));
        }
    public async Task <IEnumerable <DeployedEnvironment> > GetDeployments(
        [Service] IPublishingService service,
        [Parent] ApplicationPart applicationPart,
        CancellationToken cancellationToken)
    {
        IReadOnlyList <Environment> envs = await service
                                           .GetDeployedEnvironmentByPartIdAsync(applicationPart.Id, cancellationToken);

        return(envs.Select(x => new DeployedEnvironment(applicationPart.Id, x.Id)));
    }
Beispiel #12
0
        public void PopulateFeature(ApplicationPart applicationPart, ControllerFeature feature)
        {
            if (feature == null)
            {
                throw new ArgumentNullException(nameof(feature));
            }

            foreach (var provider in this._applicationFeatureProviders)
            {
                provider.PopulateFeature(applicationPart, feature);
            }
        }
        public static void Start()
        {
            SiteAdmin.Register("~/Rabbit/", "Rabbit Framework Admin",
                               "Administrative Tools to manage modules, to generate code and to run unit tests.");

            var appPart = new ApplicationPart(typeof(PreApplicationStartCode).Assembly,
                                              SiteAdmin.GetVirtualPath("~/Rabbit/"));

            ApplicationPart.Register(appPart);

            SiteEngine.Start();
        }
Beispiel #14
0
        public void DisableModule(string moduleName)
        {
            ApplicationPart last = _partManager.ApplicationParts.First(p => p.Name == moduleName);

            _partManager.ApplicationParts.Remove(last);

            CollectibleAssemblyLoadContext context = PluginsLoadContexts.Get(moduleName);

            context.Disable();

            ResetControllActions();
        }
        public ResourceHandler(ApplicationPart applicationPart, string path) {
            if (applicationPart == null) {
                throw new ArgumentNullException("applicationPart");
            }

            if (String.IsNullOrEmpty(path)) {
                throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "path");
            }

            _applicationPart = applicationPart;
            _path = path;
        }
Beispiel #16
0
        public void GetResourceNameFromVirtualPathForItemInSubDir()
        {
            // Arrange
            var moduleName = "my-module";
            var path       = "/bar/foo";

            // Act
            var name = ApplicationPart.GetResourceNameFromVirtualPath(moduleName, path);

            // Assert
            Assert.Equal(name, "my-module.bar.foo");
        }
Beispiel #17
0
        public void GetResourceNameFromVirtualPathForTopLevelPath()
        {
            // Arrange
            var moduleName = "my-module";
            var path       = "foo.baz";

            // Act
            var name = ApplicationPart.GetResourceNameFromVirtualPath(moduleName, path);

            // Assert
            Assert.Equal(name, moduleName + "." + path);
        }
Beispiel #18
0
        public void GetResourceNameFromVirtualPathForItemWithSpaces()
        {
            // Arrange
            var moduleName = "my-module";
            var path       = "/program files/data files/my file .foo";

            // Act
            var name = ApplicationPart.GetResourceNameFromVirtualPath(moduleName, path);

            // Assert
            Assert.Equal(name, "my-module.program_files.data_files.my file .foo");
        }
Beispiel #19
0
        public void GetResourceVirtualPathEncodesModuleName()
        {
            // Arrange
            var moduleName = "Debugger Package v?&%";
            var moduleRoot = "~/root-path/sub-path";
            var path       = moduleRoot + "/foo.txt";

            // Act
            var virtualPath = ApplicationPart.GetResourceVirtualPath(moduleName, moduleRoot, path);

            // Assert
            Assert.Equal(virtualPath, "~/r.ashx/" + "Debugger%20Package%20v?&%" + "/" + "foo.txt");
        }
        public void ResourceHandlerThrows404IfResourceNotFound() {
            // Arrange
            var applicationPart = new ApplicationPart(BuildAssembly(), "~/my-app-assembly");
            MemoryStream stream = new MemoryStream();
            var response = new Mock<HttpResponseBase>();
            response.SetupGet(c => c.OutputStream).Returns(stream);
            response.SetupSet(c => c.ContentType = "image/jpeg").Verifiable();
            var resourceHandler = new ResourceHandler(applicationPart, "does-not-exist");

            // Act and Assert
            ExceptionAssert.Throws<HttpException>(() => resourceHandler.ProcessRequest(response.Object),
                "The resource file \"does-not-exist\" could not be found.");
        }
Beispiel #21
0
        public void ResolveVirtualPathResolvesAppRelativePathsUsingAppVirtualPath()
        {
            // Arrange
            var basePath    = "~/base";
            var path        = "@/somefile";
            var appPartRoot = "~/app/";

            // Act
            var virtualPath = ApplicationPart.ResolveVirtualPath(appPartRoot, basePath, path);

            // Assert
            Assert.Equal(virtualPath, "~/app/somefile");
        }
Beispiel #22
0
        public void ResolveVirtualPathResolvesRegularPathsUsingBaseVirtualPath()
        {
            // Arrange
            var basePath    = "~/base/";
            var path        = "somefile";
            var appPartRoot = "~/app/";

            // Act
            var virtualPath = ApplicationPart.ResolveVirtualPath(appPartRoot, basePath, path);

            // Assert
            Assert.Equal("~/base/somefile", virtualPath);
        }
Beispiel #23
0
        public void ResolveVirtualPathDoesNotAffectRootRelativePaths()
        {
            // Arrange
            var basePath    = "~/base";
            var path        = "~/somefile";
            var appPartRoot = "~/app/";

            // Act
            var virtualPath = ApplicationPart.ResolveVirtualPath(appPartRoot, basePath, path);

            // Assert
            Assert.Equal(virtualPath, "~/somefile");
        }
Beispiel #24
0
        public void GetResourceVirtualPathForTopLevelItemAndNestedModuleRootPath()
        {
            // Arrange
            var moduleName = "my-module";
            var moduleRoot = "~/root-path/sub-path";
            var path       = moduleRoot + "/foo.txt";

            // Act
            var virtualPath = ApplicationPart.GetResourceVirtualPath(moduleName, moduleRoot, path);

            // Assert
            Assert.Equal(virtualPath, "~/r.ashx/" + moduleName + "/" + "foo.txt");
        }
        public void ApplicationPartRegistryLooksUpPartsByAssembly() {
            // Arrange
            var assembly = BuildAssembly();
            var part = new ApplicationPart(assembly, "~/mymodule");
            var dictionary = new DictionaryBasedVirtualPathFactory();
            var registry = new ApplicationPartRegistry(dictionary);
            Func<object> myFunc = () => "foo";

            // Act
            registry.Register(part, myFunc);

            // Assert
            Assert.AreEqual(registry[assembly], part);
        }
        public void RegisterThrowsIfAssemblyAlreadyRegistered() {
            // Arrange
            var part = new ApplicationPart(BuildAssembly(), "~/mymodule");
            var dictionary = new DictionaryBasedVirtualPathFactory();
            var registry = new ApplicationPartRegistry(dictionary);
            Func<object> myFunc = () => "foo";

            // Act
            registry.Register(part, myFunc);

            // Assert
            ExceptionAssert.Throws<InvalidOperationException>(() => registry.Register(part, myFunc),
                String.Format("The assembly \"{0}\" is already registered.", part.Assembly.ToString()));
        }
Beispiel #27
0
        public void GetResourceVirtualPathForItemPathWithParameters()
        {
            // Arrange
            var moduleName = "DebuggerPackage";
            var moduleRoot = "~/root-path/sub-path";
            var itemPath   = "some-path/some-more-please/foo.jpg?size=45&height=20";
            var path       = moduleRoot + "/" + itemPath;

            // Act
            var virtualPath = ApplicationPart.GetResourceVirtualPath(moduleName, moduleRoot, path);

            // Assert
            Assert.Equal(virtualPath, "~/r.ashx/" + moduleName + "/" + itemPath);
        }
Beispiel #28
0
        public void GetResourceVirtualPathForNestedItemPath()
        {
            // Arrange
            var moduleName = "DebuggerPackage";
            var moduleRoot = "~/root-path/sub-path";
            var itemPath   = "some-path/some-more-please/foo.txt";
            var path       = moduleRoot + "/" + itemPath;

            // Act
            var virtualPath = ApplicationPart.GetResourceVirtualPath(moduleName, moduleRoot, path);

            // Assert
            Assert.Equal(virtualPath, "~/r.ashx/" + moduleName + "/" + itemPath);
        }
 public void PopulateFeature(ApplicationPart parts, ControllerFeature feature)
 {
     if (parts.Types == null)
     {
         return;
     }
     foreach (var type in parts.Types)
     {
         if (type.IsGrain() && this.IsController(type))
         {
             feature.Controllers.Add(type);
         }
     }
 }
Beispiel #30
0
        public void ControllerFeature_should_have_all_kind_of_close_generic_type_controller()
        {
            var parts   = new ApplicationPart[] { new AssemblyPart(typeof(UserController <>).GetTypeInfo().Assembly) };
            var feature = new ControllerFeature();

            new GenericControllerFeatureProvider().PopulateFeature(parts, feature);
            Assert.Equal(3, feature.Controllers.Count);
            var controllers   = feature.Controllers;
            var actualSummary = controllers.Contains(typeof(UserController <User>).GetTypeInfo()) &&
                                controllers.Contains(typeof(UserController <Owner>).GetTypeInfo()) &&
                                controllers.Contains(typeof(UserController <Admin>).GetTypeInfo());

            Assert.True(actualSummary);
        }
        public void ResourceHandlerThrows404IfResourceNotFound()
        {
            // Arrange
            var          applicationPart = new ApplicationPart(BuildAssembly(), "~/my-app-assembly");
            MemoryStream stream          = new MemoryStream();
            var          response        = new Mock <HttpResponseBase>();

            response.SetupGet(c => c.OutputStream).Returns(stream);
            response.SetupSet(c => c.ContentType = "image/jpeg").Verifiable();
            var resourceHandler = new ResourceHandler(applicationPart, "does-not-exist");

            // Act and Assert
            Assert.Throws <HttpException>(() => resourceHandler.ProcessRequest(response.Object),
                                          "The resource file \"does-not-exist\" could not be found.");
        }
        public void RegisterThrowsIfPathAlreadyRegistered() {
            // Arrange
            var part = new ApplicationPart(BuildAssembly(), "~/mymodule");
            var dictionary = new DictionaryBasedVirtualPathFactory();
            var registry = new ApplicationPartRegistry(dictionary);
            Func<object> myFunc = () => "foo";

            // Act
            registry.Register(part, myFunc);

            // Assert
            var newPart = new ApplicationPart(BuildAssembly("different-assembly"), "~/mymodule");
            ExceptionAssert.Throws<InvalidOperationException>(() => registry.Register(newPart, myFunc),
                "An application module is already registered for virtual path \"~/mymodule/\".");
        }
        public void ApplicationPartRegistryLooksUpPartsByName()
        {
            // Arrange
            var part = new ApplicationPart(BuildAssembly(), "~/mymodule");
            var dictionary = new DictionaryBasedVirtualPathFactory();
            var registry = new ApplicationPartRegistry(dictionary);
            Func<object> myFunc = () => "foo";

            // Act
            registry.Register(part, myFunc);

            // Assert
            Assert.Equal(registry["my-assembly"], part);
            Assert.Equal(registry["MY-aSSembly"], part);
        }
Beispiel #34
0
        public ResourceHandler(ApplicationPart applicationPart, string path)
        {
            if (applicationPart == null)
            {
                throw new ArgumentNullException("applicationPart");
            }

            if (String.IsNullOrEmpty(path))
            {
                throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "path");
            }

            _applicationPart = applicationPart;
            _path            = path;
        }
Beispiel #35
0
        private static IEnumerable <Type> GetApplicationControllerTypes()
        {
            var controllerProvider = new ControllerFeatureProvider();

            var applicationParts = new ApplicationPart[]
            {
                new AssemblyPart(typeof(HomeController).GetTypeInfo().Assembly)
            };

            var feature = new ControllerFeature();

            controllerProvider.PopulateFeature(applicationParts, feature);

            return(feature.Controllers.Select(c => c.AsType()));
        }
        public void RegisterThrowsIfAssemblyAlreadyRegistered()
        {
            // Arrange
            var           part       = new ApplicationPart(BuildAssembly(), "~/mymodule");
            var           dictionary = new DictionaryBasedVirtualPathFactory();
            var           registry   = new ApplicationPartRegistry(dictionary);
            Func <object> myFunc     = () => "foo";

            // Act
            registry.Register(part, myFunc);

            // Assert
            Assert.Throws <InvalidOperationException>(() => registry.Register(part, myFunc),
                                                      String.Format("The assembly \"{0}\" is already registered.", part.Assembly.ToString()));
        }
        public void ApplicationPartRegistryLooksUpPartsByAssembly()
        {
            // Arrange
            var           assembly   = BuildAssembly();
            var           part       = new ApplicationPart(assembly, "~/mymodule");
            var           dictionary = new DictionaryBasedVirtualPathFactory();
            var           registry   = new ApplicationPartRegistry(dictionary);
            Func <object> myFunc     = () => "foo";

            // Act
            registry.Register(part, myFunc);

            // Assert
            Assert.Equal(registry[assembly], part);
        }
        public void ResourceHandlerWritesContentsOfFileToStream() {
            // Arrange
            var applicationPart = new ApplicationPart(BuildAssembly(), "~/my-app-assembly");
            MemoryStream stream = new MemoryStream();
            var response = new Mock<HttpResponseBase>();
            response.SetupGet(c => c.OutputStream).Returns(stream);
            response.SetupSet(c => c.ContentType = "image/jpeg").Verifiable();
            var resourceHandler = new ResourceHandler(applicationPart, "bar.foo.jpg");

            // Act
            resourceHandler.ProcessRequest(response.Object);

            // Assert
            response.Verify();
            Assert.AreEqual(Encoding.Default.GetString(stream.ToArray()), _fileContent);
        }
        private void RegisterWebPage(ApplicationPart module, Type webPageType, Func<object> registerPageAction) {
            var virtualPathAttribute = webPageType.GetCustomAttributes(typeof(PageVirtualPathAttribute), false)
                                                  .Cast<PageVirtualPathAttribute>()
                                                  .SingleOrDefault();

            // Ignore it if it doesn't have a PageVirtualPathAttribute
            if (virtualPathAttribute == null) {
                return;
            }

            // Get the path of the page relative to the module root
            string virtualPath = GetRootRelativeVirtualPath(module.RootVirtualPath, virtualPathAttribute.VirtualPath);

            // Create a factory for the page type
            Func<object> pageFactory = registerPageAction ?? NewTypeInstance(webPageType);

            // Register a page factory for it
            _virtualPathFactory.RegisterPath(virtualPath, pageFactory);
        }
        public void RegisterCreatesRoutesForValidPages() {
            // Arrange
            var part = new ApplicationPart(BuildAssembly(), "~/mymodule");
            var dictionary = new DictionaryBasedVirtualPathFactory();
            var registry = new ApplicationPartRegistry(dictionary);
            Func<object> myFunc = () => "foo";

            // Act
            registry.Register(part, myFunc);

            // Assert
            Assert.IsTrue(dictionary.Exists("~/mymodule/Page1"));
            Assert.AreEqual(dictionary.CreateInstance("~/mymodule/Page1"), "foo");
            Assert.IsFalse(dictionary.Exists("~/mymodule/Page2"));
            Assert.IsFalse(dictionary.Exists("~/mymodule/Page3"));
        }
 // Register an assembly as an application module, which makes its compiled web pages
 // and embedded resources available
 public void Register(ApplicationPart applicationPart)
 {
     Register(applicationPart, registerPageAction: null); // Use default action which creates a new page
 }