Permalink
Browse files

Added Servlets#getRequestURI()/getRequestQueryString() which

transparently takes into account forward URI and query string. Rework
Faces/FacesLocal to make use of it. Deprecate getForwardXxx() methods as
JSF code has no business of directly knowing them and it is generally
confusing to endusers. When getting a request URI or query string, one
would expect those to exactly match the one in browser's address bar.
  • Loading branch information...
1 parent d43dac0 commit 56b03cd1fa7f9d19831351f414fafd2159e5621e @BalusC BalusC committed Jun 7, 2016
@@ -1080,7 +1080,10 @@ public static String getRequestURIWithQueryString() {
* This does not include the request query string.
* @return The original HTTP request URI behind this forwarded request, if any.
* @since 1.8
+ * @deprecated Since 2.4. This is abstracted away by {@link #getRequestURI()}. Use it instead.
+ * JSF has as to retrieving request URI no business of knowing if the request is forwarded/rewritten or not.
*/
+ @Deprecated // TODO: Remove in OmniFaces 3.0.
public static String getForwardRequestURI() {
return FacesLocal.getForwardRequestURI(getContext());
}
@@ -1089,7 +1092,10 @@ public static String getForwardRequestURI() {
* Returns the original HTTP request query string behind this forwarded request, if any.
* @return The original HTTP request query string behind this forwarded request, if any.
* @since 1.8
+ * @deprecated Since 2.4. This is abstracted away by {@link #getRequestQueryString()}. Use it instead.
+ * JSF has as to retrieving request URI no business of knowing if the request is forwarded/rewritten or not.
*/
+ @Deprecated // TODO: Remove in OmniFaces 3.0.
public static String getForwardRequestQueryString() {
return FacesLocal.getForwardRequestQueryString(getContext());
}
@@ -1098,7 +1104,10 @@ public static String getForwardRequestQueryString() {
* Returns the original HTTP request URI with query string behind this forwarded request, if any.
* @return The original HTTP request URI with query string behind this forwarded request, if any.
* @since 1.8
+ * @deprecated Since 2.4. This is abstracted away by {@link #getRequestURIWithQueryString()}. Use it instead.
+ * JSF has as to retrieving request URI no business of knowing if the request is forwarded/rewritten or not.
*/
+ @Deprecated // TODO: Remove in OmniFaces 3.0.
public static String getForwardRequestURIWithQueryString() {
return FacesLocal.getForwardRequestURIWithQueryString(getContext());
}
@@ -785,23 +785,23 @@ public static String getRequestDomainURL(FacesContext context) {
* @see Faces#getRequestURL()
*/
public static String getRequestURL(FacesContext context) {
- return getRequest(context).getRequestURL().toString();
+ return Servlets.getRequestURL(getRequest(context));
}
/**
* {@inheritDoc}
* @see Faces#getRequestURI()
*/
public static String getRequestURI(FacesContext context) {
- return getRequest(context).getRequestURI();
+ return Servlets.getRequestURI(getRequest(context));
}
/**
* {@inheritDoc}
* @see Faces#getRequestQueryString()
*/
public static String getRequestQueryString(FacesContext context) {
- return getRequest(context).getQueryString();
+ return Servlets.getRequestQueryString(getRequest(context));
}
/**
@@ -831,23 +831,32 @@ public static String getRequestURIWithQueryString(FacesContext context) {
/**
* {@inheritDoc}
* @see Faces#getForwardRequestURI()
+ * @deprecated Since 2.4. This is abstracted away by {@link #getRequestURI(FacesContext)}. Use it instead.
+ * JSF has as to retrieving request URI no business of knowing if the request is forwarded/rewritten or not.
*/
+ @Deprecated // TODO: Remove in OmniFaces 3.0.
public static String getForwardRequestURI(FacesContext context) {
return Servlets.getForwardRequestURI(getRequest(context));
}
/**
* {@inheritDoc}
* @see Faces#getForwardRequestQueryString()
+ * @deprecated Since 2.4. This is abstracted away by {@link #getRequestQueryString(FacesContext)}. Use it instead.
+ * JSF has as to retrieving request URI no business of knowing if the request is forwarded/rewritten or not.
*/
+ @Deprecated // TODO: Remove in OmniFaces 3.0.
public static String getForwardRequestQueryString(FacesContext context) {
return Servlets.getForwardRequestQueryString(getRequest(context));
}
/**
* {@inheritDoc}
* @see Faces#getForwardRequestURIWithQueryString()
+ * @deprecated Since 2.4. This is abstracted away by {@link #getRequestURIWithQueryString(FacesContext)}. Use it instead.
+ * JSF has as to retrieving request URI no business of knowing if the request is forwarded/rewritten or not.
*/
+ @Deprecated // TODO: Remove in OmniFaces 3.0.
public static String getForwardRequestURIWithQueryString(FacesContext context) {
return Servlets.getForwardRequestURIWithQueryString(getRequest(context));
}
@@ -21,6 +21,7 @@
import static javax.servlet.RequestDispatcher.FORWARD_QUERY_STRING;
import static javax.servlet.RequestDispatcher.FORWARD_REQUEST_URI;
import static org.omnifaces.util.JNDI.lookup;
+import static org.omnifaces.util.Utils.coalesce;
import static org.omnifaces.util.Utils.decodeURL;
import static org.omnifaces.util.Utils.encodeURL;
import static org.omnifaces.util.Utils.isEmpty;
@@ -41,6 +42,7 @@
import javax.faces.application.ResourceHandler;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
+import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
@@ -138,73 +140,106 @@ public static String getRequestBaseURL(HttpServletRequest request) {
}
/**
- * Returns the HTTP request URI relative to the context root of a web application. This is the request URI
- * minus the context path. Note that this includes path parameters.
- *
+ * Returns the HTTP request URI, regardless of any forward. This is the part after the domain in the request URL,
+ * including the leading slash.
* @param request The involved HTTP servlet request.
- * @return the request URI relative to the context root
- * @since 1.8
+ * @return The HTTP request URI, regardless of any forward.
+ * @since 2.4
+ * @see HttpServletRequest#getRequestURI()
+ * @see RequestDispatcher#FORWARD_REQUEST_URI
*/
- public static String getRequestRelativeURI(HttpServletRequest request) {
- return request.getRequestURI().substring(request.getContextPath().length());
+ public static String getRequestURI(HttpServletRequest request) {
+ return coalesce((String) request.getAttribute(FORWARD_REQUEST_URI), request.getRequestURI());
}
/**
- * Returns the HTTP request URI relative to the context root of a web application. This is the servlet path
- * plus the path info (if any).
- *
+ * Returns the HTTP request query string, regardless of any forward.
* @param request The involved HTTP servlet request.
- * @return the request URI relative to the context root
- * @since 1.8
+ * @return The HTTP request query string, regardless of any forward.
+ * @since 2.4
+ * @see HttpServletRequest#getRequestURI()
+ * @see RequestDispatcher#FORWARD_QUERY_STRING
*/
- public static String getRequestRelativeURIWithoutPathParameters(HttpServletRequest request) {
- return request.getPathInfo() == null? request.getServletPath() : request.getServletPath() + request.getPathInfo();
+ public static String getRequestQueryString(HttpServletRequest request) {
+ return coalesce((String) request.getAttribute(FORWARD_QUERY_STRING), request.getQueryString());
}
/**
- * Returns the HTTP request URL with query string. This is the full request URL with query string as the enduser
- * sees in browser address bar.
+ * Returns the HTTP request query string as parameter values map. Note this method returns <strong>only</strong>
+ * the request URL (GET) parameters, as opposed to {@link HttpServletRequest#getParameterMap()}, which contains both
+ * the request URL (GET) parameters and and the request body (POST) parameters.
+ * The map entries are in the same order as they appear in the query string.
* @param request The involved HTTP servlet request.
- * @return The HTTP request URL with query string.
- * @see HttpServletRequest#getRequestURL()
- * @see HttpServletRequest#getQueryString()
+ * @return The HTTP request query string as parameter values map.
*/
- public static String getRequestURLWithQueryString(HttpServletRequest request) {
- StringBuffer requestURL = request.getRequestURL();
- String queryString = request.getQueryString();
- return (queryString == null) ? requestURL.toString() : requestURL.append('?').append(queryString).toString();
+ public static Map<String, List<String>> getRequestQueryStringMap(HttpServletRequest request) {
+ String queryString = getRequestQueryString(request);
+
+ if (isEmpty(queryString)) {
+ return new LinkedHashMap<>(0);
+ }
+
+ return toParameterMap(queryString);
}
/**
- * Returns the HTTP request URI with query string. This is the part after the domain in the request URL, including
- * the leading slash and the request query string.
+ * Returns the HTTP request URI with query string, regardless of any forward. This is the part after the domain in
+ * the request URL, including the leading slash and the request query string.
* @param request The involved HTTP servlet request.
* @return The HTTP request URI with query string.
- * @see HttpServletRequest#getRequestURI()
- * @see HttpServletRequest#getQueryString()
+ * @see #getRequestURI(HttpServletRequest)
+ * @see #getRequestQueryString(HttpServletRequest)
*/
public static String getRequestURIWithQueryString(HttpServletRequest request) {
- String requestURI = request.getRequestURI();
- String queryString = request.getQueryString();
+ String requestURI = getRequestURI(request);
+ String queryString = getRequestQueryString(request);
return (queryString == null) ? requestURI : (requestURI + "?" + queryString);
}
/**
- * Returns the HTTP request query string as parameter values map. Note this method returns <strong>only</strong>
- * the request URL (GET) parameters, as opposed to {@link HttpServletRequest#getParameterMap()}, which contains both
- * the request URL (GET) parameters and and the request body (POST) parameters.
- * The map entries are in the same order as they appear in the query string.
- * @param request The request for which the base URL is computed.
- * @return The HTTP request query string as parameter values map.
+ * Returns the HTTP request URI relative to the context root, regardless of any forward. This is the request URI
+ * minus the context path. Note that this includes path parameters.
+ * @param request The involved HTTP servlet request.
+ * @return The HTTP request URI relative to the context root.
+ * @since 1.8
*/
- public static Map<String, List<String>> getRequestQueryStringMap(HttpServletRequest request) {
- String queryString = request.getQueryString();
+ public static String getRequestRelativeURI(HttpServletRequest request) {
+ return getRequestURI(request).substring(request.getContextPath().length());
+ }
- if (isEmpty(queryString)) {
- return new LinkedHashMap<>(0);
- }
+ /**
+ * Returns the HTTP request URI relative to the context root without path parameters, regardless of any forward.
+ * This is the request URI minus the context path and path parameters.
+ * @param request The involved HTTP servlet request.
+ * @return The HTTP request URI relative to the context root without path parameters.
+ * @since 1.8
+ */
+ public static String getRequestRelativeURIWithoutPathParameters(HttpServletRequest request) {
+ return getRequestRelativeURI(request).split(";", 2)[0];
+ }
- return toParameterMap(queryString);
+ /**
+ * Returns the HTTP request URL with query string, regardless of any forward. This is the full request URL without
+ * query string as the enduser sees in browser address bar.
+ * @param request The involved HTTP servlet request.
+ * @return The HTTP request URL without query string, regardless of any forward.
+ * @since 2.4
+ * @see HttpServletRequest#getRequestURL()
+ */
+ public static String getRequestURL(HttpServletRequest request) {
+ return getRequestDomainURL(request) + getRequestURI(request);
+ }
+
+ /**
+ * Returns the HTTP request URL with query string. This is the full request URL with query string as the enduser
+ * sees in browser address bar.
+ * @param request The involved HTTP servlet request.
+ * @return The HTTP request URL with query string, regardless of any forward.
+ * @see HttpServletRequest#getRequestURL()
+ * @see HttpServletRequest#getQueryString()
+ */
+ public static String getRequestURLWithQueryString(HttpServletRequest request) {
+ return getRequestDomainURL(request) + getRequestURIWithQueryString(request);
}
/**
@@ -213,7 +248,9 @@ public static String getRequestURIWithQueryString(HttpServletRequest request) {
* @param request The involved HTTP servlet request.
* @return The original HTTP request URI behind this forwarded request, if any.
* @since 1.8
+ * @deprecated Since 2.4. Use {@link #getRequestURI(HttpServletRequest)} instead.
*/
+ @Deprecated // TODO: Remove in OmniFaces 3.0.
public static String getForwardRequestURI(HttpServletRequest request) {
return (String) request.getAttribute(FORWARD_REQUEST_URI);
}
@@ -223,7 +260,9 @@ public static String getForwardRequestURI(HttpServletRequest request) {
* @param request The involved HTTP servlet request.
* @return The original HTTP request query string behind this forwarded request, if any.
* @since 1.8
+ * @deprecated Since 2.4. Use {@link #getRequestQueryString(HttpServletRequest)} instead.
*/
+ @Deprecated // TODO: Remove in OmniFaces 3.0.
public static String getForwardRequestQueryString(HttpServletRequest request) {
return (String) request.getAttribute(FORWARD_QUERY_STRING);
}
@@ -233,7 +272,9 @@ public static String getForwardRequestQueryString(HttpServletRequest request) {
* @param request The involved HTTP servlet request.
* @return The original HTTP request URI with query string behind this forwarded request, if any.
* @since 1.8
+ * @deprecated Since 2.4. Use {@link #getRequestURIWithQueryString(HttpServletRequest)} instead.
*/
+ @Deprecated // TODO: Remove in OmniFaces 3.0.
public static String getForwardRequestURIWithQueryString(HttpServletRequest request) {
String requestURI = getForwardRequestURI(request);
String queryString = getForwardRequestQueryString(request);
@@ -556,7 +597,7 @@ public static boolean isFacesAjaxRequest(HttpServletRequest request) {
* @see ResourceHandler#RESOURCE_IDENTIFIER
*/
public static boolean isFacesResourceRequest(HttpServletRequest request) {
- return request.getRequestURI().startsWith(request.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER + "/");
+ return getRequestURI(request).startsWith(request.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER + "/");
}
/**

0 comments on commit 56b03cd

Please sign in to comment.