001/*
002 *  Copyright 2013 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.core.util;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.component.Component;
021import org.apache.avalon.framework.context.Context;
022import org.apache.avalon.framework.context.ContextException;
023import org.apache.avalon.framework.context.Contextualizable;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.avalon.framework.service.Serviceable;
027import org.apache.cocoon.components.ContextHelper;
028import org.apache.cocoon.environment.ObjectModelHelper;
029import org.apache.cocoon.environment.Request;
030import org.apache.commons.lang.StringUtils;
031
032/**
033 * Helper to get JS parameters from request
034 *
035 */
036public class ServerCommHelper implements Component, Serviceable, Contextualizable
037{
038    /** The Avalon Role */
039    public static final String ROLE = ServerCommHelper.class.getName();
040    
041    private JSONUtils _jsonUtils;
042    private Context _context;
043    
044    @Override
045    public void service(ServiceManager manager) throws ServiceException
046    {
047        _jsonUtils = (JSONUtils) manager.lookup(JSONUtils.ROLE);
048    }
049    
050    @Override
051    public void contextualize(Context context) throws ContextException
052    {
053        _context = context;
054    }
055    
056    /**
057     * Get the JS parameters as a Map object
058     * @return The JS parameters
059     */
060    @SuppressWarnings("unchecked")
061    public Map<String, Object> getJsParameters()
062    {
063        Map<String, Object> objectModel = ContextHelper.getObjectModel(_context);
064        
065        Map<String, Object> jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
066        if (jsParameters != null && !jsParameters.isEmpty())
067        {
068            return jsParameters;
069        }
070        
071        Request request = ObjectModelHelper.getRequest(objectModel);
072        
073        String parametersAsJSONString = request.getParameter("parameters");
074        if (StringUtils.isNotEmpty(parametersAsJSONString))
075        {
076            jsParameters = _jsonUtils.convertJsonToMap(parametersAsJSONString);
077        }
078        
079        // Set context parameters in request attributes
080        String contextAsJSONString = request.getParameter("context.parameters");
081        if (StringUtils.isNotEmpty(contextAsJSONString))
082        {
083            Map<String, Object> contextAsMap = _jsonUtils.convertJsonToMap(contextAsJSONString);
084            for (String name : contextAsMap.keySet())
085            {
086                request.setAttribute(name, contextAsMap.get(name));
087            }
088        }
089        
090        return jsParameters;
091    }
092}