001: package packi;
002:
003: import java.io.InputStream;
004: import java.net.HttpURLConnection;
005: import java.net.MalformedURLException;
006: import java.net.URL;
007: import java.util.Calendar;
008: import java.util.LinkedList;
009: import java.util.Map;
010:
011: import javax.jbi.component.ComponentContext;
012: import javax.jbi.messaging.DeliveryChannel;
013: import javax.jbi.messaging.MessageExchange;
014: import javax.jbi.messaging.NormalizedMessage;
015: import javax.swing.text.TabExpander;
016: import javax.xml.transform.Source;
017: import javax.xml.transform.dom.DOMSource;
018: import javax.xml.transform.stream.StreamSource;
019:
020: import org.apache.commons.logging.Log;
021: import org.w3c.dom.Document;
022: import org.w3c.dom.Element;
023: import org.w3c.dom.Node;
024:
025: import com.bostechcorp.cbesb.runtime.ccsl.lib.DumpNormalizedMessage;
026: import com.bostechcorp.cbesb.runtime.ccsl.lib.IUpocInterface;
027: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.NormalizedMessageHandler;
028: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.StringSource;
029:
030: public class httpUPOC implements IUpocInterface {
031: /**
032: * The httpPres is a Upoc method.
033: * It returns a linked list of exchanges to be sent from the
034: * endpoint.
035: * @param logger - the loggger object
036: * @param rootDir - the root direcory of the SU
037: * @param componentContext - the componentContext object
038: * @param channel - the delivery channel object
039: * @param exchange - the MessageExchange object passed in
040: * @param params - the user paramaters in name/value pair
041: * @return - the LinkList containning one of many MessageExchange objects
042: * @throws Exception
043: */
044:
045: public LinkedList httpPres(Log logger, String rootDir,
046: ComponentContext componentContext, DeliveryChannel channel,
047: MessageExchange exchange, Map<String, String> params)
048: throws Exception {
049:
050: LinkedList<MessageExchange> sendList = new LinkedList<MessageExchange>();
051: logger.debug("Running the beutiful Script :) ");
052:
053: NormalizedMessage inMessage = exchange.getMessage("in");
054: NormalizedMessageHandler nmh = new NormalizedMessageHandler(
055: inMessage);
056:
057: Source content = nmh.getRecordAtIndex(0);
058:
059: String zipCode = "";
060:
061: //
062: if (content instanceof DOMSource) {
063: DOMSource domContent = (DOMSource) content;
064: Node node = domContent.getNode();
065: Element root = null;
066: if (node instanceof Element) {
067: root = (Element) node;
068: } else {
069: root = ((Document) node).getDocumentElement();
070: }
071:
072: // Node zipNode = root.getFirstChild();
073:
074: // //getting the zip code
075: // System.out.println("ROOT Node"+root.getNodeName());
076: // Node message=root.getChildNodes().item(0);
077: // Node part=message.getChildNodes().item(0);
078: // Node dataEnvelope=part.getChildNodes().item(0);
079: // Node xmlRecord=dataEnvelope.getChildNodes().item(0);
080: // Node httpData=xmlRecord.getChildNodes().item(0);
081:
082: for (int i = 0; i < root.getChildNodes().getLength(); i++) {
083: Node child = root.getChildNodes().item(i);
084: logger.debug("Http data Nodes:" + child.getNodeName());
085: if (child.getNodeName().equals("zip")) {
086: zipCode = child.getNodeValue();
087: break;
088: }
089: }
090:
091: //asking yahoo info about zip code;
092: String zipcodeInfo = askYahooAboutZip(zipCode);
093:
094: // Doctor up the zip code info with today's date
095: Calendar today = Calendar.getInstance();
096: String todayString = "" + today.get(Calendar.YEAR) + "-"
097: + (today.get(Calendar.MONTH) + 1) + "-"
098: + today.get(Calendar.DAY_OF_MONTH);
099: zipcodeInfo = zipcodeInfo.replace("<Latitude",
100: "<DateToday>" + todayString
101: + "</DateToday><Latitude");
102:
103: //update the message content
104: StringSource newStringSource = new StringSource(zipcodeInfo);
105: exchange.getMessage("in").setContent(null);
106: NormalizedMessageHandler nmh2 = new NormalizedMessageHandler(
107: exchange.getMessage("in"));
108: StreamSource newStreamSource = new StreamSource(
109: newStringSource.getInputStream());
110: nmh2.addRecord(newStreamSource);
111: nmh2.generateMessageContent();
112: logger.debug("\n\n\nNEW MESSAGE\n"
113: + DumpNormalizedMessage.dump(exchange
114: .getMessage("in")) + "\n\n\n");
115: } else {
116: logger.error("Impossible to get here");
117: }
118:
119: sendList.add(exchange);
120: return sendList;
121: }
122:
123: private String askYahooAboutZip(String zipCode) throws Exception {
124: URL url = new URL(
125: "http://api.local.yahoo.com/MapsService/V1/geocode?appid=YahooDemo&zip="
126: + zipCode);
127: HttpURLConnection huc = (HttpURLConnection) url
128: .openConnection();
129: huc.setRequestMethod("GET");
130: huc.connect();
131: InputStream is = huc.getInputStream();
132: StringBuffer response = new StringBuffer();
133: int code = huc.getResponseCode();
134: System.out.print("Yahoo response is:" + code);
135: if (code >= 200 && code < 300) {
136: int ch;
137: while ((ch = is.read()) > 0) {
138: //System.out.println((char)ch);
139: response.append((char) ch);
140: }
141: huc.disconnect();
142: return new String(response);
143: }
144:
145: return null;
146: }
147:
148: static int tabber = 0;
149:
150: private void printAllNodes(Node root) {
151: tabber++;
152: if (root.getChildNodes() != null) {
153: org.w3c.dom.NodeList nodeList = root.getChildNodes();
154: for (int i = 0; i < nodeList.getLength(); i++) {
155: //Node child=nodeList.item(i);
156: //System.out.println("Child Node"+child.getNodeName());
157: printAllNodes(nodeList.item(i));
158: }
159: } else {
160: String spaces = "";
161: for (int i = 0; i < tabber; i++) {
162: spaces += "-";
163: }
164: System.out.println("Node Name:" + spaces
165: + root.getNodeName());
166: }
167:
168: }
169:
170: }
|