1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.servlet;
21  
22  import com.liferay.portal.NoSuchLayoutException;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.util.ContentTypes;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.model.Portlet;
29  import com.liferay.portal.service.PortletLocalServiceUtil;
30  import com.liferay.portal.util.Portal;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portal.util.PropsValues;
33  import com.liferay.portal.util.WebKeys;
34  import com.liferay.util.servlet.ServletResponseUtil;
35  
36  import java.io.IOException;
37  
38  import javax.servlet.ServletException;
39  import javax.servlet.http.HttpServlet;
40  import javax.servlet.http.HttpServletRequest;
41  import javax.servlet.http.HttpServletResponse;
42  
43  /**
44   * <a href="GoogleGadgetServlet.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Alberto Montero
47   *
48   */
49  public class GoogleGadgetServlet extends HttpServlet {
50  
51      public void service(
52              HttpServletRequest request, HttpServletResponse response)
53          throws IOException, ServletException {
54  
55          try {
56              String content = getContent(request);
57  
58              if (content == null) {
59                  PortalUtil.sendError(
60                      HttpServletResponse.SC_NOT_FOUND,
61                      new NoSuchLayoutException(), request, response);
62              }
63              else {
64                  request.setAttribute(WebKeys.GOOGLE_GADGET, Boolean.TRUE);
65  
66                  response.setContentType(ContentTypes.TEXT_XML);
67  
68                  ServletResponseUtil.write(response, content);
69              }
70          }
71          catch (Exception e) {
72              _log.error(e, e);
73  
74              PortalUtil.sendError(
75                  HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
76                  response);
77          }
78      }
79  
80      protected String getContent(HttpServletRequest request) throws Exception {
81          String path = GetterUtil.getString(request.getPathInfo());
82  
83          if (Validator.isNull(path)) {
84              return null;
85          }
86  
87          int pos = path.indexOf(Portal.FRIENDLY_URL_SEPARATOR);
88  
89          if (pos == -1) {
90              return null;
91          }
92  
93          long companyId = PortalUtil.getCompanyId(request);
94  
95          String portletId = path.substring(
96              pos + Portal.FRIENDLY_URL_SEPARATOR.length());
97  
98          Portlet portlet = PortletLocalServiceUtil.getPortletById(
99              companyId, portletId);
100 
101         String title = portlet.getDisplayName();
102 
103         String widgetJsURL =
104             PortalUtil.getPortalURL(request) + PortalUtil.getPathContext() +
105                 "/html/js/liferay/widget.js";
106 
107         String widgetURL = request.getRequestURL().toString();
108 
109         widgetURL = widgetURL.replaceFirst(
110             PropsValues.GOOGLE_GADGET_SERVLET_MAPPING,
111             PropsValues.WIDGET_SERVLET_MAPPING);
112 
113         StringBuilder sb = new StringBuilder();
114 
115         sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
116         sb.append("<Module>");
117         sb.append("<ModulePrefs title=\"" + title + "\"/>");
118         sb.append("<Content type=\"html\">");
119         sb.append("<![CDATA[");
120         sb.append("<script src=\"" + widgetJsURL + "\" ");
121         sb.append("type=\"text/javascript\"></script>");
122         sb.append("<script type=\"text/javascript\">");
123         sb.append("window.Liferay.Widget({url:'" + widgetURL + "'});");
124         sb.append("</script>");
125         sb.append("]]>");
126         sb.append("</Content>");
127         sb.append("</Module>");
128 
129         return sb.toString();
130     }
131 
132     private static Log _log = LogFactoryUtil.getLog(GoogleGadgetServlet.class);
133 
134 }