001/* 002 Licensed to the Apache Software Foundation (ASF) under one 003 or more contributor license agreements. See the NOTICE file 004 distributed with this work for additional information 005 regarding copyright ownership. The ASF licenses this file 006 to you under the Apache License, Version 2.0 (the 007 "License"); you may not use this file except in compliance 008 with the License. You may obtain a copy of the License at 009 010 http://www.apache.org/licenses/LICENSE-2.0 011 012 Unless required by applicable law or agreed to in writing, 013 software distributed under the License is distributed on an 014 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 KIND, either express or implied. See the License for the 016 specific language governing permissions and limitations 017 under the License. 018 */ 019package org.apache.wiki.plugin; 020 021import org.apache.wiki.api.core.Context; 022import org.apache.wiki.api.core.ContextEnum; 023import org.apache.wiki.api.core.Engine; 024import org.apache.wiki.api.exceptions.PluginException; 025import org.apache.wiki.api.plugin.Plugin; 026import org.apache.wiki.auth.authorize.GroupManager; 027import org.apache.wiki.url.URLConstructor; 028import org.apache.wiki.util.comparators.PrincipalComparator; 029 030import java.security.Principal; 031import java.util.Arrays; 032import java.util.Comparator; 033import java.util.Map; 034 035/** 036 * <p>Prints the groups managed by this wiki, separated by commas. 037 * <br>The groups are sorted in ascending order, and are hyperlinked to the page that displays the group's members. 038 * </p> 039 * <p>Parameters : </p> 040 * NONE 041 * 042 * @since 2.4.19 043 */ 044public class Groups implements Plugin { 045 046 private static final Comparator<Principal> COMPARATOR = new PrincipalComparator(); 047 048 /** 049 * {@inheritDoc} 050 */ 051 @Override 052 public String execute( final Context context, final Map<String, String> params ) throws PluginException { 053 // Retrieve groups, and sort by name 054 final Engine engine = context.getEngine(); 055 final GroupManager groupMgr = engine.getManager( GroupManager.class ); 056 final Principal[] groups = groupMgr.getRoles(); 057 Arrays.sort( groups, COMPARATOR ); 058 059 final StringBuilder s = new StringBuilder(); 060 061 for ( int i = 0; i < groups.length; i++ ) { 062 final String name = groups[ i ].getName(); 063 064 // Make URL 065 final String url = engine.getManager( URLConstructor.class ).makeURL( ContextEnum.GROUP_VIEW.getRequestContext(), name, null ); 066 067 // Create hyperlink 068 s.append( "<a href=\"" ); 069 s.append( url ); 070 s.append( "\">" ); 071 s.append( name ); 072 s.append( "</a>" ); 073 074 // If not the last one, add a comma and space 075 if ( i < ( groups.length - 1 ) ) { 076 s.append( ',' ); 077 s.append( ' ' ); 078 } 079 } 080 return s.toString(); 081 } 082 083}