001    /**
002     * Copyright (c) 2000-2012 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.struts;
016    
017    import com.liferay.portal.kernel.servlet.HttpHeaders;
018    import com.liferay.portal.kernel.servlet.ServletContextPool;
019    import com.liferay.portal.kernel.util.ContentTypes;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.util.PortalUtil;
024    import com.liferay.portal.util.WebKeys;
025    
026    import java.io.PrintWriter;
027    
028    import javax.servlet.RequestDispatcher;
029    import javax.servlet.ServletContext;
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpServletResponse;
032    
033    import org.apache.struts.action.Action;
034    import org.apache.struts.action.ActionForm;
035    import org.apache.struts.action.ActionForward;
036    import org.apache.struts.action.ActionMapping;
037    
038    /**
039     * @author Ming-Gih Lam
040     */
041    public abstract class JSONAction extends Action {
042    
043            @Override
044            public ActionForward execute(
045                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
046                            HttpServletResponse response)
047                    throws Exception {
048    
049                    if (rerouteExecute(request, response)) {
050                            return null;
051                    }
052    
053                    String callback = ParamUtil.getString(request, "callback");
054                    String instance = ParamUtil.getString(request, "inst");
055    
056                    String json = null;
057    
058                    try {
059                            json = getJSON(mapping, form, request, response);
060    
061                            if (Validator.isNotNull(callback)) {
062                                    json = callback + "(" + json + ");";
063                            }
064                            else if (Validator.isNotNull(instance)) {
065                                    json = "var " + instance + "=" + json + ";";
066                            }
067                    }
068                    catch (Exception e) {
069                            PortalUtil.sendError(
070                                    HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
071                                    response);
072    
073                            return null;
074                    }
075    
076                    boolean refresh = ParamUtil.getBoolean(request, "refresh");
077    
078                    if (refresh) {
079                            return mapping.findForward(ActionConstants.COMMON_REFERER);
080                    }
081                    else if (Validator.isNotNull(json)) {
082                            response.setContentType(ContentTypes.TEXT_JAVASCRIPT);
083                            response.setHeader(
084                                    HttpHeaders.CACHE_CONTROL,
085                                    HttpHeaders.CACHE_CONTROL_NO_CACHE_VALUE);
086    
087                            PrintWriter printWriter = response.getWriter();
088    
089                            printWriter.write(json);
090    
091                            printWriter.close();
092                    }
093    
094                    return null;
095            }
096    
097            public abstract String getJSON(
098                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
099                            HttpServletResponse response)
100                    throws Exception;
101    
102            public void setServletContext(ServletContext servletContext) {
103                    _servletContext = servletContext;
104            }
105    
106            protected String getReroutePath() {
107                    return null;
108            }
109    
110            protected boolean rerouteExecute(
111                            HttpServletRequest request, HttpServletResponse response)
112                    throws Exception {
113    
114                    String reroutePath = getReroutePath();
115    
116                    if (Validator.isNull(reroutePath)) {
117                            return false;
118                    }
119    
120                    String requestServletContextName = ParamUtil.getString(
121                            request, "servletContextName");
122    
123                    if (Validator.isNull(requestServletContextName)) {
124                            return false;
125                    }
126    
127                    ServletContext servletContext = _servletContext;
128    
129                    if (servletContext == null) {
130                            servletContext = (ServletContext)request.getAttribute(WebKeys.CTX);
131                    }
132    
133                    String servletContextName = GetterUtil.getString(
134                            servletContext.getServletContextName());
135    
136                    if (servletContextName.equals(requestServletContextName)) {
137                            return false;
138                    }
139    
140                    ServletContext requestServletContext = ServletContextPool.get(
141                            requestServletContextName);
142    
143                    if (requestServletContext == null) {
144                            return false;
145                    }
146    
147                    RequestDispatcher requestDispatcher =
148                            requestServletContext.getRequestDispatcher(reroutePath);
149    
150                    if (requestDispatcher == null) {
151                            return false;
152                    }
153    
154                    requestDispatcher.forward(request, response);
155    
156                    return true;
157            }
158    
159            protected ServletContext _servletContext;
160    
161    }