Java tutorial
/******************************************************************************* * EventExceptionResolver.java * insights-event-logger * Created by Gooru on 2014 * Copyright (c) 2014 Gooru. All rights reserved. * http://www.goorulearning.org/ * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************************/ package org.logger.event.web.spring.exception; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException; import org.jets3t.service.S3ServiceException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.security.access.AccessDeniedException; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver; import flexjson.JSONSerializer; public class EventExceptionResolver extends SimpleMappingExceptionResolver { private final Logger logger = LoggerFactory.getLogger(EventExceptionResolver.class); @Override public ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { ErrorObject errorObject = null; ex.printStackTrace(); logger.error("Error in Resolver : {}", ex); if (ex instanceof AccessDeniedException) { errorObject = new ErrorObject(403, ex.getMessage()); response.setStatus(403); } else if (ex instanceof SizeLimitExceededException) { response.setStatus(HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE); errorObject = new ErrorObject(413, ex.getMessage()); } else if (ex instanceof S3ServiceException) { response.setStatus(500); errorObject = new ErrorObject(500, ((S3ServiceException) ex).getErrorMessage()); } else { errorObject = new ErrorObject(500, ex.getMessage()); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } ModelAndView jsonModel = new ModelAndView("rest/model"); jsonModel.addObject("model", new JSONSerializer().exclude("*.class").serialize(errorObject)); return jsonModel; } }