1
22
23 package com.liferay.portal.struts;
24
25 import com.liferay.portal.util.CompanyPropsUtil;
26 import com.liferay.portal.util.Constants;
27 import com.liferay.portal.util.PortalUtil;
28 import com.liferay.portal.util.PropsUtil;
29 import com.liferay.util.BrowserSniffer;
30 import com.liferay.util.StringUtil;
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.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
39 import javax.servlet.jsp.PageContext;
40
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43
44
51 public class StrutsUtil {
52
53 public static void forward(
54 String uri, ServletContext ctx, HttpServletRequest req,
55 HttpServletResponse res)
56 throws ServletException {
57
58 if (uri.equals(Constants.COMMON_NULL)) {
59 return;
60 }
61
62 if (!res.isCommitted()) {
63 String path = Constants.TEXT_HTML_DIR + uri;
64 if (BrowserSniffer.is_wml(req)) {
65 path = Constants.TEXT_WML_DIR + uri;
66 }
67
68 ServletContext portalCtx = _getPortalCtx(ctx, req);
69
70 RequestDispatcher rd = portalCtx.getRequestDispatcher(path);
71
72 try {
73 rd.forward(req, res);
74 }
75 catch (IOException ioe1) {
76 _log.warn(StringUtil.stackTrace(ioe1));
77 }
78 catch (ServletException se1) {
79 req.setAttribute(PageContext.EXCEPTION, se1.getRootCause());
80
81 String errorPath =
82 Constants.TEXT_HTML_DIR + Constants.COMMON_ERROR;
83 if (BrowserSniffer.is_wml(req)) {
84 path = Constants.TEXT_WML_DIR + Constants.COMMON_ERROR;
85 }
86
87 rd = portalCtx.getRequestDispatcher(errorPath);
88
89 try {
90 rd.forward(req, res);
91 }
92 catch (IOException ioe2) {
93 _log.warn(StringUtil.stackTrace(ioe2));
94 }
95 catch (ServletException se2) {
96 throw se2;
97 }
98 }
99 }
100 else {
101 _log.warn(uri + " is already committed");
102 }
103 }
104
105 public static void include(
106 String uri, ServletContext ctx, HttpServletRequest req,
107 HttpServletResponse res)
108 throws ServletException {
109
110 String path = Constants.TEXT_HTML_DIR + uri;
111 if (BrowserSniffer.is_wml(req)) {
112 path = Constants.TEXT_WML_DIR + uri;
113 }
114
115 ServletContext portalCtx = _getPortalCtx(ctx, req);
116
117 RequestDispatcher rd = portalCtx.getRequestDispatcher(path);
118
119 try {
120 rd.include(req, res);
121 }
122 catch (IOException ioe) {
123 _log.warn(StringUtil.stackTrace(ioe));
124 }
125 }
126
127 private static ServletContext _getPortalCtx(
128 ServletContext ctx, HttpServletRequest req) {
129 String companyId = PortalUtil.getCompanyId(req);
130
131 ServletContext portalCtx = ctx.getContext(
132 CompanyPropsUtil.get(companyId, PropsUtil.PORTAL_CTX));
133 if (portalCtx == null) {
134 portalCtx = ctx;
135 }
136
137 return portalCtx;
138 }
139
140 private static final Log _log = LogFactory.getLog(StrutsUtil.class);
141
142 }