MultiSiteConfiguration

Introduction

This page explains how to set up Brix CMS for multiple websites (domains). Sometimes we may want to run multiple websites with one webapp instance of Brix.

Details

First make the domains (workspaces in Brix) configurable in ‘application.properties’, found in ‘/src/main/resources/brix/demo’

    workspaceName_1 = localhost
    workspaceName_2 = www.mysite1.com
    workspaceName_3 = www.mysite2.com

Next configure the UriMapper? in brix.demo.web.WicketApplication

    ... 
    // create uri mapper for the cms 
    UriMapper mapper = new PrefixUriMapper(Path.ROOT) 
    { 
        public Workspace getWorkspaceForRequest(WebRequestCycle requestCycle, Brix brix) 
        { 
            final WebRequest req = requestCycle.getWebRequest(); 
            final String name = req.getHttpServletRequest().getServerName();  // #1
            SitePlugin sitePlugin = SitePlugin.get(brix); 
            return sitePlugin.getSiteWorkspace(name, "");  // #2 
        } 
    }; 
...
  1. Get the domain name from the request
  2. Get the workspace matching what we got from the request, we configured this in application.properties
  3. Now finally we will initialise the workspaces for the configured domains
    ...
    initializeRepository();
    initializeWorkspaces();  // #1
    ...

    private void initWorkspaces()
    {
        try 
        { 
            final String defaultState = ""; 
            final List workspaceNames = getProperties().getWorkspaceNames();  // #2
            final SitePlugin sp = SitePlugin.get(brix); 

            for (Iterator iter = workspaceNames.iterator(); iter.hasNext();)  // #3
            { 
                String wn = (String) iter.next(); 
                if (!sp.siteExists(wn, defaultState)) 
                { 
                    Workspace w = sp.createSite(wn, defaultState); 
                    JcrSession session = brix.getCurrentSession(w.getId()); 
                    session.importXML( 
                        "/", 
                        getClass().getResourceAsStream("workspace.xml"), 
                        ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING); 
                    brix.initWorkspace(w, session); 
                    session.save(); 
                } 
            } 
        } 
        catch (Exception e) 
        { 
            throw new RuntimeException("Could not initialize jackrabbit workspace with Brix", e); 
        } 
    }
    ...
  1. Rename the method to initializeWorkspaces
  2. Get our list of workspaces from ‘application.properties’ using a new method we will implement in brix.demo.ApplicationProperties
  3. Iterate over all of our workspaces to initialise them Add the new method to the ‘ApplicationProperties’
    ...
    @SuppressWarnings("unchecked") 
    public List getWorkspaceNames() 
    { 
        ArrayList wns = new ArrayList(); 
        Enumeration<String> keys = (Enumeration<String>) properties.propertyNames(); 
        while (keys.hasMoreElements()) 
        { 
            String key = (String) keys.nextElement(); 
            if (key.startsWith("workspaceName_"))
            { 
                String value = properties.getProperty(key); 
                wns.add(value); 
            } 
        } 
        return wns; 
    }

Thats it, each domain or site now has its own workspace. To test this change the text on the default start page. If you go to ‘http://localhost:8080/’ in your browser you will get the ‘localhost’ workspace.

If you do not want the select box which allows you to switch between configured sites in your webapp just modify brix.demo.web.DemoAuthorizationStrategy

    ...
    /** {@inheritDoc} */ 
    public boolean isActionAuthorized(Action action) 
    { 
        if (action instanceof AccessWorkspaceSwitcherToolbarAction) 
        { 
                return false; 
        } 
        return true; 
    } 
    ...

This is a unique website which will require a more modern browser to work!

Please upgrade today!