-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Task 44 : Revise Filter Process in product and user service
- Loading branch information
1 parent
b871186
commit 8bbb6a0
Showing
10 changed files
with
165 additions
and
13 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
3 changes: 2 additions & 1 deletion
3
...ce/src/main/java/com/springbootmicroservices/productservice/client/UserServiceClient.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
93 changes: 93 additions & 0 deletions
93
...ce/src/main/java/com/springbootmicroservices/productservice/config/FeignClientConfig.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,93 @@ | ||
package com.springbootmicroservices.productservice.config; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import feign.FeignException; | ||
import feign.Request; | ||
import feign.Response; | ||
import feign.codec.Decoder; | ||
import feign.codec.ErrorDecoder; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.HttpStatus; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.lang.reflect.Type; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
|
||
@Slf4j | ||
@Configuration | ||
public class FeignClientConfig { | ||
|
||
@Bean | ||
public Decoder feignDecoder(ObjectMapper objectMapper) { | ||
return new CustomDecoder(objectMapper); | ||
} | ||
|
||
@Bean | ||
public ErrorDecoder errorDecoder() { | ||
return new CustomErrorDecoder(); | ||
} | ||
|
||
private static class CustomDecoder implements Decoder { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
public CustomDecoder(ObjectMapper objectMapper) { | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
@Override | ||
public Object decode(Response response, Type type) throws IOException { | ||
// Handle specific HTTP status codes and throw corresponding FeignExceptions | ||
if (response.status() == HttpStatus.UNAUTHORIZED.value()) { | ||
throw new FeignException.Unauthorized("Unauthorized", response.request(), response.request().body(), response.headers()); | ||
} | ||
if (response.status() == HttpStatus.FORBIDDEN.value()) { | ||
throw new FeignException.Forbidden("Forbidden", response.request(), response.request().body(), response.headers()); | ||
} | ||
if (response.status() == HttpStatus.NOT_FOUND.value()) { | ||
throw new FeignException.NotFound("Not Found", response.request(), response.request().body(), response.headers()); | ||
} | ||
if (response.status() == HttpStatus.METHOD_NOT_ALLOWED.value()) { | ||
throw new FeignException.MethodNotAllowed("Method Not Allowed", response.request(), response.request().body(), response.headers()); | ||
} | ||
if (response.status() == HttpStatus.BAD_REQUEST.value()) { | ||
throw new FeignException.BadRequest("Bad Request", response.request(), response.request().body(), response.headers()); | ||
} | ||
|
||
// Deserialize the response body using Jackson | ||
if (response.body() != null) { | ||
InputStream inputStream = response.body().asInputStream(); | ||
return objectMapper.readValue(inputStream, objectMapper.constructType(type)); | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
|
||
private static class CustomErrorDecoder implements ErrorDecoder { | ||
|
||
@Override | ||
public Exception decode(String methodKey, Response response) { | ||
HttpStatus status = HttpStatus.valueOf(response.status()); | ||
// Handle specific HTTP status codes and return corresponding FeignExceptions | ||
if (status == HttpStatus.UNAUTHORIZED) { | ||
return new FeignException.Unauthorized("Unauthorized", response.request(), response.request().body(), response.headers()); | ||
} | ||
if (status == HttpStatus.FORBIDDEN) { | ||
return new FeignException.Forbidden("Forbidden", response.request(), response.request().body(), response.headers()); | ||
} | ||
if (status == HttpStatus.NOT_FOUND) { | ||
return new FeignException.NotFound("Not Found", response.request(), response.request().body(), response.headers()); | ||
} | ||
if (status == HttpStatus.METHOD_NOT_ALLOWED) { | ||
return new FeignException.MethodNotAllowed("Method Not Allowed", response.request(), response.request().body(), response.headers()); | ||
} | ||
return new FeignException.BadRequest("Bad Request", response.request(), response.request().body(), response.headers()); | ||
|
||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...ervice/src/main/java/com/springbootmicroservices/productservice/config/JacksonConfig.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,18 @@ | ||
package com.springbootmicroservices.productservice.config; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.springbootmicroservices.productservice.serializer.UsernamePasswordAuthenticationTokenMixin; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
|
||
@Configuration | ||
public class JacksonConfig { | ||
|
||
@Bean | ||
public ObjectMapper objectMapper() { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
mapper.addMixIn(UsernamePasswordAuthenticationToken.class, UsernamePasswordAuthenticationTokenMixin.class); | ||
return mapper; | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
...roservices/productservice/serializer/UsernamePasswordAuthenticationTokenDeserializer.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.springbootmicroservices.productservice.serializer; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
|
||
import java.io.IOException; | ||
|
||
public class UsernamePasswordAuthenticationTokenDeserializer extends JsonDeserializer<UsernamePasswordAuthenticationToken> { | ||
|
||
@Override | ||
public UsernamePasswordAuthenticationToken deserialize(JsonParser p, DeserializationContext ctxt) | ||
throws IOException, JsonProcessingException { | ||
JsonNode node = p.getCodec().readTree(p); | ||
|
||
// Assuming the response contains the necessary fields | ||
String principal = node.get("principal").asText(); | ||
String credentials = node.get("credentials").asText(); | ||
|
||
return new UsernamePasswordAuthenticationToken(principal, credentials); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...bootmicroservices/productservice/serializer/UsernamePasswordAuthenticationTokenMixin.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,11 @@ | ||
package com.springbootmicroservices.productservice.serializer; | ||
|
||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
|
||
@JsonDeserialize(using = UsernamePasswordAuthenticationTokenDeserializer.class) | ||
public class UsernamePasswordAuthenticationTokenMixin extends UsernamePasswordAuthenticationToken { | ||
public UsernamePasswordAuthenticationTokenMixin(Object principal, Object credentials) { | ||
super(principal, credentials); | ||
} | ||
} |
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
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