Iofacture
Iofacture

Reputation: 685

Trouble connecting java soap client to Ws-Security enabled .NET webservice

I am getting a variety of wild and wonderful errors ranging from "400 bad request" to "incorrect username/password" and "no username" and "no password or callback handler available" when trying to connect a code generated java client to a .NET soap service that has WS-Security enabled.

The following is the WSDL for the .NET service:

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:tns="http://filesubmissionservice.fset.rsi" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="FileSubmissionService" targetNamespace="http://filesubmissionservice.fset.rsi">
<wsp:Policy wsu:Id="BasicHttpsBinding_IFileSubmissionService_policy">
<wsp:ExactlyOne>
<wsp:All>
<sp:TransportBinding xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
<wsp:Policy>
<sp:TransportToken>
<wsp:Policy>
<sp:HttpsToken RequireClientCertificate="false"/>
</wsp:Policy>
</sp:TransportToken>
<sp:AlgorithmSuite>
<wsp:Policy>
<sp:Basic256/>
</wsp:Policy>
</sp:AlgorithmSuite>
<sp:Layout>
<wsp:Policy>
<sp:Lax/>
</wsp:Policy>
</sp:Layout>
<sp:IncludeTimestamp/>
</wsp:Policy>
</sp:TransportBinding>
<sp:SignedSupportingTokens xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
<wsp:Policy>
<sp:UsernameToken sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient">
<wsp:Policy>
<sp:WssUsernameToken10/>
</wsp:Policy>
</sp:UsernameToken>
</wsp:Policy>
</sp:SignedSupportingTokens>
<sp:Wss10 xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
<wsp:Policy/>
</sp:Wss10>
</wsp:All>
</wsp:ExactlyOne>
</wsp:Policy>
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://filesubmissionservice.fset.rsi">
<xs:import namespace="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Service.FileSubmissionService"/>
<xs:import namespace="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Core"/>
<xs:element name="ValidateFlatFile">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="fileName" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="file" nillable="true" type="xs:base64Binary"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="ValidateFlatFileResponse">
<xs:complexType>
<xs:sequence>
<xs:element xmlns:q1="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Service.FileSubmissionService" minOccurs="0" name="ValidateFlatFileResult" nillable="true" type="q1:SubmissionValidationResults"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="ValidateXmlFile">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="xmlContents" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="submissionType" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="schemaVersion" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="ValidateXmlFileResponse">
<xs:complexType>
<xs:sequence>
<xs:element xmlns:q2="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Service.FileSubmissionService" minOccurs="0" name="ValidateXmlFileResult" nillable="true" type="q2:SubmissionValidationResults"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="SubmitMessage">
<xs:complexType>
<xs:sequence>
<xs:element xmlns:q3="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Core" minOccurs="0" name="message" nillable="true" type="q3:MessageData"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="SubmitMessageResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="SubmitMessageResult" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.microsoft.com/2003/10/Serialization/" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/">
<xs:element name="anyType" nillable="true" type="xs:anyType"/>
<xs:element name="anyURI" nillable="true" type="xs:anyURI"/>
<xs:element name="base64Binary" nillable="true" type="xs:base64Binary"/>
<xs:element name="boolean" nillable="true" type="xs:boolean"/>
<xs:element name="byte" nillable="true" type="xs:byte"/>
<xs:element name="dateTime" nillable="true" type="xs:dateTime"/>
<xs:element name="decimal" nillable="true" type="xs:decimal"/>
<xs:element name="double" nillable="true" type="xs:double"/>
<xs:element name="float" nillable="true" type="xs:float"/>
<xs:element name="int" nillable="true" type="xs:int"/>
<xs:element name="long" nillable="true" type="xs:long"/>
<xs:element name="QName" nillable="true" type="xs:QName"/>
<xs:element name="short" nillable="true" type="xs:short"/>
<xs:element name="string" nillable="true" type="xs:string"/>
<xs:element name="unsignedByte" nillable="true" type="xs:unsignedByte"/>
<xs:element name="unsignedInt" nillable="true" type="xs:unsignedInt"/>
<xs:element name="unsignedLong" nillable="true" type="xs:unsignedLong"/>
<xs:element name="unsignedShort" nillable="true" type="xs:unsignedShort"/>
<xs:element name="char" nillable="true" type="tns:char"/>
<xs:simpleType name="char">
<xs:restriction base="xs:int"/>
</xs:simpleType>
<xs:element name="duration" nillable="true" type="tns:duration"/>
<xs:simpleType name="duration">
<xs:restriction base="xs:duration">
<xs:pattern value="\-?P(\d*D)?(T(\d*H)?(\d*M)?(\d*(\.\d*)?S)?)?"/>
<xs:minInclusive value="-P10675199DT2H48M5.4775808S"/>
<xs:maxInclusive value="P10675199DT2H48M5.4775807S"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="guid" nillable="true" type="tns:guid"/>
<xs:simpleType name="guid">
<xs:restriction base="xs:string">
<xs:pattern value="[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}"/>
</xs:restriction>
</xs:simpleType>
<xs:attribute name="FactoryType" type="xs:QName"/>
<xs:attribute name="Id" type="xs:ID"/>
<xs:attribute name="Ref" type="xs:IDREF"/>
</xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Service.FileSubmissionService" elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Service.FileSubmissionService">
<xs:import namespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
<xs:import namespace="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Core"/>
<xs:complexType name="SubmissionValidationResults">
<xs:sequence>
<xs:element xmlns:q1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" minOccurs="0" name="ResultVars" nillable="true" type="q1:ArrayOfKeyValueOfstringstring"/>
<xs:element xmlns:q2="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Core" minOccurs="0" name="Submissions" nillable="true" type="q2:ArrayOfSubmissionFormData"/>
<xs:element xmlns:q3="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Core" minOccurs="0" name="ValidationErrors" nillable="true" type="q3:ArrayOfErrorInfo"/>
</xs:sequence>
</xs:complexType>
<xs:element name="SubmissionValidationResults" nillable="true" type="tns:SubmissionValidationResults"/>
</xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.microsoft.com/2003/10/Serialization/Arrays" elementFormDefault="qualified" targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<xs:complexType name="ArrayOfKeyValueOfstringstring">
<xs:annotation>
<xs:appinfo>
<IsDictionary xmlns="http://schemas.microsoft.com/2003/10/Serialization/">true</IsDictionary>
</xs:appinfo>
</xs:annotation>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="KeyValueOfstringstring">
<xs:complexType>
<xs:sequence>
<xs:element name="Key" nillable="true" type="xs:string"/>
<xs:element name="Value" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:element name="ArrayOfKeyValueOfstringstring" nillable="true" type="tns:ArrayOfKeyValueOfstringstring"/>
<xs:complexType name="ArrayOfKeyValueOfstringanyType">
<xs:annotation>
<xs:appinfo>
<IsDictionary xmlns="http://schemas.microsoft.com/2003/10/Serialization/">true</IsDictionary>
</xs:appinfo>
</xs:annotation>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="KeyValueOfstringanyType">
<xs:complexType>
<xs:sequence>
<xs:element name="Key" nillable="true" type="xs:string"/>
<xs:element name="Value" nillable="true" type="xs:anyType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:element name="ArrayOfKeyValueOfstringanyType" nillable="true" type="tns:ArrayOfKeyValueOfstringanyType"/>
</xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Core" elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Core">
<xs:import namespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
<xs:import namespace="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Plugin"/>
<xs:complexType name="ArrayOfSubmissionFormData">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="SubmissionFormData" nillable="true" type="tns:SubmissionFormData"/>
</xs:sequence>
</xs:complexType>
<xs:element name="ArrayOfSubmissionFormData" nillable="true" type="tns:ArrayOfSubmissionFormData"/>
<xs:complexType name="SubmissionFormData">
<xs:sequence>
<xs:element minOccurs="0" name="ContainsPayment" type="xs:boolean"/>
<xs:element minOccurs="0" name="ETIN" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="ElectronicPostmark" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="FilerName" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="FormDataStr" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="IdentityNumber" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="IdentityType" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="Jurisdiction" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="MessageId" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="MessageKey" nillable="true" type="xs:int"/>
<xs:element minOccurs="0" name="MessageSource" nillable="true" type="xs:int"/>
<xs:element xmlns:q1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" minOccurs="0" name="PluginVars" nillable="true" type="q1:ArrayOfKeyValueOfstringanyType"/>
<xs:element minOccurs="0" name="ReceivedTimestamp" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="SWID" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="SchemaLocation" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="SchemaVersion" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="SoftwareId" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="Status" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="SubmissionCategory" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="SubmissionId" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="SubmissionKey" nillable="true" type="xs:int"/>
<xs:element minOccurs="0" name="SubmissionType" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="TaxYear" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:element name="SubmissionFormData" nillable="true" type="tns:SubmissionFormData"/>
<xs:complexType name="ArrayOfErrorInfo">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="ErrorInfo" nillable="true" type="tns:ErrorInfo"/>
</xs:sequence>
</xs:complexType>
<xs:element name="ArrayOfErrorInfo" nillable="true" type="tns:ArrayOfErrorInfo"/>
<xs:complexType name="ErrorInfo">
<xs:sequence>
<xs:element minOccurs="0" name="DataValue" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="DocumentId" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="ErrorCategory" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="ErrorMessage" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="RuleNumber" nillable="true" type="xs:string"/>
<xs:element xmlns:q2="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Plugin" minOccurs="0" name="Severity" type="q2:RuleSeverity"/>
<xs:element minOccurs="0" name="XPath" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="ErrorInfo" nillable="true" type="tns:ErrorInfo"/>
<xs:complexType name="MessageData">
<xs:sequence>
<xs:element minOccurs="0" name="MessageID" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="MessageKey" nillable="true" type="xs:int"/>
<xs:element minOccurs="0" name="Source" type="xs:int"/>
<xs:element minOccurs="0" name="Submissions" nillable="true" type="tns:ArrayOfSubmissionFormData"/>
</xs:sequence>
</xs:complexType>
<xs:element name="MessageData" nillable="true" type="tns:MessageData"/>
</xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Plugin" elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Plugin">
<xs:simpleType name="RuleSeverity">
<xs:restriction base="xs:string">
<xs:enumeration value="RejectAndStop"/>
<xs:enumeration value="Reject"/>
<xs:enumeration value="Alert"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="RuleSeverity" nillable="true" type="tns:RuleSeverity"/>
</xs:schema>
</wsdl:types>
<wsdl:message name="IFileSubmissionService_ValidateFlatFile_InputMessage">
<wsdl:part name="parameters" element="tns:ValidateFlatFile"/>
</wsdl:message>
<wsdl:message name="IFileSubmissionService_ValidateFlatFile_OutputMessage">
<wsdl:part name="parameters" element="tns:ValidateFlatFileResponse"/>
</wsdl:message>
<wsdl:message name="IFileSubmissionService_ValidateXmlFile_InputMessage">
<wsdl:part name="parameters" element="tns:ValidateXmlFile"/>
</wsdl:message>
<wsdl:message name="IFileSubmissionService_ValidateXmlFile_OutputMessage">
<wsdl:part name="parameters" element="tns:ValidateXmlFileResponse"/>
</wsdl:message>
<wsdl:message name="IFileSubmissionService_SubmitMessage_InputMessage">
<wsdl:part name="parameters" element="tns:SubmitMessage"/>
</wsdl:message>
<wsdl:message name="IFileSubmissionService_SubmitMessage_OutputMessage">
<wsdl:part name="parameters" element="tns:SubmitMessageResponse"/>
</wsdl:message>
<wsdl:portType name="IFileSubmissionService">
<wsdl:operation name="ValidateFlatFile">
<wsdl:input wsaw:Action="http://filesubmissionservice.fset.rsi/IFileSubmissionService/ValidateFlatFile" message="tns:IFileSubmissionService_ValidateFlatFile_InputMessage"/>
<wsdl:output wsaw:Action="http://filesubmissionservice.fset.rsi/IFileSubmissionService/ValidateFlatFileResponse" message="tns:IFileSubmissionService_ValidateFlatFile_OutputMessage"/>
</wsdl:operation>
<wsdl:operation name="ValidateXmlFile">
<wsdl:input wsaw:Action="http://filesubmissionservice.fset.rsi/IFileSubmissionService/ValidateXmlFile" message="tns:IFileSubmissionService_ValidateXmlFile_InputMessage"/>
<wsdl:output wsaw:Action="http://filesubmissionservice.fset.rsi/IFileSubmissionService/ValidateXmlFileResponse" message="tns:IFileSubmissionService_ValidateXmlFile_OutputMessage"/>
</wsdl:operation>
<wsdl:operation name="SubmitMessage">
<wsdl:input wsaw:Action="http://filesubmissionservice.fset.rsi/IFileSubmissionService/SubmitMessage" message="tns:IFileSubmissionService_SubmitMessage_InputMessage"/>
<wsdl:output wsaw:Action="http://filesubmissionservice.fset.rsi/IFileSubmissionService/SubmitMessageResponse" message="tns:IFileSubmissionService_SubmitMessage_OutputMessage"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="BasicHttpsBinding_IFileSubmissionService" type="tns:IFileSubmissionService">
<wsp:PolicyReference URI="#BasicHttpsBinding_IFileSubmissionService_policy"/>
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="ValidateFlatFile">
<soap:operation soapAction="http://filesubmissionservice.fset.rsi/IFileSubmissionService/ValidateFlatFile" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="ValidateXmlFile">
<soap:operation soapAction="http://filesubmissionservice.fset.rsi/IFileSubmissionService/ValidateXmlFile" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="SubmitMessage">
<soap:operation soapAction="http://filesubmissionservice.fset.rsi/IFileSubmissionService/SubmitMessage" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="FileSubmissionService">
<wsdl:port name="BasicHttpsBinding_IFileSubmissionService" binding="tns:BasicHttpsBinding_IFileSubmissionService">
<soap:address location="https://10.1.2.166/FileSubmissionService/FileSubmissionService.svc"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

