use of com.pratilipi.api.shared.GenericFileDownloadResponse in project pratilipi by Pratilipi.
the class GenericApi method dispatchApiResponse.
final void dispatchApiResponse(Object apiResponse, HttpServletRequest request, HttpServletResponse response) throws IOException {
if (apiResponse instanceof GenericFileDownloadResponse) {
GenericFileDownloadResponse gfdResponse = (GenericFileDownloadResponse) apiResponse;
String eTag = request.getHeader("If-None-Match");
if (eTag == null)
logger.log(Level.INFO, "No eTag found !");
if (eTag != null && eTag.equals(gfdResponse.getETag())) {
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
} else {
response.setContentType(gfdResponse.getMimeType());
response.setHeader("Cache-Control", "max-age=315360000");
response.setHeader("ETag", gfdResponse.getETag());
OutputStream out = response.getOutputStream();
out.write(gfdResponse.getData());
out.close();
}
} else if (apiResponse instanceof GenericResponse) {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
if (SystemProperty.STAGE.equals(SystemProperty.STAGE_GAMMA)) {
// response.setContentType( "application/json" );
response.addHeader("Access-Control-Allow-Origin", getAccessControlAllowOrigin());
}
PrintWriter writer = response.getWriter();
writer.println(new Gson().toJson(apiResponse));
writer.close();
} else if (apiResponse instanceof Throwable) {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
if (SystemProperty.STAGE.equals(SystemProperty.STAGE_GAMMA)) {
// response.setContentType( "application/json" );
response.addHeader("Access-Control-Allow-Origin", getAccessControlAllowOrigin());
}
PrintWriter writer = response.getWriter();
if (apiResponse instanceof InvalidArgumentException) {
logger.log(Level.INFO, ((Throwable) apiResponse).getMessage());
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
} else if (apiResponse instanceof InsufficientAccessException) {
logger.log(Level.INFO, ((Throwable) apiResponse).getMessage());
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
} else if (apiResponse instanceof UnexpectedServerException)
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
else
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
writer.println(((Throwable) apiResponse).getMessage());
writer.close();
}
}
Aggregations