-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updating exception handling, add new jobs and logging
- Loading branch information
1 parent
a952d63
commit 4272dce
Showing
21 changed files
with
373 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 8 additions & 1 deletion
9
src/main/java/com/library/spring/scheduler/job/MongoSyncJob.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,20 @@ | ||
package com.library.spring.scheduler.job; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.quartz.Job; | ||
import org.quartz.JobDataMap; | ||
import org.quartz.JobExecutionContext; | ||
import org.quartz.JobExecutionException; | ||
|
||
@Slf4j | ||
public class MongoSyncJob implements Job { | ||
|
||
@Override | ||
public void execute(JobExecutionContext context) throws JobExecutionException { | ||
System.out.println("MongoSyncJob is now running.."); | ||
JobDataMap jobMap = context.getMergedJobDataMap(); | ||
String collection = jobMap.getString("collection"); | ||
String service = jobMap.getString("service"); | ||
|
||
log.info(String.format("Syncing data from service url %s into mongodb collection name %s...", service, collection)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/main/java/com/library/spring/scheduler/model/MongoSyncJobData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/com/library/spring/web/advice/GlobalBindingInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.library.spring.web.advice; | ||
|
||
import com.library.spring.web.formatter.DateEditor; | ||
import org.springframework.web.bind.WebDataBinder; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.InitBinder; | ||
|
||
import java.util.Date; | ||
|
||
@ControllerAdvice | ||
public class GlobalBindingInitializer { | ||
|
||
/* Initialize a global InitBinder for dates instead of cloning its code in every Controller */ | ||
|
||
@InitBinder | ||
public void binder(WebDataBinder binder) { | ||
binder.registerCustomEditor(Date.class, new DateEditor()); | ||
} | ||
} |
16 changes: 0 additions & 16 deletions
16
src/main/java/com/library/spring/web/advice/GlobalExceptionHandler.java
This file was deleted.
Oops, something went wrong.
42 changes: 42 additions & 0 deletions
42
src/main/java/com/library/spring/web/advice/ServiceExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.library.spring.web.advice; | ||
|
||
import com.library.spring.web.model.ResponseBean; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
|
||
import java.nio.file.AccessDeniedException; | ||
import java.util.UUID; | ||
|
||
@Slf4j | ||
@ControllerAdvice(basePackages = {"com.library.spring.web.controller"}) | ||
public class ServiceExceptionHandler { | ||
|
||
@ExceptionHandler({Exception.class}) | ||
protected Object handleInvalidRequest(Exception exception) { | ||
|
||
log.error("Service Failure", exception); | ||
HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR; | ||
|
||
if (exception instanceof AccessDeniedException) { | ||
httpStatus = HttpStatus.FORBIDDEN; | ||
} | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
ResponseBean responseBean = createResponseBean(exception); | ||
return new ResponseEntity<>(responseBean, headers, httpStatus); | ||
} | ||
|
||
private ResponseBean createResponseBean(Exception exception) { | ||
ResponseBean responseBean = new ResponseBean(); | ||
responseBean.setId(UUID.randomUUID().toString()); | ||
responseBean.setMessage(exception.getMessage()); | ||
responseBean.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()); | ||
return responseBean; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/library/spring/web/advice/ViewExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.library.spring.web.advice; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import java.util.Date; | ||
|
||
@Slf4j | ||
@ControllerAdvice(basePackages = {"com.library.spring.web.view"}) | ||
public class ViewExceptionHandler { | ||
|
||
@ExceptionHandler({Exception.class}) | ||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||
public String applicationError(HttpServletRequest req, Exception exception, final Model model) { | ||
log.error("View Request Failed: " + req.getRequestURI(), exception); | ||
model.addAttribute("exception", exception); | ||
model.addAttribute("url", req.getRequestURL()); | ||
model.addAttribute("timestamp", new Date().toString()); | ||
return "error"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/com/library/spring/web/formatter/DateEditor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.library.spring.web.formatter; | ||
|
||
import java.beans.PropertyEditorSupport; | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
|
||
/** | ||
* @see <a href="https://dzone.com/articles/how-serialize-javautildate">How to Serialize Java.util.Date with Jackson JSON Processor</a> | ||
* @see <a href="http://www.baeldung.com/jackson-serialize-dates">Jackson Date</a> | ||
* @see <a href="http://magicmonster.com/kb/prg/java/spring/webmvc/jackson_custom.html">Customise the Jackson JSON mapper</a> | ||
* @see <a href="https://github.com/FasterXML/jackson-docs/wiki/JacksonHowToCustomSerializers">Jackson How-To: Custom Serializers</a> | ||
* @see <a href="https://www.concretepage.com/spring/spring-mvc/spring-mvc-validator-with-initbinder-webdatabinder-registercustomeditor-example">@InitBinder</a> | ||
* @see <a href="How To create a global InitBinder in Spring with @ControllerAdvice">Create a global InitBinder in Spring with @ControllerAdvice</a> | ||
*/ | ||
public class DateEditor extends PropertyEditorSupport { | ||
|
||
public static final String DATE_FORMAT = "dd-MM-yyyy hh:mm:ss"; | ||
|
||
public void setAsText(String value) { | ||
try { | ||
setValue(new SimpleDateFormat(DATE_FORMAT).parse(value)); | ||
} catch (ParseException e) { | ||
setValue(null); | ||
} | ||
} | ||
|
||
public String getAsText() { | ||
String s = ""; | ||
if (getValue() != null) { | ||
s = new SimpleDateFormat(DATE_FORMAT).format((Date) getValue()); | ||
} | ||
return s; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/com/library/spring/web/model/ResponseBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.library.spring.web.model; | ||
|
||
import java.io.Serializable; | ||
|
||
public class ResponseBean implements Serializable { | ||
|
||
private String id; | ||
private String message; | ||
private String responseCode; | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public void setMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
public String getResponseCode() { | ||
return responseCode; | ||
} | ||
|
||
public void setResponseCode(String responseCode) { | ||
this.responseCode = responseCode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
...spring/web/controller/ViewController.java → ...brary/spring/web/view/ViewController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.