View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.portals.bridges.struts;
17  
18  import java.io.IOException;
19  
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  import javax.servlet.http.HttpServletResponseWrapper;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  /***
28   * PortletServletResponseWrapper
29   * 
30   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
31   * @version $Id: PortletServletResponseWrapper.java 188322 2005-02-04 18:34:26 +0100 (Fri, 04 Feb 2005) ate $
32   */
33  public class PortletServletResponseWrapper extends HttpServletResponseWrapper
34  {
35      private static final Log log = LogFactory
36              .getLog(PortletServletResponseWrapper.class);
37      private HttpServletRequest request;
38      private boolean actionResponse;
39      public PortletServletResponseWrapper(HttpServletRequest request,
40              HttpServletResponse response)
41      {
42          super(response);
43          this.request = request;
44          this.actionResponse = request.getAttribute(StrutsPortlet.REQUEST_TYPE)
45                  .equals(StrutsPortlet.ACTION_REQUEST);
46      }
47      public String encodeURL(String path)
48      {
49          if (actionResponse)
50              return path;
51          else
52              return super.encodeURL(path);
53      }
54      public String encodeRedirectURL(String path)
55      {
56          return path;
57      }
58      public String encodeUrl(String path)
59      {
60          if (actionResponse)
61              return path;
62          else
63              return super.encodeUrl(path);
64      }
65      public String encodeRedirectUrl(String path)
66      {
67          return path;
68      }
69      public void sendError(int errorCode, String errorMessage)
70              throws IOException
71      {
72          StrutsPortletErrorContext errorContext = (StrutsPortletErrorContext) request
73                  .getAttribute(StrutsPortlet.ERROR_CONTEXT);
74          if (errorContext == null)
75          {
76              errorContext = new StrutsPortletErrorContext();
77              request.setAttribute(StrutsPortlet.ERROR_CONTEXT, errorContext);
78          }
79          errorContext.setErrorCode(errorCode);
80          errorContext.setErrorMessage(errorMessage);
81          errorContext.setError(null);
82      }
83      public void sendError(int errorCode) throws IOException
84      {
85          sendError(errorCode, null);
86      }
87      public void sendRedirect(String path) throws IOException
88      {
89          if (request.getAttribute(StrutsPortlet.REDIRECT_URL) != null)
90          {
91              return;
92          }
93          if (path.startsWith("http://") || path.startsWith("https://"))
94          {
95              request.setAttribute(StrutsPortlet.REDIRECT_URL, path);
96          }
97          else
98          {
99              String contextPath = request.getContextPath();
100 
101             // context targeted url captured as Struts Page URL
102             if (path.startsWith(contextPath+"/"))
103             {
104                 request.setAttribute(StrutsPortlet.REDIRECT_PAGE_URL, path
105                         .substring(contextPath.length()));
106             }
107             // servlet container root url relative url NOT targetted at the Struts App
108             else if ( path.startsWith("/"))
109             {
110                 request.setAttribute(StrutsPortlet.REDIRECT_URL, path);
111             }
112             // context relative url captured as Struts Page URL
113             else
114             {
115                 // TODO: I think this should be translated as relative to current Struts PAGE_URL
116                 request.setAttribute(StrutsPortlet.REDIRECT_PAGE_URL, path);
117             }
118         }
119     }
120 }