I would like to know if there are libraries or solutions to handle requests containing headers such as Last-Modified
, If-Modified-Since
, If-Range
If-None-Match
, etc. and produce replies with Cache-Control
, ETags , Pragma
, 304
, Expires
, etc. in Servlets.
While I know that these cache and download control headers are more relevant to static resources, I ran into a situation (which I think is fairly common) where support for these headers increases performance and decreases application costs.
In my specific case, I'm building an open-source application to create collages on Facebook with a colleague (see link if relevant: link ). This app is hosted on GAE that imposes limits and charges for external requests .
In our case we coded a Servlet that acts as a Proxy and image resizer of Facebook (link: ProxyServlet ). The proxy is required to bypass security exceptions at the time of exporting Canvas content related to Politics of the same origin since the Facebook's static resource servers do not implement CORS headers.
In this proxy case we could simply delegate the headers of request from Servlet to Facebook:
public static final ImmutableSet<String> excludedHeaders =
ImmutableSet.of("Cookie", "Host");
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
final HttpURLConnection connection;
try {
// código para abrir o request
final Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
final String headerName = headerNames.nextElement();
final String headerValue = request.getHeader(headerName);
if (!excludedHeaders.contains(headerName)) {
log.info(headerName + " : " + headerValue);
connection.setRequestProperty(headerName, headerValue);
}
}
}
// Restante do código
}
And copy the headers and status code of the Facebook response back to the Servlet.
final int httpCode = connection.getResponseCode();
response.setStatus(httpCode);
for (Map.Entry<String, List<String>> entry : connection.getHeaderFields().entrySet()) {
final String header = entry.getKey();
log.info(entry.getKey() + " = " + entry.getValue());
for (String headerValue : entry.getValue()) {
response.addHeader(header, headerValue);
}
}
// Código para tratar o corpo da resposta, e em caso de status 200, redimensioná-la.
This was enough to make the application more responsive and greatly alleviate the amount of external requests (repeated images are cached in the browser and requests consecutive do not hit the Servlet).
However, I do not consider this a complete or generic solution (since it is specific to proxy servlets).
In Stack Overflow in English I found references to FileServlet bo < BalusC , which implements the Servlet issue for local downloads quite a bit (its code treats all the headings mentioned "on the nail"). in addition to supporting GZIP compression ).
But I wondered if there is a more generic library or solution to address this type of problem (for example, using filters and / or a simplified API to abstract implementation details).
Has anyone heard of anything like this? (And if not, does anyone apply to start writing something like this?: D).