Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow call with SOAP envelope #266

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/server/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/http"
"time"

"github.com/hooklift/gowsdl/example/server/gen"
"github.com/ParticleHealth/gowsdl/example/server/gen"
"github.com/hooklift/gowsdl/soap"
)

Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module github.com/hooklift/gowsdl
module github.com/ParticleHealth/gowsdl

go 1.15

require github.com/stretchr/testify v1.6.1
require (
github.com/hooklift/gowsdl v0.5.0 // indirect
github.com/stretchr/testify v1.6.1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/hooklift/gowsdl v0.5.0 h1:DE8RevqhGPLchumV/V7OwbCzfJ8lcozFg1uWC/ESCBQ=
github.com/hooklift/gowsdl v0.5.0/go.mod h1:9kRc402w9Ci/Mek5a1DNgTmU14yPY8fMumxNVvxhis4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
3 changes: 1 addition & 2 deletions soap/MMAEncoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net/textproto"
Expand Down Expand Up @@ -130,7 +129,7 @@ func (d *mmaDecoder) Decode(v interface{}) error {
if contentID == "" {
return errors.New("Invalid multipart content ID")
}
content, err := ioutil.ReadAll(p)
content, err := io.ReadAll(p)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions soap/MTOMEncoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"math/rand"
"mime"
"mime/multipart"
Expand Down Expand Up @@ -253,7 +252,7 @@ func (d *mtomDecoder) Decode(v interface{}) error {
if contentID == "" {
return errors.New("Invalid multipart content ID")
}
content, err := ioutil.ReadAll(p)
content, err := io.ReadAll(p)
if err != nil {
return err
}
Expand Down
115 changes: 87 additions & 28 deletions soap/soap.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,25 @@ import (
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"time"
)

type SOAPResponseEnvelopeInterface interface {
GetBody() SoapResponseBodyInterface
GetHeader() interface{}
SetBody(body SoapResponseBodyInterface)
GetAttachments() []MIMEMultipartAttachment
SetHeader(interface{})
SetXMLName(xml.Name)
}

type SoapResponseBodyInterface interface {
ErrorFromFault() error
SetContent(interface{})
}

type SOAPEncoder interface {
Encode(v interface{}) error
Flush() error
Expand All @@ -25,10 +38,34 @@ type SOAPDecoder interface {
type SOAPEnvelopeResponse struct {
XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"`
Header *SOAPHeaderResponse
Body SOAPBodyResponse
Body SoapResponseBodyInterface
Attachments []MIMEMultipartAttachment `xml:"attachments,omitempty"`
}

func (s *SOAPEnvelopeResponse) GetBody() SoapResponseBodyInterface {
return s.Body
}

func (s *SOAPEnvelopeResponse) GetHeader() interface{} {
return s.Header
}

func (s *SOAPEnvelopeResponse) SetBody(body SoapResponseBodyInterface) {
s.Body = body
}

func (s *SOAPEnvelopeResponse) SetHeader(header interface{}) {
s.Header = header.(*SOAPHeaderResponse)
}

func (s *SOAPEnvelopeResponse) SetXMLName(xmlName xml.Name) {
s.XMLName = xmlName
}

func (s *SOAPEnvelopeResponse) GetAttachments() []MIMEMultipartAttachment {
return s.Attachments
}

type SOAPEnvelope struct {
XMLName xml.Name `xml:"soap:Envelope"`
XmlNS string `xml:"xmlns:soap,attr"`
Expand Down Expand Up @@ -138,6 +175,10 @@ func (b *SOAPBodyResponse) ErrorFromFault() error {
return nil
}

func (b *SOAPBodyResponse) SetContent(content interface{}) {
b.Content = content
}

type DetailContainer struct {
Detail interface{}
}
Expand Down Expand Up @@ -375,47 +416,55 @@ func (s *Client) SetHeaders(headers ...interface{}) {

// CallContext performs HTTP POST request with a context
func (s *Client) CallContext(ctx context.Context, soapAction string, request, response interface{}) error {
return s.call(ctx, soapAction, request, response, nil, nil)
return s.call(ctx, soapAction, nil, request, response, nil, nil, nil)
}

// Call performs HTTP POST request.
// Note that if the server returns a status code >= 400, a HTTPError will be returned
func (s *Client) Call(soapAction string, request, response interface{}) error {
return s.call(context.Background(), soapAction, request, response, nil, nil)
return s.call(context.Background(), soapAction, nil, request, response, nil, nil, nil)
}

// CallContextWithAttachmentsAndFaultDetail performs HTTP POST request.
// Note that if SOAP fault is returned, it will be stored in the error.
// On top the attachments array will be filled with attachments returned from the SOAP request.
func (s *Client) CallContextWithAttachmentsAndFaultDetail(ctx context.Context, soapAction string, request,
response interface{}, faultDetail FaultError, attachments *[]MIMEMultipartAttachment) error {
return s.call(ctx, soapAction, request, response, faultDetail, attachments)
return s.call(ctx, soapAction, nil, request, response, nil, faultDetail, attachments)
}

// CallContextWithFault performs HTTP POST request.
// Note that if SOAP fault is returned, it will be stored in the error.
func (s *Client) CallContextWithFaultDetail(ctx context.Context, soapAction string, request, response interface{}, faultDetail FaultError) error {
return s.call(ctx, soapAction, request, response, faultDetail, nil)
return s.call(ctx, soapAction, nil, request, response, nil, faultDetail, nil)
}

// CallWithFaultDetail performs HTTP POST request.
// Note that if SOAP fault is returned, it will be stored in the error.
// the passed in fault detail is expected to implement FaultError interface,
// which allows to condense the detail into a short error message.
func (s *Client) CallWithFaultDetail(soapAction string, request, response interface{}, faultDetail FaultError) error {
return s.call(context.Background(), soapAction, request, response, faultDetail, nil)
return s.call(context.Background(), soapAction, nil, request, response, nil, faultDetail, nil)
}

func (s *Client) call(ctx context.Context, soapAction string, request, response interface{}, faultDetail FaultError,
func (s *Client) CallWithEnvelope(ctx context.Context, soapAction string, requestEnvelope interface{}, responseEnvelope SOAPResponseEnvelopeInterface, faultDetail FaultError,
retAttachments *[]MIMEMultipartAttachment) error {
// SOAP envelope capable of namespace prefixes
envelope := SOAPEnvelope{
XmlNS: XmlNsSoapEnv,
}
return s.call(ctx, soapAction, requestEnvelope, nil, nil, responseEnvelope, faultDetail, retAttachments)
}

envelope.Headers = s.headers
func (s *Client) call(ctx context.Context, soapAction string, requestEnvelope, request, response interface{}, responseEnvelope SOAPResponseEnvelopeInterface, faultDetail FaultError,
retAttachments *[]MIMEMultipartAttachment) error {
if requestEnvelope == nil {
// SOAP envelope capable of namespace prefixes
soapEnvelope := SOAPEnvelope{
XmlNS: XmlNsSoapEnv,
}
soapEnvelope.Headers = s.headers
soapEnvelope.Body.Content = request
requestEnvelope = soapEnvelope

}

envelope.Body.Content = request
buffer := new(bytes.Buffer)
var encoder SOAPEncoder
if s.opts.mtom && s.opts.mma {
Expand All @@ -428,7 +477,7 @@ func (s *Client) call(ctx context.Context, soapAction string, request, response
encoder = xml.NewEncoder(buffer)
}

if err := encoder.Encode(envelope); err != nil {
if err := encoder.Encode(requestEnvelope); err != nil {
return err
}

Expand Down Expand Up @@ -482,21 +531,31 @@ func (s *Client) call(ctx context.Context, soapAction string, request, response
defer res.Body.Close()

if res.StatusCode >= 400 && res.StatusCode != 500 {
body, _ := ioutil.ReadAll(res.Body)
body, _ := io.ReadAll(res.Body)
return &HTTPError{
StatusCode: res.StatusCode,
ResponseBody: body,
}
}
if responseEnvelope == nil {
// xml Decoder (used with and without MTOM) cannot handle namespace prefixes (yet),
// so we have to use a namespace-less response envelope
// soapResponse := new(SOAPEnvelopeResponse)
// soapResponse.Body = SOAPBodyResponse{
// Content: response,
// Fault: &SOAPFault{
// Detail: faultDetail,
// },
// }
responseEnvelope = &SOAPEnvelopeResponse{
Body: &SOAPBodyResponse{
Content: response,
Fault: &SOAPFault{
Detail: faultDetail,
},
},
}

// xml Decoder (used with and without MTOM) cannot handle namespace prefixes (yet),
// so we have to use a namespace-less response envelope
respEnvelope := new(SOAPEnvelopeResponse)
respEnvelope.Body = SOAPBodyResponse{
Content: response,
Fault: &SOAPFault{
Detail: faultDetail,
},
}

mtomBoundary, err := getMtomHeader(res.Header.Get("Content-Type"))
Expand Down Expand Up @@ -533,7 +592,7 @@ func (s *Client) call(ctx context.Context, soapAction string, request, response
dec = xml.NewDecoder(body)
}

if err := dec.Decode(respEnvelope); err != nil {
if err := dec.Decode(responseEnvelope); err != nil {
// the response doesn't contain a Fault/SOAPBody, so we return the original body
if res.StatusCode == 500 {
return &HTTPError{
Expand All @@ -544,8 +603,8 @@ func (s *Client) call(ctx context.Context, soapAction string, request, response
return err
}

if respEnvelope.Attachments != nil {
*retAttachments = respEnvelope.Attachments
if responseEnvelope.GetAttachments() != nil {
*retAttachments = responseEnvelope.GetAttachments()
}
return respEnvelope.Body.ErrorFromFault()
return responseEnvelope.GetBody().ErrorFromFault()
}
Loading