1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.blossom.support;
35
36 import javax.servlet.http.HttpServletRequest;
37
38 import org.apache.commons.lang.StringUtils;
39 import org.springframework.web.util.WebUtils;
40
41
42
43
44
45
46
47 public class ForwardRequestWrapper extends SpecialAttributeRequestWrapper {
48
49 private final String requestUri;
50 private final String originalRequestUri;
51 private final String contextPath;
52 private final String originalContextPath;
53 private final String servletPath;
54 private final String originalServletPath;
55 private final String pathInfo;
56 private final String originalPathInfo;
57
58 public ForwardRequestWrapper(HttpServletRequest request, String requestUri, String contextPath, String servletPath, String pathInfo) {
59 super(request);
60 this.requestUri = requestUri;
61 this.originalRequestUri = request.getRequestURI();
62 this.servletPath = servletPath;
63 this.originalServletPath = request.getServletPath();
64 this.contextPath = contextPath;
65 this.originalContextPath = request.getContextPath();
66 this.pathInfo = pathInfo;
67 this.originalPathInfo = request.getPathInfo();
68 if (request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE) == null) {
69 setSpecialAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, request.getRequestURI());
70 setSpecialAttribute(WebUtils.FORWARD_CONTEXT_PATH_ATTRIBUTE, request.getContextPath());
71 setSpecialAttribute(WebUtils.FORWARD_SERVLET_PATH_ATTRIBUTE, request.getServletPath());
72 setSpecialAttribute(WebUtils.FORWARD_PATH_INFO_ATTRIBUTE, request.getPathInfo());
73 setSpecialAttribute(WebUtils.FORWARD_QUERY_STRING_ATTRIBUTE, request.getQueryString());
74 }
75 }
76
77 @Override
78 public String getRequestURI() {
79 String currentRequestUri = super.getRequestURI();
80 if (!StringUtils.equals(currentRequestUri, originalRequestUri)) {
81 return currentRequestUri;
82 }
83 if (!StringUtils.equals(super.getServletPath(), originalServletPath)) {
84 return currentRequestUri;
85 }
86 if (!StringUtils.equals(super.getPathInfo(), originalPathInfo)) {
87 return currentRequestUri;
88 }
89 if (!StringUtils.equals(super.getContextPath(), originalContextPath)) {
90 return currentRequestUri;
91 }
92 return requestUri;
93 }
94
95 @Override
96 public String getContextPath() {
97 String currentContextPath = super.getContextPath();
98 if (!StringUtils.equals(super.getRequestURI(), originalRequestUri)) {
99 return currentContextPath;
100 }
101 if (!StringUtils.equals(super.getServletPath(), originalServletPath)) {
102 return currentContextPath;
103 }
104 if (!StringUtils.equals(super.getPathInfo(), originalPathInfo)) {
105 return currentContextPath;
106 }
107 if (!StringUtils.equals(currentContextPath, originalContextPath)) {
108 return currentContextPath;
109 }
110 return contextPath;
111 }
112
113 @Override
114 public String getServletPath() {
115 String currentServletPath = super.getServletPath();
116 if (!StringUtils.equals(super.getRequestURI(), originalRequestUri)) {
117 return currentServletPath;
118 }
119 if (!StringUtils.equals(currentServletPath, originalServletPath)) {
120 return currentServletPath;
121 }
122 if (!StringUtils.equals(super.getPathInfo(), originalPathInfo)) {
123 return currentServletPath;
124 }
125 if (!StringUtils.equals(super.getContextPath(), originalContextPath)) {
126 return currentServletPath;
127 }
128 return servletPath;
129 }
130
131 @Override
132 public String getPathInfo() {
133 String superPathInfo = super.getPathInfo();
134 if (!StringUtils.equals(super.getRequestURI(), originalRequestUri)) {
135 return superPathInfo;
136 }
137 if (!StringUtils.equals(super.getServletPath(), originalServletPath)) {
138 return superPathInfo;
139 }
140 if (!StringUtils.equals(superPathInfo, originalPathInfo)) {
141 return superPathInfo;
142 }
143 if (!StringUtils.equals(super.getContextPath(), originalContextPath)) {
144 return superPathInfo;
145 }
146 return pathInfo;
147 }
148 }