Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package dispatcherRoute;
- import domain.Product;
- import domain.Supplier;
- import java.util.List;
- import javax.swing.JOptionPane;
- import javax.swing.JPasswordField;
- import org.apache.camel.Exchange;
- import org.apache.camel.builder.RouteBuilder;
- import org.apache.camel.model.dataformat.JsonLibrary;
- import org.apache.http.auth.UsernamePasswordCredentials;
- import org.apache.http.impl.auth.BasicScheme;
- /**
- *
- * @author liuch216
- */
- public class DispatchRouteBuilder extends RouteBuilder {
- String vendUsername = "";
- String vendPassword = "";
- // getPassword("Enter your Vend password");
- UsernamePasswordCredentials vendCreds = new UsernamePasswordCredentials(vendUsername, vendPassword);
- // create the Base 64 encoded Basic Access header
- String basicAuthHeader = BasicScheme.authenticate(vendCreds, "US-ASCII", false).getValue();
- @Override
- public void configure() {
- // SimpleRegistry registry = this.getContext().getRegistry(SimpleRegistry.class);
- from("websocket://localhost:9083/consignments")
- .log("Received new message via WebSocket: ${body}")
- .to("jms:queue:shipped-consign");
- from("jms:queue:shipped-consign")
- // copy current message body into a property so we don't lose it
- .setHeader("originalConsignment").body()
- .removeHeaders("*", "originalConsignment")
- // add basic access authentication header
- .setHeader("Authorization", constant(basicAuthHeader))
- // GET requests have no body, so remove it to avoid problems
- .setBody(constant(null))
- .setHeader(Exchange.HTTP_METHOD, constant("GET"))
- // send to authentication service
- .to("http://soblinux01.otago.ac.nz:8090/vend-oauth/restricted/token")
- // copy token from response body into a header
- .convertBodyTo(String.class) // body needs to be a string so we can trim it
- // copy token into OAuth authentication header
- .setHeader("Authorization").simple("Bearer ${body.trim()}") // trim is necessary!
- .setHeader("originalBody").exchangeProperty("originalBody")
- .to("jms:queue:shipped-consign");
- from("jms:queue:shipped-consign")
- // convert JSON to domain object
- .bean(Supplier.class, "createSupplier()")
- .to("jms:queue:vend-create-consignment");
- from("jms:queue:vend-create-consignment")
- // copy current message body into a property so we don't lose it
- .setProperty("originalBody").body()
- .removeHeaders("*", "originalConsignment")
- // add basic access authentication header
- .setHeader("Authorization", constant(basicAuthHeader))
- // GET requests have no body, so remove it to avoid problems
- .setBody(constant(null))
- .setHeader(Exchange.HTTP_METHOD, constant("GET"))
- // send to authentication service
- .to("http://soblinux01.otago.ac.nz:8090/vend-oauth/restricted/token")
- // copy token from response body into a header
- .convertBodyTo(String.class) // body needs to be a string so we can trim it
- // copy token into OAuth authentication header
- .setHeader("Authorization").simple("Bearer ${body.trim()}") // trim is necessary!
- // put original message body back
- .setBody().exchangeProperty("originalBody")
- .removeProperty("originalBody")
- .to("jms:queue:shipped-consign");
- from("jms:queue:shipped-consign")
- .setProperty("originalConsignment").header("originalConsignment")
- .removeHeaders("*", "Authorization") // remove headers to stop them being sent to the service
- .setHeader(Exchange.HTTP_METHOD, constant("POST"))
- .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
- .marshal().json(JsonLibrary.Gson)
- .log("${body}")
- .to("https://localhost:8085/api/consignment")
- // .to("http4://localhost:8085/api/consignment")
- .setHeader("originalConsignment").exchangeProperty("originalConsignment")
- .to("jms:queue:new-consign-vend");
- from("jms:queue:new-consign-vend")
- .setHeader("conId").jsonpath("$.id")
- .setBody().header("originalConsignment")
- .to("jms:queue:array");
- from("jms:queue:array")
- .unmarshal().json(JsonLibrary.Gson, List.class)
- .split().body()
- .to("jms:queue:split");
- from("jms:queue:split")
- .bean(Product.class, "createProduct(${header.conId},${body[productId]}, ${body[amount]})")
- .log("${body}")
- .to("jms:queue:vend-product");
- from("jms:queue:vend-product")
- .removeHeaders("*", "Authorization") // remove headers to stop them being sent to the service
- .setHeader(Exchange.HTTP_METHOD, constant("POST"))
- .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
- .marshal().json(JsonLibrary.Gson)
- .log("${body}")
- .to("https4://localhost:8085/api/consignment_product")
- // .to("http4://localhost:8085/api/consignment")
- .to("jms:queue:vend-product-response");
- }
- public static String getPassword(String prompt) {
- JPasswordField txtPasswd = new JPasswordField();
- int resp = JOptionPane.showConfirmDialog(null, txtPasswd, prompt, JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
- if (resp == JOptionPane.OK_OPTION) {
- return new String(txtPasswd.getPassword());
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement