//Libraries needed to build SOAP message
import jakarta.xml.soap.SOAPMessage;
import jakarta.xml.soap.SOAPPart;
import jakarta.xml.soap.SOAPEnvelope;
import jakarta.xml.soap.SOAPBody;
import jakarta.xml.soap.SOAPElement;
import jakarta.xml.soap.MessageFactory;
import jakarta.xml.soap.AttachmentPart;
import jakarta.xml.soap.Name
//Libraries needed to work with attachments (Java Activation Framework API)
import java.net.URL;
import jakarta.activation.DataHandler;
//Libraries needed to convert the SOAP message to a JMS message and to send it
import com.sun.messaging.xml.MessageTransformer;
import com.sun.messaging.BasicConnectionFactory;
//Libraries needed to set up a JMS connection and to send a message
import jakarta.jms.TopicConnectionFactory;
import jakarta.jms.TopicConnection;
import jakarta.jms.JMSException;
import jakarta.jms.Session;
import jakarta.jms.Message;
import jakarta.jms.TopicSession;
import jakarta.jms.Topic;
import jakarta.jms.TopicPublisher;
//Define class that sends JMS message with SOAP payload
public class SendSOAPMessageWithJMS{
TopicConnectionFactory tcf = null;
TopicConnection tc = null;
TopicSession session = null;
Topic topic = null;
TopicPublisher publisher = null;
//default constructor method
public SendSOAPMessageWithJMS(String topicName){
init(topicName);
}
//Method to nitialize JMS Connection, Session, Topic, and Publisher
public void init(String topicName) {
try {
tcf = new com.sun.messaging.TopicConnectionFactory();
tc = tcf.createTopicConnection();
session = tc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
topic = session.createTopic(topicName);
publisher = session.createPublisher(topic);
}
//Method to create and send the SOAP/JMS message
public void send() throws Exception{
MessageFactory mf = MessageFactory.newInstance(); //create default factory
SOAPMessage soapMessage=mfcreateMessage(); //create SOAP message object
SOAPPart soapPart = soapMessage.getSOAPPart();//start to drill down to body
SOAPEnvelope soapEnvelope = soapPart.getEnvelope(); //first the envelope
SOAPBody soapBody = soapEnvelope.getBody();
Name myName = soapEnvelope.createName("HelloWorld", "hw",
http://www.sun.com/imq');
//name for body element
SOAPElement element = soapBody.addChildElement(myName); //add body element
element.addTextNode("Welcome to SUnOne Web Services."); //add text value
//Create an attachment with the Java Framework Activation API
URL url = new URL("http://java.sun.com/webservices/");
DataHandler dh = new DataHnadler (url);
AttachmentPart ap = soapMessage.createAttachmentPart(dh);
//Set content type and ID
ap.setContentType("text/html");
ap.setContentID('cid-001");
//Add attachment to the SOAP message
soapMessage.addAttachmentPart(ap);
soapMessage.saveChanges();
//Convert SOAP to JMS message.
Message m = MessageTransformer.SOAPMessageIntoJMSMessage
(soapMessage,session);
//Publish JMS message
publisher.publish(m);
//Close JMS connection
public void close() throws JMSException {
tc.close();
}
//Main program to send SOAP message with JMS
public static void main (String[] args) {
try {
String topicName = System.getProperty("TopicName");
if(topicName == null) {
topicName = "test";
}
SendSOAPMEssageWithJMS ssm = new SendSOAPMEssageWithJMS(topicName);
ssm.send();
ssm.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}