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 */
016package org.ametys.web.workspace;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.parameters.Parameters;
021import org.apache.cocoon.environment.ObjectModelHelper;
022import org.apache.cocoon.environment.Request;
023import org.apache.cocoon.sitemap.PatternException;
024
025import org.ametys.runtime.workspace.Workspace;
026
027/**
028 * Same as {@link FrontAwareWorkspaceMatcher} but that :<br>
029 * <ul>
030 *    <li>add an offset in wildcard expression,</li>
031 *    <li>will refuse workspaces that are not front-aware,</li>
032 *    <li>will set the "site" attribute</li>
033 * </ul> 
034 */
035public class FrontAwareWorkspaceMatcher extends org.ametys.runtime.workspace.WorkspaceMatcher
036{
037    @Override
038    protected int getWildcardStartIndex()
039    {
040        return 2;
041    }
042    
043    @Override
044    protected Workspace getWorkspace(String workspaceName)
045    {
046        Workspace workspace = super.getWorkspace(workspaceName);
047        if (workspace != null)
048        {
049            // We only want to authorize "front" tagged workspaces... thoses who may know that they are exposed
050            if (!workspace.getTags().contains("front"))
051            {
052                return null;
053            }
054        }
055        
056        return workspace;
057    }
058    
059    @Override
060    public Map match(String pattern, Map objectModel, Parameters parameters) throws PatternException
061    {
062        Map result = super.match(pattern, objectModel, parameters);
063        if (result != null)
064        {
065            // Set additionnal response
066            Request request = ObjectModelHelper.getRequest(objectModel);
067            request.setAttribute("site", result.get("1"));
068        }
069        return result;
070    }
071}