001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.HtmlUtil;
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 mapping, ActionForm form, HttpServletRequest request,
045                            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 = HtmlUtil.escape(
061                                                    ParamUtil.getString(request, name));
062    
063                                            if (useHttpSession) {
064                                                    SessionClicks.put(session, name, value);
065                                            }
066                                            else {
067                                                    SessionClicks.put(request, name, value);
068                                            }
069                                    }
070                            }
071    
072                            String value = getValue(request);
073    
074                            if (value != null) {
075                                    ServletOutputStream servletOutputStream =
076                                            response.getOutputStream();
077    
078                                    servletOutputStream.print(value);
079                            }
080    
081                            return null;
082                    }
083                    catch (Exception e) {
084                            PortalUtil.sendError(e, request, response);
085    
086                            return null;
087                    }
088            }
089    
090            protected String getValue(HttpServletRequest request) {
091                    HttpSession session = request.getSession();
092    
093                    String cmd = ParamUtil.getString(request, "cmd");
094    
095                    boolean useHttpSession = ParamUtil.getBoolean(
096                            request, "useHttpSession");
097    
098                    if (cmd.equals("get")) {
099                            String key = ParamUtil.getString(request, "key");
100                            String value = StringPool.BLANK;
101    
102                            if (useHttpSession) {
103                                    value = SessionClicks.get(session, key, cmd);
104                            }
105                            else {
106                                    value = SessionClicks.get(request, key, cmd);
107                            }
108    
109                            return value;
110                    }
111                    else if (cmd.equals("getAll")) {
112                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
113    
114                            String[] keys = request.getParameterValues("key");
115    
116                            for (String key : keys) {
117                                    String value = StringPool.BLANK;
118    
119                                    if (useHttpSession) {
120                                            value = SessionClicks.get(session, key, cmd);
121                                    }
122                                    else {
123                                            value = SessionClicks.get(request, key, cmd);
124                                    }
125    
126                                    jsonObject.put(key, value);
127                            }
128    
129                            return jsonObject.toString();
130                    }
131    
132                    return null;
133            }
134    
135    }