* info from the httpRequest object.
*/
public WaybackRequest parse(HttpServletRequest httpRequest,
AccessPoint wbContext) throws BadQueryException {
WaybackRequest wbRequest = null;
@SuppressWarnings("unchecked")
Map<String,String[]> queryMap = httpRequest.getParameterMap();
String query = AccessPoint.getMapParam(queryMap, SEARCH_QUERY);
if (query == null) {
return null;
}
wbRequest = new WaybackRequest();
String base = wbContext.translateRequestPath(httpRequest);
if (base.startsWith(REPLAY_BASE)) {
wbRequest.setReplayRequest();
} else if(base.startsWith(QUERY_BASE)){
wbRequest.setCaptureQueryRequest();
} else if(base.startsWith(XQUERY_BASE)){
wbRequest.setCaptureQueryRequest();
wbRequest.setXMLMode(true);
} else {
return null;
}
String numResults = AccessPoint.getMapParam(queryMap, SEARCH_RESULTS);
String startPage = AccessPoint.getMapParam(queryMap, START_PAGE);
if (numResults != null) {
int nr = Integer.parseInt(numResults);
wbRequest.setResultsPerPage(nr);
} else {
wbRequest.setResultsPerPage(getMaxRecords());
}
if (startPage != null) {
int sp = Integer.parseInt(startPage);
wbRequest.setPageNum(sp);
} else {
wbRequest.setPageNum(1);
}
// first try the entire line_tokens:
for (int i = 0; i < lineTokens.length; i++) {
String token = lineTokens[i] + ":";
int index = query.indexOf(token);
if (index > -1) {
// found it, take value as the remainder of the query
String value = query.substring(index + token.length());
// TODO: trim trailing whitespace?
wbRequest.put(lineTokens[i], value);
query = query.substring(0, index);
}
}
// now split whatever is left on whitespace:
String[] parts = WHITESPACE_PATTERN.split(query);
for (int i = 0; i < parts.length; i++) {
String token = parts[i];
int colonIndex = token.indexOf(":");
if (colonIndex == -1) {
throw new BadQueryException("Bad search token(" + token + ")");
}
try {
String key = URLDecoder.decode(token.substring(0, colonIndex),
"UTF-8");
String value = URLDecoder.decode(
token.substring(colonIndex + 1), "UTF-8");
// TODO: make sure key is in singleTokens?
// let's just let em all thru for now:
wbRequest.put(key, value);
} catch (UnsupportedEncodingException e) {
throw new BadQueryException("Unsupported encoding: UTF-8");
}
}
if (wbRequest.getStartTimestamp() == null) {
wbRequest.setStartTimestamp(getEarliestTimestamp());
}
if (wbRequest.getEndTimestamp() == null) {
wbRequest.setEndTimestamp(getLatestTimestamp());
}
return wbRequest;
}