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