You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Alex Florentino <fl...@gmail.com> on 2008/02/19 02:18:14 UTC

Hard problem with tomcat 4.1 and jwsdp-1.3

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.io.IOException;
import java.io.OutputStream;
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 ReceivingServlet.java 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 {
        super.init(config);
        try {

            messageFactory = MessageFactory.newInstance();

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

    }

    /**
     * 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
response)
                            throws ServletException, IOException {

        try {

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




//System.out.println("request["+request.getInputStream().available()+"]");

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



            // 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
().getEnvelope().getBody().hasFault();
                response.setStatus(hasFault ?

HttpServletResponse.SC_INTERNAL_SERVER_ERROR :
                                    HttpServletResponse.SC_OK);

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

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

                // Send the completed message
                OutputStream os = response.getOutputStream();
                reply.writeTo(os);
                os.flush();
            } else {
                // No reply - set the HTTP status to indicate this
                response.setStatus(HttpServletResponse.SC_NO_CONTENT);
            }
        } 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
SOAPException;

    /**
     * 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)iter.next();
            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) {
                    buffer.append(',');
                }
                buffer.append(headerValues[i]);
            }
            response.setHeader(headerName, buffer.toString());
        }
    }
}


*my servlet

*package test;

import java.io.StringWriter;

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;
import javax.xml.transform.stream.StreamResult;


public class MyServletSOAP extends SAAJServlet {


    private static final long serialVersionUID = 1L;

    protected SOAPMessage onMessage(SOAPMessage message) throws
SOAPException {

        System.out.println("body:["+message.getSOAPBody()+"]");

        return message;
    }

}



*and finally my client test :*

package test;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;

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();

            test.soapClientSimpleMessage();

        } catch(Exception ex) {
            //System.out.println("exception:["+ex.getMessage()+"]");
            ex.printStackTrace();
        }

    }

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

        SOAPConnectionFactory connectionFactory =
SOAPConnectionFactory.newInstance();

        SOAPConnection connection = connectionFactory.createConnection();


        SOAPMessage message = messageFactory.createMessage();


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



        envelope.addNamespaceDeclaration("pedido", "
http://www.alexflorentino.com/pedido");

        SOAPBody body = envelope.getBody();



        DocumentBuilderFactory dbFactory =
DocumentBuilderFactory.newInstance();
        dbFactory.setNamespaceAware(true);

        DocumentBuilder builder = dbFactory.newDocumentBuilder();

        PedidoDocument pedidoDoc = PedidoDocument.Factory.parse(new
File("conf/pedido.xml"));


        Document document = builder.parse(new InputSource(new StringReader(
pedidoDoc.toString())));

        body.addDocument(document);

        java.net.URL endpoint = new java.net.URL("
http://localhost:8090/firstsoap/first2");

        connection.call(message, endpoint);


        connection.close();

    }
}



*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="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:pedido="http://www.alexflorentino.com/pedido"><SOAP-ENV:Header/><SOAP-ENV:Body><Pedido
client_id="1" xmlns="http://www.alexflorentino.com/pedido" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="pedido.xsd">
  <Item produto_id="10" quantidade="1"/>
</Pedido></SOAP-ENV:Body></SOAP-ENV:Envelope>



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