001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.web.common.session;
023
024import java.util.Date;
025
026/**
027 * Stores informations about a user's Http Session
028 *
029 * @author Tiry (tdelprat@nuxeo.com)
030 * @since 5.4.2
031 */
032public class SessionInfo implements Comparable<SessionInfo> {
033
034    protected final String sessionId;
035
036    protected final long creationTime;
037
038    protected long lastAccessTime;
039
040    protected int nbAccess = 0;
041
042    protected String lastAccessUrl;
043
044    protected String loginName;
045
046    public SessionInfo(String sid) {
047        creationTime = System.currentTimeMillis();
048        lastAccessTime = creationTime;
049        sessionId = sid;
050    }
051
052    public long getLastAccessTime() {
053        return lastAccessTime;
054    }
055
056    public void updateLastAccessTime() {
057        lastAccessTime = System.currentTimeMillis();
058    }
059
060    public String getLastAccessUrl() {
061        return lastAccessUrl;
062    }
063
064    public void setLastAccessUrl(String lastAccessUrl) {
065        this.lastAccessUrl = lastAccessUrl;
066        nbAccess += 1;
067    }
068
069    public long getCreationTime() {
070        return creationTime;
071    }
072
073    public String getLoginName() {
074        return loginName;
075    }
076
077    public void setLoginName(String loginName) {
078        this.loginName = loginName;
079    }
080
081    public String getSessionId() {
082        return sessionId;
083    }
084
085    public long getDurationInS() {
086        return (System.currentTimeMillis() - creationTime) / 1000;
087    }
088
089    public long getInactivityInS() {
090        return (System.currentTimeMillis() - lastAccessTime) / 1000;
091    }
092
093    protected String formatDuration(long nbs) {
094        StringBuffer sb = new StringBuffer();
095
096        long nbh = nbs / 3600;
097        nbs = nbs - nbh * 3600;
098        long nbm = nbs / 60;
099        nbs = nbs - nbm * 60;
100
101        if (nbh > 0) {
102            sb.append(nbh);
103            sb.append("h ");
104        }
105        if (nbm > 0 || nbh > 0) {
106            sb.append(nbm);
107            sb.append("m ");
108        }
109
110        sb.append(nbs);
111        sb.append("s ");
112
113        return sb.toString();
114    }
115
116    public String getDurationAsString() {
117        return formatDuration(getDurationInS());
118    }
119
120    public String getInactivityAsString() {
121        return formatDuration(getInactivityInS());
122    }
123
124    public Date getLastAccessDate() {
125        return new Date(getLastAccessTime());
126    }
127
128    @Override
129    public int compareTo(SessionInfo o) {
130        if (getInactivityInS() == o.getInactivityInS()) {
131            return 0;
132        }
133        return getInactivityInS() > o.getInactivityInS() ? 1 : -1;
134    }
135
136    @Override
137    public String toString() {
138        StringBuilder sb = new StringBuilder();
139        sb.append("Sid=");
140        sb.append(sessionId);
141        sb.append(" : login=");
142        sb.append(loginName);
143        return sb.toString();
144    }
145
146    public int getAccessedPagesCount() {
147        return nbAccess;
148    }
149}