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.GetterUtil;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.util.Portal;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.portal.util.WebKeys;
026    
027    import java.io.IOException;
028    
029    import javax.servlet.RequestDispatcher;
030    import javax.servlet.ServletContext;
031    import javax.servlet.ServletException;
032    import javax.servlet.http.HttpServlet;
033    import javax.servlet.http.HttpServletRequest;
034    import javax.servlet.http.HttpServletResponse;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     */
039    public class WidgetServlet extends HttpServlet {
040    
041            public void service(
042                            HttpServletRequest request, HttpServletResponse response)
043                    throws IOException, ServletException {
044    
045                    try {
046                            String redirect = getRedirect(request);
047    
048                            if ((redirect == null) || !PortalUtil.isValidResourceId(redirect)) {
049                                    PortalUtil.sendError(
050                                            HttpServletResponse.SC_NOT_FOUND,
051                                            new NoSuchLayoutException(), request, response);
052                            }
053                            else {
054                                    request.setAttribute(WebKeys.WIDGET, Boolean.TRUE);
055    
056                                    ServletContext servletContext = getServletContext();
057    
058                                    RequestDispatcher requestDispatcher =
059                                            servletContext.getRequestDispatcher(redirect);
060    
061                                    requestDispatcher.forward(request, response);
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 getRedirect(HttpServletRequest request) {
074                    String path = GetterUtil.getString(request.getPathInfo());
075    
076                    if (Validator.isNull(path)) {
077                            return null;
078                    }
079    
080                    String ppid = ParamUtil.getString(request, "p_p_id");
081    
082                    int pos = path.indexOf(Portal.FRIENDLY_URL_SEPARATOR);
083    
084                    if (Validator.isNull(ppid) && (pos == -1)) {
085                            return null;
086                    }
087    
088                    return path;
089            }
090    
091            private static Log _log = LogFactoryUtil.getLog(WidgetServlet.class);
092    
093    }