And the following is where I'm calling the code generated java client and adding the headers:

private IFileSubmissionService createFileSubmissionServiceClient() {

    final String webserviceURL = applicationSessionBean.getExtProperties().getProperty("fileSubmissionServiceURL");
    final String username = applicationSessionBean.getExtProperties().getProperty("wsuser");
    final String password = applicationSessionBean.getExtProperties().getProperty("wspass");

    FileSubmissionService fsService = null;

    try {
        fsService = new FileSubmissionService();
    } catch (Exception e) {
        e.printStackTrace();
    }

    if(fsService == null) {
        //get service with default WSDL location
        fsService = new FileSubmissionService();
    }

    IFileSubmissionService client = fsService.getBasicHttpsBindingIFileSubmissionService(); 

    String        salt           = "XXXXXXXX";


    // Add security header
    Map<String, Object> ctx = ((BindingProvider)client).getRequestContext();
    ctx.put("ws-security.username", username);
    ctx.put("ws-security.password", getSecurePassword(password, salt.getBytes()));
    ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, webserviceURL);

    // Add headers
    List<Handler> handlerChain = new ArrayList<Handler>();
    handlerChain.add(new HeaderHandler(username, getSecurePassword(password, salt.getBytes())));
    ((BindingProvider)client).getBinding().setHandlerChain(handlerChain);

    return client;
}

