1
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
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 }