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.HashMap;
023import java.util.Iterator;
024import java.util.Map;
025
026import org.apache.wiki.WikiContext;
027import org.apache.wiki.WikiEngine;
028import org.apache.wiki.WikiSession;
029import org.apache.wiki.api.exceptions.PluginException;
030import org.apache.wiki.api.plugin.WikiPlugin;
031import org.apache.wiki.util.TextUtil;
032
033/**
034 *  <p>Displays information about active wiki sessions. The parameter
035 *  <code>property</code> specifies what information is displayed.
036 *  If omitted, the number of sessions is returned.
037 *
038 *  <p>Parameters : </p>
039 *  <ul>
040 *  <li><b>property</b> - specify what output to display, valid values are:</li>
041 *  <ul>
042 *    <li><code>users</code> - returns a comma-separated list of
043 *    users</li>
044 *    <li><code>distinctUsers</code> - will only show
045 *    distinct users.
046 *  </ul>
047 *  </ul>
048 *  @since 2.3.84
049 */
050public class SessionsPlugin
051    implements WikiPlugin
052{
053    /** The parameter name for setting the property value. */
054    public static final String PARAM_PROP = "property";
055
056    /**
057     *  {@inheritDoc}
058     */
059    public String execute( WikiContext context, Map<String, String> params )
060        throws PluginException
061    {
062        WikiEngine engine = context.getEngine();
063        String prop = params.get( PARAM_PROP );
064
065        if ( "users".equals( prop ) )
066        {
067            Principal[] principals = WikiSession.userPrincipals( engine );
068            StringBuilder s = new StringBuilder();
069            for ( int i = 0; i < principals.length; i++ )
070            {
071                s.append(principals[i].getName() + ", ");
072            }
073            // remove the last comma and blank :
074            return TextUtil.replaceEntities( s.substring(0, s.length() - (s.length() > 2 ? 2 : 0) ) );
075        }
076
077        //
078        // show each user session only once (with a counter that indicates the
079        // number of sessions for each user)
080        if ("distinctUsers".equals(prop))
081        {
082            Principal[] principals = WikiSession.userPrincipals(engine);
083            // we do not assume that the principals are sorted, so first count
084            // them :
085            HashMap<String,Integer> distinctPrincipals = new HashMap<String,Integer>();
086            for (int i = 0; i < principals.length; i++)
087            {
088                String principalName = principals[i].getName();
089
090                if (distinctPrincipals.containsKey(principalName))
091                {
092                    // we already have an entry, increase the counter:
093                    int numSessions = distinctPrincipals.get(principalName).intValue();
094                    // store the new value:
095                    distinctPrincipals.put(principalName, ++numSessions);
096                }
097                else
098                {
099                    // first time we see this entry, add entry to HashMap with value 1
100                    distinctPrincipals.put(principalName, 1);
101                }
102            }
103            //
104            //
105            StringBuilder s = new StringBuilder();
106            Iterator<Map.Entry<String, Integer>> entries = distinctPrincipals.entrySet().iterator();
107            while (entries.hasNext())
108            {
109                Map.Entry<String, Integer> entry = entries.next();
110                s.append( entry.getKey() + "(" + entry.getValue().toString() + "), " );
111            }
112            // remove the last comma and blank :
113            //if (s.length() >= 2) return s.substring(0,    s.length() - 2);
114            return TextUtil.replaceEntities( s.substring(0, s.length() - (s.length() > 2 ? 2 : 0) ) );
115
116        }
117
118        return String.valueOf( WikiSession.sessions( engine ) );
119    }
120}