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