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