001: /*
002: * Copyright (c) 2007, Sun Microsystems, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * * Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * * Neither the name of Sun Microsystems, Inc. nor the names of its contributors
015: * may be used to endorse or promote products derived from this software without
016: * specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package partnerservices.callback;
031:
032: import java.io.BufferedInputStream;
033: import java.io.IOException;
034: import java.io.InputStreamReader;
035: import java.io.LineNumberReader;
036: import java.io.OutputStream;
037: import java.net.HttpURLConnection;
038: import java.net.MalformedURLException;
039: import java.net.URL;
040: import java.rmi.RemoteException;
041: import javax.ejb.*;
042: import javax.jms.MapMessage;
043: import javax.jms.Message;
044: import javax.jms.MessageListener;
045: import javax.naming.InitialContext;
046: import javax.naming.NamingException;
047: import javax.xml.parsers.ParserConfigurationException;
048: import javax.xml.soap.SOAPException;
049: import javax.xml.transform.TransformerConfigurationException;
050: import javax.xml.transform.TransformerException;
051: import org.w3c.dom.Document;
052: import org.xml.sax.SAXException;
053:
054: public class ReservationCallbackProviderBean implements
055: MessageDrivenBean, MessageListener {
056: private MessageDrivenContext context;
057:
058: /**
059: * @see javax.ejb.MessageDrivenBean#setMessageDrivenContext(javax.ejb.MessageDrivenContext)
060: */
061: public void setMessageDrivenContext(MessageDrivenContext aContext) {
062: context = aContext;
063: }
064:
065: /**
066: * See section 15.4.4 of the EJB 2.0 specification
067: * See section 15.7.3 of the EJB 2.1 specification
068: */
069: public void ejbCreate() {
070: }
071:
072: /**
073: * @see javax.ejb.MessageDrivenBean#ejbRemove()
074: */
075: public void ejbRemove() {
076: }
077:
078: public void onMessage(Message aMessage) {
079: try {
080: String messageType, uniqueID;
081:
082: MapMessage mapMessage = (MapMessage) aMessage;
083: messageType = mapMessage.getString(MESSAGE_TYPE);
084: uniqueID = mapMessage.getString(UNIQUE_ID);
085:
086: String soapStr = "";
087: URL cbURL = null;
088:
089: // Initialise to defaults.
090: String airlineCallbackURL = "http://localhost:18181/TravelReservationService/airlineReserved";
091: String vehicleCallbackURL = "http://localhost:18181/TravelReservationService/vehicleReserved";
092: String hotelCallbackURL = "http://localhost:18181/TravelReservationService/hotelReserved";
093:
094: // Lookup URL's defined in the deployment descriptor.
095: try {
096: InitialContext ic = new InitialContext();
097: airlineCallbackURL = (String) ic
098: .lookup("java:comp/env/AirlineCallbackURL");
099: vehicleCallbackURL = (String) ic
100: .lookup("java:comp/env/VehicleCallbackURL");
101: hotelCallbackURL = (String) ic
102: .lookup("java:comp/env/HotelCallbackURL");
103: } catch (NamingException ne) {
104: }
105:
106: if (messageType.equals(AIRLINE_RESERVATION)) {
107: cbURL = new URL(airlineCallbackURL);
108: soapStr = createSOAPString(AIRLINE_RESERVATION_SIM_FILE);
109:
110: } else if (messageType.equals(VEHICLE_RESERVATION)) {
111: cbURL = new URL(vehicleCallbackURL);
112: soapStr = createSOAPString(VEHICLE_RESERVATION_SIM_FILE);
113:
114: } else if (messageType.equals(HOTEL_RESERVATION)) {
115: cbURL = new URL(hotelCallbackURL);
116: soapStr = createSOAPString(HOTEL_RESERVATION_SIM_FILE);
117: } else {
118: return;
119: }
120: soapStr = soapStr.replaceAll("ENTER_ID_HERE", uniqueID);
121: sendSOAPMsg(cbURL, soapStr);
122:
123: } catch (javax.jms.JMSException jmse) {
124: jmse.printStackTrace();
125: }
126:
127: catch (SOAPException soapE) {
128: soapE.printStackTrace();
129: } catch (RemoteException re) {
130: re.printStackTrace();
131: } catch (IOException ioe) {
132: ioe.printStackTrace();
133: } catch (ParserConfigurationException pce) {
134: pce.printStackTrace();
135: } catch (SAXException saxe) {
136: saxe.printStackTrace();
137: } catch (TransformerConfigurationException tce) {
138: tce.printStackTrace();
139: } catch (TransformerException te) {
140: te.printStackTrace();
141: }
142: }
143:
144: private String createSOAPString(String simulationMsgFileName)
145: throws SOAPException, IOException,
146: ParserConfigurationException, SAXException,
147: TransformerConfigurationException, TransformerException {
148:
149: BufferedInputStream source = null;
150: StringBuffer sb = new StringBuffer();
151:
152: try {
153: source = new BufferedInputStream(getClass()
154: .getResourceAsStream(simulationMsgFileName));
155:
156: LineNumberReader reader = new LineNumberReader(
157: new InputStreamReader(source));
158: String s;
159: while ((s = reader.readLine()) != null) {
160: sb.append(s);
161: sb.append("\n");
162: }
163: return sb.toString();
164: } finally {
165: if (source != null)
166: source.close();
167: }
168: }
169:
170: private void sendSOAPMsg(URL dest, String msg) throws IOException {
171: HttpURLConnection urlC = null;
172: OutputStream os = null;
173: try {
174:
175: urlC = (HttpURLConnection) dest.openConnection();
176: urlC.setDoOutput(true);
177:
178: urlC.setRequestProperty("Content-Type", "text/xml");
179: urlC.setRequestMethod("POST");
180: os = urlC.getOutputStream();
181: os.write(msg.getBytes());
182: os.flush();
183: urlC.getResponseMessage();
184: }
185:
186: catch (MalformedURLException e) {
187: e.printStackTrace();
188:
189: } finally {
190: try {
191: if (os != null)
192: os.close();
193: } catch (Exception e) {
194: }
195: }
196: }
197:
198: public static final String AIRLINE_RESERVATION = "airline";
199: public static final String VEHICLE_RESERVATION = "vehicle";
200: public static final String HOTEL_RESERVATION = "hotel";
201: public static final String AIRLINE_RESERVATION_SIM_FILE = "ItineraryPlusAirline.xml";
202: public static final String VEHICLE_RESERVATION_SIM_FILE = "ItineraryPlusVehicle.xml";
203: public static final String HOTEL_RESERVATION_SIM_FILE = "ItineraryPlusHotel.xml";
204: public static final String MESSAGE_TYPE = "MESSAGE_TYPE";
205: public static final String UNIQUE_ID = "UNIQUE_ID";
206: public static final String BPEL_PROC_NS = "http://www.sun.com/javaone/05/ItineraryReservationService";
207: public static final String OTA_NS = "http://www.opentravel.org/OTA/2003/05";
208: }
|