001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.ac.usecases;
019:
020: import java.util.ArrayList;
021: import java.util.WeakHashMap;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.Vector;
026:
027: import javax.servlet.http.*;
028:
029: import org.apache.lenya.ac.Identity;
030: import org.apache.lenya.ac.User;
031: import org.apache.lenya.cms.ac.usecases.AccessControlUsecase;
032: import org.apache.lenya.cms.publication.URLInformation;
033: import org.apache.lenya.util.SessionListener;
034:
035: /**
036: * SessionViewer usecase which gets all session objects from the SessionListener and extract the
037: * necessary informations.
038: *
039: * @version $Id: SessionViewer.java 407305 2006-05-17 16:21:49Z andreas $
040: */
041: public class SessionViewer extends AccessControlUsecase {
042:
043: private static final String IDENTITY = Identity.class.getName();
044:
045: private static final String HISTORY = "org.apache.lenya.cms.cocoon.acting.History";
046:
047: protected static final String USERS = "users";
048:
049: /**
050: * @see org.apache.lenya.cms.usecase.AbstractUsecase#initParameters()
051: */
052: protected void initParameters() {
053: super .initParameters();
054: this .getSourceURL();
055:
056: SessionListener sessions = new SessionListener();
057: WeakHashMap allSessions = sessions.getAllSessions();
058: List userList = new ArrayList();
059:
060: Iterator userit = allSessions.entrySet().iterator();
061: while (userit.hasNext()) {
062: Map.Entry entry = (Map.Entry) userit.next();
063: HttpSession nextsession = (HttpSession) entry.getValue();
064:
065: Identity identity = (Identity) nextsession
066: .getAttribute(IDENTITY);
067:
068: if (identity == null) {
069: continue;
070: }
071:
072: User user = identity.getUser();
073: if (user != null) {
074: Vector history = (Vector) nextsession
075: .getAttribute(HISTORY);
076: String publicationID = getPublicationIDfromHistory(history);
077: if (publicationID.equals(getPublicationIDfromURL())) {
078: userList.add(identity.getUser());
079: }
080: }
081: }
082: setParameter(USERS, userList);
083: }
084:
085: /**
086: * Extract the publicationID from the history information stored in the session
087: *
088: * @param history The history stored in the session.. An entry looks like \/
089: * <publication-id>/authoring/index.html
090: * @return A publication ID.
091: */
092: private String getPublicationIDfromHistory(Vector history) {
093:
094: String firstElement = history.firstElement().toString();
095: String publicationID = firstElement.substring(1, firstElement
096: .indexOf("/", 1));
097:
098: return publicationID;
099: }
100:
101: /**
102: * @return The publicationID from an URL.
103: */
104: private String getPublicationIDfromURL() {
105: URLInformation info = new URLInformation(getSourceURL());
106: return info.getPublicationId();
107: }
108: }
|