Author: mgrigorov
Date: Mon Oct 31 15:16:52 2011
New Revision: 1195512
URL: http://svn.apache.org/viewvc?rev=1195512&view=rev
Log:
WICKET-4138 BookmarkablePageLinks not working on a forwarded page
Added:
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/ForwardAttributes.java
Modified:
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/ServletWebRequest.java
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/protocol/http/servlet/ServletWebRequestTest.java
Added:
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/ForwardAttributes.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/ForwardAttributes.java?rev=1195512&view=auto
==============================================================================
---
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/ForwardAttributes.java
(added)
+++
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/ForwardAttributes.java
Mon Oct 31 15:16:52 2011
@@ -0,0 +1,141 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.protocol.http.servlet;
+
+import javax.servlet.ServletRequest;
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.wicket.util.lang.Args;
+import org.apache.wicket.util.string.Strings;
+
+/**
+ * Represents additional error parameters present in a {@link ServletRequest}
when the servlet
+ * container is handling an error or a forward to an error page mapped by
{@code error-page} element
+ * in {@code web.xml}.
+ *
+ * See documentation for the following request attributes for the values
stored in this object:
+ * <ul>
+ * <li>javax.servlet.error.status_code</li>
+ * <li>javax.servlet.error.message</li>
+ * <li>javax.servlet.error.request_uri</li>
+ * <li>javax.servlet.error.servlet_name</li>
+ * <li>javax.servlet.error.exception_type</li>
+ * <li>javax.servlet.error.exception</li>
+ * </ul>
+ *
+ */
+public class ForwardAttributes
+{
+ // javax.servlet.forward.request_uri
+ private final String requestUri;
+
+ // javax.servlet.forward.servlet_path
+ private final String servletPath;
+
+ // javax.servlet.forward.context_path
+ private final String contextPath;
+
+ // javax.servlet.forward.query_string
+ private final String queryString;
+
+ /**
+ * Constructor.
+ *
+ * @param requestUri
+ * @param servletPath
+ * @param contextPath
+ * @param queryString
+ */
+ private ForwardAttributes(String requestUri, String servletPath, String
contextPath,
+ String queryString)
+ {
+ this.requestUri = requestUri;
+ this.servletPath = servletPath;
+ this.contextPath = contextPath;
+ this.queryString = queryString;
+ }
+
+ /**
+ * Gets requestUri.
+ *
+ * @return requestUri
+ */
+ public String getRequestUri()
+ {
+ return requestUri;
+ }
+
+ /**
+ * Gets servletPath.
+ *
+ * @return servletPath
+ */
+ public String getServletPath()
+ {
+ return servletPath;
+ }
+
+ /**
+ * Gets contextPath.
+ *
+ * @return contextPath
+ */
+ public String getContextPath()
+ {
+ return contextPath;
+ }
+
+ /**
+ * Gets the query string.
+ *
+ * @return the query string
+ */
+ public String getQueryString()
+ {
+ return queryString;
+ }
+
+ /**
+ * Factory for creating instances of this class.
+ *
+ * @param request
+ * @return instance of request contains forward attributes or {@code
null} if it does not.
+ */
+ public static ForwardAttributes of(HttpServletRequest request)
+ {
+ Args.notNull(request, "request");
+
+ final String requestUri =
(String)request.getAttribute("javax.servlet.forward.request_uri");
+ final String servletPath =
(String)request.getAttribute("javax.servlet.forward.servlet_path");
+ final String contextPath =
(String)request.getAttribute("javax.servlet.forward.context_path");
+ final String queryString =
(String)request.getAttribute("javax.servlet.forward.query_string");
+
+ if (!Strings.isEmpty(requestUri) ||
!Strings.isEmpty(servletPath) ||
+ !Strings.isEmpty(contextPath) ||
!Strings.isEmpty(queryString))
+ {
+ return new ForwardAttributes(requestUri, servletPath,
contextPath, queryString);
+ }
+ return null;
+ }
+
+ @Override
+ public String toString()
+ {
+ return "ForwardAttributes [requestUri=" + requestUri + ",
servletPath=" + servletPath +
+ ", contextPath=" + contextPath + ", queryString=" +
queryString + "]";
+ }
+}
Modified:
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/ServletWebRequest.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/ServletWebRequest.java?rev=1195512&r1=1195511&r2=1195512&view=diff
==============================================================================
---
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/ServletWebRequest.java
(original)
+++
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/ServletWebRequest.java
Mon Oct 31 15:16:52 2011
@@ -66,6 +66,8 @@ public class ServletWebRequest extends W
private final ErrorAttributes errorAttributes;
+ private final ForwardAttributes forwardAttributes;
+
/**
* Construct.
*
@@ -96,6 +98,8 @@ public class ServletWebRequest extends W
errorAttributes = ErrorAttributes.of(httpServletRequest);
+ forwardAttributes = ForwardAttributes.of(httpServletRequest);
+
if (url != null)
{
this.url = url;
@@ -129,6 +133,12 @@ public class ServletWebRequest extends W
.toString();
return getContextRelativeUrl(problematicURI,
filterPrefix);
}
+ else if (forwardAttributes != null &&
!Strings.isEmpty(forwardAttributes.getRequestUri()))
+ {
+ String forwardURI =
Url.parse(forwardAttributes.getRequestUri(), getCharset())
+ .toString();
+ return getContextRelativeUrl(forwardURI, filterPrefix);
+ }
else if (!isAjax())
{
return
getContextRelativeUrl(httpServletRequest.getRequestURI(), filterPrefix);
@@ -456,6 +466,7 @@ public class ServletWebRequest extends W
@Override
public boolean shouldPreserveClientUrl()
{
- return errorAttributes != null &&
!Strings.isEmpty(errorAttributes.getRequestUri());
+ return (errorAttributes != null &&
!Strings.isEmpty(errorAttributes.getRequestUri()) || forwardAttributes != null
&&
+ !Strings.isEmpty(forwardAttributes.getRequestUri()));
}
}
Modified:
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/protocol/http/servlet/ServletWebRequestTest.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/org/apache/wicket/protocol/http/servlet/ServletWebRequestTest.java?rev=1195512&r1=1195511&r2=1195512&view=diff
==============================================================================
---
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/protocol/http/servlet/ServletWebRequestTest.java
(original)
+++
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/protocol/http/servlet/ServletWebRequestTest.java
Mon Oct 31 15:16:52 2011
@@ -85,6 +85,30 @@ public class ServletWebRequestTest exten
}
/**
+ * https://issues.apache.org/jira/browse/WICKET-4138
+ *
+ * Relative Urls should be calculated against
'javax.servlet.forward.request_uri'
+ */
+ @Test
+ public void parseForwardAttributes()
+ {
+ MockHttpServletRequest httpRequest = new
MockHttpServletRequest(null, null, null);
+ httpRequest.setURL(httpRequest.getContextPath() +
"/request/Uri");
+
+ String forwardedURI = httpRequest.getContextPath() +
"/some/forwarded/url";
+
+ httpRequest.setAttribute("javax.servlet.forward.request_uri",
forwardedURI);
+
+ ServletWebRequest forwardWebRequest = new
ServletWebRequest(httpRequest, "");
+
+ Url forwardClientUrl = forwardWebRequest.getClientUrl();
+
+ assertEquals("some/forwarded/url", forwardClientUrl.toString());
+
+
+ }
+
+ /**
* https://issues.apache.org/jira/browse/WICKET-4123
*/
@Test