001
014
015 package com.liferay.portal.struts;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019
020 import java.io.IOException;
021
022 import java.util.Enumeration;
023 import java.util.HashMap;
024 import java.util.Map;
025
026 import javax.portlet.PortletContext;
027 import javax.portlet.PortletRequest;
028
029 import javax.servlet.RequestDispatcher;
030 import javax.servlet.ServletContext;
031 import javax.servlet.ServletException;
032 import javax.servlet.http.HttpServletRequest;
033 import javax.servlet.http.HttpServletResponse;
034 import javax.servlet.jsp.PageContext;
035
036 import org.apache.struts.Globals;
037
038
041 public class StrutsUtil {
042
043 public static final String STRUTS_PACKAGE = "org.apache.struts.";
044
045 public static final String TEXT_HTML_DIR = "/html";
046
047 public static void forward(
048 String uri, ServletContext servletContext,
049 HttpServletRequest request, HttpServletResponse response)
050 throws ServletException {
051
052 if (_log.isDebugEnabled()) {
053 _log.debug("Forward URI " + uri);
054 }
055
056 if (uri.equals(ActionConstants.COMMON_NULL)) {
057 return;
058 }
059
060 if (!response.isCommitted()) {
061 String path = TEXT_HTML_DIR + uri;
062
063 if (_log.isDebugEnabled()) {
064 _log.debug("Forward path " + path);
065 }
066
067 RequestDispatcher requestDispatcher =
068 servletContext.getRequestDispatcher(path);
069
070 try {
071 requestDispatcher.forward(request, response);
072 }
073 catch (IOException ioe) {
074 if (_log.isWarnEnabled()) {
075 _log.warn(ioe, ioe);
076 }
077 }
078 catch (ServletException se1) {
079 request.setAttribute(PageContext.EXCEPTION, se1.getRootCause());
080
081 String errorPath = TEXT_HTML_DIR + "/common/error.jsp";
082
083 requestDispatcher = servletContext.getRequestDispatcher(
084 errorPath);
085
086 try {
087 requestDispatcher.forward(request, response);
088 }
089 catch (IOException ioe2) {
090 if (_log.isWarnEnabled()) {
091 _log.warn(ioe2, ioe2);
092 }
093 }
094 catch (ServletException se2) {
095 throw se2;
096 }
097 }
098 }
099 else if (_log.isWarnEnabled()) {
100 _log.warn(uri + " is already committed");
101 }
102 }
103
104 public static void include(
105 String uri, ServletContext servletContext,
106 HttpServletRequest request, HttpServletResponse response)
107 throws ServletException {
108
109 if (_log.isDebugEnabled()) {
110 _log.debug("Include URI " + uri);
111 }
112
113 String path = TEXT_HTML_DIR + uri;
114
115 if (_log.isDebugEnabled()) {
116 _log.debug("Include path " + path);
117 }
118
119 RequestDispatcher requestDispatcher =
120 servletContext.getRequestDispatcher(path);
121
122 try {
123 requestDispatcher.include(request, response);
124 }
125 catch (IOException ioe) {
126 if (_log.isWarnEnabled()) {
127 _log.warn(ioe, ioe);
128 }
129 }
130 }
131
132 public static Map<String, Object> removeStrutsAttributes(
133 PortletContext portletContext, PortletRequest portletRequest) {
134
135 Map<String, Object> strutsAttributes = new HashMap<>();
136
137 Enumeration<String> enu = portletRequest.getAttributeNames();
138
139 while (enu.hasMoreElements()) {
140 String attributeName = enu.nextElement();
141
142 if (attributeName.startsWith(STRUTS_PACKAGE)) {
143 strutsAttributes.put(
144 attributeName, portletRequest.getAttribute(attributeName));
145 }
146 }
147
148 for (String attributeName : strutsAttributes.keySet()) {
149 portletRequest.setAttribute(attributeName, null);
150 }
151
152 Object moduleConfig = portletContext.getAttribute(Globals.MODULE_KEY);
153
154 portletRequest.setAttribute(Globals.MODULE_KEY, moduleConfig);
155
156 return strutsAttributes;
157 }
158
159 public static void setStrutsAttributes(
160 PortletRequest portletRequest, Map<String, Object> strutsAttributes) {
161
162 for (Map.Entry<String, Object> entry : strutsAttributes.entrySet()) {
163 String key = entry.getKey();
164 Object value = entry.getValue();
165
166 portletRequest.setAttribute(key, value);
167 }
168 }
169
170 private static final Log _log = LogFactoryUtil.getLog(StrutsUtil.class);
171
172 }