Created RequestUtils, a place for HTTP request related utility functions.

Added getLocation() and getLocationEncoded() which reconstruct the URL
used to make this request (complete with query parameters if there are
any). Modified UserManager to use getLocationEncoded() rather than doing
it itself.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@127 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2001-05-26 23:18:11 +00:00
parent ea519467c2
commit c8ec99a93b
2 changed files with 48 additions and 7 deletions
@@ -1,5 +1,5 @@
// //
// $Id: UserManager.java,v 1.5 2001/05/26 04:37:35 mdb Exp $ // $Id: UserManager.java,v 1.6 2001/05/26 23:18:11 mdb Exp $
package com.samskivert.servlet.user; package com.samskivert.servlet.user;
@@ -10,6 +10,7 @@ import javax.servlet.http.*;
import com.samskivert.Log; import com.samskivert.Log;
import com.samskivert.servlet.RedirectException; import com.samskivert.servlet.RedirectException;
import com.samskivert.servlet.util.RequestUtils;
import com.samskivert.util.*; import com.samskivert.util.*;
/** /**
@@ -128,12 +129,7 @@ public class UserManager
// the login page // the login page
if (user == null) { if (user == null) {
// first construct the redirect URL // first construct the redirect URL
StringBuffer rurl = HttpUtils.getRequestURL(req); String eurl = RequestUtils.getEncodedLocation(req);
String qs = req.getQueryString();
if (qs != null) {
rurl.append("?").append(qs);
}
String eurl = URLEncoder.encode(rurl.toString());
String target = StringUtil.replace(_loginURL, "%R", eurl); String target = StringUtil.replace(_loginURL, "%R", eurl);
throw new RedirectException(target); throw new RedirectException(target);
} }
@@ -0,0 +1,45 @@
//
// $Id: RequestUtils.java,v 1.1 2001/05/26 23:18:11 mdb Exp $
package com.samskivert.servlet.util;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpUtils;
/**
* A repository of utility functions related to HTTP servlet stuff.
*/
public class RequestUtils
{
/**
* Reconstructs the current location (URL) from the request and
* servlet configuration (which is the only way to know which server
* we're on because that's not provided in the request) and returns it
* URL encoded so that it can be substituted into another URL.
*
* @return The URL encoded URL that represents our current location.
*/
public static String getEncodedLocation (HttpServletRequest req)
{
return URLEncoder.encode(getLocation(req));
}
/**
* Reconstructs the current location (URL) from the request and
* servlet configuration (which is the only way to know which server
* we're on because that's not provided in the request) and returns
* it.
*
* @return The URL that represents our current location.
*/
public static String getLocation (HttpServletRequest req)
{
StringBuffer rurl = HttpUtils.getRequestURL(req);
String qs = req.getQueryString();
if (qs != null) {
rurl.append("?").append(qs);
}
return rurl.toString();
}
}