1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
102 if (path.startsWith(contextPath+"/"))
103 {
104 request.setAttribute(StrutsPortlet.REDIRECT_PAGE_URL, path
105 .substring(contextPath.length()));
106 }
107
108 else if ( path.startsWith("/"))
109 {
110 request.setAttribute(StrutsPortlet.REDIRECT_URL, path);
111 }
112
113 else
114 {
115
116 request.setAttribute(StrutsPortlet.REDIRECT_PAGE_URL, path);
117 }
118 }
119 }
120 }