001    /**
002     * Copyright (c) 2000-2013 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            @Override
042            public void service(
043                            HttpServletRequest request, HttpServletResponse response)
044                    throws IOException, ServletException {
045    
046                    try {
047                            String redirect = getRedirect(request);
048    
049                            if ((redirect == null) || !PortalUtil.isValidResourceId(redirect)) {
050                                    PortalUtil.sendError(
051                                            HttpServletResponse.SC_NOT_FOUND,
052                                            new NoSuchLayoutException(), request, response);
053                            }
054                            else {
055                                    request.setAttribute(WebKeys.WIDGET, Boolean.TRUE);
056    
057                                    ServletContext servletContext = getServletContext();
058    
059                                    RequestDispatcher requestDispatcher =
060                                            servletContext.getRequestDispatcher(redirect);
061    
062                                    requestDispatcher.forward(request, response);
063                            }
064                    }
065                    catch (Exception e) {
066                            _log.error(e, e);
067    
068                            PortalUtil.sendError(
069                                    HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
070                                    response);
071                    }
072            }
073    
074            protected String getRedirect(HttpServletRequest request) {
075                    String path = GetterUtil.getString(request.getPathInfo());
076    
077                    if (Validator.isNull(path)) {
078                            return null;
079                    }
080    
081                    String ppid = ParamUtil.getString(request, "p_p_id");
082    
083                    int pos = path.indexOf(Portal.FRIENDLY_URL_SEPARATOR);
084    
085                    if (Validator.isNull(ppid) && (pos == -1)) {
086                            return null;
087                    }
088    
089                    return path;
090            }
091    
092            private static Log _log = LogFactoryUtil.getLog(WidgetServlet.class);
093    
094    }