/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.portal.servlet;
import com.liferay.portal.kernel.exception.NoSuchLayoutException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.model.Portlet;
import com.liferay.portal.kernel.service.PortletLocalServiceUtil;
import com.liferay.portal.kernel.servlet.PortalWebResourceConstants;
import com.liferay.portal.kernel.servlet.PortalWebResourcesUtil;
import com.liferay.portal.kernel.servlet.ServletResponseUtil;
import com.liferay.portal.kernel.util.ContentTypes;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.HtmlUtil;
import com.liferay.portal.kernel.util.Portal;
import com.liferay.portal.kernel.util.PortalUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.util.PropsValues;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Alberto Montero
* @author Julio Camarero
*/
public class NetvibesServlet extends HttpServlet {
@Override
public void service(
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
try {
String content = getContent(request);
if (content == null) {
PortalUtil.sendError(
HttpServletResponse.SC_NOT_FOUND,
new NoSuchLayoutException(), request, response);
}
else {
request.setAttribute(WebKeys.NETVIBES, Boolean.TRUE);
response.setContentType(ContentTypes.TEXT_HTML);
ServletResponseUtil.write(response, content);
}
}
catch (Exception e) {
_log.error(e, e);
PortalUtil.sendError(
HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
response);
}
}
protected String getContent(HttpServletRequest request) throws Exception {
String path = GetterUtil.getString(request.getPathInfo());
if (Validator.isNull(path)) {
return null;
}
int pos = path.indexOf(Portal.FRIENDLY_URL_SEPARATOR);
if (pos == -1) {
return null;
}
long companyId = PortalUtil.getCompanyId(request);
String portletId = path.substring(
pos + Portal.FRIENDLY_URL_SEPARATOR.length());
Portlet portlet = PortletLocalServiceUtil.getPortletById(
companyId, portletId);
String title = HtmlUtil.escape(portlet.getDisplayName());
String portalURL = PortalUtil.getPortalURL(request);
String iconURL =
portalURL + PortalUtil.getPathContext() + portlet.getIcon();
String widgetJsURL = portalURL;
widgetJsURL += PortalWebResourcesUtil.getContextPath(
PortalWebResourceConstants.RESOURCE_TYPE_JS);
widgetJsURL += "/liferay/widget.js";
String widgetURL = String.valueOf(request.getRequestURL());
widgetURL = widgetURL.replaceFirst(
PropsValues.NETVIBES_SERVLET_MAPPING,
PropsValues.WIDGET_SERVLET_MAPPING);
widgetURL = HtmlUtil.escapeJS(widgetURL);
StringBundler sb = new StringBundler(31);
sb.append("<!DOCTYPE html>");
sb.append("<html>");
sb.append("<head>");
sb.append("<link href=\"");
sb.append(_NETVIBES_CSS);
sb.append("\" rel=\"stylesheet\" type=\"text/css\" ");
sb.append("/>");
sb.append("<script src=\"");
sb.append(_NETVIBES_JS);
sb.append("\" ");
sb.append("type=\"text/javascript\"></script>");
sb.append("<title>");
sb.append(title);
sb.append("</title>");
sb.append("<link href=\"");
sb.append(iconURL);
sb.append("\" rel=\"icon\" type=\"image/png\" ");
sb.append("/>");
sb.append("</head>");
sb.append("<body>");
sb.append("<script src=\"");
sb.append(widgetJsURL);
sb.append("\" ");
sb.append("type=\"text/javascript\"></script>");
sb.append("<script type=\"text/javascript\">");
sb.append("Liferay.Widget({url:\"");
sb.append(widgetURL);
sb.append("\"});");
sb.append("</script>");
sb.append("</body>");
sb.append("</html>");
return sb.toString();
}
private static final String _NETVIBES_CSS =
"http://www.netvibes.com/themes/uwa/style.css";
private static final String _NETVIBES_JS =
"http://www.netvibes.com/js/UWA/load.js.php?env=Standalone";
private static final Log _log = LogFactoryUtil.getLog(
NetvibesServlet.class);
}