diff --git a/prototype/src/main/java/org/hso/ecommerce/action/shop/EnableTrackingAction.java b/prototype/src/main/java/org/hso/ecommerce/action/shop/EnableTrackingAction.java index d3e2dc7..d608dea 100644 --- a/prototype/src/main/java/org/hso/ecommerce/action/shop/EnableTrackingAction.java +++ b/prototype/src/main/java/org/hso/ecommerce/action/shop/EnableTrackingAction.java @@ -1,11 +1,13 @@ package org.hso.ecommerce.action.shop; +import org.hso.ecommerce.api.RestServiceForDelivery; import org.hso.ecommerce.entities.shop.CustomerOrder; +import org.springframework.web.client.ResourceAccessException; public class EnableTrackingAction { - public static void addTrackingInfo(CustomerOrder customerOrder) { - // TODO: - customerOrder.trackingId = "555-NASE"; + public static void addTrackingInfo(CustomerOrder customerOrder) throws ResourceAccessException { + + customerOrder.trackingId = new RestServiceForDelivery().getDeliveryID(customerOrder); } } diff --git a/prototype/src/main/java/org/hso/ecommerce/api/RestServiceForDelivery.java b/prototype/src/main/java/org/hso/ecommerce/api/RestServiceForDelivery.java index 7cfdfc5..4f118c0 100644 --- a/prototype/src/main/java/org/hso/ecommerce/api/RestServiceForDelivery.java +++ b/prototype/src/main/java/org/hso/ecommerce/api/RestServiceForDelivery.java @@ -1,8 +1,5 @@ package org.hso.ecommerce.api; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; import org.hso.ecommerce.controller.intern.customers.CustomerOrderController; import org.hso.ecommerce.entities.shop.CustomerOrder; import org.springframework.beans.factory.annotation.Autowired; @@ -10,6 +7,7 @@ import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.http.*; import org.springframework.stereotype.Service; +import org.springframework.web.client.ResourceAccessException; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; @@ -22,7 +20,7 @@ public class RestServiceForDelivery { private final RestTemplateBuilder restTemplateBuilder = null; - public String getDeliveryID(CustomerOrder customerOrder) + public String getDeliveryID(CustomerOrder customerOrder) throws ResourceAccessException { String url = "http://[::1]:8082/newDelivery"; @@ -44,7 +42,7 @@ public class RestServiceForDelivery { { return response.getBody(); } else { - return null; + throw new ResourceAccessException("Http Status wrong"); } } @@ -61,15 +59,22 @@ public class RestServiceForDelivery { HttpEntity entity = new HttpEntity<>(headers); - ResponseEntity response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, CustomerOrderController.DeliveryData.class); + ResponseEntity response; - if (response.getStatusCode() == HttpStatus.OK) + try { - return response.getBody(); - } else { - return null; + response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, CustomerOrderController.DeliveryData.class); + if (response.getStatusCode() == HttpStatus.OK) + { + return response.getBody(); + } else { + return new CustomerOrderController.DeliveryData("Postserver gerade nicht erreichbar","--:--:----"); + } + } + catch (ResourceAccessException e) + { + return new CustomerOrderController.DeliveryData("Postserver gerade nicht erreichbar","--:--:----"); } - } }