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