And the following is my "header handler" where I've attached the security headers:

   public boolean handleMessage(SOAPMessageContext context)
    {
        Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        if (outboundProperty.booleanValue())
        {
            try
            {
                SOAPEnvelope envelope = context.getMessage().getSOAPPart().getEnvelope();
                SOAPFactory factory = SOAPFactory.newInstance();
                SOAPHeader header = envelope.getHeader();

                if (header == null)
                {
                    header = envelope.addHeader();
                }

                header.setPrefix("S");

                envelope.addAttribute(QName.valueOf("xmlns:wsu"), utility_uri);

                SOAPElement securityElem = header
                        .addChildElement(new QName(uri, "Security", prefix));
                securityElem.addAttribute(QName.valueOf("S:mustUnderstand"), "1");

                SOAPElement timestampElem = securityElem.addChildElement("Timestamp", "wsu");
                timestampElem.addAttribute(QName.valueOf("wsu:Id"), "_0");

                TimeZone tz = TimeZone.getTimeZone("UTC");
                DateFormat df = new SimpleDateFormat("yyy-MM-dd'T'HH:mm:ss.SSS'Z'");
                df.setTimeZone(tz);

                String created = df.format(new Date());

                SOAPElement createdTimestampElem = factory.createElement("Created", utility_prefix,
                        utility_uri);
                createdTimestampElem.addTextNode(created);

                Calendar expiresCal = Calendar.getInstance();
                expiresCal.add(Calendar.MINUTE, 1);
                Date expireDate = expiresCal.getTime();

                String expires = df.format(expireDate);
                SOAPElement expiresTimestampElem = factory.createElement("Expires", utility_prefix,
                        utility_uri);
                expiresTimestampElem.addTextNode(expires);

                timestampElem.addChildElement(createdTimestampElem);
                timestampElem.addChildElement(expiresTimestampElem);
                securityElem.addChildElement(timestampElem);

                SOAPElement tokenElem = securityElem.addChildElement("UsernameToken", prefix);
                tokenElem.addAttribute(QName.valueOf("wsu:Id"),
                        "uuid-" + UUID.randomUUID().toString());

                SOAPElement userElem = factory.createElement("Username", prefix, uri);
                userElem.addTextNode(username);

                SOAPElement pwdElem = factory.createElement("Password", prefix, uri);
                pwdElem.addTextNode(getSecurePassword(password, salt.getBytes()));
                pwdElem.addAttribute(QName.valueOf("Type"),
                        "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");

                tokenElem.addChildElement(userElem);
                tokenElem.addChildElement(pwdElem);
                securityElem.addChildElement(tokenElem);
                header.addChildElement(securityElem);


                SOAPMessage msg = context.getMessage();
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                msg.writeTo(out);
                String strMsg = new String(out.toByteArray());
                System.out.println(strMsg);

            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        else
        {
            try
            {
                // does nothing
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }

        return true;
    }

And finally here is a sample of the generated SOAP message:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <S:Header xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
        <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" S:mustUnderstand="1">
            <wsu:Timestamp wsu:Id="_0">
                <wsu:Created>2016-08-12T20:37:42.282Z</wsu:Created>
                <wsu:Expires>2016-08-12T20:38:42.282Z</wsu:Expires>
            </wsu:Timestamp>
            <wsse:UsernameToken wsu:Id="uuid-a5eb8c04-b9d0-4035-acd5-50fae8254d4a">
                <wsse:Username>XXXXXXXX</wsse:Username>
                <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">XXXXXXXX</wsse:Password>
            </wsse:UsernameToken>
        </wsse:Security>
    </S:Header>
    <soap:Body>
        <ValidateXmlFile xmlns="http://filesubmissionservice.fset.rsi" xmlns:ns2="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:ns3="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Service.FileSubmissionService" xmlns:ns4="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Core" xmlns:ns5="http://schemas.microsoft.com/2003/10/Serialization/" xmlns:ns6="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Plugin">
            <xmlContents>A</xmlContents>
            <submissionType>NC3</submissionType>
            <schemaVersion>FSETStateAnnualFilingV5.2</schemaVersion>
        </ValidateXmlFile>
    </soap:Body>
</soap:Envelope>

The problem is I keep getting the following error message:

 org.apache.cxf.transport.http.HTTPException: HTTP response '400: Bad Request' when communicating with https://10.1.2.166/FileSubmissionService/FileSubmissionService.svc

I am also getting the above messages when I try playing around with removing the header injection and or not setting or setting the request context username / password variables.

What I do not understand is A) why am i getting any error related to apache CXF when I am not using apache CXF and B) why is it a bad request?

Can someone point me in the right direction? I simply want to use vanilla java to create the client. I am not certain that I need any 3rd party toolkits to do this.

Thank you!

Upvotes: 1

Views: 1168

Answers (1)

Iofacture
Iofacture

Reputation: 685

Okay so here is the reason. Apparently intercepting the message and manually setting the headers and setting the username and password in the request context are two completely different approaches in JAX-WS that accomplish the same thing.

The following post explains the difference between the two: Difference between RequestContext and MessageContext in JAX WS

What I was doing was using both methods, which resulted in a request that was mal-formed and caused the webservice to puke and return a HTTP 400 code.

The reason this was not caught is that I was logging the SOAP message in the handler chain (the manual method) but was not printing the final outgoing message after the request context had been modified.

If someone can elaborate further on these two different approaches I would be forever thankful. I still do not totally understand the difference here and I could be wrong in my conclusions

Upvotes: 1

Related Questions