Added a method to reconstruct a URL, query parameters and all, and use that

when logging a logic failure so as to be maximally informative.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@1760 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2006-01-16 23:10:47 +00:00
parent a6909f4c77
commit e2d440a121
2 changed files with 41 additions and 2 deletions
@@ -20,6 +20,9 @@
package com.samskivert.servlet.util;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.samskivert.util.StringUtil;
@@ -96,4 +99,40 @@ public class RequestUtils
buf.append(path);
return buf.toString();
}
/**
* Reconstructs the request URL including query parameters. <em>Note:</em>
* the output of this method is purely for logging purposes only, thus POST
* parameters are shown as if they were GET parameters and parameters are
* <em>not</em> URL encoded.
*/
public static String reconstructURL (HttpServletRequest req)
{
StringBuffer buf = req.getRequestURL();
Map map = req.getParameterMap();
if (map.size() > 0) {
buf.append("?");
for (Iterator iter = map.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry entry = (Map.Entry)iter.next();
buf.append(entry.getKey()).append("=");
String[] values = (String[])entry.getValue();
if (values.length == 1) {
buf.append(values[0]);
} else {
buf.append("(");
for (int ii = 0; ii < values.length; ii++) {
if (ii > 0) {
buf.append(", ");
}
buf.append(values[ii]);
}
buf.append(")");
}
if (iter.hasNext()) {
buf.append("&");
}
}
}
return buf.toString();
}
}
@@ -36,6 +36,7 @@ import com.samskivert.servlet.RedirectException;
import com.samskivert.servlet.SiteIdentifier;
import com.samskivert.servlet.SiteResourceLoader;
import com.samskivert.servlet.util.ExceptionMap;
import com.samskivert.servlet.util.RequestUtils;
import com.samskivert.util.StringUtil;
/**
@@ -224,8 +225,7 @@ public class Application
protected String handleException (
HttpServletRequest req, Logic logic, Exception error)
{
Log.warning("Choked on request [logic=" + logic +
", req=" + req.getRequestURI() + "].");
Log.warning(logic + " failed on: " + RequestUtils.reconstructURL(req));
Log.logStackTrace(error);
return ExceptionMap.getMessage(error);
}