1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.servlet;
21  
22  import com.liferay.portal.NoSuchLayoutException;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.util.Portal;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portal.util.WebKeys;
31  
32  import java.io.IOException;
33  
34  import javax.servlet.RequestDispatcher;
35  import javax.servlet.ServletContext;
36  import javax.servlet.ServletException;
37  import javax.servlet.http.HttpServlet;
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.http.HttpServletResponse;
40  
41  /**
42   * <a href="WidgetServlet.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class WidgetServlet extends HttpServlet {
48  
49      public void service(
50              HttpServletRequest request, HttpServletResponse response)
51          throws IOException, ServletException {
52  
53          try {
54              String redirect = getRedirect(request);
55  
56              if (redirect == null) {
57                  PortalUtil.sendError(
58                      HttpServletResponse.SC_NOT_FOUND,
59                      new NoSuchLayoutException(), request, response);
60              }
61              else {
62                  request.setAttribute(WebKeys.WIDGET, Boolean.TRUE);
63  
64                  ServletContext servletContext = getServletContext();
65  
66                  RequestDispatcher requestDispatcher =
67                      servletContext.getRequestDispatcher(redirect);
68  
69                  requestDispatcher.forward(request, response);
70              }
71          }
72          catch (Exception e) {
73              _log.error(e, e);
74  
75              PortalUtil.sendError(
76                  HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
77                  response);
78          }
79      }
80  
81      protected String getRedirect(HttpServletRequest request) {
82          String path = GetterUtil.getString(request.getPathInfo());
83  
84          if (Validator.isNull(path)) {
85              return null;
86          }
87  
88          String ppid = ParamUtil.getString(request, "p_p_id");
89  
90          int pos = path.indexOf(Portal.FRIENDLY_URL_SEPARATOR);
91  
92          if (Validator.isNull(ppid) && (pos == -1)) {
93              return null;
94          }
95  
96          return path;
97      }
98  
99      private static Log _log = LogFactoryUtil.getLog(WidgetServlet.class);
100 
101 }