resp.setContentType("text/html");
resp.setCharacterEncoding("UTF-8");
GadgetContext context = new HttpGadgetContext(req);
RenderingResults results = renderer.render(context);
switch (results.getStatus()) {
case OK:
if (context.getIgnoreCache() ||
urlstatus == UrlValidationStatus.INVALID) {
HttpUtil.setCachingHeaders(resp, 0);
} else if (urlstatus == UrlValidationStatus.VALID_VERSIONED) {
// Versioned files get cached indefinitely
HttpUtil.setCachingHeaders(resp, true);
} else {
// Unversioned files get cached for 5 minutes by default, but this can be overridden
// with a query parameter.
int ttl = DEFAULT_CACHE_TTL;
String ttlStr = req.getParameter(ProxyBase.REFRESH_PARAM);
if (!StringUtils.isEmpty(ttlStr)) {
try {
ttl = Integer.parseInt(ttlStr);
} catch (NumberFormatException e) {
// Ignore malformed TTL value
LOG.info("Bad TTL value '" + ttlStr + "' was ignored");
}
}
HttpUtil.setCachingHeaders(resp, ttl, true);
}
resp.getWriter().print(results.getContent());
break;
case ERROR:
resp.setStatus(results.getHttpStatusCode());
resp.getWriter().print(StringEscapeUtils.escapeHtml(results.getErrorMessage()));
break;
case MUST_REDIRECT:
resp.sendRedirect(results.getRedirect().toString());
break;
}
}