001/*
002 *  Copyright 2017 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016
017package org.ametys.runtime.workspace;
018
019import java.util.Map;
020
021import org.apache.avalon.framework.parameters.Parameters;
022import org.apache.cocoon.environment.ObjectModelHelper;
023import org.apache.cocoon.environment.Request;
024import org.apache.cocoon.matching.WildcardURIMatcher;
025import org.apache.cocoon.sitemap.PatternException;
026
027import org.ametys.runtime.servlet.RuntimeConfig;
028
029/**
030 * Workspace aware Cocoon matcher.<br>
031 * Retrieve workspaces URI from names through the WorkspaceManager.
032 */
033public class WorkspaceMatcher extends WildcardURIMatcher
034{
035    /** The request attribute name where the current workspace is saved */
036    public static final String WORKSPACE_NAME = "workspaceName";
037    /** The request attribute name where the current workspace uri is saved */
038    public static final String WORKSPACE_URI = "workspaceURI";
039    /** The request attribute name where the current url inside the workspace is saved */
040    public static final String IN_WORKSPACE_URL = "inWorkspaceURL";
041    /** The request attribute name where the current workspace theme name is saved */
042    public static final String WORKSPACE_THEME = "workspaceTheme";
043    /** The request attribute name where the current workspace theme url is saved */
044    public static final String WORKSPACE_THEME_URL = "workspaceThemeURL";
045    
046    /**
047     * In the wildcard expression where to find the first relevant information
048     * @return An integer &gt; 0
049     */
050    protected int getWildcardStartIndex()
051    {
052        return 1;
053    }
054    
055    /**
056     * Get the selected workspace
057     * @param workspaceName The workspace name to get
058     * @return The workspace if it exists and is supported
059     */
060    protected Workspace getWorkspace(String workspaceName)
061    {
062        WorkspaceManager wm = WorkspaceManager.getInstance();
063        return wm.getWorkspaces().get(workspaceName);
064    }
065    
066    @Override
067    public Map match(String pattern, Map objectModel, Parameters parameters) throws PatternException
068    {
069        // Get the url wildcard part
070        Map<String, String> result = super.match(pattern, objectModel, parameters);
071        if (result == null)
072        {
073            return null;
074        }
075        
076        int wildcardStartIndex = getWildcardStartIndex();
077        
078        // Get the workspace name
079        String workspaceName;
080        boolean defaultWorkspace = "true".equals(parameters.getParameter("default", null)); 
081        if (defaultWorkspace)
082        {
083            workspaceName = RuntimeConfig.getInstance().getDefaultWorkspace();
084        }
085        else
086        {
087            workspaceName = result.get(Integer.toString(wildcardStartIndex)); 
088        }
089        
090        // Check and get workspace name
091        Workspace workspace = getWorkspace(workspaceName);
092        if (workspace == null)
093        {
094            return null;
095        }
096        
097        // Set response
098        Request request = ObjectModelHelper.getRequest(objectModel);
099        request.setAttribute(WORKSPACE_NAME, workspaceName);
100        request.setAttribute(WORKSPACE_URI, defaultWorkspace ? "" : "/_" + workspaceName);
101        request.setAttribute(IN_WORKSPACE_URL, result.get(Integer.toString(wildcardStartIndex + (defaultWorkspace ? 0 : 1))));
102        request.setAttribute(WORKSPACE_THEME, workspace.getThemeName());
103        request.setAttribute(WORKSPACE_THEME_URL, workspace.getThemeURL());
104        
105        result.put("workspaceName", workspaceName);
106        
107        return result;
108    }
109}