-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
14 changed files
with
337 additions
and
1 deletion.
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
14 changes: 14 additions & 0 deletions
14
hr-microservice-hexagonal/src/main/java/com/example/hr/dto/EmployeeRequest.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
31 changes: 31 additions & 0 deletions
31
hr-microservice-hexagonal/src/main/java/com/example/hr/dto/RestErrorMessage.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,31 @@ | ||
package com.example.hr.dto; | ||
|
||
public class RestErrorMessage { | ||
private int errorId; | ||
private String message; | ||
private String debugId; | ||
|
||
public RestErrorMessage(int errorId, String message, String debugId) { | ||
this.errorId = errorId; | ||
this.message = message; | ||
this.debugId = debugId; | ||
} | ||
|
||
public int getErrorId() { | ||
return errorId; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public String getDebugId() { | ||
return debugId; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "RestErrorMessage [errorId=" + errorId + ", message=" + message + ", debugId=" + debugId + "]"; | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
hr-microservice-hexagonal/src/main/java/com/example/hr/handler/RestErrorHandler.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,49 @@ | ||
package com.example.hr.handler; | ||
|
||
import java.util.stream.Collectors; | ||
|
||
import javax.validation.ConstraintViolation; | ||
import javax.validation.ConstraintViolationException; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.validation.ObjectError; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
import com.example.hr.dto.RestErrorMessage; | ||
|
||
@RestControllerAdvice | ||
public class RestErrorHandler { | ||
|
||
@ExceptionHandler(RuntimeException.class) | ||
@ResponseStatus(code = HttpStatus.BAD_REQUEST) | ||
public RestErrorMessage handleException(Exception e) { | ||
return new RestErrorMessage(100,e.getMessage(),"6b8038e5-3b8f-4230-bfe8-4de2c422469f"); | ||
} | ||
|
||
@ExceptionHandler(ConstraintViolationException.class) | ||
@ResponseStatus(code = HttpStatus.BAD_REQUEST) | ||
public RestErrorMessage handleConstraintViolationException(ConstraintViolationException e) { | ||
var violations = e.getConstraintViolations() | ||
.stream() | ||
.map(ConstraintViolation::getMessage) | ||
.collect(Collectors.joining("|")); | ||
return new RestErrorMessage(200,violations,"7c248b97-9aa8-46fd-b5b2-9f8ea537635c"); | ||
} | ||
|
||
|
||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
@ResponseStatus(code = HttpStatus.BAD_REQUEST) | ||
public RestErrorMessage handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { | ||
var violations = e.getBindingResult() | ||
.getAllErrors() | ||
.stream() | ||
.map(ObjectError::getDefaultMessage) | ||
.collect(Collectors.joining("|")); | ||
return new RestErrorMessage(200,violations,"7c248b97-9aa8-46fd-b5b2-9f8ea537635c"); | ||
} | ||
|
||
|
||
} |
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
22 changes: 22 additions & 0 deletions
22
hr-microservice-hexagonal/src/main/java/com/example/validation/Email.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,22 @@ | ||
package com.example.validation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
import javax.validation.constraints.Pattern; | ||
|
||
@Target({ ElementType.FIELD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Pattern(regexp = "^\\+?[a-z0-9](([-+.]|[_]+)?[a-z0-9]+)*@([a-z0-9]+(\\.|\\-))+[a-z]{2,6}$", message = "{validation.email}") | ||
@Constraint(validatedBy = {}) | ||
public @interface Email { | ||
String message() default "{validation.email}"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
} |
21 changes: 21 additions & 0 deletions
21
hr-microservice-hexagonal/src/main/java/com/example/validation/Iban.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,21 @@ | ||
package com.example.validation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
|
||
@Target({ ElementType.FIELD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Constraint(validatedBy = IbanValidator.class) | ||
public @interface Iban { | ||
String message() default "{validation.iban}"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
hr-microservice-hexagonal/src/main/java/com/example/validation/IbanValidator.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,43 @@ | ||
package com.example.validation; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
|
||
public class IbanValidator implements ConstraintValidator<Iban, String> { | ||
private static final long MAX = 999999999; | ||
private static final long MODULUS = 97; | ||
|
||
@Override | ||
public void initialize(Iban arg0) { | ||
} | ||
|
||
@Override | ||
public boolean isValid(String value, ConstraintValidatorContext ctx) { | ||
if (value == null || value.length() < 5) { | ||
return false; | ||
} | ||
try { | ||
int modulusResult = calculateModulus(value); | ||
return (modulusResult == 1); | ||
} catch (Exception ex) { | ||
return false; | ||
} | ||
} | ||
|
||
private int calculateModulus(String code) throws Exception { | ||
String reformattedCode = code.substring(4) + code.substring(0, 4); | ||
long total = 0; | ||
for (int i = 0; i < reformattedCode.length(); i++) { | ||
int charValue = Character.getNumericValue(reformattedCode.charAt(i)); | ||
if (charValue < 0 || charValue > 35) { | ||
throw new Exception("Invalid Character[" + i + "] = '" + charValue + "'"); | ||
} | ||
total = (charValue > 9 ? total * 100 : total * 10) + charValue; | ||
if (total > MAX) { | ||
total = (total % MODULUS); | ||
} | ||
} | ||
return (int) (total % MODULUS); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
hr-microservice-hexagonal/src/main/java/com/example/validation/StrongPassword.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,25 @@ | ||
package com.example.validation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
import javax.validation.constraints.Pattern; | ||
import javax.validation.constraints.Size; | ||
|
||
@Target({ ElementType.FIELD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Pattern.List({ @Pattern(regexp = "^.*\\d+.*$", message = "{validation.strongPassword2}"), | ||
@Pattern(regexp = "^.*[-_]+.*$", message = "{validation.strongPassword3}") }) | ||
@Size(min = 6) | ||
@Constraint(validatedBy = {}) | ||
public @interface StrongPassword { | ||
String message() default "{validation.strongPassword1}"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
} |
20 changes: 20 additions & 0 deletions
20
hr-microservice-hexagonal/src/main/java/com/example/validation/TcKimlikNo.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,20 @@ | ||
package com.example.validation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
|
||
@Target({ ElementType.FIELD, ElementType.PARAMETER }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Constraint(validatedBy = TcKimlikNoValidator.class) | ||
public @interface TcKimlikNo { | ||
String message() default "{validation.identityNo}"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
} |
Oops, something went wrong.