github.com/Axway/agent-sdk@v1.1.101/pkg/apic/specwsdlprocessor.go (about)

     1  package apic
     2  
     3  import (
     4  	"net"
     5  	"net/url"
     6  	"strconv"
     7  
     8  	"github.com/Axway/agent-sdk/pkg/util/log"
     9  	"github.com/Axway/agent-sdk/pkg/util/wsdl"
    10  )
    11  
    12  type wsdlProcessor struct {
    13  	wsdlDef *wsdl.Definitions
    14  	spec    []byte
    15  }
    16  
    17  func newWsdlProcessor(wsdlDef *wsdl.Definitions, spec []byte) *wsdlProcessor {
    18  	return &wsdlProcessor{wsdlDef: wsdlDef, spec: spec}
    19  }
    20  
    21  func (p *wsdlProcessor) GetResourceType() string {
    22  	return Wsdl
    23  }
    24  
    25  // GetVersion -
    26  func (p *wsdlProcessor) GetVersion() string {
    27  	return ""
    28  }
    29  
    30  // GetDescription -
    31  func (p *wsdlProcessor) GetDescription() string {
    32  	return ""
    33  }
    34  
    35  // GetEndpoints -
    36  func (p *wsdlProcessor) GetEndpoints() ([]EndpointDefinition, error) {
    37  	endPoints := []EndpointDefinition{}
    38  	ports := p.wsdlDef.Service.Ports
    39  	for _, val := range ports {
    40  		loc := val.Address.Location
    41  		fixed, err := url.Parse(loc)
    42  		if err != nil {
    43  			log.Errorf("Error parsing service location in WSDL to get endpoints: %v", err.Error())
    44  			return nil, err
    45  		}
    46  		protocol := fixed.Scheme
    47  		host := fixed.Hostname()
    48  		portStr := fixed.Port()
    49  		if portStr == "" {
    50  			p, err := net.LookupPort("tcp", protocol)
    51  			if err != nil {
    52  				log.Errorf("Error finding port for endpoint: %v", err.Error())
    53  				return nil, err
    54  			}
    55  			portStr = strconv.Itoa(p)
    56  		}
    57  		port, _ := strconv.Atoi(portStr)
    58  
    59  		endPoint := EndpointDefinition{
    60  			Host:     host,
    61  			Port:     int32(port),
    62  			Protocol: protocol,
    63  			BasePath: fixed.Path,
    64  		}
    65  		if !p.contains(endPoints, endPoint) {
    66  			endPoints = append(endPoints, endPoint)
    67  		}
    68  	}
    69  
    70  	return endPoints, nil
    71  }
    72  
    73  func (p *wsdlProcessor) contains(endpts []EndpointDefinition, endpt EndpointDefinition) bool {
    74  	for _, pt := range endpts {
    75  		if pt.Host == endpt.Host && pt.Port == endpt.Port &&
    76  			pt.Protocol == endpt.Protocol && pt.BasePath == endpt.BasePath {
    77  			return true
    78  		}
    79  	}
    80  	return false
    81  }
    82  
    83  // GetSpecBytes -
    84  func (p *wsdlProcessor) GetSpecBytes() []byte {
    85  	return p.spec
    86  }