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.plugins.core.impl.right; 017 018import java.util.HashSet; 019import java.util.Set; 020import java.util.stream.Collectors; 021 022import org.apache.avalon.framework.context.Context; 023import org.apache.avalon.framework.context.ContextException; 024import org.apache.avalon.framework.context.Contextualizable; 025import org.apache.avalon.framework.service.ServiceException; 026import org.apache.avalon.framework.service.ServiceManager; 027import org.apache.avalon.framework.service.Serviceable; 028import org.apache.cocoon.components.ContextHelper; 029import org.apache.cocoon.environment.Request; 030import org.apache.commons.lang.StringUtils; 031 032import org.ametys.core.right.RightContextConvertor; 033import org.ametys.core.right.RightContextConvertorExtensionPoint; 034import org.ametys.runtime.workspace.WorkspaceMatcher; 035 036/** 037 * Covnert /${WorkspaceName} to the name of the current workspace and recursiverly relaunch the convertors 038 */ 039public class WorkspaceRightContextConvertor implements RightContextConvertor, Contextualizable, Serviceable 040{ 041 /** The avalon context */ 042 protected Context _context; 043 /** The extension point for the Right Context Convertors */ 044 protected RightContextConvertorExtensionPoint _rightContextConvertorEP; 045 046 public void service(ServiceManager manager) throws ServiceException 047 { 048 _rightContextConvertorEP = (RightContextConvertorExtensionPoint) manager.lookup(RightContextConvertorExtensionPoint.ROLE); 049 } 050 051 public void contextualize(Context context) throws ContextException 052 { 053 _context = context; 054 } 055 056 public Set<Object> convert(Object object) 057 { 058 Set<Object> objects = new HashSet<>(); 059 060 if (object instanceof String) 061 { 062 String context = (String) object; 063 if (context.indexOf("${WorkspaceName}") != -1) 064 { 065 Request request = ContextHelper.getRequest(_context); 066 String currentWorkspace = (String) request.getAttribute(WorkspaceMatcher.WORKSPACE_NAME); 067 if (StringUtils.isBlank(currentWorkspace)) 068 { 069 // When comming from schedulable for example... lets use the most restrictive cas 070 currentWorkspace = "admin"; 071 } 072 073 final String resolvedContext = StringUtils.replace(context, "${WorkspaceName}", currentWorkspace); 074 075 objects.addAll(_rightContextConvertorEP.getExtensionsIds().stream() 076 .map(_rightContextConvertorEP::getExtension) 077 .flatMap(convertor -> convertor.convert(resolvedContext).stream()) 078 .collect(Collectors.toSet())); 079 objects.add(resolvedContext); 080 } 081 } 082 083 return objects; 084 } 085}