package se.callista.microservices.composite.product.service; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import se.callista.microservices.composite.product.model.ProductAggregated; import se.callista.microservices.composite.product.model.Vendor; import se.callista.microservices.util.ServiceUtils; import se.callista.microservises.core.product.model.Product; import se.callista.microservises.core.recommendation.model.Recommendation; import se.callista.microservises.core.review.model.Review; import javax.ws.rs.Consumes; import javax.ws.rs.Produces; import java.util.Date; import java.util.List; import static javax.ws.rs.core.MediaType.APPLICATION_JSON; /** * Created by magnus on 04/03/15. */ @Produces(APPLICATION_JSON) @Consumes(APPLICATION_JSON) @RestController public class ProductCompositeService { private static final Logger LOG = LoggerFactory.getLogger(ProductCompositeService.class); @Autowired ProductCompositeIntegration integration; @Autowired ServiceUtils util; @RequestMapping("/") public String getProduct() { return "{\"timestamp\":\"" + new Date() + "\",\"content\":\"Hello from ProductAPi\"}"; } @RequestMapping("/product/{productId}") public ResponseEntity getProduct(@PathVariable int productId) { // 1. First get mandatory product information ResponseEntity productResult = integration.getProduct(productId); if (!productResult.getStatusCode().is2xxSuccessful()) { // We can't proceed, return whatever fault we got from the getProduct call return util.createResponse(null, productResult.getStatusCode()); } // 2. Get optional recommendations List recommendations = null; try { ResponseEntity> recommendationResult = integration.getRecommendations(productId); if (!recommendationResult.getStatusCode().is2xxSuccessful()) { // Something went wrong with getRecommendations, simply skip the recommendation-information in the response LOG.debug("Call to getRecommendations failed: {}", recommendationResult.getStatusCode()); } else { recommendations = recommendationResult.getBody(); } } catch (Throwable t) { LOG.error("getProduct error", t); throw t; } // 3. Get optional reviews ResponseEntity> reviewsResult = integration.getReviews(productId); List reviews = null; if (!reviewsResult.getStatusCode().is2xxSuccessful()) { // Something went wrong with getReviews, simply skip the review-information in the response LOG.debug("Call to getReviews failed: {}", reviewsResult.getStatusCode()); } else { reviews = reviewsResult.getBody(); } // 4. Get optional vendors ResponseEntity> vendorsResult = integration.getVendors(productId); List vendors = null; if (!vendorsResult.getStatusCode().is2xxSuccessful()) { // Something went wrong with getVendors, simply skip the vendors-information in the response LOG.debug("Call to getVendors failed: {}", vendorsResult.getStatusCode()); } else { vendors = vendorsResult.getBody(); } return util.createOkResponse(new ProductAggregated(productResult.getBody(), recommendations, reviews, vendors)); } }