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.struts;
016    
017    import com.liferay.portal.kernel.servlet.HttpHeaders;
018    import com.liferay.portal.kernel.util.ContentTypes;
019    import com.liferay.portal.util.PortalUtil;
020    
021    import java.io.PrintWriter;
022    
023    import javax.servlet.http.HttpServletRequest;
024    import javax.servlet.http.HttpServletResponse;
025    
026    import org.apache.struts.action.Action;
027    import org.apache.struts.action.ActionForm;
028    import org.apache.struts.action.ActionForward;
029    import org.apache.struts.action.ActionMapping;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public abstract class AJAXAction extends Action {
035    
036            @Override
037            public ActionForward execute(
038                            ActionMapping actionMapping, ActionForm actionForm,
039                            HttpServletRequest request, HttpServletResponse response)
040                    throws Exception {
041    
042                    String text = null;
043    
044                    try {
045                            text = getText(actionMapping, actionForm, request, response);
046                    }
047                    catch (Exception e) {
048                            PortalUtil.sendError(
049                                    HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
050                                    response);
051    
052                            return null;
053                    }
054    
055                    response.setContentType(ContentTypes.TEXT_PLAIN_UTF8);
056                    response.setHeader(
057                            HttpHeaders.CACHE_CONTROL,
058                            HttpHeaders.CACHE_CONTROL_NO_CACHE_VALUE);
059    
060                    PrintWriter printWriter = response.getWriter();
061    
062                    printWriter.write(text);
063    
064                    printWriter.close();
065    
066                    return null;
067            }
068    
069            public abstract String getText(
070                            ActionMapping actionMapping, ActionForm actionForm,
071                            HttpServletRequest request, HttpServletResponse response)
072                    throws Exception;
073    
074    }