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  public class NetvibesServlet extends HttpServlet {
53  
54      public void service(
55              HttpServletRequest request, HttpServletResponse response)
56          throws IOException, ServletException {
57  
58          try {
59              String content = getContent(request);
60  
61              if (content == null) {
62                  PortalUtil.sendError(
63                      HttpServletResponse.SC_NOT_FOUND,
64                      new NoSuchLayoutException(), request, response);
65              }
66              else {
67                  request.setAttribute(WebKeys.NETVIBES, Boolean.TRUE);
68  
69                  response.setContentType(ContentTypes.TEXT_XML);
70  
71                  ServletResponseUtil.write(response, content);
72              }
73          }
74          catch (Exception e) {
75              _log.error(e, e);
76  
77              PortalUtil.sendError(
78                  HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
79                  response);
80          }
81      }
82  
83      protected String getContent(HttpServletRequest request) throws Exception {
84          String path = GetterUtil.getString(request.getPathInfo());
85  
86          if (Validator.isNull(path)) {
87              return null;
88          }
89  
90          int pos = path.indexOf(Portal.FRIENDLY_URL_SEPARATOR);
91  
92          if (pos == -1) {
93              return null;
94          }
95  
96          long companyId = PortalUtil.getCompanyId(request);
97  
98          String portletId = path.substring(
99              pos + Portal.FRIENDLY_URL_SEPARATOR.length());
100 
101         Portlet portlet = PortletLocalServiceUtil.getPortletById(
102             companyId, portletId);
103 
104         String title = portlet.getDisplayName();
105 
106         String portalURL = PortalUtil.getPortalURL(request);
107 
108         String iconURL =
109             portalURL + PortalUtil.getPathContext() + portlet.getIcon();
110 
111         String widgetJsURL =
112             portalURL + PortalUtil.getPathContext() +
113                 "/html/js/liferay/widget.js";
114 
115         String widgetURL = request.getRequestURL().toString();
116 
117         widgetURL = widgetURL.replaceFirst(
118             PropsValues.NETVIBES_SERVLET_MAPPING,
119             PropsValues.WIDGET_SERVLET_MAPPING);
120 
121         StringBuilder sb = new StringBuilder();
122 
123         sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
124         sb.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 ");
125         sb.append("Strict//EN\" ");
126         sb.append("\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
127         sb.append("<html xmlns=\"http://www.w3.org/1999/xhtml\" ");
128         sb.append("xmlns:widget=\"http://www.netvibes.com/ns/\">");
129         sb.append("<head>");
130         sb.append("<link href=\"" + _NETVIBES_CSS + "\" rel=\"stylesheet\" ");
131         sb.append("type=\"text/css\" />");
132         sb.append("<script src=\"" + _NETVIBES_JS + "\" ");
133         sb.append("type=\"text/javascript\"></script>");
134         sb.append("<title>" + title + "</title>");
135         sb.append("<link href=\"" + iconURL + "\" rel=\"icon\" ");
136         sb.append("type=\"image/png\" />");
137         sb.append("</head>");
138         sb.append("<body>");
139         sb.append("<script src=\"" + widgetJsURL + "\" ");
140         sb.append("type=\"text/javascript\"></script>");
141         sb.append("<script type=\"text/javascript\">");
142         sb.append("Liferay.Widget({url:\"" + widgetURL + "\"});");
143         sb.append("</script>");
144         sb.append("</body>");
145         sb.append("</html>");
146 
147         return sb.toString();
148     }
149 
150     private static final String _NETVIBES_CSS =
151         "http://www.netvibes.com/themes/uwa/style.css";
152 
153     private static final String _NETVIBES_JS =
154         "http://www.netvibes.com/js/UWA/load.js.php?env=Standalone";
155 
156     private static Log _log = LogFactoryUtil.getLog(NetvibesServlet.class);
157 
158 }