Issue invoking web service within websphere 6.1 environment

879 views Asked by At

I'm trying to invoke a SOAP web service from within WebSphere 6.1. I can run the code fine using Apache Tomcat 6.0.36 runtime. However, with WebSphere 6.1, I get the following error:

Jan 5, 2015 7:19:23 PM com.ibm.ws.ssl.config.SSLConfigManager INFO:ssl.disable.url.hostname.verification.CWPKI0027I 
Jan 5, 2015 7:19:24 PM com.ibm.ws.channel.framework.impl.WSChannelFrameworkImpl AUDIT: chain.started 
Jan 5, 2015 7:19:25 PM com.ibm.ws.webservices.engine.PivotHandlerWrapper invoke WARNING:
WSWS3734W: Warning: Exception caught from invocation to com.ibm.ws.webservices.engine.transport.http.HTTPSender:
WebServicesFault  faultCode: 
HTTP  faultString: ( 401 ) Unauthorized faultActor: http://server.customer.com:80
faultDetail:    null:
WSWS3192E: Error: return code:  ( 401 ) Unauthorized

Here's the code I'm running, which works fine in Apache Tomcat:

public class Test {

    public void submitOrder()
    {
        try {
            // Create SOAP Connection
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();

            // Send SOAP Message to SOAP Server
            String url = "http://server.customer.com/serviceEndpoint";

            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

            // Process the SOAP Response
            printSOAPResponse(soapResponse);

            soapConnection.close();
        } catch (Exception e) {
            System.err.println("Error occurred while sending SOAP Request to Server");
            e.printStackTrace();
        }
    }

    private static SOAPMessage createSOAPRequest() throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();

        MimeHeaders hd = soapMessage.getMimeHeaders();
        String username = "xxxxx";
        String password = "xxxxx";
        byte [] auth = (username+":"+password).getBytes();
        String authorization = new String ( Base64.encodeBase64(auth) );

        System.out.println ( "authorization = " + authorization );

        hd.addHeader("Authorization", "Basic " + authorization);

        SOAPPart soapPart = soapMessage.getSOAPPart();

        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();

        // SOAP Body
        SOAPBody soapBody = envelope.getBody();
        SOAPElement ordersElem = soapBody.addChildElement("Orders");

//      Need to add two attributes to this node!
        SOAPElement orderElem = ordersElem.addChildElement("Order");

        Name codeAttributeName = envelope.createName("code");
        orderElem.addAttribute(codeAttributeName, "00001");

        Name timeAttributeName = envelope.createName("time");
        orderElem.addAttribute(timeAttributeName, "2015-01-04 12:00:00 PM");

        SOAPElement salesOrderConfElem = orderElem.addChildElement("SalesOrderConfirmationCode");
        salesOrderConfElem.addTextNode("16041");

        SOAPElement statusElem = orderElem.addChildElement("Status");
        Name statusCodeAttributeName = envelope.createName("code");
        statusElem.addAttribute(statusCodeAttributeName, "COMPLETE");

        SOAPElement entriesElem = orderElem.addChildElement("Entries");
        SOAPElement entryElem = entriesElem.addChildElement("Entry");

        SOAPElement entryNumElem = entryElem.addChildElement("EntryNumber");
        entryNumElem.addTextNode("1");

        SOAPElement productElem = entryElem.addChildElement("ProductCode");
        productElem.addTextNode("738053571");

        SOAPElement qtyElem = entryElem.addChildElement("Quantity");
        qtyElem.addTextNode("1");

        SOAPElement shippedElem = entryElem.addChildElement("Shipped");
        shippedElem.addTextNode("1");

        SOAPElement backOrderElem = entryElem.addChildElement("Backordered");
        backOrderElem.addTextNode("1");

        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", "http://sap.com/xi/WebService/soap1.1");

        soapMessage.saveChanges();

        /* Print the request message */
        System.out.print("Request SOAP Message = ");
        soapMessage.writeTo(System.out);
        System.out.println();

        return soapMessage;
    }

    /**
     * Method used to print the SOAP Response
     */
    private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        Source sourceContent = soapResponse.getSOAPPart().getContent();
        System.out.print("\nResponse SOAP Message = ");
        StreamResult result = new StreamResult(System.out);
        transformer.transform(sourceContent, result);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        Test t = new Test();

        t.submitOrder();

    }
}

CORRECTION: the SOAP envelopes being created by Tomcat and WebSphere are not the same. Tomcat, using Java 1.6, creates the following:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <Orders>

while WebSphere, using IBM's version of 1.5, creates the following:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Header/>
    <soapenv:Body>
        <Orders>

How would I get WebSphere to create the same SOAP envelop as Tomcat?

0

There are 0 answers