001    /**
002     * Copyright (c) 2000-2013 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.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.HtmlUtil;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.model.Portlet;
027    import com.liferay.portal.service.PortletLocalServiceUtil;
028    import com.liferay.portal.util.Portal;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portal.util.PropsValues;
031    import com.liferay.portal.util.WebKeys;
032    
033    import java.io.IOException;
034    
035    import javax.servlet.ServletException;
036    import javax.servlet.http.HttpServlet;
037    import javax.servlet.http.HttpServletRequest;
038    import javax.servlet.http.HttpServletResponse;
039    
040    /**
041     * @author Alberto Montero
042     * @author Julio Camarero
043     */
044    public class NetvibesServlet 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.NETVIBES, Boolean.TRUE);
061    
062                                    response.setContentType(ContentTypes.TEXT_HTML);
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 = HtmlUtil.escape(portlet.getDisplayName());
098    
099                    String portalURL = PortalUtil.getPortalURL(request);
100    
101                    String iconURL =
102                            portalURL + PortalUtil.getPathContext() + portlet.getIcon();
103    
104                    String widgetJsURL =
105                            portalURL + PortalUtil.getPathContext() +
106                                    "/html/js/liferay/widget.js";
107    
108                    String widgetURL = request.getRequestURL().toString();
109    
110                    widgetURL = widgetURL.replaceFirst(
111                            PropsValues.NETVIBES_SERVLET_MAPPING,
112                            PropsValues.WIDGET_SERVLET_MAPPING);
113                    widgetURL = HtmlUtil.escapeJS(widgetURL);
114    
115                    StringBundler sb = new StringBundler(31);
116    
117                    sb.append("<!DOCTYPE html>");
118                    sb.append("<html>");
119                    sb.append("<head>");
120                    sb.append("<link href=\"");
121                    sb.append(_NETVIBES_CSS);
122                    sb.append("\" rel=\"stylesheet\" ");
123                    sb.append("type=\"text/css\" />");
124                    sb.append("<script src=\"");
125                    sb.append(_NETVIBES_JS);
126                    sb.append("\" ");
127                    sb.append("type=\"text/javascript\"></script>");
128                    sb.append("<title>");
129                    sb.append(title);
130                    sb.append("</title>");
131                    sb.append("<link href=\"");
132                    sb.append(iconURL);
133                    sb.append("\" rel=\"icon\" ");
134                    sb.append("type=\"image/png\" />");
135                    sb.append("</head>");
136                    sb.append("<body>");
137                    sb.append("<script src=\"");
138                    sb.append(widgetJsURL);
139                    sb.append("\" ");
140                    sb.append("type=\"text/javascript\"></script>");
141                    sb.append("<script type=\"text/javascript\">");
142                    sb.append("Liferay.Widget({url:\"");
143                    sb.append(widgetURL);
144                    sb.append("\"});");
145                    sb.append("</script>");
146                    sb.append("</body>");
147                    sb.append("</html>");
148    
149                    return sb.toString();
150            }
151    
152            private static final String _NETVIBES_CSS =
153                    "http://www.netvibes.com/themes/uwa/style.css";
154    
155            private static final String _NETVIBES_JS =
156                    "http://www.netvibes.com/js/UWA/load.js.php?env=Standalone";
157    
158            private static Log _log = LogFactoryUtil.getLog(NetvibesServlet.class);
159    
160    }