001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet;
016    
017    import com.liferay.portal.kernel.util.WebKeys;
018    
019    import java.util.HashMap;
020    import java.util.Map;
021    import java.util.concurrent.ConcurrentHashMap;
022    
023    import javax.servlet.http.HttpServletRequest;
024    import javax.servlet.http.HttpSession;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class RenderParametersPool {
030    
031            public static Map<String, Map<String, String[]>> clear(
032                    HttpServletRequest request, long plid) {
033    
034                    HttpSession session = request.getSession();
035    
036                    if (plid <= 0) {
037                            return null;
038                    }
039    
040                    Map<Long, Map<String, Map<String, String[]>>> pool =
041                            (Map<Long, Map<String, Map<String, String[]>>>)session.getAttribute(
042                                    WebKeys.PORTLET_RENDER_PARAMETERS);
043    
044                    if (pool == null) {
045                            return null;
046                    }
047    
048                    return pool.remove(plid);
049            }
050    
051            public static Map<String, String[]> clear(
052                    HttpServletRequest request, long plid, String portletId) {
053    
054                    Map<String, Map<String, String[]>> plidPool = clear(request, plid);
055    
056                    if (plidPool == null) {
057                            return null;
058                    }
059    
060                    return plidPool.remove(portletId);
061            }
062    
063            public static Map<String, Map<String, String[]>> get(
064                    HttpServletRequest request, long plid) {
065    
066                    HttpSession session = request.getSession();
067    
068                    if (plid <= 0) {
069                            return null;
070                    }
071    
072                    Map<Long, Map<String, Map<String, String[]>>> pool =
073                            (Map<Long, Map<String, Map<String, String[]>>>)session.getAttribute(
074                                    WebKeys.PORTLET_RENDER_PARAMETERS);
075    
076                    if (pool == null) {
077                            return null;
078                    }
079    
080                    return pool.get(plid);
081            }
082    
083            public static Map<String, String[]> get(
084                    HttpServletRequest request, long plid, String portletId) {
085    
086                    Map<String, Map<String, String[]>> plidPool = get(request, plid);
087    
088                    if (plidPool == null) {
089                            return null;
090                    }
091    
092                    return plidPool.get(portletId);
093            }
094    
095            public static Map<String, Map<String, String[]>> getOrCreate(
096                    HttpServletRequest request, long plid) {
097    
098                    HttpSession session = request.getSession();
099    
100                    if (plid <= 0) {
101                            return new ConcurrentHashMap<>();
102                    }
103    
104                    Map<Long, Map<String, Map<String, String[]>>> pool =
105                            _getOrCreateRenderParametersPool(session);
106    
107                    Map<String, Map<String, String[]>> plidPool = pool.get(plid);
108    
109                    if (plidPool == null) {
110                            plidPool = new ConcurrentHashMap<>();
111    
112                            pool.put(plid, plidPool);
113                    }
114    
115                    return plidPool;
116            }
117    
118            public static Map<String, String[]> getOrCreate(
119                    HttpServletRequest request, long plid, String portletId) {
120    
121                    Map<String, Map<String, String[]>> plidPool = getOrCreate(
122                            request, plid);
123    
124                    Map<String, String[]> params = plidPool.get(portletId);
125    
126                    if (params == null) {
127                            params = new HashMap<>();
128    
129                            plidPool.put(portletId, params);
130                    }
131    
132                    return params;
133            }
134    
135            public static void put(
136                    HttpServletRequest request, long plid, String portletId,
137                    Map<String, String[]> params) {
138    
139                    if (params.isEmpty()) {
140                            return;
141                    }
142    
143                    Map<String, Map<String, String[]>> plidPool = getOrCreate(
144                            request, plid);
145    
146                    plidPool.put(portletId, params);
147            }
148    
149            private static Map<Long, Map<String, Map<String, String[]>>>
150                    _getOrCreateRenderParametersPool(HttpSession session) {
151    
152                    Map<Long, Map<String, Map<String, String[]>>> renderParametersPool =
153                            (Map<Long, Map<String, Map<String, String[]>>>)session.getAttribute(
154                                    WebKeys.PORTLET_RENDER_PARAMETERS);
155    
156                    if (renderParametersPool == null) {
157                            renderParametersPool = new ConcurrentHashMap<>();
158    
159                            session.setAttribute(
160                                    WebKeys.PORTLET_RENDER_PARAMETERS, renderParametersPool);
161                    }
162    
163                    return renderParametersPool;
164            }
165    
166    }