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.PortalUtil;
29  import com.liferay.portal.util.WebKeys;
30  
31  import java.io.IOException;
32  
33  import javax.servlet.RequestDispatcher;
34  import javax.servlet.ServletContext;
35  import javax.servlet.ServletException;
36  import javax.servlet.http.HttpServlet;
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.http.HttpServletResponse;
39  
40  /**
41   * <a href="WidgetServlet.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class WidgetServlet extends HttpServlet {
47  
48      public void service(
49              HttpServletRequest request, HttpServletResponse response)
50          throws IOException, ServletException {
51  
52          try {
53              String redirect = getRedirect(request);
54  
55              if (redirect == null) {
56                  PortalUtil.sendError(
57                      HttpServletResponse.SC_NOT_FOUND,
58                      new NoSuchLayoutException(), request, response);
59              }
60              else {
61                  request.setAttribute(WebKeys.WIDGET, Boolean.TRUE);
62  
63                  ServletContext servletContext = getServletContext();
64  
65                  RequestDispatcher requestDispatcher =
66                      servletContext.getRequestDispatcher(redirect);
67  
68                  requestDispatcher.forward(request, response);
69              }
70          }
71          catch (Exception e) {
72              _log.error(e, e);
73  
74              PortalUtil.sendError(
75                  HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
76                  response);
77          }
78      }
79  
80      protected String getRedirect(HttpServletRequest request) {
81          String path = GetterUtil.getString(request.getPathInfo());
82  
83          if (Validator.isNull(path)) {
84              return null;
85          }
86  
87          String ppid = ParamUtil.getString(request, "p_p_id");
88  
89          int pos = path.indexOf("/-/");
90  
91          if (Validator.isNull(ppid) && (pos == -1)) {
92              return null;
93          }
94  
95          return path;
96      }
97  
98      private static Log _log = LogFactoryUtil.getLog(WidgetServlet.class);
99  
100 }