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