From 4ec80159499917973f8f56ae06878f3a62b0d962 Mon Sep 17 00:00:00 2001 From: Roshan Thomas Date: Wed, 6 Apr 2016 18:22:46 -0400 Subject: [PATCH 1/5] Spring HATEOAS support --- spring-security-rest/pom.xml | 8 +++ .../baeldung/persistence/model/Customer.java | 56 +++++++++++++++ .../org/baeldung/persistence/model/Order.java | 50 ++++++++++++++ .../web/controller/CustomerController.java | 53 ++++++++++++++ .../baeldung/web/service/CustomerService.java | 18 +++++ .../web/service/CustomerServiceImpl.java | 69 +++++++++++++++++++ 6 files changed, 254 insertions(+) create mode 100644 spring-security-rest/src/main/java/org/baeldung/persistence/model/Customer.java create mode 100644 spring-security-rest/src/main/java/org/baeldung/persistence/model/Order.java create mode 100644 spring-security-rest/src/main/java/org/baeldung/web/controller/CustomerController.java create mode 100644 spring-security-rest/src/main/java/org/baeldung/web/service/CustomerService.java create mode 100644 spring-security-rest/src/main/java/org/baeldung/web/service/CustomerServiceImpl.java diff --git a/spring-security-rest/pom.xml b/spring-security-rest/pom.xml index 48791a83c1..5928a41530 100644 --- a/spring-security-rest/pom.xml +++ b/spring-security-rest/pom.xml @@ -76,6 +76,14 @@ spring-webmvc ${org.springframework.version} + + + + + org.springframework.hateoas + spring-hateoas + 0.19.0.RELEASE + diff --git a/spring-security-rest/src/main/java/org/baeldung/persistence/model/Customer.java b/spring-security-rest/src/main/java/org/baeldung/persistence/model/Customer.java new file mode 100644 index 0000000000..16b482ecf6 --- /dev/null +++ b/spring-security-rest/src/main/java/org/baeldung/persistence/model/Customer.java @@ -0,0 +1,56 @@ +package org.baeldung.persistence.model; + +import java.util.Map; + +import org.springframework.hateoas.ResourceSupport; + +public class Customer extends ResourceSupport { + private String customerId; + private String customerName; + private String companyName; + private Map orders; + + public Customer() { + super(); + } + + public Customer(final String customerId, final String customerName, final String companyName) { + super(); + this.customerId = customerId; + this.customerName = customerName; + this.companyName = companyName; + } + + public String getCustomerId() { + return customerId; + } + + public void setCustomerId(final String customerId) { + this.customerId = customerId; + } + + public String getCustomerName() { + return customerName; + } + + public void setCustomerName(final String customerName) { + this.customerName = customerName; + } + + public String getCompanyName() { + return companyName; + } + + public void setCompanyName(final String companyName) { + this.companyName = companyName; + } + + public Map getOrders() { + return orders; + } + + public void setOrders(final Map orders) { + this.orders = orders; + } + +} diff --git a/spring-security-rest/src/main/java/org/baeldung/persistence/model/Order.java b/spring-security-rest/src/main/java/org/baeldung/persistence/model/Order.java new file mode 100644 index 0000000000..ca551423e8 --- /dev/null +++ b/spring-security-rest/src/main/java/org/baeldung/persistence/model/Order.java @@ -0,0 +1,50 @@ +package org.baeldung.persistence.model; + +import org.springframework.hateoas.ResourceSupport; + +public class Order extends ResourceSupport { + private String orderId; + private double price; + private int quantity; + + public Order() { + super(); + } + + public Order(final String orderId, final double price, final int quantity) { + super(); + this.orderId = orderId; + this.price = price; + this.quantity = quantity; + } + + public String getOrderId() { + return orderId; + } + + public void setOrderId(final String orderId) { + this.orderId = orderId; + } + + public double getPrice() { + return price; + } + + public void setPrice(final double price) { + this.price = price; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(final int quantity) { + this.quantity = quantity; + } + + @Override + public String toString() { + return "Order [orderId=" + orderId + ", price=" + price + ", quantity=" + quantity + "]"; + } + +} \ No newline at end of file diff --git a/spring-security-rest/src/main/java/org/baeldung/web/controller/CustomerController.java b/spring-security-rest/src/main/java/org/baeldung/web/controller/CustomerController.java new file mode 100644 index 0000000000..ffc3921623 --- /dev/null +++ b/spring-security-rest/src/main/java/org/baeldung/web/controller/CustomerController.java @@ -0,0 +1,53 @@ +package org.baeldung.web.controller; + +import java.util.List; + +import org.baeldung.persistence.model.Customer; +import org.baeldung.persistence.model.Order; +import org.baeldung.web.service.CustomerService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.hateoas.Link; +import org.springframework.hateoas.mvc.ControllerLinkBuilder; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class CustomerController { + @Autowired + private CustomerService customerService; + + @RequestMapping(value = "/customer/{customerId}", method = RequestMethod.GET) + public Customer getCustomerById(@PathVariable final String customerId) { + return customerService.getCustomerDetail(customerId); + } + + @RequestMapping(value = "/customer/{customerId}/{orderId}", method = RequestMethod.GET) + public Order getOrderByIdForCustomer(@PathVariable final String customerId, @PathVariable final String orderId) { + return customerService.getOrderByIdForCustomer(customerId, orderId); + } + + @RequestMapping(value = "/customer/{customerId}/orders", method = RequestMethod.GET) + public List getOrdersForCustomer(@PathVariable final String customerId) { + return customerService.getAllOrdersForCustomer(customerId); + } + + @RequestMapping(value = "/customers", method = RequestMethod.GET) + public List getAllCustomers() { + final List allCustomers = customerService.allCustomers(); + for (final Customer customer : allCustomers) { + final Link selfLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getCustomerById(customer.getCustomerId())).withSelfRel(); + customer.add(selfLink); + final Link ordersLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrdersForCustomer(customer.getCustomerId())).withRel("allOrders"); + customer.add(ordersLink); + + for (final Order order : customer.getOrders().values()) { + final Link orderLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrderByIdForCustomer(customer.getCustomerId(), order.getOrderId())).withRel("order"); + order.add(orderLink); + } + } + return allCustomers; + } + +} diff --git a/spring-security-rest/src/main/java/org/baeldung/web/service/CustomerService.java b/spring-security-rest/src/main/java/org/baeldung/web/service/CustomerService.java new file mode 100644 index 0000000000..abad3ac0c6 --- /dev/null +++ b/spring-security-rest/src/main/java/org/baeldung/web/service/CustomerService.java @@ -0,0 +1,18 @@ +package org.baeldung.web.service; + +import java.util.List; + +import org.baeldung.persistence.model.Customer; +import org.baeldung.persistence.model.Order; + +public interface CustomerService { + + List allCustomers(); + + Customer getCustomerDetail(final String id); + + List getAllOrdersForCustomer(String customerId); + + Order getOrderByIdForCustomer(String customerId, String orderId); + +} diff --git a/spring-security-rest/src/main/java/org/baeldung/web/service/CustomerServiceImpl.java b/spring-security-rest/src/main/java/org/baeldung/web/service/CustomerServiceImpl.java new file mode 100644 index 0000000000..621abcdf1f --- /dev/null +++ b/spring-security-rest/src/main/java/org/baeldung/web/service/CustomerServiceImpl.java @@ -0,0 +1,69 @@ +package org.baeldung.web.service; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import org.baeldung.persistence.model.Customer; +import org.baeldung.persistence.model.Order; +import org.springframework.stereotype.Service; + +@Service +public class CustomerServiceImpl implements CustomerService { + + private HashMap customerMap; + private HashMap customerOneOrderMap; + private HashMap customerTwoOrderMap; + + public CustomerServiceImpl() { + + customerMap = new HashMap<>(); + customerOneOrderMap = new HashMap<>(); + customerTwoOrderMap = new HashMap<>(); + + final Customer customerOne = new Customer("10A", "Jane", "ABC Company"); + final Customer customerTwo = new Customer("20B", "Bob", "XYZ Company"); + + customerOneOrderMap.put("001A", new Order("001A", 150.00, 25)); + customerOneOrderMap.put("002A", new Order("002A", 250.00, 15)); + + customerTwoOrderMap.put("002B", new Order("002B", 550.00, 325)); + customerTwoOrderMap.put("002B", new Order("002B", 450.00, 525)); + + customerOne.setOrders(customerOneOrderMap); + customerTwo.setOrders(customerTwoOrderMap); + customerMap.put("10A", customerOne); + customerMap.put("20B", customerTwo); + + } + + @Override + public List allCustomers() { + return new ArrayList<>(customerMap.values()); + } + + @Override + public Customer getCustomerDetail(final String customerId) { + return customerMap.get(customerId); + } + + @Override + public List getAllOrdersForCustomer(final String customerId) { + return new ArrayList<>(customerMap.get(customerId).getOrders().values()); + } + + @Override + public Order getOrderByIdForCustomer(final String customerId, final String orderId) { + + final List orders = (List) customerMap.get(customerId).getOrders().values(); + Order selectedOrder = null; + for (final Order order : orders) { + if (order.getId().equals(orderId)) { + selectedOrder = order; + } + } + return selectedOrder; + + } + +} From cdeb7147b4ff6104c8e0ed650b2ce61a688f4c70 Mon Sep 17 00:00:00 2001 From: Roshan Thomas Date: Wed, 6 Apr 2016 23:45:35 -0400 Subject: [PATCH 2/5] Spring HATEOAS support - multiple links --- .../main/java/org/baeldung/spring/SecurityJavaConfig.java | 2 ++ .../org/baeldung/web/controller/CustomerController.java | 7 ++++--- .../java/org/baeldung/web/service/CustomerServiceImpl.java | 5 +++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/spring-security-rest/src/main/java/org/baeldung/spring/SecurityJavaConfig.java b/spring-security-rest/src/main/java/org/baeldung/spring/SecurityJavaConfig.java index 9abe604def..bb51f5fd17 100644 --- a/spring-security-rest/src/main/java/org/baeldung/spring/SecurityJavaConfig.java +++ b/spring-security-rest/src/main/java/org/baeldung/spring/SecurityJavaConfig.java @@ -43,6 +43,8 @@ public class SecurityJavaConfig extends WebSecurityConfigurerAdapter { .and() .authorizeRequests() .antMatchers("/api/csrfAttacker*").permitAll() + .antMatchers("/api/customers**").permitAll() + .antMatchers("/api/customer/**").permitAll() .antMatchers("/api/**").authenticated() .and() .formLogin() diff --git a/spring-security-rest/src/main/java/org/baeldung/web/controller/CustomerController.java b/spring-security-rest/src/main/java/org/baeldung/web/controller/CustomerController.java index ffc3921623..ac63a81d97 100644 --- a/spring-security-rest/src/main/java/org/baeldung/web/controller/CustomerController.java +++ b/spring-security-rest/src/main/java/org/baeldung/web/controller/CustomerController.java @@ -39,9 +39,10 @@ public class CustomerController { for (final Customer customer : allCustomers) { final Link selfLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getCustomerById(customer.getCustomerId())).withSelfRel(); customer.add(selfLink); - final Link ordersLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrdersForCustomer(customer.getCustomerId())).withRel("allOrders"); - customer.add(ordersLink); - + if (customer.getOrders().values().size() > 0) { + final Link ordersLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrdersForCustomer(customer.getCustomerId())).withRel("allOrders"); + customer.add(ordersLink); + } for (final Order order : customer.getOrders().values()) { final Link orderLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrderByIdForCustomer(customer.getCustomerId(), order.getOrderId())).withRel("order"); order.add(orderLink); diff --git a/spring-security-rest/src/main/java/org/baeldung/web/service/CustomerServiceImpl.java b/spring-security-rest/src/main/java/org/baeldung/web/service/CustomerServiceImpl.java index 621abcdf1f..d41b532f78 100644 --- a/spring-security-rest/src/main/java/org/baeldung/web/service/CustomerServiceImpl.java +++ b/spring-security-rest/src/main/java/org/baeldung/web/service/CustomerServiceImpl.java @@ -14,15 +14,18 @@ public class CustomerServiceImpl implements CustomerService { private HashMap customerMap; private HashMap customerOneOrderMap; private HashMap customerTwoOrderMap; + private HashMap customerThreeOrderMap; public CustomerServiceImpl() { customerMap = new HashMap<>(); customerOneOrderMap = new HashMap<>(); customerTwoOrderMap = new HashMap<>(); + customerThreeOrderMap = new HashMap<>(); final Customer customerOne = new Customer("10A", "Jane", "ABC Company"); final Customer customerTwo = new Customer("20B", "Bob", "XYZ Company"); + final Customer customerThree = new Customer("30C", "Tim", "CKV Company"); customerOneOrderMap.put("001A", new Order("001A", 150.00, 25)); customerOneOrderMap.put("002A", new Order("002A", 250.00, 15)); @@ -32,8 +35,10 @@ public class CustomerServiceImpl implements CustomerService { customerOne.setOrders(customerOneOrderMap); customerTwo.setOrders(customerTwoOrderMap); + customerThree.setOrders(customerThreeOrderMap); customerMap.put("10A", customerOne); customerMap.put("20B", customerTwo); + customerMap.put("30C", customerThree); } From c6575d0bc50ef2e91cbda29d5a590fa3a1722138 Mon Sep 17 00:00:00 2001 From: RoshanThomas Date: Wed, 6 Apr 2016 23:50:08 -0400 Subject: [PATCH 3/5] formating Spring HATEOAS dependency --- spring-security-rest/pom.xml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spring-security-rest/pom.xml b/spring-security-rest/pom.xml index 5928a41530..5973f8fa5e 100644 --- a/spring-security-rest/pom.xml +++ b/spring-security-rest/pom.xml @@ -79,11 +79,11 @@ - - org.springframework.hateoas - spring-hateoas - 0.19.0.RELEASE - + + org.springframework.hateoas + spring-hateoas + 0.19.0.RELEASE + @@ -337,4 +337,4 @@ - \ No newline at end of file + From 17a72d00a3d79e9fa9072b85980e05a1d3a2cec9 Mon Sep 17 00:00:00 2001 From: Roshan Thomas Date: Sat, 9 Apr 2016 15:54:40 -0400 Subject: [PATCH 4/5] Spring HATEOAS - separating order service --- .../baeldung/persistence/model/Customer.java | 4 ++ .../web/controller/CustomerController.java | 17 ++--- .../baeldung/web/service/CustomerService.java | 5 -- .../web/service/CustomerServiceImpl.java | 35 ---------- .../baeldung/web/service/OrderService.java | 13 ++++ .../web/service/OrderServiceImpl.java | 64 +++++++++++++++++++ 6 files changed, 90 insertions(+), 48 deletions(-) create mode 100644 spring-security-rest/src/main/java/org/baeldung/web/service/OrderService.java create mode 100644 spring-security-rest/src/main/java/org/baeldung/web/service/OrderServiceImpl.java diff --git a/spring-security-rest/src/main/java/org/baeldung/persistence/model/Customer.java b/spring-security-rest/src/main/java/org/baeldung/persistence/model/Customer.java index 16b482ecf6..b302ec057a 100644 --- a/spring-security-rest/src/main/java/org/baeldung/persistence/model/Customer.java +++ b/spring-security-rest/src/main/java/org/baeldung/persistence/model/Customer.java @@ -4,6 +4,10 @@ import java.util.Map; import org.springframework.hateoas.ResourceSupport; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +@JsonInclude(Include.NON_NULL) public class Customer extends ResourceSupport { private String customerId; private String customerName; diff --git a/spring-security-rest/src/main/java/org/baeldung/web/controller/CustomerController.java b/spring-security-rest/src/main/java/org/baeldung/web/controller/CustomerController.java index ac63a81d97..e103edcc19 100644 --- a/spring-security-rest/src/main/java/org/baeldung/web/controller/CustomerController.java +++ b/spring-security-rest/src/main/java/org/baeldung/web/controller/CustomerController.java @@ -5,6 +5,7 @@ import java.util.List; import org.baeldung.persistence.model.Customer; import org.baeldung.persistence.model.Order; import org.baeldung.web.service.CustomerService; +import org.baeldung.web.service.OrderService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.hateoas.Link; import org.springframework.hateoas.mvc.ControllerLinkBuilder; @@ -18,19 +19,22 @@ public class CustomerController { @Autowired private CustomerService customerService; + @Autowired + private OrderService orderService; + @RequestMapping(value = "/customer/{customerId}", method = RequestMethod.GET) public Customer getCustomerById(@PathVariable final String customerId) { return customerService.getCustomerDetail(customerId); } @RequestMapping(value = "/customer/{customerId}/{orderId}", method = RequestMethod.GET) - public Order getOrderByIdForCustomer(@PathVariable final String customerId, @PathVariable final String orderId) { - return customerService.getOrderByIdForCustomer(customerId, orderId); + public Order getOrderById(@PathVariable final String customerId, @PathVariable final String orderId) { + return orderService.getOrderByIdForCustomer(customerId, orderId); } @RequestMapping(value = "/customer/{customerId}/orders", method = RequestMethod.GET) public List getOrdersForCustomer(@PathVariable final String customerId) { - return customerService.getAllOrdersForCustomer(customerId); + return orderService.getAllOrdersForCustomer(customerId); } @RequestMapping(value = "/customers", method = RequestMethod.GET) @@ -39,14 +43,11 @@ public class CustomerController { for (final Customer customer : allCustomers) { final Link selfLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getCustomerById(customer.getCustomerId())).withSelfRel(); customer.add(selfLink); - if (customer.getOrders().values().size() > 0) { + if (orderService.getAllOrdersForCustomer(customer.getCustomerId()).size() > 0) { final Link ordersLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrdersForCustomer(customer.getCustomerId())).withRel("allOrders"); customer.add(ordersLink); } - for (final Order order : customer.getOrders().values()) { - final Link orderLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrderByIdForCustomer(customer.getCustomerId(), order.getOrderId())).withRel("order"); - order.add(orderLink); - } + } return allCustomers; } diff --git a/spring-security-rest/src/main/java/org/baeldung/web/service/CustomerService.java b/spring-security-rest/src/main/java/org/baeldung/web/service/CustomerService.java index abad3ac0c6..da016af2d5 100644 --- a/spring-security-rest/src/main/java/org/baeldung/web/service/CustomerService.java +++ b/spring-security-rest/src/main/java/org/baeldung/web/service/CustomerService.java @@ -3,7 +3,6 @@ package org.baeldung.web.service; import java.util.List; import org.baeldung.persistence.model.Customer; -import org.baeldung.persistence.model.Order; public interface CustomerService { @@ -11,8 +10,4 @@ public interface CustomerService { Customer getCustomerDetail(final String id); - List getAllOrdersForCustomer(String customerId); - - Order getOrderByIdForCustomer(String customerId, String orderId); - } diff --git a/spring-security-rest/src/main/java/org/baeldung/web/service/CustomerServiceImpl.java b/spring-security-rest/src/main/java/org/baeldung/web/service/CustomerServiceImpl.java index d41b532f78..e179de2554 100644 --- a/spring-security-rest/src/main/java/org/baeldung/web/service/CustomerServiceImpl.java +++ b/spring-security-rest/src/main/java/org/baeldung/web/service/CustomerServiceImpl.java @@ -5,37 +5,21 @@ import java.util.HashMap; import java.util.List; import org.baeldung.persistence.model.Customer; -import org.baeldung.persistence.model.Order; import org.springframework.stereotype.Service; @Service public class CustomerServiceImpl implements CustomerService { private HashMap customerMap; - private HashMap customerOneOrderMap; - private HashMap customerTwoOrderMap; - private HashMap customerThreeOrderMap; public CustomerServiceImpl() { customerMap = new HashMap<>(); - customerOneOrderMap = new HashMap<>(); - customerTwoOrderMap = new HashMap<>(); - customerThreeOrderMap = new HashMap<>(); final Customer customerOne = new Customer("10A", "Jane", "ABC Company"); final Customer customerTwo = new Customer("20B", "Bob", "XYZ Company"); final Customer customerThree = new Customer("30C", "Tim", "CKV Company"); - customerOneOrderMap.put("001A", new Order("001A", 150.00, 25)); - customerOneOrderMap.put("002A", new Order("002A", 250.00, 15)); - - customerTwoOrderMap.put("002B", new Order("002B", 550.00, 325)); - customerTwoOrderMap.put("002B", new Order("002B", 450.00, 525)); - - customerOne.setOrders(customerOneOrderMap); - customerTwo.setOrders(customerTwoOrderMap); - customerThree.setOrders(customerThreeOrderMap); customerMap.put("10A", customerOne); customerMap.put("20B", customerTwo); customerMap.put("30C", customerThree); @@ -52,23 +36,4 @@ public class CustomerServiceImpl implements CustomerService { return customerMap.get(customerId); } - @Override - public List getAllOrdersForCustomer(final String customerId) { - return new ArrayList<>(customerMap.get(customerId).getOrders().values()); - } - - @Override - public Order getOrderByIdForCustomer(final String customerId, final String orderId) { - - final List orders = (List) customerMap.get(customerId).getOrders().values(); - Order selectedOrder = null; - for (final Order order : orders) { - if (order.getId().equals(orderId)) { - selectedOrder = order; - } - } - return selectedOrder; - - } - } diff --git a/spring-security-rest/src/main/java/org/baeldung/web/service/OrderService.java b/spring-security-rest/src/main/java/org/baeldung/web/service/OrderService.java new file mode 100644 index 0000000000..9a23488c50 --- /dev/null +++ b/spring-security-rest/src/main/java/org/baeldung/web/service/OrderService.java @@ -0,0 +1,13 @@ +package org.baeldung.web.service; + +import java.util.List; + +import org.baeldung.persistence.model.Order; + +public interface OrderService { + + List getAllOrdersForCustomer(String customerId); + + Order getOrderByIdForCustomer(String customerId, String orderId); + +} diff --git a/spring-security-rest/src/main/java/org/baeldung/web/service/OrderServiceImpl.java b/spring-security-rest/src/main/java/org/baeldung/web/service/OrderServiceImpl.java new file mode 100644 index 0000000000..1b8e7b0dde --- /dev/null +++ b/spring-security-rest/src/main/java/org/baeldung/web/service/OrderServiceImpl.java @@ -0,0 +1,64 @@ +package org.baeldung.web.service; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import org.baeldung.persistence.model.Customer; +import org.baeldung.persistence.model.Order; +import org.springframework.stereotype.Service; + +@Service +public class OrderServiceImpl implements OrderService { + + private HashMap customerMap; + private HashMap customerOneOrderMap; + private HashMap customerTwoOrderMap; + private HashMap customerThreeOrderMap; + + public OrderServiceImpl() { + + customerMap = new HashMap<>(); + customerOneOrderMap = new HashMap<>(); + customerTwoOrderMap = new HashMap<>(); + customerThreeOrderMap = new HashMap<>(); + + customerOneOrderMap.put("001A", new Order("001A", 150.00, 25)); + customerOneOrderMap.put("002A", new Order("002A", 250.00, 15)); + + customerTwoOrderMap.put("002B", new Order("002B", 550.00, 325)); + customerTwoOrderMap.put("002B", new Order("002B", 450.00, 525)); + + final Customer customerOne = new Customer("10A", "Jane", "ABC Company"); + final Customer customerTwo = new Customer("20B", "Bob", "XYZ Company"); + final Customer customerThree = new Customer("30C", "Tim", "CKV Company"); + + customerOne.setOrders(customerOneOrderMap); + customerTwo.setOrders(customerTwoOrderMap); + customerThree.setOrders(customerThreeOrderMap); + customerMap.put("10A", customerOne); + customerMap.put("20B", customerTwo); + customerMap.put("30C", customerThree); + + } + + @Override + public List getAllOrdersForCustomer(final String customerId) { + return new ArrayList<>(customerMap.get(customerId).getOrders().values()); + } + + @Override + public Order getOrderByIdForCustomer(final String customerId, final String orderId) { + + final List orders = (List) customerMap.get(customerId).getOrders().values(); + Order selectedOrder = null; + for (final Order order : orders) { + if (order.getId().equals(orderId)) { + selectedOrder = order; + } + } + return selectedOrder; + + } + +} From c2d84ae1d2125168b7f34473d97409d3cdb92ce6 Mon Sep 17 00:00:00 2001 From: Roshan Thomas Date: Fri, 15 Apr 2016 00:13:17 -0400 Subject: [PATCH 5/5] allOrders URL implementation --- .../org/baeldung/web/controller/CustomerController.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/spring-security-rest/src/main/java/org/baeldung/web/controller/CustomerController.java b/spring-security-rest/src/main/java/org/baeldung/web/controller/CustomerController.java index e103edcc19..3a14094440 100644 --- a/spring-security-rest/src/main/java/org/baeldung/web/controller/CustomerController.java +++ b/spring-security-rest/src/main/java/org/baeldung/web/controller/CustomerController.java @@ -34,7 +34,12 @@ public class CustomerController { @RequestMapping(value = "/customer/{customerId}/orders", method = RequestMethod.GET) public List getOrdersForCustomer(@PathVariable final String customerId) { - return orderService.getAllOrdersForCustomer(customerId); + final List orders = orderService.getAllOrdersForCustomer(customerId); + for (final Order order : orders) { + final Link selfLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrderById(customerId, order.getOrderId())).withSelfRel(); + order.add(selfLink); + } + return orders; } @RequestMapping(value = "/customers", method = RequestMethod.GET)