View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  
21  package org.waarp.http.protocol;
22  
23  import org.waarp.http.protocol.servlet.HttpAuthent;
24  
25  import java.util.concurrent.ConcurrentHashMap;
26  
27  /**
28   * Handler for all sessions (upload only)
29   */
30  public class HttpSessions {
31    private static final HttpSessions httpSessions = new HttpSessions();
32  
33    private final ConcurrentHashMap<String, HttpResumableSession> sessions =
34        new ConcurrentHashMap<String, HttpResumableSession>();
35  
36    private HttpSessions() {
37      // Empty and private
38    }
39  
40    /**
41     * @return the current Factory for HttpSession
42     */
43    public static HttpSessions getInstance() {
44      return httpSessions;
45    }
46  
47    /**
48     * Get a already existing HttpResumableSession or create a new one if not
49     * exists
50     *
51     * @param resumableInfo
52     * @param rulename
53     * @param comment
54     * @param authent
55     *
56     * @return the HttpResumableSession
57     */
58    public synchronized HttpResumableSession getOrCreateResumableSession(
59        final HttpResumableInfo resumableInfo, final String rulename,
60        final String comment, final HttpAuthent authent) {
61      HttpResumableSession session = sessions.get(resumableInfo.getIdentifier());
62      if (session == null) {
63        session =
64            new HttpResumableSession(resumableInfo, rulename, comment, authent);
65        sessions.put(resumableInfo.getIdentifier(), session);
66      }
67      return session;
68    }
69  
70    /**
71     * Check if session is already existing
72     *
73     * @param resumableInfo
74     *
75     * @return True if contained
76     */
77    protected final boolean contains(final HttpResumableInfo resumableInfo) {
78      return sessions.containsKey(resumableInfo.getIdentifier());
79    }
80  
81    /**
82     * Removes the current session
83     *
84     * @param resumableInfo
85     *
86     * @return True if found
87     */
88    public final boolean removeSession(final HttpResumableInfo resumableInfo) {
89      return sessions.remove(resumableInfo.getIdentifier()) != null;
90    }
91  
92    /**
93     * Removes the current session
94     *
95     * @param resumableSession
96     *
97     * @return True if found
98     */
99    public final boolean removeSession(
100       final HttpResumableSession resumableSession) {
101     return sessions.remove(
102         resumableSession.getHttpResumableInfo().getIdentifier()) != null;
103   }
104 }