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