Add ability to parse arguments with parseOptionMap
The RestApiServlet wasn't able to handle command-line parsers which included required arguments. These can now be specified by passing a Set of which param names to look for and treat as arguments. Change-Id: Ia1400c7decbb896f038ab016c063e9ee6e5991d9 Signed-off-by: Brad Larson <bklarson@gmail.com>
This commit is contained in:
@@ -34,7 +34,9 @@ import java.io.IOException;
|
|||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.http.HttpServlet;
|
import javax.servlet.http.HttpServlet;
|
||||||
@@ -182,11 +184,16 @@ public abstract class RestApiServlet extends HttpServlet {
|
|||||||
|
|
||||||
public <T> boolean parse(T param, HttpServletRequest req,
|
public <T> boolean parse(T param, HttpServletRequest req,
|
||||||
HttpServletResponse res) throws IOException {
|
HttpServletResponse res) throws IOException {
|
||||||
|
return parse(param, req, res, Collections.<String>emptySet());
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> boolean parse(T param, HttpServletRequest req,
|
||||||
|
HttpServletResponse res, Set<String> argNames) throws IOException {
|
||||||
CmdLineParser clp = parserFactory.create(param);
|
CmdLineParser clp = parserFactory.create(param);
|
||||||
try {
|
try {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
Map<String, String[]> parameterMap = req.getParameterMap();
|
Map<String, String[]> parameterMap = req.getParameterMap();
|
||||||
clp.parseOptionMap(parameterMap);
|
clp.parseOptionMap(parameterMap, argNames);
|
||||||
} catch (CmdLineException e) {
|
} catch (CmdLineException e) {
|
||||||
if (!clp.wasHelpRequestedByOption()) {
|
if (!clp.wasHelpRequestedByOption()) {
|
||||||
res.setStatus(HttpServletResponse.SC_BAD_REQUEST);
|
res.setStatus(HttpServletResponse.SC_BAD_REQUEST);
|
||||||
|
|||||||
@@ -54,9 +54,11 @@ import java.io.StringWriter;
|
|||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extended command line parser which handles --foo=value arguments.
|
* Extended command line parser which handles --foo=value arguments.
|
||||||
@@ -209,6 +211,12 @@ public class CmdLineParser {
|
|||||||
|
|
||||||
public void parseOptionMap(Map<String, String[]> parameters)
|
public void parseOptionMap(Map<String, String[]> parameters)
|
||||||
throws CmdLineException {
|
throws CmdLineException {
|
||||||
|
parseOptionMap(parameters, Collections.<String>emptySet());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void parseOptionMap(Map<String, String[]> parameters,
|
||||||
|
Set<String> argNames)
|
||||||
|
throws CmdLineException {
|
||||||
ArrayList<String> tmp = new ArrayList<String>();
|
ArrayList<String> tmp = new ArrayList<String>();
|
||||||
for (Map.Entry<String, String[]> ent : parameters.entrySet()) {
|
for (Map.Entry<String, String[]> ent : parameters.entrySet()) {
|
||||||
String name = ent.getKey();
|
String name = ent.getKey();
|
||||||
@@ -230,7 +238,9 @@ public class CmdLineParser {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (String value : ent.getValue()) {
|
for (String value : ent.getValue()) {
|
||||||
tmp.add(name);
|
if (!argNames.contains(ent.getKey())) {
|
||||||
|
tmp.add(name);
|
||||||
|
}
|
||||||
tmp.add(value);
|
tmp.add(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user