001/*
002 *  Copyright 2016 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.rights;
017
018import java.util.ArrayList;
019import java.util.Arrays;
020import java.util.List;
021import java.util.Map;
022import java.util.stream.Collectors;
023
024import org.apache.cocoon.components.ContextHelper;
025import org.apache.cocoon.environment.Request;
026import org.apache.commons.lang3.StringUtils;
027
028import org.ametys.plugins.core.impl.right.StringRightAssignmentContext;
029import org.ametys.web.WebHelper;
030
031/**
032 * The same as its parent class but add the sitename in second position
033 */
034public class WebStringRightAssignmentContext extends StringRightAssignmentContext
035{
036    @Override
037    public Object convertJSContext(Object context)
038    {
039        if (context instanceof String)
040        {
041            List<String> contexts = new ArrayList<>(Arrays.asList(StringUtils.split((String) context, '/')));
042            contexts.add(1, _getSiteName());
043            return "/" + StringUtils.join(contexts, "/");
044        }
045        return null;
046    }
047    
048    @Override
049    public List<Object> getRootContexts(Map<String, Object> contextParameters)
050    {
051        return super.getRootContexts(contextParameters).stream().map(context -> context + "/" + _getSiteName()).collect(Collectors.toList());
052    }
053    
054    private String _getSiteName()
055    {
056        Request request = ContextHelper.getRequest(_context);
057        return WebHelper.getSiteName(request);
058    }
059}