Search in sources :

Example 1 with GenericFileDownloadResponse

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();
    }
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) GenericResponse(com.pratilipi.api.shared.GenericResponse) OutputStream(java.io.OutputStream) GenericFileDownloadResponse(com.pratilipi.api.shared.GenericFileDownloadResponse) Gson(com.google.gson.Gson) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) PrintWriter(java.io.PrintWriter)

Aggregations

Gson (com.google.gson.Gson)1 GenericFileDownloadResponse (com.pratilipi.api.shared.GenericFileDownloadResponse)1 GenericResponse (com.pratilipi.api.shared.GenericResponse)1 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)1 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)1 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1