Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
chtrembl committed Dec 28, 2021
1 parent 1c9c7f2 commit 528a840
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public StoreApiCache(ObjectMapper objectMapper) {

@Cacheable("orders")
public Order getOrder(String id) {
log.info(String.format("Creating new order id:%s and caching it", id));
log.info(String.format("PetStoreOrderService creating new order id:%s and caching it", id));
return new Order();
}

Expand Down Expand Up @@ -93,7 +93,7 @@ public List<Product> getProducts() {
// wipe this every 12 hours... 60 secs * 60 mins * 12 hrs * 1000 (1 sec in ms)
@Scheduled(fixedRate = 60 * 60 * 12 * 1000)
public void evictAllcachesAtIntervals() {
log.info("evictAllcachesAtIntervals...");
log.info("PetStoreOrderService evictAllcachesAtIntervals...");

// should probably wipe when an order is complete or dangling, but for
// simplicity in this pet store guide, just wipe everything on a set interval...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.validation.Valid;
import javax.validation.constraints.Min;
Expand Down Expand Up @@ -65,21 +64,22 @@ public StoreApiController(ObjectMapper objectMapper, NativeWebRequest request) {
this.request = request;
}

@Override
public Optional<NativeWebRequest> getRequest() {
// should really be in an interceptor
public void conigureThreadForLogging() {
try {
this.containerEnvironment.setContainerHostName(
InetAddress.getLocalHost().getHostAddress() + "/" + InetAddress.getLocalHost().getHostName());
} catch (UnknownHostException e) {
e.printStackTrace();
log.info("PetStoreOrderService getRequest() error: " + e.getMessage());
}
MDC.put("containerHostName", this.containerEnvironment.getContainerHostName());
MDC.put("session_Id", request.getHeader("session-id"));
return Optional.ofNullable(request);
}

@RequestMapping(value = "store/info", produces = { "application/json" }, method = RequestMethod.GET)
public ResponseEntity<String> info() {
conigureThreadForLogging();

// password used for cred scan demo
String password = "foobar";
log.info("PetStoreOrderService incoming GET request to petstoreorderservice/v2/info");
Expand All @@ -105,6 +105,8 @@ public ResponseEntity<String> info() {
@Override
public ResponseEntity<Order> placeOrder(
@ApiParam(value = "order placed for purchasing the product", required = true) @Valid @RequestBody Order body) {
conigureThreadForLogging();

String acceptType = request.getHeader("Content-Type");
String contentType = request.getHeader("Content-Type");
if (acceptType != null && contentType != null && acceptType.contains("application/json")
Expand Down Expand Up @@ -177,6 +179,8 @@ public ResponseEntity<Order> placeOrder(
@Override
public ResponseEntity<Order> getOrderById(
@ApiParam(value = "ID of the order that needs to be deleted", required = true) @PathVariable("orderId") String orderId) {
conigureThreadForLogging();

String acceptType = request.getHeader("Content-Type");
String contentType = request.getHeader("Content-Type");
if (acceptType != null && contentType != null && acceptType.contains("application/json")
Expand Down Expand Up @@ -219,6 +223,8 @@ public ResponseEntity<Order> getOrderById(
}

private Product getProduct(List<Product> products, Long id) {
conigureThreadForLogging();

return products.stream().filter(product -> id.equals(product.getId())).findAny().orElse(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Optional;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
Expand Down Expand Up @@ -63,21 +62,22 @@ public PetApiController(ObjectMapper objectMapper, NativeWebRequest request) {
this.request = request;
}

@Override
public Optional<NativeWebRequest> getRequest() {
// should really be in an interceptor
public void conigureThreadForLogging() {
try {
this.containerEnvironment.setContainerHostName(
InetAddress.getLocalHost().getHostAddress() + "/" + InetAddress.getLocalHost().getHostName());
} catch (UnknownHostException e) {
e.printStackTrace();
log.info("PetStoreOrderService getRequest() error: " + e.getMessage());
}
MDC.put("containerHostName", this.containerEnvironment.getContainerHostName());
MDC.put("session_Id", request.getHeader("session-id"));
return Optional.ofNullable(request);
}

@RequestMapping(value = "pet/info", produces = { "application/json" }, method = RequestMethod.GET)
public ResponseEntity<String> info() {
conigureThreadForLogging();

// password used for cred scan demo
String password = "foobar";
log.info("PetStorePetService incoming GET request to petstorepetservice/v2/info");
Expand All @@ -90,6 +90,8 @@ public ResponseEntity<String> info() {
@Override
public ResponseEntity<List<Pet>> findPetsByStatus(
@NotNull @ApiParam(value = "Status values that need to be considered for filter", required = true, allowableValues = "available, pending, sold") @Valid @RequestParam(value = "status", required = true) List<String> status) {
conigureThreadForLogging();

String acceptType = request.getHeader("Content-Type");
String contentType = request.getHeader("Content-Type");
if (acceptType != null && contentType != null && acceptType.contains("application/json")
Expand All @@ -102,7 +104,7 @@ public ResponseEntity<List<Pet>> findPetsByStatus(
ApiUtil.setResponse(request, "application/json", petsJSON);
return new ResponseEntity<>(HttpStatus.OK);
} catch (JsonProcessingException e) {
PetApiController.log.error(e.getMessage());
PetApiController.log.error("PetStorePetService with findPetsByStatus() " + e.getMessage());
ApiUtil.setResponse(request, "application/json", e.getMessage());
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
Expand Down Expand Up @@ -136,7 +138,7 @@ public ResponseEntity<List<Pet>> findPetsByTags(
"[ { \"photoUrls\" : [ \"photoUrls\", \"photoUrls\" ], \"name\" : \"doggie\", \"id\" : 0, \"category\" : { \"name\" : \"name\", \"id\" : 6 }, \"tags\" : [ { \"name\" : \"name\", \"id\" : 1 }, { \"name\" : \"name\", \"id\" : 1 } ], \"status\" : \"available\"}, { \"photoUrls\" : [ \"photoUrls\", \"photoUrls\" ], \"name\" : \"doggie\", \"id\" : 0, \"category\" : { \"name\" : \"name\", \"id\" : 6 }, \"tags\" : [ { \"name\" : \"name\", \"id\" : 1 }, { \"name\" : \"name\", \"id\" : 1 } ], \"status\" : \"available\"} ]",
List.class), HttpStatus.NOT_IMPLEMENTED);
} catch (IOException e) {
log.error("Couldn't serialize response for content type application/json", e);
log.error("PetStorePetService Couldn't serialize response for content type application/json", e);
return new ResponseEntity<List<Pet>>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Expand All @@ -147,7 +149,7 @@ public ResponseEntity<List<Pet>> findPetsByTags(
"<Pet> <id>123456789</id> <name>doggie</name> <photoUrls> <photoUrls>aeiou</photoUrls> </photoUrls> <tags> </tags> <status>aeiou</status></Pet>",
List.class), HttpStatus.NOT_IMPLEMENTED);
} catch (IOException e) {
log.error("Couldn't serialize response for content type application/xml", e);
log.error("PetStorePetService Couldn't serialize response for content type application/xml", e);
return new ResponseEntity<List<Pet>>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Expand All @@ -165,7 +167,7 @@ public ResponseEntity<Pet> getPetById(
"{ \"photoUrls\" : [ \"photoUrls\", \"photoUrls\" ], \"name\" : \"doggie\", \"id\" : 0, \"category\" : { \"name\" : \"name\", \"id\" : 6 }, \"tags\" : [ { \"name\" : \"name\", \"id\" : 1 }, { \"name\" : \"name\", \"id\" : 1 } ], \"status\" : \"available\"}",
Pet.class), HttpStatus.NOT_IMPLEMENTED);
} catch (IOException e) {
log.error("Couldn't serialize response for content type application/json", e);
log.error("PetStorePetService Couldn't serialize response for content type application/json", e);
return new ResponseEntity<Pet>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Expand All @@ -176,7 +178,7 @@ public ResponseEntity<Pet> getPetById(
"<Pet> <id>123456789</id> <name>doggie</name> <photoUrls> <photoUrls>aeiou</photoUrls> </photoUrls> <tags> </tags> <status>aeiou</status></Pet>",
Pet.class), HttpStatus.NOT_IMPLEMENTED);
} catch (IOException e) {
log.error("Couldn't serialize response for content type application/xml", e);
log.error("PetStorePetService Couldn't serialize response for content type application/xml", e);
return new ResponseEntity<Pet>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Expand Down Expand Up @@ -213,7 +215,7 @@ public ResponseEntity<ModelApiResponse> uploadFile(
ModelApiResponse.class),
HttpStatus.NOT_IMPLEMENTED);
} catch (IOException e) {
log.error("Couldn't serialize response for content type application/json", e);
log.error("PetStorePetService Couldn't serialize response for content type application/json", e);
return new ResponseEntity<ModelApiResponse>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Optional;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
Expand Down Expand Up @@ -64,21 +63,22 @@ public ProductApiController(ObjectMapper objectMapper, NativeWebRequest request)
this.request = request;
}

@Override
public Optional<NativeWebRequest> getRequest() {
// should really be in an interceptor
public void conigureThreadForLogging() {
try {
this.containerEnvironment.setContainerHostName(
InetAddress.getLocalHost().getHostAddress() + "/" + InetAddress.getLocalHost().getHostName());
} catch (UnknownHostException e) {
e.printStackTrace();
log.info("PetStoreOrderService getRequest() error: " + e.getMessage());
}
MDC.put("containerHostName", this.containerEnvironment.getContainerHostName());
MDC.put("session_Id", request.getHeader("session-id"));
return Optional.ofNullable(request);
}

@RequestMapping(value = "product/info", produces = { "application/json" }, method = RequestMethod.GET)
public ResponseEntity<String> info() {
conigureThreadForLogging();

// password used for cred scan demo
String password = "foobar";
log.info("PetStoreProductService incoming GET request to petstoreproductservice/v2/info");
Expand All @@ -91,6 +91,8 @@ public ResponseEntity<String> info() {
@Override
public ResponseEntity<List<Product>> findProductsByStatus(
@NotNull @ApiParam(value = "Status values that need to be considered for filter", required = true, allowableValues = "available, pending, sold") @Valid @RequestParam(value = "status", required = true) List<String> status) {
conigureThreadForLogging();

String acceptType = request.getHeader("Content-Type");
String contentType = request.getHeader("Content-Type");
if (acceptType != null && contentType != null && acceptType.contains("application/json")
Expand All @@ -103,7 +105,7 @@ public ResponseEntity<List<Product>> findProductsByStatus(
ApiUtil.setResponse(request, "application/json", petsJSON);
return new ResponseEntity<>(HttpStatus.OK);
} catch (JsonProcessingException e) {
ProductApiController.log.error(e.getMessage());
ProductApiController.log.error("PetStoreProductService with findProductsByStatus() " + e.getMessage());
ApiUtil.setResponse(request, "application/json", e.getMessage());
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
Expand Down

0 comments on commit 528a840

Please sign in to comment.