* 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 = getMapParam(queryMap, SEARCH_QUERY);
if(query == null) {
return null;
}
wbRequest = new WaybackRequest();
String base = wbContext.translateRequestPath(httpRequest);
if(base.startsWith(REPLAY_BASE)) {
wbRequest.put(WaybackConstants.REQUEST_TYPE,
WaybackConstants.REQUEST_REPLAY_QUERY);
} else if(base.startsWith(QUERY_BASE)){
wbRequest.put(WaybackConstants.REQUEST_TYPE,
WaybackConstants.REQUEST_URL_QUERY);
} else if(base.startsWith(XQUERY_BASE)){
wbRequest.put(WaybackConstants.REQUEST_TYPE,
WaybackConstants.REQUEST_URL_QUERY);
wbRequest.put(WaybackConstants.REQUEST_XML_DATA,"1");
} else {
return null;
}
String numResults = getMapParam(queryMap, SEARCH_RESULTS);
String startPage = getMapParam(queryMap, START_PAGE);
if (numResults != null) {
int nr = Integer.parseInt(numResults);
wbRequest.setResultsPerPage(nr);
} else {
wbRequest.setResultsPerPage(maxRecords);
}
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 + ")");
}
String key = token.substring(0, colonIndex);
String value = token.substring(colonIndex + 1);
// TODO: make sure key is in singleTokens?
// let's just let em all thru for now:
wbRequest.put(key, value);
}
if(wbRequest.get(WaybackConstants.REQUEST_START_DATE) == null) {
wbRequest.put(WaybackConstants.REQUEST_START_DATE,
getEarliestTimestamp());
}
if(wbRequest.get(WaybackConstants.REQUEST_END_DATE) == null) {
wbRequest.put(WaybackConstants.REQUEST_END_DATE,
getLatestTimestamp());
}
wbRequest.fixup(httpRequest);
return wbRequest;
}