001    /**
002     * Copyright (c) 2000-2010 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.util.ContentTypes;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.Portlet;
025    import com.liferay.portal.service.PortletLocalServiceUtil;
026    import com.liferay.portal.util.Portal;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portal.util.PropsValues;
029    import com.liferay.portal.util.WebKeys;
030    import com.liferay.util.servlet.ServletResponseUtil;
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    /**
040     * @author Alberto Montero
041     * @author Julio Camarero
042     */
043    public class NetvibesServlet extends HttpServlet {
044    
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.NETVIBES, 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 portalURL = PortalUtil.getPortalURL(request);
098    
099                    String iconURL =
100                            portalURL + PortalUtil.getPathContext() + portlet.getIcon();
101    
102                    String widgetJsURL =
103                            portalURL + PortalUtil.getPathContext() +
104                                    "/html/js/liferay/widget.js";
105    
106                    String widgetURL = request.getRequestURL().toString();
107    
108                    widgetURL = widgetURL.replaceFirst(
109                            PropsValues.NETVIBES_SERVLET_MAPPING,
110                            PropsValues.WIDGET_SERVLET_MAPPING);
111    
112                    StringBundler sb = new StringBundler(35);
113    
114                    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
115                    sb.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 ");
116                    sb.append("Strict//EN\" ");
117                    sb.append("\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
118                    sb.append("<html xmlns=\"http://www.w3.org/1999/xhtml\" ");
119                    sb.append("xmlns:widget=\"http://www.netvibes.com/ns/\">");
120                    sb.append("<head>");
121                    sb.append("<link href=\"");
122                    sb.append(_NETVIBES_CSS);
123                    sb.append("\" rel=\"stylesheet\" ");
124                    sb.append("type=\"text/css\" />");
125                    sb.append("<script src=\"");
126                    sb.append(_NETVIBES_JS);
127                    sb.append("\" ");
128                    sb.append("type=\"text/javascript\"></script>");
129                    sb.append("<title>");
130                    sb.append(title);
131                    sb.append("</title>");
132                    sb.append("<link href=\"");
133                    sb.append(iconURL);
134                    sb.append("\" rel=\"icon\" ");
135                    sb.append("type=\"image/png\" />");
136                    sb.append("</head>");
137                    sb.append("<body>");
138                    sb.append("<script src=\"");
139                    sb.append(widgetJsURL);
140                    sb.append("\" ");
141                    sb.append("type=\"text/javascript\"></script>");
142                    sb.append("<script type=\"text/javascript\">");
143                    sb.append("Liferay.Widget({url:\"");
144                    sb.append(widgetURL);
145                    sb.append("\"});");
146                    sb.append("</script>");
147                    sb.append("</body>");
148                    sb.append("</html>");
149    
150                    return sb.toString();
151            }
152    
153            private static final String _NETVIBES_CSS =
154                    "http://www.netvibes.com/themes/uwa/style.css";
155    
156            private static final String _NETVIBES_JS =
157                    "http://www.netvibes.com/js/UWA/load.js.php?env=Standalone";
158    
159            private static Log _log = LogFactoryUtil.getLog(NetvibesServlet.class);
160    
161    }