Sonar Java Filter throw an UnsupportedOperationException

James :

i'm working in java spring web project and all works fine for me , i use a filter like that :

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CORSFilter implements Filter{

    @Value("${permitted.url}")
    private String permittedUrl;

    @Override
    public void init(FilterConfig fc) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
            throws IOException, ServletException {

        HttpServletResponse response = (HttpServletResponse) resp;
        HttpServletRequest request = (HttpServletRequest) req;
        response.setHeader("Access-Control-Allow-Origin", permittedUrl); // 
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, DELETE, PUT");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Headers","x-requested-with, authorization, Content-Type, Authorization, credential, X-XSRF-TOKEN");

        if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
            response.setStatus(HttpServletResponse.SC_OK);
        } else {
            chain.doFilter(req, resp);
        }

    }

    @Override
    public void destroy() {
    }

my problem is with Sonar , it alert me about 2 critical issues for the first and last methods ( init(FilterConfig fc) and destroy()) and it show me the following message "Add a nested comment explaining why this method is empty, throw an UnsupportedOperationException or complete the implementation." do you have any idea on how i can solve this issue.

and what i can do in this two methods as implementation ?

Thanks in advance.

Arnaud :

Sonar complains about the fact that your overriden methods do absolutely nothing.

So either it is normal in your case, but you should add a comment in your code telling that, or those methods aren't meant to be called in your implementation , and they should throw an UnsupportedOperationException if they are called .

Solution 1 :

@Override
public void init(FilterConfig fc) {
  // Do nothing because of X and Y.
}

Solution 2 :

@Override
public void init(FilterConfig fc) {
  throw new UnsupportedOperationException();
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=358275&siteId=1