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