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 java.security.Principal; 022import java.util.Arrays; 023import java.util.Comparator; 024import java.util.Map; 025 026import org.apache.wiki.WikiContext; 027import org.apache.wiki.WikiEngine; 028import org.apache.wiki.api.exceptions.PluginException; 029import org.apache.wiki.api.plugin.WikiPlugin; 030import org.apache.wiki.auth.PrincipalComparator; 031import org.apache.wiki.auth.authorize.GroupManager; 032 033/** 034 * <p>Prints the groups managed by this wiki, separated by commas. 035 * <br>The groups are sorted in ascending order, and are hyperlinked to the page that displays the group's members. 036 * </p> 037 * <p>Parameters : </p> 038 * NONE 039 * 040 * 041 * @since 2.4.19 042 */ 043public class Groups 044 implements WikiPlugin 045{ 046 private static final Comparator<Principal> COMPARATOR = new PrincipalComparator(); 047 048 /** 049 * {@inheritDoc} 050 */ 051 public String execute( WikiContext context, Map<String, String> params ) 052 throws PluginException 053 { 054 // Retrieve groups, and sort by name 055 WikiEngine engine = context.getEngine(); 056 GroupManager groupMgr = engine.getGroupManager(); 057 Principal[] groups = groupMgr.getRoles(); 058 Arrays.sort( groups, COMPARATOR ); 059 060 StringBuilder s = new StringBuilder(); 061 062 for ( int i = 0; i < groups.length; i++ ) 063 { 064 String name = groups[i].getName(); 065 066 // Make URL 067 String url = engine.getURLConstructor().makeURL( WikiContext.VIEW_GROUP, name, false, null ); 068 069 // Create hyperlink 070 s.append( "<a href=\"" ); 071 s.append( url ); 072 s.append( "\">" ); 073 s.append( name ); 074 s.append( "</a>" ); 075 076 // If not the last one, add a comma and space 077 if ( i < ( groups.length - 1 ) ) 078 { 079 s.append( ',' ); 080 s.append( ' ' ); 081 } 082 } 083 return s.toString(); 084 } 085}