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