Skip to content

Commit

Permalink
Added code to grab the target address URL from the selected WSDL
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Fitzpatrick <bfitzpat@redhat.com>
  • Loading branch information
bfitzpat committed Dec 13, 2017
1 parent 63244b5 commit c5cd2ab
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 14 deletions.
Expand Up @@ -21,6 +21,10 @@
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.eclipse.core.databinding.Binding;
import org.eclipse.core.databinding.DataBindingContext;
Expand Down Expand Up @@ -72,6 +76,7 @@
import org.fusesource.ide.wsdl2rest.ui.wizard.Wsdl2RestOptions;
import org.fusesource.ide.wsdl2rest.ui.wizard.Wsdl2RestWizard;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
Expand Down Expand Up @@ -332,6 +337,10 @@ protected void initIfNotEmpty (Text textControl, String value) {
}
}

/**
* Utility method to find the target service URL for the WSDL if it is available.
* @return String
*/
protected String getLocationFromWSDL() {
String locationURL = null;
if (!Strings.isEmpty(getOptionsFromWizard().getWsdlURL())) {
Expand All @@ -347,23 +356,22 @@ protected String getLocationFromWSDL() {
// Load the input XML document, parse it and return an instance of the
// Document class.
Document document = builder.parse(testStream);
NodeList nodeList = document.getDocumentElement().getElementsByTagName("address");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Node locationAttr = node.getAttributes().getNamedItem("location");
locationURL = locationAttr.getNodeValue();
}
} catch (MalformedURLException e) {

// now use XPath to find the particular element we need
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
Object rawLocation =
xpath.evaluate("/definitions/service/port/address/@location", document, XPathConstants.STRING);

// if we found it, stash it!
if (rawLocation != null) {
locationURL = (String) rawLocation;
}
} catch (IOException | ParserConfigurationException | SAXException | XPathExpressionException e) {
// should be valid at this point
Wsdl2RestUIActivator.pluginLog().logError(e);
} catch (IOException e) {
// should be valid at this point
Wsdl2RestUIActivator.pluginLog().logError(e);
} catch (ParserConfigurationException e) {
Wsdl2RestUIActivator.pluginLog().logError(e);
} catch (SAXException e) {
Wsdl2RestUIActivator.pluginLog().logError(e);
} finally {
// make sure we close the stream
if (testStream != null) {
try {
testStream.close();
Expand Down
Expand Up @@ -27,6 +27,8 @@
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Text;
import org.eclipse.core.databinding.observable.ChangeEvent;
import org.eclipse.core.databinding.observable.IChangeListener;
import org.fusesource.ide.foundation.core.util.Strings;
import org.fusesource.ide.wsdl2rest.ui.internal.UIMessages;

Expand Down Expand Up @@ -67,6 +69,7 @@ public void createControl(Composite parent) {
@Override
public void widgetSelected(SelectionEvent e) {
selectWSDL();
setTargetAddressFromWsdlSelection();
urlTextControl.notifyListeners(SWT.Modify, new Event());
}
});
Expand All @@ -88,9 +91,11 @@ public void widgetSelected(SelectionEvent e) {

// define the data bindings
Binding wsdlBinding = createBinding(urlTextControl, "wsdlURL", new WsdlValidator()); //$NON-NLS-1$
wsdlBinding.getModel().addChangeListener(new WsdlChangeListener());
ControlDecorationSupport.create(wsdlBinding, SWT.LEFT | SWT.TOP);

Binding projectTextBinding = createBinding(projectTextControl, "projectName", new ProjectNameValidator()); //$NON-NLS-1$
projectTextBinding.getModel().addChangeListener(new ProjectChangeListener());
ControlDecorationSupport.create(projectTextBinding, SWT.LEFT | SWT.TOP);

// set initial values
Expand All @@ -106,6 +111,26 @@ public void widgetSelected(SelectionEvent e) {
setErrorMessage(null); // clear any error messages at first
}

class WsdlChangeListener implements IChangeListener {

@Override
public void handleChange(ChangeEvent arg0) {
setTargetAddressFromWsdlSelection();
}
}

class ProjectChangeListener implements IChangeListener {

@Override
public void handleChange(ChangeEvent arg0) {
IProject selectedProject =
ResourcesPlugin.getWorkspace().getRoot().getProject(getOptionsFromWizard().getProjectName());
if (selectedProject != null) {
setPathsFromProjectSelection(selectedProject);
}
}
}

/**
* Uses the selected project to default the Java and Camel config paths.
* @param selectedProject
Expand All @@ -130,5 +155,15 @@ private void setPathsFromProjectSelection(IProject selectedProject) {
getOptionsFromWizard().setDestinationJava(projectJavaPath);
getOptionsFromWizard().setDestinationCamel(projectConfigPath);
}

/**
* Use the selected wsdl to determine a default target address URL for the generated REST service.
*/
private void setTargetAddressFromWsdlSelection() {
String address = getLocationFromWSDL();
if (!Strings.isEmpty(address)) {
getOptionsFromWizard().setTargetServiceAddress(address);
}
}

}

0 comments on commit c5cd2ab

Please sign in to comment.