001
014
015 package com.liferay.portal.servlet;
016
017 import com.liferay.portal.NoSuchLayoutException;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.servlet.ServletResponseUtil;
021 import com.liferay.portal.kernel.util.ContentTypes;
022 import com.liferay.portal.kernel.util.GetterUtil;
023 import com.liferay.portal.kernel.util.StringBundler;
024 import com.liferay.portal.kernel.util.Validator;
025 import com.liferay.portal.model.Portlet;
026 import com.liferay.portal.service.PortletLocalServiceUtil;
027 import com.liferay.portal.util.Portal;
028 import com.liferay.portal.util.PortalUtil;
029 import com.liferay.portal.util.PropsValues;
030 import com.liferay.portal.util.WebKeys;
031
032 import java.io.IOException;
033
034 import javax.servlet.ServletException;
035 import javax.servlet.http.HttpServlet;
036 import javax.servlet.http.HttpServletRequest;
037 import javax.servlet.http.HttpServletResponse;
038
039
042 public class GoogleGadgetServlet extends HttpServlet {
043
044 @Override
045 public void service(
046 HttpServletRequest request, HttpServletResponse response)
047 throws IOException, ServletException {
048
049 try {
050 String content = getContent(request);
051
052 if (content == null) {
053 PortalUtil.sendError(
054 HttpServletResponse.SC_NOT_FOUND,
055 new NoSuchLayoutException(), request, response);
056 }
057 else {
058 request.setAttribute(WebKeys.GOOGLE_GADGET, Boolean.TRUE);
059
060 response.setContentType(ContentTypes.TEXT_XML);
061
062 ServletResponseUtil.write(response, content);
063 }
064 }
065 catch (Exception e) {
066 _log.error(e, e);
067
068 PortalUtil.sendError(
069 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
070 response);
071 }
072 }
073
074 protected String getContent(HttpServletRequest request) throws Exception {
075 String path = GetterUtil.getString(request.getPathInfo());
076
077 if (Validator.isNull(path)) {
078 return null;
079 }
080
081 int pos = path.indexOf(Portal.FRIENDLY_URL_SEPARATOR);
082
083 if (pos == -1) {
084 return null;
085 }
086
087 long companyId = PortalUtil.getCompanyId(request);
088
089 String portletId = path.substring(
090 pos + Portal.FRIENDLY_URL_SEPARATOR.length());
091
092 Portlet portlet = PortletLocalServiceUtil.getPortletById(
093 companyId, portletId);
094
095 String title = portlet.getDisplayName();
096
097 String widgetJsURL =
098 PortalUtil.getPortalURL(request) + PortalUtil.getPathContext() +
099 "/html/js/liferay/widget.js";
100
101 String widgetURL = request.getRequestURL().toString();
102
103 widgetURL = widgetURL.replaceFirst(
104 PropsValues.GOOGLE_GADGET_SERVLET_MAPPING,
105 PropsValues.WIDGET_SERVLET_MAPPING);
106
107 StringBundler sb = new StringBundler(19);
108
109 sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
110 sb.append("<Module>");
111 sb.append("<ModulePrefs title=\"");
112 sb.append(title);
113 sb.append("\"/>");
114 sb.append("<Content type=\"html\">");
115 sb.append("<![CDATA[");
116 sb.append("<script src=\"");
117 sb.append(widgetJsURL);
118 sb.append("\" ");
119 sb.append("type=\"text/javascript\"></script>");
120 sb.append("<script type=\"text/javascript\">");
121 sb.append("window.Liferay.Widget({url:'");
122 sb.append(widgetURL);
123 sb.append("'});");
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 }