1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.jetspeed.container.url.impl; |
18 |
|
|
19 |
|
import java.io.UnsupportedEncodingException; |
20 |
|
import java.util.Map; |
21 |
|
|
22 |
|
import javax.portlet.PortletMode; |
23 |
|
import javax.portlet.WindowState; |
24 |
|
import javax.servlet.http.HttpServletRequest; |
25 |
|
|
26 |
|
import org.apache.jetspeed.PortalContext; |
27 |
|
import org.apache.jetspeed.container.ContainerConstants; |
28 |
|
import org.apache.jetspeed.container.state.NavigationalState; |
29 |
|
import org.apache.jetspeed.container.url.BasePortalURL; |
30 |
|
import org.apache.jetspeed.container.url.PortalURL; |
31 |
|
import org.apache.jetspeed.util.ArgUtil; |
32 |
|
import org.apache.pluto.om.window.PortletWindow; |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
public abstract class AbstractPortalURL implements PortalURL |
43 |
|
{ |
44 |
|
public static final String DEFAULT_NAV_STATE_PARAMETER = "_ns"; |
45 |
|
|
46 |
|
protected static String navStateParameter; |
47 |
|
|
48 |
|
protected NavigationalState navState; |
49 |
0 |
protected BasePortalURL base = null; |
50 |
|
|
51 |
|
protected static Boolean relativeOnly; |
52 |
|
protected String contextPath; |
53 |
|
protected String basePath; |
54 |
|
protected String path; |
55 |
|
protected String encodedNavState; |
56 |
|
protected String secureBaseURL; |
57 |
|
protected String nonSecureBaseURL; |
58 |
0 |
protected String characterEncoding = "UTF-8"; |
59 |
|
|
60 |
|
|
61 |
|
public AbstractPortalURL(NavigationalState navState, PortalContext portalContext, BasePortalURL base) |
62 |
|
{ |
63 |
0 |
this(navState, portalContext); |
64 |
0 |
this.base = base; |
65 |
0 |
} |
66 |
|
|
67 |
|
public AbstractPortalURL(NavigationalState navState, PortalContext portalContext) |
68 |
0 |
{ |
69 |
0 |
if ( navStateParameter == null ) |
70 |
|
{ |
71 |
0 |
navStateParameter = |
72 |
|
portalContext.getConfigurationProperty("portalurl.navigationalstate.parameter.name", |
73 |
|
DEFAULT_NAV_STATE_PARAMETER); |
74 |
|
} |
75 |
0 |
this.navState = navState; |
76 |
0 |
if ( relativeOnly == null ) |
77 |
|
{ |
78 |
0 |
relativeOnly = new Boolean(portalContext.getConfiguration().getBoolean("portalurl.relative.only", false)); |
79 |
|
} |
80 |
0 |
} |
81 |
|
|
82 |
|
|
83 |
|
public AbstractPortalURL(String characterEncoding, NavigationalState navState, PortalContext portalContext) |
84 |
|
{ |
85 |
0 |
this(navState, portalContext); |
86 |
0 |
this.characterEncoding = characterEncoding; |
87 |
0 |
} |
88 |
|
|
89 |
|
public AbstractPortalURL(HttpServletRequest request, String characterEncoding, NavigationalState navState, PortalContext portalContext) |
90 |
|
{ |
91 |
0 |
this(characterEncoding, navState, portalContext); |
92 |
0 |
setRequest(request); |
93 |
0 |
} |
94 |
|
|
95 |
|
public boolean isRelativeOnly() |
96 |
|
{ |
97 |
0 |
return relativeOnly.booleanValue(); |
98 |
|
} |
99 |
|
|
100 |
|
public static String getNavigationalStateParameterName() |
101 |
|
{ |
102 |
0 |
return navStateParameter; |
103 |
|
} |
104 |
|
|
105 |
|
public String createNavigationalEncoding(PortletWindow window, Map parameters, PortletMode mode, WindowState state, boolean action) |
106 |
|
{ |
107 |
|
try |
108 |
|
{ |
109 |
0 |
return getNavigationalStateParameterName() + ":" + getNavigationalState().encode(window, parameters, mode, state, action); |
110 |
|
} |
111 |
0 |
catch (UnsupportedEncodingException e) |
112 |
|
{ |
113 |
0 |
e.printStackTrace(); |
114 |
0 |
return ""; |
115 |
|
} |
116 |
|
} |
117 |
|
|
118 |
|
public String createNavigationalEncoding(PortletWindow window, PortletMode mode, WindowState state) |
119 |
|
{ |
120 |
|
try |
121 |
|
{ |
122 |
0 |
return getNavigationalStateParameterName() + ":" + getNavigationalState().encode(window, mode, state); |
123 |
|
} |
124 |
0 |
catch (UnsupportedEncodingException e) |
125 |
|
{ |
126 |
0 |
e.printStackTrace(); |
127 |
0 |
return ""; |
128 |
|
} |
129 |
|
} |
130 |
|
|
131 |
|
protected void decodeBaseURL(HttpServletRequest request) |
132 |
|
{ |
133 |
0 |
if (base == null) |
134 |
|
{ |
135 |
0 |
base = new BasePortalURLImpl(); |
136 |
0 |
base.setServerScheme(request.getScheme()); |
137 |
0 |
base.setServerName(request.getServerName()); |
138 |
0 |
base.setServerPort(request.getServerPort()); |
139 |
0 |
base.setSecure(request.isSecure()); |
140 |
|
} |
141 |
0 |
if ( relativeOnly.booleanValue() ) |
142 |
|
{ |
143 |
0 |
this.secureBaseURL = class="keyword">this.nonSecureBaseURL = ""; |
144 |
|
} |
145 |
|
else |
146 |
|
{ |
147 |
|
StringBuffer buffer; |
148 |
|
|
149 |
0 |
buffer = new StringBuffer(HTTPS); |
150 |
0 |
buffer.append("://").append(base.getServerName()); |
151 |
0 |
if (base.getServerPort() != 443 && base.getServerPort() != 80) |
152 |
|
{ |
153 |
0 |
buffer.append(":").append(base.getServerPort()); |
154 |
|
} |
155 |
0 |
this.secureBaseURL = buffer.toString(); |
156 |
|
|
157 |
0 |
buffer = new StringBuffer(HTTP); |
158 |
0 |
buffer.append("://").append(base.getServerName()); |
159 |
0 |
if (base.getServerPort() != 443 && base.getServerPort() != 80) |
160 |
|
{ |
161 |
0 |
buffer.append(":").append(base.getServerPort()); |
162 |
|
} |
163 |
0 |
this.nonSecureBaseURL = buffer.toString(); |
164 |
|
} |
165 |
0 |
} |
166 |
|
|
167 |
|
protected void decodeBasePath(HttpServletRequest request) |
168 |
|
{ |
169 |
0 |
this.contextPath = (String) request.getAttribute(ContainerConstants.PORTAL_CONTEXT); |
170 |
0 |
if (contextPath == null) |
171 |
|
{ |
172 |
0 |
contextPath = request.getContextPath(); |
173 |
|
} |
174 |
0 |
if (contextPath == null) |
175 |
|
{ |
176 |
0 |
contextPath = ""; |
177 |
|
} |
178 |
0 |
String servletPath = request.getServletPath(); |
179 |
0 |
if (servletPath == null) |
180 |
|
{ |
181 |
0 |
servletPath = ""; |
182 |
|
} |
183 |
0 |
this.basePath = contextPath + servletPath; |
184 |
0 |
} |
185 |
|
|
186 |
|
protected void setEncodedNavigationalState(String encodedNavigationalState) |
187 |
|
{ |
188 |
0 |
this.encodedNavState = encodedNavigationalState; |
189 |
|
try |
190 |
|
{ |
191 |
0 |
navState.init(encodedNavState, characterEncoding); |
192 |
|
} |
193 |
0 |
catch (UnsupportedEncodingException e) |
194 |
|
{ |
195 |
0 |
IllegalStateException ise = new IllegalStateException("An unsupported encoding was defined for this NavigationalState."); |
196 |
0 |
ise.initCause(e); |
197 |
0 |
throw ise; |
198 |
0 |
} |
199 |
0 |
} |
200 |
|
|
201 |
|
protected void setPath(String path) |
202 |
|
{ |
203 |
0 |
this.path = path; |
204 |
0 |
} |
205 |
|
|
206 |
|
public String getBaseURL() |
207 |
|
{ |
208 |
0 |
return getBaseURL(base.isSecure()); |
209 |
|
} |
210 |
|
|
211 |
|
public String getBaseURL(boolean secure) |
212 |
|
{ |
213 |
|
|
214 |
|
|
215 |
|
|
216 |
0 |
return secure ? secureBaseURL : nonSecureBaseURL; |
217 |
|
} |
218 |
|
|
219 |
|
public String getBasePath() |
220 |
|
{ |
221 |
0 |
return basePath; |
222 |
|
} |
223 |
|
|
224 |
|
public String getPath() |
225 |
|
{ |
226 |
0 |
return path; |
227 |
|
} |
228 |
|
|
229 |
|
public String getPageBasePath() |
230 |
|
{ |
231 |
0 |
if ( null == path || (1 == path.length() && '/' == path.charAt(0)) ) |
232 |
|
{ |
233 |
0 |
return basePath; |
234 |
|
} |
235 |
0 |
else if ( -1 != path.indexOf('/') && !path.endsWith("/") ) |
236 |
|
{ |
237 |
0 |
return basePath + path.substring(0, path.lastIndexOf('/') ); |
238 |
|
} |
239 |
|
else |
240 |
|
{ |
241 |
0 |
return basePath + path; |
242 |
|
} |
243 |
|
} |
244 |
|
|
245 |
|
public boolean isSecure() |
246 |
|
{ |
247 |
0 |
return base.isSecure(); |
248 |
|
} |
249 |
|
|
250 |
|
public NavigationalState getNavigationalState() |
251 |
|
{ |
252 |
0 |
return navState; |
253 |
|
} |
254 |
|
|
255 |
|
public String createPortletURL(PortletWindow window, Map parameters, PortletMode mode, WindowState state, boolean action, class="keyword">boolean secure) |
256 |
|
{ |
257 |
|
try |
258 |
|
{ |
259 |
0 |
return createPortletURL(navState.encode(window,parameters,mode,state,action), secure); |
260 |
|
} |
261 |
0 |
catch (UnsupportedEncodingException e) |
262 |
|
{ |
263 |
|
|
264 |
0 |
e.printStackTrace(); |
265 |
|
|
266 |
0 |
return null; |
267 |
|
} |
268 |
|
} |
269 |
|
|
270 |
|
public String createPortletURL(PortletWindow window, PortletMode mode, WindowState state, boolean secure) |
271 |
|
{ |
272 |
|
try |
273 |
|
{ |
274 |
0 |
return createPortletURL(navState.encode(window,mode,state), secure); |
275 |
|
} |
276 |
0 |
catch (UnsupportedEncodingException e) |
277 |
|
{ |
278 |
|
|
279 |
0 |
e.printStackTrace(); |
280 |
|
|
281 |
0 |
return null; |
282 |
|
} |
283 |
|
} |
284 |
|
|
285 |
|
protected abstract void decodePathAndNavigationalState(HttpServletRequest request); |
286 |
|
|
287 |
|
protected abstract String createPortletURL(String encodedNavState, boolean secure); |
288 |
|
|
289 |
|
public void setRequest(HttpServletRequest request) |
290 |
|
{ |
291 |
0 |
ArgUtil.assertNotNull(HttpServletRequest.class, request, this, "setRequest"); |
292 |
0 |
decodeBaseURL(request); |
293 |
0 |
decodeBasePath(request); |
294 |
0 |
decodePathAndNavigationalState(request); |
295 |
0 |
} |
296 |
|
|
297 |
|
public void setCharacterEncoding(String characterEncoding) |
298 |
|
{ |
299 |
0 |
this.characterEncoding = characterEncoding; |
300 |
0 |
} |
301 |
|
|
302 |
|
public String getPortalURL() |
303 |
|
{ |
304 |
|
try |
305 |
|
{ |
306 |
0 |
return createPortletURL(navState.encode(), isSecure()); |
307 |
|
} |
308 |
0 |
catch (UnsupportedEncodingException e) |
309 |
|
{ |
310 |
|
|
311 |
0 |
e.printStackTrace(); |
312 |
|
|
313 |
0 |
return null; |
314 |
|
} |
315 |
|
} |
316 |
|
|
317 |
|
public boolean hasEncodedNavState() |
318 |
|
{ |
319 |
0 |
return encodedNavState != null; |
320 |
|
} |
321 |
|
|
322 |
|
public boolean isPathInfoEncodingNavState() |
323 |
|
{ |
324 |
0 |
return false; |
325 |
|
} |
326 |
|
} |