View Javadoc

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