/**
 * Licensed to ESUP-Portail under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for
 * additional information regarding copyright ownership.
 *
 * ESUP-Portail licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package fr.univrouen.poste.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import fr.univrouen.poste.domain.PosteCandidature;
import fr.univrouen.poste.services.LogService;


@Service
@Controller
public class ExceptionController implements HandlerExceptionResolver {
	
	private final Logger log = Logger.getLogger(getClass());

	@Autowired
	LogService logService;

	
	@Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
		
		if(ex instanceof AccessDeniedException) {
			return this.deniedHandler(request, response);
		}
		
        String ip = request.getRemoteAddr();	
		if(ex instanceof MultipartException) {
			log.warn("MultipartException with this client " + ip + ". We can assume that the client has canceled his request (because of a double-click / double-submit of the form for example).", ex);
		} else {	
			log.error("Uncaught exception  with this client " + ip, ex);
		}
		
		// hack for logging uploads failed 
		if(request.getServletPath().matches("/postecandidatures/[0-9]*/addFile")) {
			String posteCandidatureId = request.getServletPath().replaceAll("/postecandidatures/([0-9]*)/addFile", "$1");
			PosteCandidature posteCandidature = PosteCandidature.findPosteCandidature(Long.valueOf(posteCandidatureId));
			logService.logActionFile(LogService.UPLOAD_FAILED_ACTION, posteCandidature, null, request, new Date());
		}
		
    	//response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        ModelAndView modelAndview = new ModelAndView("uncaughtException");
        modelAndview.addObject("exception", ex);
        return modelAndview;
    }

	@RequestMapping("/denied")
    public ModelAndView deniedHandler(HttpServletRequest request, HttpServletResponse response) {
        String ip = request.getRemoteAddr();	
	    log.warn("Access Denied for " + ip);
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
		return new ModelAndView("accessDeniedException");
    }
	
   
}

@@ -808,16 +808,44 @@
 e.web;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -2071,24 +2071,53 @@
 artException
+ %7C%7C ex instanceof IOException
 ) %7B%0A%09%09%09log.w
@@ -2140,16 +2140,31 @@
 ception 
+or IOException 
 with thi
@@ -2849,24 +2849,131 @@
 ());%0A%09%09%7D%0A%09%09%0A
+%09%09%0A%09%09if(response.isCommitted()) %7B%0A%09%09%09// Client can't get exception page here. %0A%09%09%09return null;%0A%09%09%7D else %7B%0A%09
     %09//respo
@@ -3033,16 +3033,17 @@
 ERROR);%0A
+%09
         
@@ -3105,24 +3105,25 @@
 xception%22);%0A
+%09
         mode
@@ -3159,16 +3159,17 @@
 %22, ex);%0A
+%09
         
@@ -3189,16 +3189,20 @@
 ndview;%0A
+%09%09%7D%0A
     %7D%0A%0A%09

Improve ExceptionController : ClientAbortException/IOException -> warning and not error