001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.HtmlUtil;
026    import com.liferay.portal.kernel.util.StringBundler;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.kernel.util.WebKeys;
029    import com.liferay.portal.model.Portlet;
030    import com.liferay.portal.service.PortletLocalServiceUtil;
031    import com.liferay.portal.util.Portal;
032    import com.liferay.portal.util.PortalUtil;
033    import com.liferay.portal.util.PropsValues;
034    
035    import java.io.IOException;
036    
037    import javax.servlet.ServletException;
038    import javax.servlet.http.HttpServlet;
039    import javax.servlet.http.HttpServletRequest;
040    import javax.servlet.http.HttpServletResponse;
041    
042    /**
043     * @author Alberto Montero
044     * @author Julio Camarero
045     */
046    public class NetvibesServlet extends HttpServlet {
047    
048            @Override
049            public void service(
050                            HttpServletRequest request, HttpServletResponse response)
051                    throws IOException, ServletException {
052    
053                    try {
054                            String content = getContent(request);
055    
056                            if (content == null) {
057                                    PortalUtil.sendError(
058                                            HttpServletResponse.SC_NOT_FOUND,
059                                            new NoSuchLayoutException(), request, response);
060                            }
061                            else {
062                                    request.setAttribute(WebKeys.NETVIBES, Boolean.TRUE);
063    
064                                    response.setContentType(ContentTypes.TEXT_HTML);
065    
066                                    ServletResponseUtil.write(response, content);
067                            }
068                    }
069                    catch (Exception e) {
070                            _log.error(e, e);
071    
072                            PortalUtil.sendError(
073                                    HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
074                                    response);
075                    }
076            }
077    
078            protected String getContent(HttpServletRequest request) throws Exception {
079                    String path = GetterUtil.getString(request.getPathInfo());
080    
081                    if (Validator.isNull(path)) {
082                            return null;
083                    }
084    
085                    int pos = path.indexOf(Portal.FRIENDLY_URL_SEPARATOR);
086    
087                    if (pos == -1) {
088                            return null;
089                    }
090    
091                    long companyId = PortalUtil.getCompanyId(request);
092    
093                    String portletId = path.substring(
094                            pos + Portal.FRIENDLY_URL_SEPARATOR.length());
095    
096                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
097                            companyId, portletId);
098    
099                    String title = HtmlUtil.escape(portlet.getDisplayName());
100    
101                    String portalURL = PortalUtil.getPortalURL(request);
102    
103                    String iconURL =
104                            portalURL + PortalUtil.getPathContext() + portlet.getIcon();
105    
106                    String widgetJsURL = portalURL;
107    
108                    widgetJsURL += PortalWebResourcesUtil.getContextPath(
109                            PortalWebResourceConstants.RESOURCE_TYPE_JS);
110                    widgetJsURL += "/liferay/widget.js";
111    
112                    String widgetURL = String.valueOf(request.getRequestURL());
113    
114                    widgetURL = widgetURL.replaceFirst(
115                            PropsValues.NETVIBES_SERVLET_MAPPING,
116                            PropsValues.WIDGET_SERVLET_MAPPING);
117                    widgetURL = HtmlUtil.escapeJS(widgetURL);
118    
119                    StringBundler sb = new StringBundler(31);
120    
121                    sb.append("<!DOCTYPE html>");
122                    sb.append("<html>");
123                    sb.append("<head>");
124                    sb.append("<link href=\"");
125                    sb.append(_NETVIBES_CSS);
126                    sb.append("\" rel=\"stylesheet\" ");
127                    sb.append("type=\"text/css\" />");
128                    sb.append("<script src=\"");
129                    sb.append(_NETVIBES_JS);
130                    sb.append("\" ");
131                    sb.append("type=\"text/javascript\"></script>");
132                    sb.append("<title>");
133                    sb.append(title);
134                    sb.append("</title>");
135                    sb.append("<link href=\"");
136                    sb.append(iconURL);
137                    sb.append("\" rel=\"icon\" ");
138                    sb.append("type=\"image/png\" />");
139                    sb.append("</head>");
140                    sb.append("<body>");
141                    sb.append("<script src=\"");
142                    sb.append(widgetJsURL);
143                    sb.append("\" ");
144                    sb.append("type=\"text/javascript\"></script>");
145                    sb.append("<script type=\"text/javascript\">");
146                    sb.append("Liferay.Widget({url:\"");
147                    sb.append(widgetURL);
148                    sb.append("\"});");
149                    sb.append("</script>");
150                    sb.append("</body>");
151                    sb.append("</html>");
152    
153                    return sb.toString();
154            }
155    
156            private static final String _NETVIBES_CSS =
157                    "http://www.netvibes.com/themes/uwa/style.css";
158    
159            private static final String _NETVIBES_JS =
160                    "http://www.netvibes.com/js/UWA/load.js.php?env=Standalone";
161    
162            private static final Log _log = LogFactoryUtil.getLog(
163                    NetvibesServlet.class);
164    
165    }