1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.servlet;
16  
17  import com.liferay.portal.NoSuchLayoutException;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.util.ContentTypes;
21  import com.liferay.portal.kernel.util.GetterUtil;
22  import com.liferay.portal.kernel.util.StringBundler;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.model.Portlet;
25  import com.liferay.portal.service.PortletLocalServiceUtil;
26  import com.liferay.portal.util.Portal;
27  import com.liferay.portal.util.PortalUtil;
28  import com.liferay.portal.util.PropsValues;
29  import com.liferay.portal.util.WebKeys;
30  import com.liferay.util.servlet.ServletResponseUtil;
31  
32  import java.io.IOException;
33  
34  import javax.servlet.ServletException;
35  import javax.servlet.http.HttpServlet;
36  import javax.servlet.http.HttpServletRequest;
37  import javax.servlet.http.HttpServletResponse;
38  
39  /**
40   * <a href="NetvibesServlet.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Alberto Montero
43   * @author Julio Camarero
44   */
45  public class NetvibesServlet extends HttpServlet {
46  
47      public void service(
48              HttpServletRequest request, HttpServletResponse response)
49          throws IOException, ServletException {
50  
51          try {
52              String content = getContent(request);
53  
54              if (content == null) {
55                  PortalUtil.sendError(
56                      HttpServletResponse.SC_NOT_FOUND,
57                      new NoSuchLayoutException(), request, response);
58              }
59              else {
60                  request.setAttribute(WebKeys.NETVIBES, Boolean.TRUE);
61  
62                  response.setContentType(ContentTypes.TEXT_XML);
63  
64                  ServletResponseUtil.write(response, content);
65              }
66          }
67          catch (Exception e) {
68              _log.error(e, e);
69  
70              PortalUtil.sendError(
71                  HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
72                  response);
73          }
74      }
75  
76      protected String getContent(HttpServletRequest request) throws Exception {
77          String path = GetterUtil.getString(request.getPathInfo());
78  
79          if (Validator.isNull(path)) {
80              return null;
81          }
82  
83          int pos = path.indexOf(Portal.FRIENDLY_URL_SEPARATOR);
84  
85          if (pos == -1) {
86              return null;
87          }
88  
89          long companyId = PortalUtil.getCompanyId(request);
90  
91          String portletId = path.substring(
92              pos + Portal.FRIENDLY_URL_SEPARATOR.length());
93  
94          Portlet portlet = PortletLocalServiceUtil.getPortletById(
95              companyId, portletId);
96  
97          String title = portlet.getDisplayName();
98  
99          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 
114         StringBundler sb = new StringBundler(35);
115 
116         sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
117         sb.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 ");
118         sb.append("Strict//EN\" ");
119         sb.append("\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
120         sb.append("<html xmlns=\"http://www.w3.org/1999/xhtml\" ");
121         sb.append("xmlns:widget=\"http://www.netvibes.com/ns/\">");
122         sb.append("<head>");
123         sb.append("<link href=\"");
124         sb.append(_NETVIBES_CSS);
125         sb.append("\" rel=\"stylesheet\" ");
126         sb.append("type=\"text/css\" />");
127         sb.append("<script src=\"");
128         sb.append(_NETVIBES_JS);
129         sb.append("\" ");
130         sb.append("type=\"text/javascript\"></script>");
131         sb.append("<title>");
132         sb.append(title);
133         sb.append("</title>");
134         sb.append("<link href=\"");
135         sb.append(iconURL);
136         sb.append("\" rel=\"icon\" ");
137         sb.append("type=\"image/png\" />");
138         sb.append("</head>");
139         sb.append("<body>");
140         sb.append("<script src=\"");
141         sb.append(widgetJsURL);
142         sb.append("\" ");
143         sb.append("type=\"text/javascript\"></script>");
144         sb.append("<script type=\"text/javascript\">");
145         sb.append("Liferay.Widget({url:\"");
146         sb.append(widgetURL);
147         sb.append("\"});");
148         sb.append("</script>");
149         sb.append("</body>");
150         sb.append("</html>");
151 
152         return sb.toString();
153     }
154 
155     private static final String _NETVIBES_CSS =
156         "http://www.netvibes.com/themes/uwa/style.css";
157 
158     private static final String _NETVIBES_JS =
159         "http://www.netvibes.com/js/UWA/load.js.php?env=Standalone";
160 
161     private static Log _log = LogFactoryUtil.getLog(NetvibesServlet.class);
162 
163 }