protected void useProxiedWsdl()

in bindings/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/HttpSoapConsumerEndpoint.java [186:306]


    protected void useProxiedWsdl() throws Exception {
    	// get the component context
    	ComponentContext componentContext = this.serviceUnit.getComponent().getComponentContext();
    	// get the targetService endpoint
    	ServiceEndpoint targetEndpoint = null;
    	// if the user has defined the targetService and targetEndpoint, use it
    	if (getTargetService() != null && getTargetEndpoint() != null) {
    		targetEndpoint = componentContext.getEndpoint(getTargetService(), getTargetEndpoint());
    	}
    	// if the user has defined the targetService, use it
    	if (targetEndpoint == null && getTargetService() != null) {
    		ServiceEndpoint[] endPoints = componentContext.getEndpointsForService(this.getTargetService());
    		if (endPoints != null && endPoints.length > 0) {
    			targetEndpoint = endPoints[0];
    		}
    	}
    	// if the user has defined the targetInterfaceName, use it
    	if (targetEndpoint == null && this.getTargetInterface() != null) {
    		ServiceEndpoint[] endPoints = componentContext.getEndpoints(this.getTargetInterface());
    		if (endPoints != null && endPoints.length > 0) {
    			targetEndpoint = endPoints[0];
    		}
    	}
    	// if the targetEndpoint has not be identified, raise an JBI exception
    	if (targetEndpoint == null) {
    		throw new JBIException("The target endpoint is not found.");
    	}
    	// get the target endpoint descriptor
    	Document targetEndpointDescriptor = componentContext.getEndpointDescriptor(targetEndpoint);
        if (targetEndpointDescriptor == null) {
            throw new JBIException("The target endpoint descriptor is null.");
        }
    	// get the target endpoint definition (based on the descriptor)
    	Definition targetEndpointDefinition = javax.wsdl.factory.WSDLFactory.newInstance().newWSDLReader().readWSDL(null, targetEndpointDescriptor);
    	// check if the WSDL is the target component as a WSDL definition
    	if (targetEndpointDefinition == null) {
    		throw new JBIException("The target endpoint has no WSDL definition.");
    	}
    	// get the targetService in the endpoint definition
    	Service targetServiceInDefinition = targetEndpointDefinition.getService(this.getTargetService());
    	// get the targetEndpoint port
    	Port targetEndpointPort = (targetServiceInDefinition != null) ? targetServiceInDefinition.getPort(this.getTargetEndpoint()) : null;
    	// get the targetEndpoint port type
    	PortType targetEndpointPortType = (targetEndpointPort != null) ? targetEndpointPort.getBinding().getPortType() : null;
    	// try to get the endpoint port type using the first defined in the definition
    	if (targetEndpointPortType == null) {
    		QName[] portTypes = (QName[]) targetEndpointDefinition.getPortTypes().keySet().toArray(new QName[0]);
    		if(portTypes != null && portTypes.length > 0) {
    			targetEndpointPortType = targetEndpointDefinition.getPortType(portTypes[0]);
    		}
    	}
    	if(targetEndpointPortType == null) {
    		throw new JBIException("The target port type is not defined.");
    	}
    	// if the port type is found, make a cleanup on the definition
    	if (targetEndpointPortType != null) {
    		// cleanup port types
    		QName[] qnames = (QName[]) targetEndpointDefinition.getPortTypes().keySet().toArray(new QName[0]);
    		for(int i = 0; i < qnames.length; i++) {
    			if(!qnames[i].equals(targetEndpointPortType.getQName())) {
    				targetEndpointDefinition.removePortType(qnames[i]);
    			}
    		}
    		// cleanup services
    		qnames = (QName[]) targetEndpointDefinition.getServices().keySet().toArray(new QName[0]);
    		for(int i = 0; i < qnames.length; i++) {
    			targetEndpointDefinition.removeService(qnames[i]);
    		}
    		// cleanup bindings
    		qnames = (QName[]) targetEndpointDefinition.getBindings().keySet().toArray(new QName[0]);
    		for(int i = 0; i < qnames.length; i++) {
    			targetEndpointDefinition.removeBinding(qnames[i]);
    		}
    		// format the location URI
    		String location = this.getLocationURI();
    		if(!location.endsWith("/")) {
    			location += "/";
    		}
    		// construct the location URI using HTTP component configuration
    		HttpComponent httpComponent = (HttpComponent) this.serviceUnit.getComponent();
    		if (httpComponent.getConfiguration().isManaged()) {
    			// save the path
    			String path = new URI(location).getPath();
    			// format the new location
    			location = "http://localhost";
    			// construct the location using the HttpComponent configuration
    			if(httpComponent.getHost() != null) {
    				// prefix with the protocol
    				if(httpComponent.getProtocol() != null) {
    					// the protocol is defined in the component configuration
    					location = httpComponent.getProtocol() + "://";
    				}
    				else {
    					// define HTTP protocol as default
    					location = "http://";
    				}
    				// add the configured host
    				location += httpComponent.getHost();
    				// add the configured port number (if it's not the 80)
    				if(httpComponent.getPort() != 80) {
    					location += ":" + httpComponent.getPort();
    				}
    				// add the path (location URI)
    				if(httpComponent.getPath() != null) {
    					location += httpComponent.getPath();
    				}
    			}
    			// add the component configuration path mapping
    			location += httpComponent.getConfiguration().getMapping() + path; 
    		}
			if (targetEndpointPortType.getQName().getNamespaceURI().equals(this.getService().getNamespaceURI())) {
				PortTypeDecorator.decorate(targetEndpointDefinition, targetEndpointPortType, location, endpoint + "Binding", service.getLocalPart(), endpoint, soapVersion);
				definition = targetEndpointDefinition;
			}
			else {
				definition = PortTypeDecorator.createImportDef(targetEndpointDefinition, service.getNamespaceURI(), "porttypedef.wsdl");
				PortTypeDecorator.decorate(definition, targetEndpointPortType, location, endpoint + "Binding", service.getLocalPart(), endpoint, soapVersion);
			}
            description = javax.wsdl.factory.WSDLFactory.newInstance().newWSDLWriter().getDocument(definition);
    	}
    }