import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Vector;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Label;
import java.awt.TextField;
import java.awt.TextArea;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.applet.Applet;
public class WSDLAppletClient extends Applet {
// Array of Operation objects.
Operation[] operation;
// Flag to check whether operations are loaded or not.
boolean operationFlag = false;
// Hold Operation Names.
String[] operationsList = {" -- No Method Listed -- "};
// Soap URL field label.
Label lblSoapUrl = new Label (" Enter Soap Server URL :");
// WSDL URL field label.
Label lblWsdlUrl = new Label (" Enter WSDL File URL : ");
Label lblInst_start = new Label ();
Label lblInst_middle = new Label ();
Label lblInst_end = new Label ();
Choice cMethodsList = new Choice();
// Buttons
Button buttonDownload = new Button("Download WSDL");
Button buttonInvoke = new Button(" Invoke Method ");
Button buttonClear0 = new Button("Clear Parameter Window");
Button buttonClear1 = new Button("Clear Ouput Window");
Button buttonReset = new Button("Reset Application");
// Textfield to take SOAP Server URL.
TextField tfSoapUrl = new TextField (60);
// Textfield to take WSDL file URL.
TextField tfWsdlUrl = new TextField (60);
// Output Window to show application's output to user.
TextArea taResponse = null;
// Parameter Window to show parameters and instructions.
TextArea taParameter = null;
// Intializes and loads the GUI components
public void init()
{
setLayout(new FlowLayout(FlowLayout.CENTER));
add (lblSoapUrl);
add(tfSoapUrl);
add(lblWsdlUrl);
add(tfWsdlUrl);
buttonDownload.addActionListener
(new DownloadButtonListener());
add(buttonDownload);
if (!operationFlag)
{
cMethodsList.addItem(operationsList[0]);
buttonInvoke.setEnabled(false);
}
cMethodsList.addItemListener
(new ChoiceListener());
add(cMethodsList);
buttonInvoke.addActionListener
(new InvokeButtonListener());
add(buttonInvoke);
taParameter = new TextArea
("Parameter Window....",15,30);
taParameter.setBackground(Color.white);
add (taParameter);
taResponse = new TextArea_
("Output Window....",15,50);
taResponse.setBackground(Color.white);
add (taResponse);
buttonClear0.addActionListener
(new ClearButtonListener());
add (buttonClear0);
buttonClear.addActionListener
(new ClearButtonListener());
add (buttonClear);
buttonReset.addActionListener
(new ResetButtonListener());
add (buttonReset);
}//init()
// Downloads the WSDL file and populates method list
// with method names found in wsdl file.
private void downloadAndPopulate()
{
try {
if (tfWsdlUrl.getText().equals(""))
taResponse.setText
("Soap Url & WSDL file URL values can't be null......\r\n");
else
{
taResponse.setText("Downloading WSDL from URL .."+tfWsdlUrl.getText()+"\r\n");
// Create the WSDL Client.
// WSDLClient constructor will do -
all the needful i.e.
// 1. Download WSDL file.
// 2. Parse it.
// 3. Load Data Structures.
// We have not specified the port number here.
// WSDL client will use default port 8080, which we are using for testing.
WSDLClient wc = new WSDLClient(tfWsdlUrl.getText());
wc.setSoapAddress (tfSoapUrl.getText());
showOperationsList(wc);
}
}//try
catch (UnknownHostException ue) _
{taResponse.append
("Download UnknownHostException...."+ue.getMessage()+"\r\n"); }
catch (NullPointerException ne) {taResponse.append
("Download NullPointerException...."+ne.getMessage()+"\r\n"); }
catch (IOException io) {taResponse.append
("Download IOException...."+io.getMessage()+"\r\n"); }
}//downloadAndPopulate
// Show operations list.
public void showOperationsList(WSDLClient wc)
{
Vector opt = new Vector();
if (!wc.Error)
opt = wc.getOperations();
else
taResponse.append("Request Error .....
:"+wc.ErrorString+"\r\n");
operation = new Operation [opt.size()];
operationsList = new String [opt.size()];
cMethodsList.removeAll();
for (int i =0; i<operation.length; i++) {
operation[i] = (Operation) opt.elementAt(i);
operationsList[i] = operation[i].getName();
cMethodsList.addItem(operationsList[i]);
}//for
operationFlag = true;
}//showOperationsList()
// Show parameter names and types in Parameter Window.
private void showParametersList(int index)
{
Vector parameters = _
operation[index].getInputParameters();
if (parameters.size()>0)
{
taParameter.setText
(operation[index].getName()+" method requires following parameters..\r\n");
taParameter.append_
("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\r\n");
for (int i =0; i<parameters.size(); i++)
{
Parameter parameter = (Parameter)
parameters.elementAt(i);
taParameter.append
("Name: ["+parameter.getName()+"] Type:
["+parameter.getType()+"]\r\n");
}//for
taParameter.append
("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\r\n");
taParameter.append_
("Please enter values for each of the above\r\n");
taParameter.append
("parameter(s) seprated with $$ sign::\r\n");
}
else
taParameter.setText
(operation[index].getName()+" method requires no parameter..");
}//showParametersList()
// Provides operation invocation service.
// We will provide the SOAP server address
//to the operation object.
// It can use our SOAP client by itself, so we
//don't have to worry about SOAP details.
// Shows the response of Invocation in output
//window area.
private void invoke( )
{
Parameter parameter=null;
int index = cMethodsList.getSelectedIndex();
// Holds input parameter values.
String parameterValues = taParameter.getText();
// Getting user entered parameter values from Parameter Window.
parameterValues = parameterValues.substring(parameterValues.indexOf("::")+2);
// Holds response string.
String response="";
try
{
Vector parameters = operation[index].getInputParameters();
if ((operation[index].getSoapAddress())==null)
{
operation[index].setSoapAddress("http://localhost/soap/servlet/rpcrouter");
}
// Our GUI is very simple, so we need to parse parameter's window text.
// Holds Initial index position.
int i =0;
// Holds next index position.
int x =0;
for (int j =0; j<parameters.size(); j++)
{
parameter = (Parameter) parameters.elementAt(j);
x = parameterValues.indexOf("$$", i);
if (x!=-1)
{
parameter.setValue((parameterValues.substring(i,x)).trim());
i = x+2;
}//if
else
{
parameter.setValue((parameterValues.substring(i,parameterValues.length())).trim());
break;
}//else
}//for (int j =0; j<parameters.size(); j++)
taResponse.append ("\r\nRequest sent for ["+operation[index].getName()+"] method to ["+operation[index].getSoapAddress()+"] at "+new Date().toString()+"\r\n");
//sending response
response = operation[index].send();
taResponse.append ("\r\nResponse received at "+new Date().toString()+"\r\n");
taResponse.append (response+"\r\n");
}//try
catch (UnknownHostException ue) {taResponse.append("Invocation UnknownHostException...."+ue.getMessage()+"\r\n"); }
catch (IOException io) {taResponse.append("Invocation IOException...."+io.getMessage()+"\r\n"); }
}//Invoke()
/**********************************************************
* The following inner classes controls the user navigation
* with the GUI.
* 1. ChoiceListener
* 2. DownloadButtonListener
* 3. InvokeButtonListener
* 4. ResetButtonListener
* 5. ClearButtonListener
**********************************************************/
// Inner ItemListener Class.
class ChoiceListener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
buttonInvoke.setEnabled(true);
taParameter.setText("");
int index = cMethodsList.getSelectedIndex();
showParametersList(index);
}//itemStateChanged()
}//ChoiceListener()
class DownloadButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
downloadAndPopulate();
}
}//DownloadButtonListener
class InvokeButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
buttonDownload.setEnabled(false);
invoke();
buttonDownload.setEnabled(true);
}
}//InvokeButtonListener
class ResetButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
stop();
destroy();
tfWsdlUrl.setText("");
tfSoapUrl.setText("");
operationFlag=false;
buttonInvoke.setEnabled(false);
cMethodsList.removeAll();
cMethodsList.addItem(" -- No Method Listed -- ");
taParameter.setText("Parameter Window....");
taResponse.setText("Output Window....");
}
}//ResetButtonListener
class ClearButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
Button b = (Button) evt.getSource();
if (b.getLabel().equals("Clear Parameter Window"))
taParameter.setText("");
else
taResponse.setText("");
}
}//ClearButtonListener
}//class
|