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