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.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) || !PortalUtil.isValidResourceId(redirect)) {
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  }