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.portlet.search.action;
16  
17  import com.liferay.portal.kernel.search.OpenSearch;
18  import com.liferay.portal.kernel.util.ContentTypes;
19  import com.liferay.portal.kernel.util.StringPool;
20  import com.liferay.portal.search.PortalOpenSearchImpl;
21  import com.liferay.portal.util.PortalUtil;
22  import com.liferay.util.servlet.ServletResponseUtil;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.apache.struts.action.Action;
28  import org.apache.struts.action.ActionForm;
29  import org.apache.struts.action.ActionForward;
30  import org.apache.struts.action.ActionMapping;
31  
32  /**
33   * <a href="OpenSearchAction.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   */
37  public class OpenSearchAction extends Action {
38  
39      public ActionForward execute(
40              ActionMapping mapping, ActionForm form, HttpServletRequest request,
41              HttpServletResponse response)
42          throws Exception {
43  
44          try {
45              ServletResponseUtil.sendFile(
46                  response, null, getXML(request), ContentTypes.TEXT_XML_UTF8);
47  
48              return null;
49          }
50          catch (Exception e) {
51              PortalUtil.sendError(e, request, response);
52  
53              return null;
54          }
55      }
56  
57      protected byte[] getXML(HttpServletRequest request) throws Exception {
58          OpenSearch search = new PortalOpenSearchImpl();
59  
60          String xml = search.search(
61              request,
62              request.getRequestURL().toString() + StringPool.QUESTION +
63                  request.getQueryString());
64  
65          return xml.getBytes();
66      }
67  
68  }