tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Alex Florentino" <>
Subject Hard problem with tomcat 4.1 and jwsdp-1.3
Date Tue, 19 Feb 2008 01:18:14 GMT
Hi all,

I installed java jdk 1.4, tomcat 4.1 and jwsdp-1.3, it are configured and is
working ok.

I create a servlet that handle SOAP messages, but it have strange result.

see my codes...

*base servlet class*

package test;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.StringTokenizer;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeader;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;

 * A servlet that can be used to host a SAAJ
 * service within a web container. This is based
 * on in the JWSDP tutorial
 * examples.
public abstract class SAAJServlet extends HttpServlet {

     * The factory used to build messages
    protected MessageFactory messageFactory;

     * Initialisation - create the MessageFactory
    public void init(ServletConfig config) throws ServletException {
        try {

            messageFactory = MessageFactory.newInstance();

        } catch (SOAPException ex) {
            throw new ServletException("Failed to create MessageFactory",


     * Handles a POST request from a client. The request is assumed
     * to contain a SOAP message with the HTTP binding.
    public void doPost(HttpServletRequest request, HttpServletResponse
                            throws ServletException, IOException {

        try {

            // Get all the HTTP headers and convert them to a MimeHeaders
            MimeHeaders mimeHeaders = getMIMEHeaders(request);


            // Create a SOAPMessage from the content of the HTTP request
            SOAPMessage message = messageFactory.createMessage(mimeHeaders,

            // Let the subclass handle the message
            SOAPMessage reply = onMessage(message);

            // If there is a reply, return it to the sender.
            if (reply != null) {
                // Set OK HTTP status, unless there is a fault.
                boolean hasFault = reply.getSOAPPart
                response.setStatus(hasFault ?


                // Force generation of the MIME headers
                if (reply.saveRequired()) {

                // Copy the MIME headers to the HTTP response
                setHttpHeaders(reply.getMimeHeaders(), response);

                // Send the completed message
                OutputStream os = response.getOutputStream();
            } else {
                // No reply - set the HTTP status to indicate this
        } catch (SOAPException ex) {
            throw new ServletException("SOAPException: " + ex);

     * Method implemented by subclasses to handle a received SOAP message.
     * @param message the received SOAP message.
     * @return the reply message, or <code>null</code> if there is
     * no reply to be sent.
    protected abstract SOAPMessage onMessage(SOAPMessage message) throws

     * Creates a MIMEHeaders object from the HTTP headers
     * received with a SOAP message.
    private MimeHeaders getMIMEHeaders(HttpServletRequest request) {

        MimeHeaders mimeHeaders = new MimeHeaders();

        Enumeration enum = request.getHeaderNames();

        while (enum.hasMoreElements()) {
            String headerName = (String)enum.nextElement();
            String headerValue = request.getHeader(headerName);

            StringTokenizer st = new StringTokenizer(headerValue, ",");
            while (st.hasMoreTokens()) {
                mimeHeaders.addHeader(headerName, st.nextToken().trim());
        return mimeHeaders;

     * Converts the MIMEHeaders for a SOAP message to
     * HTTP headers in the response.
    private void setHttpHeaders(MimeHeaders mimeHeaders, HttpServletResponse
response) {
        Iterator iter = mimeHeaders.getAllHeaders();
        while (iter.hasNext()) {
            MimeHeader mimeHeader = (MimeHeader);
            String headerName = mimeHeader.getName();
            String[] headerValues = mimeHeaders.getHeader(headerName);

            int count = headerValues.length;
            StringBuffer buffer = new StringBuffer();
            for (int i = 0; i < count; i++) {
                if (i != 0) {
            response.setHeader(headerName, buffer.toString());

*my servlet

*package test;


import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;

public class MyServletSOAP extends SAAJServlet {

    private static final long serialVersionUID = 1L;

    protected SOAPMessage onMessage(SOAPMessage message) throws
SOAPException {


        return message;


*and finally my client test :*

package test;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import org.apache.xmlbeans.XmlException;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.alexflorentino.pedido.PedidoDocument;

public class Test {

    MessageFactory messageFactory;

    public  Test() throws SOAPException {

        messageFactory = MessageFactory.newInstance();

    public static void main(String[] args) throws XmlException, IOException,
UnsupportedOperationException, SOAPException {

        try {

            Test test = new Test();


        } catch(Exception ex) {


    public  void soapClientSimpleMessage() throws
UnsupportedOperationException, SOAPException, ParserConfigurationException,
SAXException, IOException, XmlException {

        SOAPConnectionFactory connectionFactory =

        SOAPConnection connection = connectionFactory.createConnection();

        SOAPMessage message = messageFactory.createMessage();

        SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();

        envelope.addNamespaceDeclaration("pedido", "");

        SOAPBody body = envelope.getBody();

        DocumentBuilderFactory dbFactory =

        DocumentBuilder builder = dbFactory.newDocumentBuilder();

        PedidoDocument pedidoDoc = PedidoDocument.Factory.parse(new

        Document document = builder.parse(new InputSource(new StringReader(

        body.addDocument(document); endpoint = new"
http://localhost:8090/firstsoap/first2");, endpoint);



*I'm using tcpmonitor and it show :*

POST /firstsoap/first2 HTTP/1.1
Content-Type: text/xml; charset="utf-8"
Content-Length: 405
SOAPAction: ""
Cache-Control: no-cache
Pragma: no-cache
User-Agent: Java/1.4.2_16
Host: localhost:8090
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive

<SOAP-ENV:Envelope xmlns:SOAP-ENV=""
client_id="1" xmlns="" xmlns:xsi="" xsi:schemaLocation="pedido.xsd">
  <Item produto_id="10" quantidade="1"/>

but the tomcat says that *message.getSOAPBody() is null , *but it not seen
correct because SOAPBody is not null.

Somebody have some idea what is happening ?

thanks a lot

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message