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 file, located at FUSE base directory in etc, for example, /opt/fuse/etc/com.foobar.it.arch.poc.ams.ams-response.cfg
CamelHttpMethod=POST Authorization=*********** Content-Type=application/json Accept-Language=en-US Application-Key=******* Organization-Key=****** EnterpriseUnitId=****** DeviceId=1 UserId=*****
…and the camel context file in the build directory tree, for example, src/main/resources/OSGI-INF/blueprint/camel-context.xml…
${properties:CamelHttpMethod} ${properties:Authorization} ${properties:Content-Type} ${properties:Accept-Language} ${properties:Application-Key} ${properties:Organization-Key} ${properties:EnterpriseUnitId} ${properties:DeviceId} ${properties:UserId}
…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…
package com.foobar.it.arch.poc.ams; import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.http.entity.*; import org.apache.http.*; import org.apache.http.client.HttpClient; import org.apache.http.impl.client.*; import org.apache.http.util.EntityUtils; import org.apache.http.client.entity.*; import org.apache.http.client.methods.*; import java.util.*; import java.io.*; import org.apache.commons.io.IOUtils; import com.google.gson.*; import com.fasterxml.jackson.databind.*; public class enhanceAmsResponse implements Processor { @Override public void process(Exchange exchange) throws Exception { try { CloseableHttpClient client = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("http://foobar.domain:8080/rewards/basket-rewards/1.0/calculate/1.1"); Mapmap = exchange.getIn().getHeaders(); httpPost.setHeader("CamelHttpMethod",map.get("CamelHttpMethod").toString()); httpPost.setHeader("Authorization",map.get("Authorization").toString()); httpPost.setHeader("Content-Type",map.get("Content-Type").toString()); httpPost.setHeader("Accept-Language",map.get("Accept-Language").toString()); httpPost.setHeader("Application-Key",map.get("Application-Key").toString()); httpPost.setHeader("Organization-Key",map.get("Organization-Key").toString()); httpPost.setHeader("EnterpriseUnitId",map.get("EnterpriseUnitId").toString()); httpPost.setHeader("DeviceId",map.get("DeviceId").toString()); httpPost.setHeader("UserId",map.get("UserId").toString()); String msg1 = exchange.getIn().getBody(String.class); httpPost.setEntity(new StringEntity(msg1)); CloseableHttpResponse response = client.execute(httpPost); InputStream in = response.getEntity().getContent(); String body = IOUtils.toString(in, "UTF-8"); exchange.getOut().setBody(body, String.class); Gson gson = new Gson(); System.out.println(gson.toJson(body)); //stubbed out for testing body = "{\"items\":[\"0123456\",\"987654\"],\"basketRewards\":{\"acceptedRedemptions\":[],\"redeemablePrograms\":[]},\"promotions\":[]}"; ObjectMapper objectMapper = new ObjectMapper(); AmsResponse amsResponse = objectMapper.readValue(body, AmsResponse.class); //do something with the object at this point. client.close(); } catch (Exception e) { e.printStackTrace(); } } }
..and the object to which we map our JSON returned from the other application…
package com.foobar.it.arch.poc.ams; import java.util.*; //stub = {"items":[],"basketRewards":{"acceptedRedemptions":[],"redeemablePrograms":[]},"promotions":[]} public class AmsResponse { String items[]; String acceptedRedemptions[]; String redeemablePrograms[]; String promotions[]; HashMap basketRewards; //AmsResponse(String items[],String acceptedRedemptions[],String redeemablePrograms[],String promotions[]) { AmsResponse() { //default constructor } public void setItems(String items[]) { this.items = items; } public String[] getItems() { return this.items; } public String[] getAcceptedRedemptions() { return this.acceptedRedemptions; } public void setAcceptedRedemptions(String acceptedRedemptions[]) { this.acceptedRedemptions = acceptedRedemptions; } public String[] getRedeemablePrograms() { return this.redeemablePrograms; } public void setRedeemablePrograms(String redeemablePrograms[]) { this.redeemablePrograms = redeemablePrograms; } public void setPromotions(String promotions[]) { this.promotions = promotions; } public String[] getPromotions() { return this.promotions; } public void setBasketRewards(HashMap basketRewards) { this.basketRewards = basketRewards; } public HashMap getBasketRewards() { return this.basketRewards; } }