try {
try {
if (pageIdxParam != null) {
pageIdx = Integer.parseInt(pageIdxParam);
if (pageIdx < 1) {
throw new ApiRequestException("Page number must be greater than 0");
}
}
if (pageSizeParam != null) {
pageSize = Integer.parseInt(pageSizeParam);
if (pageSize > MAX_PAGE_SIZE) {
throw new ApiRequestException("Page size must be less than 1000");
}
else if (pageSize < 1) {
throw new ApiRequestException("Page size must be greater than 0");
}
}
}
catch (NumberFormatException e) {
throw new ApiRequestException("Invalid pageIdx ["+pageIdxParam+"] or pageSize ["+pageSizeParam+"]. Must be an integer.");
}
if ("true".equals(sortOrderParam)) {
sortOrder = true;
}
else if (sortOrderParam == null || "false".equals(sortOrderParam)) {
sortOrder = false;
}
else {
throw new ApiRequestException("Invalid sortOrder parameter: "+sortOrderParam);
}
Matcher searchMatcher = searchPattern.matcher(path);
Matcher documentMatcher = documentPattern.matcher(path);
if (searchMatcher.find()) {
String format = searchMatcher.group(1);
String type = searchMatcher.group(2);
String uriTerm = searchMatcher.group(3);
String pagePart = searchMatcher.group(4);
String sizePart = searchMatcher.group(5);
String term = RequestUtils.getSearchString(request, uriTerm);
if (pagePart != null) {
pageIdx = Integer.valueOf(pagePart);
if (pageIdx < 1) {
throw new ApiRequestException("Page number must be greater than 0");
}
}
if (sizePart != null) {
pageSize = Integer.valueOf(sizePart);
if (pageSize > MAX_PAGE_SIZE) {
throw new ApiRequestException("Page size must be less than 1000");
}
else if (pageSize < 1) {
throw new ApiRequestException("Page size must be greater than 0");
}
}
if (!type.equals("search")) {
if (type.equals("sponsor")) {
term = "sponsor:"+uriTerm+" AND otype:bill";
String filter = RequestUtils.getSearchString(request, "");
if (!filter.isEmpty()) {
term += " AND "+filter;
}
}
else {
term = "otype:"+type.substring(0, type.length()-1)+(term.isEmpty() ? "" : " AND "+term);
}
}
else if (term.isEmpty()) {
throw new ApiRequestException("A search term is required.");
}
doSearch(request, response, format, type, term, pageIdx, pageSize, sort, sortOrder);
}
else if (documentMatcher.find()) {
String format = documentMatcher.group(1);
String otype = documentMatcher.group(2);
String oid = documentMatcher.group(3);
doSingleView(request, response, format, otype, oid);
}
else {
throw new ApiRequestException("Invalid request: "+uri);
}
}
catch (ApiRequestException e) {
logger.error(e.getMessage(), e);
response.sendError(HttpServletResponse.SC_NOT_FOUND);