Advertisement
Guest User

Untitled

a guest
May 19th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.31 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 dispatcherRoute;
  7.  
  8. import domain.Product;
  9. import domain.Supplier;
  10. import java.util.List;
  11. import javax.swing.JOptionPane;
  12. import javax.swing.JPasswordField;
  13. import org.apache.camel.Exchange;
  14. import org.apache.camel.builder.RouteBuilder;
  15. import org.apache.camel.model.dataformat.JsonLibrary;
  16. import org.apache.http.auth.UsernamePasswordCredentials;
  17.  
  18. import org.apache.http.impl.auth.BasicScheme;
  19.  
  20. /**
  21. *
  22. * @author liuch216
  23. */
  24. public class DispatchRouteBuilder extends RouteBuilder {
  25.  
  26. String vendUsername = "";
  27. String vendPassword = "";
  28. // getPassword("Enter your Vend password");
  29.  
  30. UsernamePasswordCredentials vendCreds = new UsernamePasswordCredentials(vendUsername, vendPassword);
  31.  
  32. // create the Base 64 encoded Basic Access header
  33. String basicAuthHeader = BasicScheme.authenticate(vendCreds, "US-ASCII", false).getValue();
  34.  
  35.  
  36.  
  37. @Override
  38. public void configure() {
  39.  
  40. // SimpleRegistry registry = this.getContext().getRegistry(SimpleRegistry.class);
  41.  
  42. from("websocket://localhost:9083/consignments")
  43. .log("Received new message via WebSocket: ${body}")
  44. .to("jms:queue:shipped-consign");
  45.  
  46. from("jms:queue:shipped-consign")
  47. // copy current message body into a property so we don't lose it
  48. .setHeader("originalConsignment").body()
  49. .removeHeaders("*", "originalConsignment")
  50. // add basic access authentication header
  51. .setHeader("Authorization", constant(basicAuthHeader))
  52. // GET requests have no body, so remove it to avoid problems
  53. .setBody(constant(null))
  54. .setHeader(Exchange.HTTP_METHOD, constant("GET"))
  55. // send to authentication service
  56. .to("http://soblinux01.otago.ac.nz:8090/vend-oauth/restricted/token")
  57. // copy token from response body into a header
  58. .convertBodyTo(String.class) // body needs to be a string so we can trim it
  59.  
  60. // copy token into OAuth authentication header
  61. .setHeader("Authorization").simple("Bearer ${body.trim()}") // trim is necessary!
  62.  
  63. .setHeader("originalBody").exchangeProperty("originalBody")
  64.  
  65. .to("jms:queue:shipped-consign");
  66.  
  67.  
  68.  
  69.  
  70. from("jms:queue:shipped-consign")
  71. // convert JSON to domain object
  72. .bean(Supplier.class, "createSupplier()")
  73. .to("jms:queue:vend-create-consignment");
  74.  
  75.  
  76.  
  77. from("jms:queue:vend-create-consignment")
  78. // copy current message body into a property so we don't lose it
  79. .setProperty("originalBody").body()
  80. .removeHeaders("*", "originalConsignment")
  81. // add basic access authentication header
  82. .setHeader("Authorization", constant(basicAuthHeader))
  83. // GET requests have no body, so remove it to avoid problems
  84. .setBody(constant(null))
  85. .setHeader(Exchange.HTTP_METHOD, constant("GET"))
  86. // send to authentication service
  87. .to("http://soblinux01.otago.ac.nz:8090/vend-oauth/restricted/token")
  88. // copy token from response body into a header
  89. .convertBodyTo(String.class) // body needs to be a string so we can trim it
  90.  
  91. // copy token into OAuth authentication header
  92. .setHeader("Authorization").simple("Bearer ${body.trim()}") // trim is necessary!
  93.  
  94. // put original message body back
  95. .setBody().exchangeProperty("originalBody")
  96. .removeProperty("originalBody")
  97. .to("jms:queue:shipped-consign");
  98.  
  99. from("jms:queue:shipped-consign")
  100. .setProperty("originalConsignment").header("originalConsignment")
  101. .removeHeaders("*", "Authorization") // remove headers to stop them being sent to the service
  102. .setHeader(Exchange.HTTP_METHOD, constant("POST"))
  103. .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
  104. .marshal().json(JsonLibrary.Gson)
  105. .log("${body}")
  106. .to("https://localhost:8085/api/consignment")
  107. // .to("http4://localhost:8085/api/consignment")
  108. .setHeader("originalConsignment").exchangeProperty("originalConsignment")
  109. .to("jms:queue:new-consign-vend");
  110.  
  111. from("jms:queue:new-consign-vend")
  112. .setHeader("conId").jsonpath("$.id")
  113. .setBody().header("originalConsignment")
  114. .to("jms:queue:array");
  115.  
  116. from("jms:queue:array")
  117. .unmarshal().json(JsonLibrary.Gson, List.class)
  118. .split().body()
  119. .to("jms:queue:split");
  120.  
  121. from("jms:queue:split")
  122. .bean(Product.class, "createProduct(${header.conId},${body[productId]}, ${body[amount]})")
  123. .log("${body}")
  124. .to("jms:queue:vend-product");
  125.  
  126.  
  127.  
  128. from("jms:queue:vend-product")
  129. .removeHeaders("*", "Authorization") // remove headers to stop them being sent to the service
  130. .setHeader(Exchange.HTTP_METHOD, constant("POST"))
  131. .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
  132. .marshal().json(JsonLibrary.Gson)
  133. .log("${body}")
  134. .to("https4://localhost:8085/api/consignment_product")
  135. // .to("http4://localhost:8085/api/consignment")
  136. .to("jms:queue:vend-product-response");
  137.  
  138. }
  139.  
  140. public static String getPassword(String prompt) {
  141. JPasswordField txtPasswd = new JPasswordField();
  142. int resp = JOptionPane.showConfirmDialog(null, txtPasswd, prompt, JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
  143. if (resp == JOptionPane.OK_OPTION) {
  144. return new String(txtPasswd.getPassword());
  145. }
  146. return null;
  147. }
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement