0

I have written a custom SOAP client in Java and though it works, I feel the way I wrote it is very elementary. I'm not good in OOP and as you can see, all the work is done in a single class. Can you please help to improve this or give any idea.

I have read articles about creating soap clients such as this and I can say that the classes are very well organized.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;

import org.w3c.dom.Document;

public class ConInfoService {
private final static String BLANK = "";
    private final static String SOAP_SERVER = "http://server.com/ConInfoService.asmx";
    private final static String PREFIX_XSI = "xsi"; 
    private final static String PREFIX_XSD = "xsd"; 
    private final static String PREFIX_SOAP = "soap";
    private final static String URI_HOST = "server.com";
    private final static String URI_GETCULIST = "http://tempuri.org/GetCuList";
    private final static String URI_XSI = "http://www.w3.org/1999/XMLSchema-instance";
    private final static String URI_XSD = "http://www.w3.org/1999/XMLSchema";
    private final static String URI_SOAP = "http://schemas.xmlsoap.org/soap/envelope/";
    private final static String URI_TEMPURI = "http://tempuri.org/";
    private final static String HEADER_HOST = "Host";
    private final static String HEADER_SOAPACTION = "SOAPAction";

    public SOAPMessage getCuList(Date asDate,  String asConAvailable, String asConChanged, String asMso) throws Exception {
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();

            String soapServer = SOAP_SERVER;;
            SOAPMessage soapMessage = createSOAPRequest(asDate, asConAvailable, asConChanged, asMso);
            return soapConnection.call(soapMessage, soapServer);
    }

    private static SOAPMessage createSOAPRequest(Date asDate,  String asConAvailable, String asConChanged, String asMso) throws Exception {
        if (asDate != null && asConAvailable != null && asConChanged != null && asMso != null) {
            MessageFactory messageFactory = MessageFactory.newInstance();
            SOAPMessage soapMessage = messageFactory.createMessage();
            SOAPPart soapPart = soapMessage.getSOAPPart();

            SOAPEnvelope envelope = soapPart.getEnvelope();
            envelope.addNamespaceDeclaration(PREFIX_XSI, URI_XSI);
            envelope.addNamespaceDeclaration(PREFIX_XSD, URI_XSD);
            envelope.addNamespaceDeclaration(PREFIX_SOAP, URI_SOAP);

            SOAPBody soapBody = envelope.getBody();
            SOAPElement getCuListElem = soapBody.addChildElement("GetCuList", BLANK, URI_TEMPURI);
            SOAPElement asDateElem = getCuListElem.addChildElement("asDate", BLANK);
            SOAPElement asConAvailableElem = getCuListElem.addChildElement("asConAvailable", BLANK);
            SOAPElement asConChangedElem = getCuListElem.addChildElement("asConChanged", BLANK);
            SOAPElement asMsoElem = getCuListElem.addChildElement("asMso", BLANK);      

            asDateElem.addTextNode(formatDate(asDate));
            asConAvailableElem.addTextNode(asConAvailable.trim());
            asConChangedElem.addTextNode(asConChanged.trim());
            asMsoElem.addTextNode(asMso.trim());

            MimeHeaders headers = soapMessage.getMimeHeaders();
            headers.addHeader(HEADER_HOST, URI_HOST);
            headers.addHeader(HEADER_SOAPACTION, URI_GETCULIST);

            soapMessage.saveChanges();

            return soapMessage;
        }

        return null;
    }
}

1 Answer 1

5

Why don't you use JAX-WS/JAXB in a way for wsdl-based class generation? Such as this or this?

1
  • I second the use a JAX-WS implementation (or the one built into the JDK itself). If not for easy of use, JAX-WS and JAXB are just so wonderfully documented!
    – Gimby
    Nov 28, 2013 at 9:17

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.