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; 029 030/** 031 * The same as its parent class but add the sitename in second position 032 */ 033public class WebStringRightAssignmentContext extends StringRightAssignmentContext 034{ 035 @Override 036 public Object convertJSContext(Object context) 037 { 038 if (context instanceof String) 039 { 040 List<String> contexts = new ArrayList<>(Arrays.asList(StringUtils.split((String) context, '/'))); 041 contexts.add(1, _getSiteName()); 042 return "/" + StringUtils.join(contexts, "/"); 043 } 044 return null; 045 } 046 047 @Override 048 public List<Object> getRootContexts(Map<String, Object> contextParameters) 049 { 050 return super.getRootContexts(contextParameters).stream().map(context -> context + "/" + _getSiteName()).collect(Collectors.toList()); 051 } 052 053 private String _getSiteName() 054 { 055 Request request = ContextHelper.getRequest(_context); 056 String siteName = request.getParameter("siteName"); 057 058 if (siteName == null) 059 { 060 siteName = (String) request.getAttribute("siteName"); 061 } 062 return siteName; 063 } 064}