1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.GetterUtil;
21  import com.liferay.portal.kernel.util.ParamUtil;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.util.Portal;
24  import com.liferay.portal.util.PortalUtil;
25  import com.liferay.portal.util.WebKeys;
26  
27  import java.io.IOException;
28  
29  import javax.servlet.RequestDispatcher;
30  import javax.servlet.ServletContext;
31  import javax.servlet.ServletException;
32  import javax.servlet.http.HttpServlet;
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  
36  /**
37   * <a href="WidgetServlet.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   */
41  public class WidgetServlet extends HttpServlet {
42  
43      public void service(
44              HttpServletRequest request, HttpServletResponse response)
45          throws IOException, ServletException {
46  
47          try {
48              String redirect = getRedirect(request);
49  
50              if (redirect == null) {
51                  PortalUtil.sendError(
52                      HttpServletResponse.SC_NOT_FOUND,
53                      new NoSuchLayoutException(), request, response);
54              }
55              else {
56                  request.setAttribute(WebKeys.WIDGET, Boolean.TRUE);
57  
58                  ServletContext servletContext = getServletContext();
59  
60                  RequestDispatcher requestDispatcher =
61                      servletContext.getRequestDispatcher(redirect);
62  
63                  requestDispatcher.forward(request, response);
64              }
65          }
66          catch (Exception e) {
67              _log.error(e, e);
68  
69              PortalUtil.sendError(
70                  HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
71                  response);
72          }
73      }
74  
75      protected String getRedirect(HttpServletRequest request) {
76          String path = GetterUtil.getString(request.getPathInfo());
77  
78          if (Validator.isNull(path)) {
79              return null;
80          }
81  
82          String ppid = ParamUtil.getString(request, "p_p_id");
83  
84          int pos = path.indexOf(Portal.FRIENDLY_URL_SEPARATOR);
85  
86          if (Validator.isNull(ppid) && (pos == -1)) {
87              return null;
88          }
89  
90          return path;
91      }
92  
93      private static Log _log = LogFactoryUtil.getLog(WidgetServlet.class);
94  
95  }