if (!userUtils.isLoggedIn(request, response)) {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
final JSONRenderer renderer = new JSONRenderer();
context.setRenderer(renderer);
final JSONObject ret = new JSONObject();
renderer.setJSONObject(ret);
try {
final String requestURI = request.getRequestURI();
final String path = requestURI.substring("/console/plugins/admin-cache/pages/".length());
final JSONObject requestJSONObject = Requests.buildPaginationRequest(path);
final int currentPageNum = requestJSONObject.getInt(Pagination.PAGINATION_CURRENT_PAGE_NUM);
final int pageSize = requestJSONObject.getInt(Pagination.PAGINATION_PAGE_SIZE);
final int windowSize = requestJSONObject.getInt(Pagination.PAGINATION_WINDOW_SIZE);
List<String> keys = new ArrayList<String>(PageCaches.getKeys());
// Paginates
final int pageCount =
(int) Math.ceil((double) keys.size() / (double) pageSize);
final JSONObject pagination = new JSONObject();
ret.put(Pagination.PAGINATION, pagination);
final List<Integer> pageNums = Paginator.paginate(currentPageNum, pageSize, pageCount, windowSize);
pagination.put(Pagination.PAGINATION_PAGE_COUNT, pageCount);
pagination.put(Pagination.PAGINATION_PAGE_NUMS, pageNums);
final int start = pageSize * (currentPageNum - 1);
int end = start + pageSize;
end = end > keys.size() ? keys.size() : end;
keys = keys.subList(start, end);
// Retrives cached pages
final List<JSONObject> pages = new ArrayList<JSONObject>();
for (final String key : keys) {
LOGGER.log(Level.FINER, "Cached page[key={0}]", key);
JSONObject cachedPage = PageCaches.get(key);
if (null != cachedPage) {
// Do a copy for properties removing and retrieving
cachedPage = new JSONObject(cachedPage,
JSONObject.getNames(cachedPage));
cachedPage.remove(CACHED_CONTENT);
pages.add(cachedPage);
}
}
ret.put(Page.PAGES, pages);
ret.put(Keys.STATUS_CODE, true);
} catch (final Exception e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
final JSONObject jsonObject = QueryResults.defaultResult();
renderer.setJSONObject(jsonObject);
jsonObject.put(Keys.MSG, "Admin Cache plugin exception: " + e.getMessage());
}
}