1   /**
2    * Copyright (c) 2000-2005 Liferay, LLC. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
45   * <a href="StrutsUtil.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author  Brian Wing Shun Chan
48   * @version $Revision: 1.15 $
49   *
50   */
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 }