001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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     */
042    public class GoogleGadgetServlet extends HttpServlet {
043    
044            public void service(
045                            HttpServletRequest request, HttpServletResponse response)
046                    throws IOException, ServletException {
047    
048                    try {
049                            String content = getContent(request);
050    
051                            if (content == null) {
052                                    PortalUtil.sendError(
053                                            HttpServletResponse.SC_NOT_FOUND,
054                                            new NoSuchLayoutException(), request, response);
055                            }
056                            else {
057                                    request.setAttribute(WebKeys.GOOGLE_GADGET, Boolean.TRUE);
058    
059                                    response.setContentType(ContentTypes.TEXT_XML);
060    
061                                    ServletResponseUtil.write(response, content);
062                            }
063                    }
064                    catch (Exception e) {
065                            _log.error(e, e);
066    
067                            PortalUtil.sendError(
068                                    HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
069                                    response);
070                    }
071            }
072    
073            protected String getContent(HttpServletRequest request) throws Exception {
074                    String path = GetterUtil.getString(request.getPathInfo());
075    
076                    if (Validator.isNull(path)) {
077                            return null;
078                    }
079    
080                    int pos = path.indexOf(Portal.FRIENDLY_URL_SEPARATOR);
081    
082                    if (pos == -1) {
083                            return null;
084                    }
085    
086                    long companyId = PortalUtil.getCompanyId(request);
087    
088                    String portletId = path.substring(
089                            pos + Portal.FRIENDLY_URL_SEPARATOR.length());
090    
091                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
092                            companyId, portletId);
093    
094                    String title = portlet.getDisplayName();
095    
096                    String widgetJsURL =
097                            PortalUtil.getPortalURL(request) + PortalUtil.getPathContext() +
098                                    "/html/js/liferay/widget.js";
099    
100                    String widgetURL = request.getRequestURL().toString();
101    
102                    widgetURL = widgetURL.replaceFirst(
103                            PropsValues.GOOGLE_GADGET_SERVLET_MAPPING,
104                            PropsValues.WIDGET_SERVLET_MAPPING);
105    
106                    StringBundler sb = new StringBundler(19);
107    
108                    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
109                    sb.append("<Module>");
110                    sb.append("<ModulePrefs title=\"");
111                    sb.append(title);
112                    sb.append("\"/>");
113                    sb.append("<Content type=\"html\">");
114                    sb.append("<![CDATA[");
115                    sb.append("<script src=\"");
116                    sb.append(widgetJsURL);
117                    sb.append("\" ");
118                    sb.append("type=\"text/javascript\"></script>");
119                    sb.append("<script type=\"text/javascript\">");
120                    sb.append("window.Liferay.Widget({url:'");
121                    sb.append(widgetURL);
122                    sb.append("'});");
123                    sb.append("</script>");
124                    sb.append("]]>");
125                    sb.append("</Content>");
126                    sb.append("</Module>");
127    
128                    return sb.toString();
129            }
130    
131            private static Log _log = LogFactoryUtil.getLog(GoogleGadgetServlet.class);
132    
133    }