1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.struts;
16  
17  import com.liferay.portal.kernel.servlet.HttpHeaders;
18  import com.liferay.portal.kernel.util.ContentTypes;
19  import com.liferay.portal.util.PortalUtil;
20  
21  import java.io.PrintWriter;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.apache.struts.action.Action;
27  import org.apache.struts.action.ActionForm;
28  import org.apache.struts.action.ActionForward;
29  import org.apache.struts.action.ActionMapping;
30  
31  /**
32   * <a href="AJAXAction.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public abstract class AJAXAction extends Action {
37  
38      public ActionForward execute(
39              ActionMapping mapping, ActionForm form, HttpServletRequest request,
40              HttpServletResponse response)
41          throws Exception {
42  
43          String text = null;
44  
45          try {
46              text = getText(mapping, form, request, response);
47          }
48          catch (Exception e) {
49              PortalUtil.sendError(
50                  HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
51                  response);
52  
53              return null;
54          }
55  
56          response.setContentType(ContentTypes.TEXT_PLAIN_UTF8);
57          response.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache");
58  
59          PrintWriter pw = response.getWriter();
60  
61          pw.write(text);
62  
63          pw.close();
64  
65          return null;
66      }
67  
68      public abstract String getText(
69              ActionMapping mapping, ActionForm form, HttpServletRequest request,
70              HttpServletResponse response)
71          throws Exception;
72  
73  }