Advertisement
Guest User

Untitled

a guest
May 26th, 2016
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package router;
  7.  
  8. import domain.ConsignmentDetail;
  9. import com.google.gson.Gson;
  10. import dao.ConsignmentDAO;
  11. import domain.CreateOrder;
  12. import domain.Order;
  13. import domain.OrderDetail;
  14. import domain.Sale;
  15. import java.util.Collection;
  16. import java.util.List;
  17. import java.util.Map;
  18. import javax.swing.JOptionPane;
  19. import javax.swing.JPasswordField;
  20. import org.apache.activemq.ActiveMQConnectionFactory;
  21. import org.apache.camel.CamelContext;
  22. import org.apache.camel.Exchange;
  23. import org.apache.camel.Message;
  24. import org.apache.camel.Processor;
  25. import org.apache.camel.ProducerTemplate;
  26. import org.apache.camel.builder.RouteBuilder;
  27. import org.apache.camel.component.jms.JmsComponent;
  28. import org.apache.camel.impl.DefaultCamelContext;
  29. import org.apache.camel.model.dataformat.JsonLibrary;
  30.  
  31. /**
  32. *
  33. * @author kimhu707
  34. */
  35. public class CamelRouteBuilder extends RouteBuilder {
  36.  
  37. @Override
  38. public void configure(){
  39.  
  40. //Configuration
  41. // create default context
  42. CamelContext camel = new DefaultCamelContext();
  43.  
  44. // register ActiveMQ as the JMS handler
  45. ActiveMQConnectionFactory activeMqFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
  46. camel.addComponent("jms", JmsComponent.jmsComponentAutoAcknowledge(activeMqFactory));
  47.  
  48. // trust all classes being used to send serialised domain objects
  49. activeMqFactory.setTrustAllPackages(true);
  50.  
  51. // create message producer
  52. ProducerTemplate producer = camel.createProducerTemplate();
  53.  
  54. //Routes
  55. //Route for Sale update (RMI service)
  56. from("imaps://outlook.office365.com?username=kimhu707@student.otago.ac.nz"
  57. + "&password=" + getPassword("Enter you password")
  58. + "&searchTerm.subject=Vend:SaleUpdate"
  59. + "&debugMode=false" // set to true if you want to see the authentication details
  60. + "&folderName=INBOX") // change to whatever folder your Vend messages end up in
  61. .log("${body}")
  62. .to("jms:queue:sale");
  63.  
  64. from("jms:queue:sale")
  65. .unmarshal().json(JsonLibrary.Gson, Sale.class) //converting from JSON to java object. Email message is encoded in JSON
  66. .log("${body.toString()}")
  67. .to("rmi://localhost:1099/sales?remoteInterfaces=aggregation.ISaleAggregation&method=newSale");
  68.  
  69.  
  70. //Route for Inventory update. Route is only created if count is < reorder point
  71. from("imaps://outlook.office365.com?username=kimhu707@student.otago.ac.nz"
  72. + "&password=" + getPassword("Enter you password")
  73. + "&searchTerm.subject=Vend:InventoryUpdate"
  74. + "&debugMode=false" // set to true if you want to see the authentication details
  75. + "&folderName=INBOX") // change to whatever folder your Vend messages end up in
  76. .convertBodyTo(String.class)
  77. .log("${body}")
  78. .to("jms:queue:inventory-update");
  79.  
  80.  
  81.  
  82. from("jms:queue:inventory-update")
  83. .unmarshal().json(JsonLibrary.Gson, OrderDetail.class)
  84. .log("${body.toString()}")
  85. .choice()
  86. .when().simple("${body.count} <= ${body.reorder_point}")
  87. .bean(CreateOrder.class, "createOrder(${body})")
  88. .to("jms:queue:inventory")
  89. .otherwise()
  90. .to("jms:queue:no-inventory");
  91.  
  92. from("jms:queue:inventory")
  93. .removeHeaders("*") // remove headers to stop them being sent to the service
  94. .setHeader(Exchange.HTTP_METHOD, constant("POST"))
  95. .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
  96. .marshal().json(JsonLibrary.Gson)
  97. .to("http4://localhost:8083/orders");
  98.  
  99. //WebSocket for shipping a consignment
  100. ConsignmentDetail consign = new ConsignmentDetail();
  101. Gson gson = new Gson();
  102. String consignJson = gson.toJson(consign);
  103.  
  104. from("jms:queue:null") //sending a consignment request to Vend
  105. .removeHeaders("*") // remove headers to stop them being sent to the service
  106. .setHeader(Exchange.HTTP_METHOD, constant("POST"))
  107. .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
  108. .setHeader("Authorization", simple("Bearer 5j6emC3XZsPEDsjnfKaqO8:iuUnOnRwjJmp6Bx7I"))
  109. .setBody(constant(consignJson))
  110. .log("${body}")
  111. //.marshal().json(JsonLibrary.Gson)
  112. .to("https://info323.vendhq.com/api/consignment")
  113. .to("jms:queue:vend-response"); // HTTP response ends up in this queue
  114.  
  115. from("jms:queue:vend-response") //Retrieving the consignment ID from Vend response
  116. .log("Response From the Vend ${body}")
  117. .setHeader("consignmentId").jsonpath("$.id")
  118. .setBody().header("consignmentId") //setting body to consignment id
  119. .log("Vend Consignment Id: ${body}")
  120. .to("jms:queue:single-order");
  121.  
  122. //order from the ajax client via web socket
  123. from("websocket://localhost:9083/consignments/")
  124. //.marshal().json(JsonLibrary.Gson)
  125. .unmarshal().json(JsonLibrary.Gson, List.class)
  126. .split().body() //split the array of orders
  127. .log("Received new message via WebSocket: ${body}")
  128. .to("jms:queue:single-order");
  129.  
  130. from("jms:queue:single-order")
  131. //.bean(OrderDetail.class, "OrderDetail()")
  132. .log("${body}");
  133. //.to("jms:queue:products"); // at this point the message is a product object
  134.  
  135. }
  136.  
  137. public static String getPassword(String prompt) {
  138. JPasswordField txtPasswd = new JPasswordField();
  139. int resp = JOptionPane.showConfirmDialog(null, txtPasswd, prompt,
  140. JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
  141. if (resp == JOptionPane.OK_OPTION) {
  142. String password = new String(txtPasswd.getPassword());
  143. return password;
  144. }
  145. return null;
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement