001/*
002 *  Copyright 2010 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.cms.repository;
017
018import org.apache.avalon.framework.context.Context;
019import org.apache.avalon.framework.context.ContextException;
020import org.apache.avalon.framework.context.Contextualizable;
021import org.apache.avalon.framework.logger.LogEnabled;
022import org.apache.avalon.framework.logger.Logger;
023import org.apache.cocoon.components.ContextHelper;
024import org.apache.cocoon.environment.Request;
025
026import org.ametys.plugins.repository.provider.DefaultWorkspaceSelector;
027
028/**
029 * {@link DefaultWorkspaceSelector} using a context attribute for selecting
030 * the workspace.
031 */
032public class RequestAttributeWorkspaceSelector extends DefaultWorkspaceSelector implements LogEnabled, Contextualizable
033{
034    private static final String __WORKSPACE_ATTRIBUTE = "jcr-workspace";
035    private Logger _logger;
036    private Context _context;
037
038    @Override
039    public void contextualize(Context context) throws ContextException
040    {
041        _context = context;
042    }
043    
044    @Override
045    public void enableLogging(Logger logger)
046    {
047        _logger = logger;
048    }
049    
050    /**
051     * Forces the workspace to use.
052     * @param request the current request
053     * @param workspaceName the workspace name.
054     */
055    public static void setForcedWorkspace(Request request, String workspaceName)
056    {
057        request.setAttribute(__WORKSPACE_ATTRIBUTE, workspaceName);
058    }
059    
060    /**
061     * Retrieves the workspace forced.
062     * @param request the current request
063     * @return the workspace forced or <code>null</code> for the default one.
064     */
065    public static String getForcedWorkspace(Request request)
066    {
067        return (String) request.getAttribute(__WORKSPACE_ATTRIBUTE);
068    }
069
070    @Override
071    public String getWorkspace()
072    {
073        String workspace;
074        try
075        {
076            Request request = (Request) _context.get(ContextHelper.CONTEXT_REQUEST_OBJECT);
077            workspace = getForcedWorkspace(request);
078        }
079        catch (ContextException e)
080        {
081            // no current request
082            if (_logger.isDebugEnabled())
083            {
084                _logger.debug("No current request, using default workspace");
085            }
086            
087            return super.getWorkspace();
088        }
089        
090        if (workspace != null)
091        {
092            if (_logger.isDebugEnabled())
093            {
094                _logger.debug("Using workspace: " + workspace);
095            }
096            
097            return workspace;
098        }
099        
100        if (_logger.isDebugEnabled())
101        {
102            _logger.debug("Using default workspace");
103        }
104        
105        // No value, fall back to the default workspace
106        return super.getWorkspace();
107    }
108}