Skip to main content
summaryrefslogtreecommitdiffstats
blob: 65629f3eab6a58a192ab679545eb683fc7ca6415 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*******************************************************************************
 * Copyright (c) 2005, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.wst.ws.internal.common;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import javax.wsdl.Definition;
import javax.wsdl.Port;
import javax.wsdl.Service;
import javax.wsdl.extensions.ExtensibilityElement;
import javax.wsdl.extensions.http.HTTPAddress;
import javax.wsdl.extensions.soap.SOAPAddress;
import org.eclipse.core.resources.IResource;
import org.eclipse.osgi.util.NLS;
import org.eclipse.wst.ws.internal.WstWSPluginMessages;
import org.eclipse.wst.ws.internal.parser.wsil.WebServicesParser;

/**
 * Utilities for working with a workspace WSDL file resource.
 * 
 * @author joan
 *
 */

public class WSDLUtility {

	protected Definition definition_ = null;
	protected IResource resource_ = null;
	protected String serviceName_ = "";
	protected URL url_ = null;
	
	public WSDLUtility(IResource res)
	{
 		super();
		resource_ = res;
		buildDefinition();		
	}
	
	protected boolean buildDefinition()
	{		
		if (resource_.getType() == IResource.FILE)
	    {        
	        String ext = resource_.getFileExtension();
	        String resPath = resource_.getFullPath().toString();
	        if (ext != null)
	        {
		      	
		        WebServicesParser parser = new WebServicesParser();
		      			      	
		      	try {
		      		definition_ = parser.getWSDLDefinition("platform:" + resPath);
		      	   if (definition_ == null){		      		   
		      		 throw new Exception(NLS.bind(WstWSPluginMessages.MSG_UNABLE_TO_RESOLVE_PLATFORM_URI, resPath));		      		 
		      	   }
		      	}
		      	catch (Exception e){
		      		return false;
		      	}
	        }
	        else if (resPath.indexOf('?') > 0)  //deployed resource
	        {
	        	WebServicesParser parser = new WebServicesParser();
		      			      	
		      	try {
		      		definition_ = parser.getWSDLDefinition("http:" + resPath);
		      	   if (definition_ == null){
		      		 throw new Exception(NLS.bind(WstWSPluginMessages.MSG_UNABLE_TO_RESOLVE_HTTP, resPath));		      		

		      	   }
		      	}
		      	catch (Exception e){
		      		return false;
		      	}
	        }
	    }
		serviceName_ = "";		
		return true;
	}
	
	/**
	 * Determines the name of the service defined in the WSDL file
	 * 
	 * @param resource The workspace resource for the WSDL file
	 * @return Returns a string containing the value of the name attribute of the service element 
	 */
	public String getServiceName()
	{
		if (serviceName_.equals(""))
		{
			if (definition_ != null)
			{
				   Map servicesMap = definition_.getServices();  //jvh - need to handle null definition here....
			  	   Object[] services = servicesMap.values().toArray();
			  	   
			  	   // TODO: here can detect if no service tag - send appropriate message
			  	   // TODO: what if > 1 service defined in the WSDL?
			  	   // for now return the first service name found...
			  	   for (int i = 0; i < services.length; i++) {
					   Service s = (Service)services[i];
					   String qString = s.getQName().toString();
					   
					   //get name only from qualified string
					   int index1 = qString.indexOf("}")+1;  //$NON-NLS-1$
					   int index2 = qString.length();
					   
					   serviceName_ = qString.substring(index1, index2);				   
			  	   }  	
			}				  
		}
	  	return serviceName_;
	}
	
	public URL getWSDLAddress() 
    {
	   if (url_ == null)
	   {	   
	       String locationURI = null;
	       if (definition_ != null)
	       {
	    	   Map servicesMap = definition_.getServices();
		  	   Object[] services = servicesMap.values().toArray();
		  	   
		  	   // TODO: here can detect if no service tag - send appropriate message
		  	   // TODO: what if > 1 service defined in the WSDL?
		  	   for (int i = 0; i < services.length; i++) {
				   Service s = (Service)services[i];
				   Map portsMap = s.getPorts();
				   Object[] ports = portsMap.values().toArray();
		
				   // TODO: here can detect if > 1 port for the service - determine how to handle
				   for (int j = 0; j < ports.length; j++) {
						Port p = (Port)ports[j];
						List extList = p.getExtensibilityElements();
						
						for (int k = 0; j < extList.size(); j++){
						      ExtensibilityElement extElement = (ExtensibilityElement)extList.get(k);
						      if (extElement instanceof SOAPAddress)
						      {  
						        locationURI = ((SOAPAddress)extElement).getLocationURI();
						      }
						      else if (extElement instanceof HTTPAddress)
						       {
						        locationURI = ((HTTPAddress)extElement).getLocationURI();						        
						      }
						      else
						      {
						    	  //TODO: if not SOAP or HTTP - how do we want to handle it... 
						      }
						 }					
				   }
		  	   }  		    	
		  	 
			    try {
			         url_ = new URL(locationURI + "?" + resource_.getFileExtension());	         
			    }
			    catch (MalformedURLException malExc){}
            }	  		      
	    }
	   return url_;
    }
}

Back to the top