1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.servlet;
24  
25  import com.liferay.portal.NoSuchLayoutException;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.ContentTypes;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.model.Portlet;
32  import com.liferay.portal.service.PortletLocalServiceUtil;
33  import com.liferay.portal.util.Portal;
34  import com.liferay.portal.util.PortalUtil;
35  import com.liferay.portal.util.PropsValues;
36  import com.liferay.portal.util.WebKeys;
37  import com.liferay.util.servlet.ServletResponseUtil;
38  
39  import java.io.IOException;
40  
41  import javax.servlet.ServletException;
42  import javax.servlet.http.HttpServlet;
43  import javax.servlet.http.HttpServletRequest;
44  import javax.servlet.http.HttpServletResponse;
45  
46  /**
47   * <a href="NetvibesServlet.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Alberto Montero
50   * @author Julio Camarero
51   *
52   */
53  public class NetvibesServlet extends HttpServlet {
54  
55      public void service(
56              HttpServletRequest request, HttpServletResponse response)
57          throws IOException, ServletException {
58  
59          try {
60              String content = getContent(request);
61  
62              if (content == null) {
63                  PortalUtil.sendError(
64                      HttpServletResponse.SC_NOT_FOUND,
65                      new NoSuchLayoutException(), request, response);
66              }
67              else {
68                  request.setAttribute(WebKeys.NETVIBES, Boolean.TRUE);
69  
70                  response.setContentType(ContentTypes.TEXT_XML);
71  
72                  ServletResponseUtil.write(response, content);
73              }
74          }
75          catch (Exception e) {
76              _log.error(e, e);
77  
78              PortalUtil.sendError(
79                  HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
80                  response);
81          }
82      }
83  
84      protected String getContent(HttpServletRequest request) throws Exception {
85          String path = GetterUtil.getString(request.getPathInfo());
86  
87          if (Validator.isNull(path)) {
88              return null;
89          }
90  
91          int pos = path.indexOf(Portal.FRIENDLY_URL_SEPARATOR);
92  
93          if (pos == -1) {
94              return null;
95          }
96  
97          long companyId = PortalUtil.getCompanyId(request);
98  
99          String portletId = path.substring(
100             pos + Portal.FRIENDLY_URL_SEPARATOR.length());
101 
102         Portlet portlet = PortletLocalServiceUtil.getPortletById(
103             companyId, portletId);
104 
105         String title = portlet.getDisplayName();
106 
107         String portalURL = PortalUtil.getPortalURL(request);
108 
109         String iconURL =
110             portalURL + PortalUtil.getPathContext() + portlet.getIcon();
111 
112         String widgetJsURL =
113             portalURL + PortalUtil.getPathContext() +
114                 "/html/js/liferay/widget.js";
115 
116         String widgetURL = request.getRequestURL().toString();
117 
118         widgetURL = widgetURL.replaceFirst(
119             PropsValues.NETVIBES_SERVLET_MAPPING,
120             PropsValues.WIDGET_SERVLET_MAPPING);
121 
122         StringBuilder sb = new StringBuilder();
123 
124         sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
125         sb.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 ");
126         sb.append("Strict//EN\" ");
127         sb.append("\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
128         sb.append("<html xmlns=\"http://www.w3.org/1999/xhtml\" ");
129         sb.append("xmlns:widget=\"http://www.netvibes.com/ns/\">");
130         sb.append("<head>");
131         sb.append("<link href=\"" + _NETVIBES_CSS + "\" rel=\"stylesheet\" ");
132         sb.append("type=\"text/css\" />");
133         sb.append("<script src=\"" + _NETVIBES_JS + "\" ");
134         sb.append("type=\"text/javascript\"></script>");
135         sb.append("<title>" + title + "</title>");
136         sb.append("<link href=\"" + iconURL + "\" rel=\"icon\" ");
137         sb.append("type=\"image/png\" />");
138         sb.append("</head>");
139         sb.append("<body>");
140         sb.append("<script src=\"" + widgetJsURL + "\" ");
141         sb.append("type=\"text/javascript\"></script>");
142         sb.append("<script type=\"text/javascript\">");
143         sb.append("Liferay.Widget({url:\"" + widgetURL + "\"});");
144         sb.append("</script>");
145         sb.append("</body>");
146         sb.append("</html>");
147 
148         return sb.toString();
149     }
150 
151     private static final String _NETVIBES_CSS =
152         "http://www.netvibes.com/themes/uwa/style.css";
153 
154     private static final String _NETVIBES_JS =
155         "http://www.netvibes.com/js/UWA/load.js.php?env=Standalone";
156 
157     private static Log _log = LogFactoryUtil.getLog(NetvibesServlet.class);
158 
159 }