1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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  }