package org.crunchytorch.coddy.security.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.crunchytorch.coddy.application.data.Response;
import org.crunchytorch.coddy.security.exception.ForbiddenException;
import org.crunchytorch.coddy.security.exception.NotAuthorizedException;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CatchSecurityExceptionFilter extends OncePerRequestFilter {

    private static final ObjectMapper MAPPER = new ObjectMapper();

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        try {
            filterChain.doFilter(request, response);
        } catch (ForbiddenException e) {
            this.injectResponse(response, HttpServletResponse.SC_FORBIDDEN, e);
        } catch (NotAuthorizedException e) {
            this.injectResponse(response, HttpServletResponse.SC_UNAUTHORIZED, e);
        }
    }

    private void injectResponse(HttpServletResponse response, int statusCode, Throwable t) throws IOException {
        Response errorResponse = new Response(t.getMessage());
        response.setStatus(statusCode);
        response.getWriter().write(MAPPER.writeValueAsString(errorResponse));
    }
}

@@ -94,16 +94,74 @@
 Mapper;%0A
+import org.crunchytorch.coddy.application.data.MediaType;%0A
 import o
@@ -343,32 +343,77 @@
 rizedException;%0A
+import org.springframework.http.HttpHeaders;%0A
 import org.sprin
@@ -1510,24 +1510,106 @@
 tatusCode);%0A
+        response.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);%0A
         resp

fix encoding response