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