1
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
51 public class GoogleGadgetServlet extends HttpServlet {
52
53 public void service(
54 HttpServletRequest request, HttpServletResponse response)
55 throws IOException, ServletException {
56
57 try {
58 String content = getContent(request);
59
60 if (content == null) {
61 PortalUtil.sendError(
62 HttpServletResponse.SC_NOT_FOUND,
63 new NoSuchLayoutException(), request, response);
64 }
65 else {
66 request.setAttribute(WebKeys.GOOGLE_GADGET, Boolean.TRUE);
67
68 response.setContentType(ContentTypes.TEXT_XML);
69
70 ServletResponseUtil.write(response, content);
71 }
72 }
73 catch (Exception e) {
74 _log.error(e, e);
75
76 PortalUtil.sendError(
77 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
78 response);
79 }
80 }
81
82 protected String getContent(HttpServletRequest request) throws Exception {
83 String path = GetterUtil.getString(request.getPathInfo());
84
85 if (Validator.isNull(path)) {
86 return null;
87 }
88
89 int pos = path.indexOf(Portal.FRIENDLY_URL_SEPARATOR);
90
91 if (pos == -1) {
92 return null;
93 }
94
95 long companyId = PortalUtil.getCompanyId(request);
96
97 String portletId = path.substring(
98 pos + Portal.FRIENDLY_URL_SEPARATOR.length());
99
100 Portlet portlet = PortletLocalServiceUtil.getPortletById(
101 companyId, portletId);
102
103 String title = portlet.getDisplayName();
104
105 String widgetJsURL =
106 PortalUtil.getPortalURL(request) + PortalUtil.getPathContext() +
107 "/html/js/liferay/widget.js";
108
109 String widgetURL = request.getRequestURL().toString();
110
111 widgetURL = widgetURL.replaceFirst(
112 PropsValues.GOOGLE_GADGET_SERVLET_MAPPING,
113 PropsValues.WIDGET_SERVLET_MAPPING);
114
115 StringBuilder sb = new StringBuilder();
116
117 sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
118 sb.append("<Module>");
119 sb.append("<ModulePrefs title=\"" + title + "\"/>");
120 sb.append("<Content type=\"html\">");
121 sb.append("<![CDATA[");
122 sb.append("<script src=\"" + widgetJsURL + "\" ");
123 sb.append("type=\"text/javascript\"></script>");
124 sb.append("<script type=\"text/javascript\">");
125 sb.append("window.Liferay.Widget({url:'" + widgetURL + "'});");
126 sb.append("</script>");
127 sb.append("]]>");
128 sb.append("</Content>");
129 sb.append("</Module>");
130
131 return sb.toString();
132 }
133
134 private static Log _log = LogFactoryUtil.getLog(GoogleGadgetServlet.class);
135
136 }