Revision: 3078
http://archive-access.svn.sourceforge.net/archive-access/?rev=3078&view=rev
Author: bradtofel
Date: 2010-04-27 22:34:03 +0000 (Tue, 27 Apr 2010)
Log Message:
-----------
REFACTOR: moved HTTP GET form parsing static helper methods to this class
Modified Paths:
--------------
trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/webapp/AbstractRequestHandler.java
Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/webapp/AbstractRequestHandler.java
===================================================================
--- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/webapp/AbstractRequestHandler.java 2010-04-27 22:19:46 UTC (rev 3077)
+++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/webapp/AbstractRequestHandler.java 2010-04-27 22:34:03 UTC (rev 3078)
@@ -3,10 +3,14 @@
*/
package org.archive.wayback.util.webapp;
+import java.util.Map;
+
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
+import org.archive.wayback.exception.BadQueryException;
+
/**
* Abstract RequestHandler implementation which performs the minimal behavior
* for self registration with a RequestMapper, requiring subclasses to implement
@@ -44,4 +48,55 @@
public String translateRequestPathQuery(HttpServletRequest httpRequest) {
return RequestMapper.getRequestContextPathQuery(httpRequest);
}
+
+ /**
+ * Extract the first value in the array mapped to by field in queryMap
+ * @param queryMap the Map in which to search
+ * @param field the field value desired
+ * @return the first value in the array mapped to field in queryMap, if
+ * present, null otherwise
+ */
+ public static String getMapParam(Map<String,String[]> queryMap,
+ String field) {
+ String arr[] = queryMap.get(field);
+ if (arr == null || arr.length == 0) {
+ return null;
+ }
+ return arr[0];
+ }
+
+ /**
+ * Extract the first value in the array mapped to by field in queryMap
+ * @param queryMap the Map in which to search
+ * @param field the field value desired
+ * @return the first value in the array mapped to field in queryMap, if
+ * present. A BadQueryException is thrown if there is no appropriate value
+ * @throws BadQueryException if there is nothing mapped to field, or if the
+ * Array mapped to field is empty
+ */
+ public static String getRequiredMapParam(Map<String,String[]> queryMap,
+ String field)
+ throws BadQueryException {
+ String value = getMapParam(queryMap,field);
+ if(value == null) {
+ throw new BadQueryException("missing field " + field);
+ }
+ if(value.length() == 0) {
+ throw new BadQueryException("empty field " + field);
+ }
+ return value;
+ }
+
+ /**
+ * Extract the first value in the array mapped to by field in queryMap
+ * @param map the Map in which to search
+ * @param param the field value desired
+ * @return the first value in the array mapped to field in queryMap, if
+ * present, or an empty string otherwise
+ */
+ public static String getMapParamOrEmpty(Map<String,String[]> map,
+ String param) {
+ String val = getMapParam(map,param);
+ return (val == null) ? "" : val;
+ }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|