001/*
002 *
003 * (C) Copyright 2006-2009 Nuxeo SAS (http://nuxeo.com/) and contributors.
004 *
005 * All rights reserved. This program and the accompanying materials
006 * are made available under the terms of the GNU Lesser General Public License
007 * (LGPL) version 2.1 which accompanies this distribution, and is available at
008 * http://www.gnu.org/licenses/lgpl.html
009 *
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013 * Lesser General Public License for more details.
014 *
015 * Contributors:
016 *     Nuxeo - initial API and implementation
017 *
018 * $Id$
019 */
020
021package org.nuxeo.ecm.platform.web.common.session;
022
023import java.util.Date;
024
025/**
026 * Stores informations about a user's Http Session
027 *
028 * @author Tiry (tdelprat@nuxeo.com)
029 * @since 5.4.2
030 */
031public class SessionInfo implements Comparable<SessionInfo> {
032
033    protected final String sessionId;
034
035    protected final long creationTime;
036
037    protected long lastAccessTime;
038
039    protected int nbAccess = 0;
040
041    protected String lastAccessUrl;
042
043    protected String loginName;
044
045    public SessionInfo(String sid) {
046        creationTime = System.currentTimeMillis();
047        lastAccessTime = creationTime;
048        sessionId = sid;
049    }
050
051    public long getLastAccessTime() {
052        return lastAccessTime;
053    }
054
055    public void updateLastAccessTime() {
056        lastAccessTime = System.currentTimeMillis();
057    }
058
059    public String getLastAccessUrl() {
060        return lastAccessUrl;
061    }
062
063    public void setLastAccessUrl(String lastAccessUrl) {
064        this.lastAccessUrl = lastAccessUrl;
065        nbAccess += 1;
066    }
067
068    public long getCreationTime() {
069        return creationTime;
070    }
071
072    public String getLoginName() {
073        return loginName;
074    }
075
076    public void setLoginName(String loginName) {
077        this.loginName = loginName;
078    }
079
080    public String getSessionId() {
081        return sessionId;
082    }
083
084    public long getDurationInS() {
085        return (System.currentTimeMillis() - creationTime) / 1000;
086    }
087
088    public long getInactivityInS() {
089        return (System.currentTimeMillis() - lastAccessTime) / 1000;
090    }
091
092    protected String formatDuration(long nbs) {
093        StringBuffer sb = new StringBuffer();
094
095        long nbh = nbs / 3600;
096        nbs = nbs - nbh * 3600;
097        long nbm = nbs / 60;
098        nbs = nbs - nbm * 60;
099
100        if (nbh > 0) {
101            sb.append(nbh);
102            sb.append("h ");
103        }
104        if (nbm > 0 || nbh > 0) {
105            sb.append(nbm);
106            sb.append("m ");
107        }
108
109        sb.append(nbs);
110        sb.append("s ");
111
112        return sb.toString();
113    }
114
115    public String getDurationAsString() {
116        return formatDuration(getDurationInS());
117    }
118
119    public String getInactivityAsString() {
120        return formatDuration(getInactivityInS());
121    }
122
123    public Date getLastAccessDate() {
124        return new Date(getLastAccessTime());
125    }
126
127    @Override
128    public int compareTo(SessionInfo o) {
129        if (getInactivityInS() == o.getInactivityInS()) {
130            return 0;
131        }
132        return getInactivityInS() > o.getInactivityInS() ? 1 : -1;
133    }
134
135    @Override
136    public String toString() {
137        StringBuilder sb = new StringBuilder();
138        sb.append("Sid=");
139        sb.append(sessionId);
140        sb.append(" : login=");
141        sb.append(loginName);
142        return sb.toString();
143    }
144
145    public int getAccessedPagesCount() {
146        return nbAccess;
147    }
148}