{"id":6500,"date":"2017-11-14T09:24:40","date_gmt":"2017-11-14T14:24:40","guid":{"rendered":"http:\/\/appcrawler.com\/wordpress\/?p=6500"},"modified":"2017-11-14T14:13:22","modified_gmt":"2017-11-14T19:13:22","slug":"simple-jboss-fusekaraf-camel-route-deployment-part-2","status":"publish","type":"post","link":"http:\/\/appcrawler.com\/wordpress\/2017\/11\/14\/simple-jboss-fusekaraf-camel-route-deployment-part-2\/","title":{"rendered":"Simple JBOSS Fuse\/Karaf Camel route deployment, part 2"},"content":{"rendered":"<p>A <a href=\/wordpress\/2017\/01\/04\/simple-jboss-fusekaraf-camel-route-deployment\/ target=_blank>previous post<\/a> provided a simple example of a karaf deployment with a Camel route.  This one takes it a step further and provides a Camel processor that connects to another application, fetches content, and enhances it.<\/p>\n<p>The application properties file, located at FUSE base directory in etc, for example, \/opt\/fuse\/etc\/com.foobar.it.arch.poc.ams.ams-response.cfg<\/p>\n<pre>\r\nCamelHttpMethod=POST\r\nAuthorization=***********\r\nContent-Type=application\/json\r\nAccept-Language=en-US\r\nApplication-Key=*******\r\nOrganization-Key=******\r\nEnterpriseUnitId=******\r\nDeviceId=1\r\nUserId=*****\r\n<\/pre>\n<p>&#8230;and the camel context file in the build directory tree, for example, src\/main\/resources\/OSGI-INF\/blueprint\/camel-context.xml&#8230;<\/p>\n<pre>\r\n<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n<blueprint xmlns=\"http:\/\/www.osgi.org\/xmlns\/blueprint\/v1.0.0\"\r\n           xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n           xmlns:cm=\"http:\/\/aries.apache.org\/blueprint\/xmlns\/blueprint-cm\/v1.1.0\"\r\n           xsi:schemaLocation=\"\r\n            http:\/\/www.osgi.org\/xmlns\/blueprint\/v1.0.0 http:\/\/www.osgi.org\/xmlns\/blueprint\/v1.0.0\/blueprint.xsd\r\n            http:\/\/cxf.apache.org\/blueprint\/jaxws http:\/\/cxf.apache.org\/schemas\/blueprint\/jaxws.xsd\r\n            http:\/\/cxf.apache.org\/blueprint\/core http:\/\/cxf.apache.org\/schemas\/blueprint\/core.xsd\r\n           \">\r\n\r\n   <bean id=\"enhanceAmsReponse\" class=\"com.foobar.it.arch.poc.ams.enhanceAmsResponse\"\/>\r\n\r\n   <camelContext xmlns=\"http:\/\/camel.apache.org\/schema\/blueprint\" id=\"com.foobar.it.arch.poc.ams.ams-response\">\r\n     <propertyPlaceholder id=\"properties\" location=\"file:\/opt\/fuse\/etc\/com.foobar.it.arch.poc.ams.ams-response.cfg\"\/>\r\n     <route>\r\n       <from uri=\"jetty:http:\/\/0.0.0.0:8888\/ams\"\/>\r\n         <setHeader headerName=\"CamelHttpMethod\"><simple>${properties:CamelHttpMethod}<\/simple><\/setHeader>\r\n         <setHeader headerName=\"Authorization\"><simple>${properties:Authorization}<\/simple><\/setHeader>\r\n         <setHeader headerName=\"Content-Type\"><simple>${properties:Content-Type}<\/simple><\/setHeader>\r\n         <setHeader headerName=\"Accept-Language\"><simple>${properties:Accept-Language}<\/simple><\/setHeader>\r\n         <setHeader headerName=\"Application-Key\"><simple>${properties:Application-Key}<\/simple><\/setHeader>\r\n         <setHeader headerName=\"Organization-Key\"><simple>${properties:Organization-Key}<\/simple><\/setHeader>\r\n         <setHeader headerName=\"EnterpriseUnitId\"><simple>${properties:EnterpriseUnitId}<\/simple><\/setHeader>\r\n         <setHeader headerName=\"DeviceId\"><simple>${properties:DeviceId}<\/simple><\/setHeader>\r\n         <setHeader headerName=\"UserId\"><simple>${properties:UserId}<\/simple><\/setHeader>\r\n       <process ref=\"enhanceAmsReponse\"\/>\r\n     <\/route>\r\n   <\/camelContext>\r\n<\/blueprint>\r\n<\/pre>\n<p>&#8230;and the java code that does the work, also in the build tree, for example, src\/main\/java\/com\/foobar\/it\/arch\/poc\/ams\/enhanceAmsResponse.java&#8230;<\/p>\n<pre>\r\npackage com.foobar.it.arch.poc.ams;\r\n\r\nimport org.apache.camel.Exchange;\r\nimport org.apache.camel.Processor;\r\n\r\nimport org.apache.http.entity.*;\r\nimport org.apache.http.*;\r\nimport org.apache.http.client.HttpClient;\r\nimport org.apache.http.impl.client.*;\r\nimport org.apache.http.util.EntityUtils;\r\nimport org.apache.http.client.entity.*;\r\nimport org.apache.http.client.methods.*;\r\n\r\nimport java.util.*;\r\nimport java.io.*;\r\n\r\nimport org.apache.commons.io.IOUtils;\r\n\r\nimport com.google.gson.*;\r\n\r\nimport com.fasterxml.jackson.databind.*;\r\n\r\npublic class enhanceAmsResponse implements Processor {\r\n\r\n  @Override\r\n  public void process(Exchange exchange) throws Exception {\r\n    try {\r\n      CloseableHttpClient client = HttpClients.createDefault();\r\n      HttpPost httpPost = new HttpPost(\"http:\/\/foobar.domain:8080\/rewards\/basket-rewards\/1.0\/calculate\/1.1\");\r\n      Map<String, Object> map = exchange.getIn().getHeaders();\r\n      httpPost.setHeader(\"CamelHttpMethod\",map.get(\"CamelHttpMethod\").toString());\r\n      httpPost.setHeader(\"Authorization\",map.get(\"Authorization\").toString());\r\n      httpPost.setHeader(\"Content-Type\",map.get(\"Content-Type\").toString());\r\n      httpPost.setHeader(\"Accept-Language\",map.get(\"Accept-Language\").toString());\r\n      httpPost.setHeader(\"Application-Key\",map.get(\"Application-Key\").toString());\r\n      httpPost.setHeader(\"Organization-Key\",map.get(\"Organization-Key\").toString());\r\n      httpPost.setHeader(\"EnterpriseUnitId\",map.get(\"EnterpriseUnitId\").toString());\r\n      httpPost.setHeader(\"DeviceId\",map.get(\"DeviceId\").toString());\r\n      httpPost.setHeader(\"UserId\",map.get(\"UserId\").toString());\r\n\r\n      String msg1 = exchange.getIn().getBody(String.class);\r\n      httpPost.setEntity(new StringEntity(msg1));\r\n\r\n      CloseableHttpResponse response = client.execute(httpPost);\r\n      InputStream in = response.getEntity().getContent();\r\n      String body = IOUtils.toString(in, \"UTF-8\");\r\n      exchange.getOut().setBody(body, String.class);\r\n      Gson gson = new Gson();\r\n      System.out.println(gson.toJson(body));\r\n      \/\/stubbed out for testing\r\n      body = \"{\\\"items\\\":[\\\"0123456\\\",\\\"987654\\\"],\\\"basketRewards\\\":{\\\"acceptedRedemptions\\\":[],\\\"redeemablePrograms\\\":[]},\\\"promotions\\\":[]}\";\r\n      ObjectMapper objectMapper = new ObjectMapper();\r\n      AmsResponse amsResponse = objectMapper.readValue(body, AmsResponse.class);\r\n\r\n      \/\/do something with the object at this point.\r\n\r\n      client.close();\r\n    }\r\n    catch (Exception e) {\r\n      e.printStackTrace();\r\n    }\r\n  }\r\n}\r\n<\/pre>\n<p>..and the object to which we map our JSON returned from the other application&#8230;<\/p>\n<pre>\r\npackage com.foobar.it.arch.poc.ams;\r\n\r\nimport java.util.*;\r\n\r\n    \/\/stub = {\"items\":[],\"basketRewards\":{\"acceptedRedemptions\":[],\"redeemablePrograms\":[]},\"promotions\":[]}\r\n\r\npublic class AmsResponse {\r\n\r\n  String items[];\r\n  String acceptedRedemptions[];\r\n  String redeemablePrograms[];\r\n  String promotions[];\r\n  HashMap basketRewards;\r\n  \/\/AmsResponse(String items[],String acceptedRedemptions[],String redeemablePrograms[],String promotions[]) {\r\n  AmsResponse() {\r\n    \/\/default constructor\r\n  }\r\n\r\n  public void setItems(String items[]) {\r\n    this.items = items;\r\n  }\r\n\r\n  public String[] getItems() {\r\n    return this.items;\r\n  }\r\n\r\n  public String[] getAcceptedRedemptions() {\r\n    return this.acceptedRedemptions;\r\n  }\r\n\r\n  public void setAcceptedRedemptions(String acceptedRedemptions[]) {\r\n    this.acceptedRedemptions = acceptedRedemptions;\r\n  }\r\n\r\n  public String[]  getRedeemablePrograms() {\r\n    return this.redeemablePrograms;\r\n  }\r\n\r\n  public void setRedeemablePrograms(String redeemablePrograms[]) {\r\n    this.redeemablePrograms = redeemablePrograms;\r\n  }\r\n\r\n  public void setPromotions(String promotions[]) {\r\n    this.promotions = promotions;\r\n  }\r\n\r\n  public String[] getPromotions() {\r\n    return this.promotions;\r\n  }\r\n\r\n  public void setBasketRewards(HashMap basketRewards) {\r\n    this.basketRewards = basketRewards;\r\n  }\r\n\r\n  public HashMap getBasketRewards() {\r\n    return this.basketRewards;\r\n  }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A previous post provided a simple example of a karaf deployment with a Camel route. This one takes it a step further and provides a Camel processor that connects to another application, fetches content, and enhances it. The application properties&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/appcrawler.com\/wordpress\/2017\/11\/14\/simple-jboss-fusekaraf-camel-route-deployment-part-2\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[70,69,7,80],"tags":[],"_links":{"self":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/6500"}],"collection":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/comments?post=6500"}],"version-history":[{"count":10,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/6500\/revisions"}],"predecessor-version":[{"id":6510,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/6500\/revisions\/6510"}],"wp:attachment":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/media?parent=6500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/categories?post=6500"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/tags?post=6500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}