001
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
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 }