1.
I have a StandAlone Java Client trying to Send a SOAP Message ( with
Attachment) to a Servlet deploying in Weblogic 7.0
2.
I am using only Weblogic SOAP Implementation Jars and NOT using SUN
WebServices packages.
3. I am getting the following Exception.
javax.xml.soap.SOAPException: Unable to create SOAP connection
factory: Provider com.sun.xml.messaging.client.p2p.HttpSOAPConnectionFactory
not found
at javax.xml.soap.SOAPConnectionFactory.newInstance(Unknown Source)
at SOAPTest.main(SOAPTest.java:80)
javax.xml.rpc.JAXRPCException: javax.xml.soap.SOAPException: Unable to
create SOAP connection factory: Provider
com.sun.xml.messaging.client.p2p.HttpSOAPConnectionFactory not found
at SOAPTest.main(SOAPTest.java:90)
Exception in thread "main" [ServerZipHandler] fileName=test.zip
4.
Please see my JavaClient and Servlet below.
Standalone Client
^^^^^^^^^^^^^^^^^^^
public class SOAPTest {
/** Creates a new instance of SOAPTest */
public SOAPTest() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
FileReader fr = null;
BufferedReader br = null;
String line = "";
File dir = new File("c:/temp/");
try {
// Create message factory
MessageFactory messageFactory = MessageFactory.newInstance();
// Create a message
SOAPMessage message = messageFactory.createMessage();
String fileName = "test.zip";
AttachmentPart part = message.createAttachmentPart();
part.setContentType("application/x-zip-compressed");
String fullPath = dir.getPath() + File.separator + fileName;
System.out.println("[ServerZipHandler] fileName=" + fileName);
System.out.println("[ServerZipHandler] fullPath=" + fullPath);
FileDataSource fds = new FileDataSource(fullPath);
System.out.println("[ServerZipHandler] FileDataSource=" +
fds.getName());
part.setDataHandler(new DataHandler(fds));
System.out.println("[ServerZipHandler] contentType=" +
part.getContentType());
try {
System.out.println("[ServerZipHandler]
AttachmentPart.getDataHandler().getContent().getClass().getName()="
+
part.getDataHandler().getContent().getClass().
getName());
}
catch (Exception ex) {
System.out.println("[ServerZipHandler]" + ex.toString());
ex.printStackTrace();
}
message.addAttachmentPart(part);
System.out.println("[ServerZipHandler] Added attachment...");
URL dest = new URL("http://localhost:7001/soapExample/soap");
SOAPConnectionFactory soapConnectionFactory =
SOAPConnectionFactory.
newInstance();
SOAPConnection connection =
soapConnectionFactory.createConnection();
javax.xml.messaging.URLEndpoint endpoint = new
javax.xml.messaging.URLEndpoint
("http://localhost:7001/soapExample/soap");
connection.call(message,endpoint);
}
catch (Exception e) {
e.printStackTrace();
throw new JAXRPCException(e);
}
}
}
Servlet Code
^^^^^^^^^^^^
public class SOAPServlet
extends HttpServlet {
static MessageFactory fac = null;
static {
try {
fac = MessageFactory.newInstance();
}
catch (Exception ex) {
ex.printStackTrace();
}
};
public void init(ServletConfig servletConfig) throws
ServletException {
super.init(servletConfig);
}
/** Creates a new instance of SoapServlet */
public SOAPServlet() {
}
protected void doGet(HttpServletRequest request, HttpServletResponse
response) {
process(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) {
process(request, response);
}
private void process(HttpServletRequest req, HttpServletResponse
resp) {
try {
// Get all the headers from the HTTP request
MimeHeaders headers = getHeaders(req);
// Get the body of the HTTP request
InputStream is = req.getInputStream();
// Now internalize the contents of the HTTP request
// and create a SOAPMessage
SOAPMessage msg = fac.createMessage(headers, is);
processAttachment(msg);
SOAPMessage reply = null;
reply = onMessage(msg);
if (reply != null) {
/*
* Need to call saveChanges because we're
* going to use the MimeHeaders to set HTTP
* response information. These MimeHeaders
* are generated as part of the save.
*/
if (reply.saveRequired()) {
reply.saveChanges();
}
resp.setStatus(HttpServletResponse.SC_OK);
putHeaders(reply.getMimeHeaders(), resp);
// Write out the message on the response stream
OutputStream os = resp.getOutputStream();
reply.writeTo(os);
os.flush();
} else {
resp.setStatus(
HttpServletResponse.SC_NO_CONTENT);
}
} catch (Exception ex) {
ex.printStackTrace();
/*
throw new ServletException( "SAAJ POST failed: " +
ex.getMessage());
*/
}
}
private void copyFile(InputStream is, OutputStream os) throws
IOException {
byte[] b = new byte[8192];
int nr;
while ( (nr = is.read(b)) != -1) {
os.write(b, 0, nr);
}
}
static MimeHeaders getHeaders(HttpServletRequest req) {
Enumeration enum = req.getHeaderNames();
MimeHeaders headers = new MimeHeaders();
while (enum.hasMoreElements()) {
String headerName = (String) enum.nextElement();
String headerValue = req.getHeader(headerName);
StringTokenizer values =
new StringTokenizer(headerValue, ",");
while (values.hasMoreTokens()) {
headers.addHeader(headerName,
values.nextToken().trim());
}
}
return headers;
}
static void putHeaders(MimeHeaders headers,
HttpServletResponse res) {
Iterator it = headers.getAllHeaders();
while (it.hasNext()) {
MimeHeader header = (MimeHeader) it.next();
String[] values = headers.getHeader(header.getName());
if (values.length == 1) {
res.setHeader(header.getName(), header.getValue());
}
else {
StringBuffer concat = new StringBuffer();
int i = 0;
while (i < values.length) {
if (i != 0) {
concat.append(',');
}
concat.append(values[i++]);
}
res.setHeader(header.getName(), concat.toString());
}
}
}
public SOAPMessage onMessage(SOAPMessage msg) {
SOAPMessage message = null;
try {
message = fac.createMessage();
SOAPPart part = message.getSOAPPart();
SOAPEnvelope envelope = part.getEnvelope();
SOAPBody body = envelope.getBody();
Name bodyName = envelope.createName("price-list",
"PriceList", "http://sonata.coffeebreak.com");
SOAPBodyElement list = body.addBodyElement(bodyName);
Name coffeeN = envelope.createName("coffee");
SOAPElement coffee = list.addChildElement(coffeeN);
Name coffeeNm1 = envelope.createName("coffee-name");
SOAPElement coffeeName =
coffee.addChildElement(coffeeNm1);
coffeeName.addTextNode("Arabica");
Name priceName1 = envelope.createName("price");
SOAPElement price1 = coffee.addChildElement(priceName1);
price1.addTextNode("4.50");
Name coffeeNm2 = envelope.createName("coffee-name");
SOAPElement coffeeName2 =
coffee.addChildElement(coffeeNm2);
coffeeName2.addTextNode("Espresso");
Name priceName2 = envelope.createName("price");
SOAPElement price2 = coffee.addChildElement(priceName2);
price2.addTextNode("5.00");
Name coffeeNm3 = envelope.createName("coffee-name");
SOAPElement coffeeName3 =
coffee.addChildElement(coffeeNm3);
coffeeName3.addTextNode("Dorada");
Name priceName3 = envelope.createName("price");
SOAPElement price3 = coffee.addChildElement(priceName3);
price3.addTextNode("6.00");
Name coffeeNm4 = envelope.createName("coffee-name");
SOAPElement coffeeName4 =
coffee.addChildElement(coffeeNm4);
coffeeName4.addTextNode("House Blend");
Name priceName4 = envelope.createName("price");
SOAPElement price4 = coffee.addChildElement(priceName4);
price4.addTextNode("5.00");
message.saveChanges();
} catch(Exception e) {
e.printStackTrace();
}
return message;
}
private void processAttachment(SOAPMessage msg) {
try {
if (msg.countAttachments() == 0) {
throw new JAXRPCException("** Expected attachments");
}
Iterator it = msg.getAttachments();
while (it.hasNext()) {
AttachmentPart part = (AttachmentPart) it.next();
File dir = new File("C:/tmp/");
String fileName = "testOK.zip";
System.out.println("Received file named: " + fileName);
File outFile = new File(dir, fileName);
OutputStream os = null;
InputStream myIs = null;
try {
os = new FileOutputStream(outFile);
myIs = part.getDataHandler().getInputStream();
copyFile(myIs, os);
}
catch (IOException ioe) {
ioe.printStackTrace();
throw new JAXRPCException("Exception writing file " +
fileName, ioe);
}
finally {
try {
if (myIs != null) {
myIs.close();
}
}
catch (IOException ignore) {}
try {
if (os != null) {
os.close();
}
}
catch (IOException ignore) {}
}
}
}
catch (SOAPException e) {
e.printStackTrace();
throw new JAXRPCException(e);
}
}
}