Skip to content

Instantly share code, notes, and snippets.

View erdemaydin's full-sized avatar

Erdem Aydın erdemaydin

View GitHub Profile
@WebFilter(urlPatterns = "/*", dispatcherTypes = {DispatcherType.FORWARD})
public class CustomerFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException { }
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { }
@Override
<filter>
<filter-name>CustomerFilter</filter-name>
<filter-class>com.ea.Customerfilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CustomerFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<servlet-name>dispatcherServlet</servlet-name>
</filter-mapping>
@Configuration
public class WebFilterConfiguration {
@Bean
public FilterRegistrationBean<CustomerFilter> customerFilter() {
FilterRegistrationBean<CustomerFilter> customerFilter = new FilterRegistrationBean<>();
customerFilter.setFilter(new CustomerFilter());
customerFilter.addUrlPatterns("/customers/*");
customerFilter.setOrder(1);
customerFilter.setDispatcherTypes(DispatcherType.REQUEST);
@Component
public class ProductFilter implements Filter, Ordered {
@Override
public int getOrder() {
return 0;
}
...
}
@Order(1)
@Component
public class CustomerFilter implements Filter {
...
}
@Order(2)
@Component
public class ProductFilter implements Filter {
...
@Component
public class CustomerFilter implements Filter {
...
}
@Component
public class ProductFilter implements Filter {
...
}
public class CustomerFilter implements Filter {
private static final List<String> EXCLUDE_URI_LIST = Arrays.asList("/product", "/order", "/user");
private static final Short INVALID_CUSTOMER_STATUS = Short.valueOf("-1");
@Override
public void init(FilterConfig filterConfig) throws ServletException { }
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
@ExceptionHandler(Exception.class)
@ResponseBody ResponseEntity<TaskApiError> exceptionHandler(Throwable exception) {
ResponseEntity apiError;
if(exception instanceof InvalidInputException) {
apiError = generateErrorResponse(TaskApiErrorType.INVALID_INPUT_ERROR, HttpStatus.BAD_REQUEST, exception);
} else if (exception instanceof RecordNotFoundException) {
apiError = generateErrorResponse(TaskApiErrorType.RECORD_NOT_FOUND_ERROR, HttpStatus.NOT_FOUND, exception);
} else if (exception instanceof IllegalArgumentException || exception instanceof ValidationException || exception instanceof SessionException) {
apiError = generateErrorResponse(TaskApiErrorType.ILLEGAL_ARGUMENTS_ERROR, HttpStatus.NOT_ACCEPTABLE, exception);
} else {
@PutMapping( "/tasks/{id}")
public ResponseEntity<Task> updateTask(@PathVariable Long id, @RequestBody Task requestTask) {
log.info("updating task with id " + id);
if (StringUtils.isEmpty(requestTask.getDescription())) {
throw new InvalidInputException("Description can not be empty");
}
Optional<Task> optCurrentTask = taskService.findById(id);
if (!optCurrentTask.isPresent()) {
@ControllerAdvice
public class TaskApiExceptionHandler {
private static Logger log = Logger.getLogger("TaskApiExceptionHandler");
@ExceptionHandler(InvalidInputException.class)
@ResponseBody ResponseEntity<TaskApiError> invalidInputException(InvalidInputException exception) {
return generateErrorResponse(TaskApiErrorType.INVALID_INPUT_ERROR, HttpStatus.BAD_REQUEST, exception);
}