001    /**
002     * Copyright (c) 2000-2013 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.portal.action;
016    
017    import com.liferay.portal.kernel.json.JSONFactoryUtil;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.util.PortalUtil;
023    import com.liferay.portal.util.SessionClicks;
024    
025    import java.util.Enumeration;
026    
027    import javax.servlet.ServletOutputStream;
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.http.HttpServletResponse;
030    import javax.servlet.http.HttpSession;
031    
032    import org.apache.struts.action.Action;
033    import org.apache.struts.action.ActionForm;
034    import org.apache.struts.action.ActionForward;
035    import org.apache.struts.action.ActionMapping;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class SessionClickAction extends Action {
041    
042            @Override
043            public ActionForward execute(
044                            ActionMapping actionMapping, ActionForm actionForm,
045                            HttpServletRequest request, HttpServletResponse response)
046                    throws Exception {
047    
048                    try {
049                            HttpSession session = request.getSession();
050    
051                            Enumeration<String> enu = request.getParameterNames();
052    
053                            boolean useHttpSession = ParamUtil.getBoolean(
054                                    request, "useHttpSession");
055    
056                            while (enu.hasMoreElements()) {
057                                    String name = enu.nextElement();
058    
059                                    if (!name.equals("doAsUserId")) {
060                                            String value = ParamUtil.getString(request, name);
061    
062                                            if (useHttpSession) {
063                                                    SessionClicks.put(session, name, value);
064                                            }
065                                            else {
066                                                    SessionClicks.put(request, name, value);
067                                            }
068                                    }
069                            }
070    
071                            String value = getValue(request);
072    
073                            if (value != null) {
074                                    ServletOutputStream servletOutputStream =
075                                            response.getOutputStream();
076    
077                                    servletOutputStream.print(value);
078                            }
079    
080                            return null;
081                    }
082                    catch (Exception e) {
083                            PortalUtil.sendError(e, request, response);
084    
085                            return null;
086                    }
087            }
088    
089            protected String getValue(HttpServletRequest request) {
090                    HttpSession session = request.getSession();
091    
092                    String cmd = ParamUtil.getString(request, Constants.CMD);
093    
094                    boolean useHttpSession = ParamUtil.getBoolean(
095                            request, "useHttpSession");
096    
097                    if (cmd.equals("get")) {
098                            String key = ParamUtil.getString(request, "key");
099                            String value = StringPool.BLANK;
100    
101                            if (useHttpSession) {
102                                    value = SessionClicks.get(session, key, cmd);
103                            }
104                            else {
105                                    value = SessionClicks.get(request, key, cmd);
106                            }
107    
108                            return value;
109                    }
110                    else if (cmd.equals("getAll")) {
111                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
112    
113                            String[] keys = request.getParameterValues("key");
114    
115                            for (String key : keys) {
116                                    String value = StringPool.BLANK;
117    
118                                    if (useHttpSession) {
119                                            value = SessionClicks.get(session, key, cmd);
120                                    }
121                                    else {
122                                            value = SessionClicks.get(request, key, cmd);
123                                    }
124    
125                                    jsonObject.put(key, value);
126                            }
127    
128                            return jsonObject.toString();
129                    }
130    
131                    return null;
132            }
133    
134    }