Unit Convertor end to end scenario
Author: Lukas Jungmann
Last update: $Date: 2006/07/14 21:14:07 $, $Revision: 1.5 $
Introduction:
This end to end scenario should test Web service client creation.
The scenario covers developing of web service client with simple message
handler.
The document is not intended as test specification for these
features but describes developing web service client in NetBeans 5.5
(version for NetBeans 4.1 is here)
Table of Contents
UnitConvertor sources are avalaible
here.
- Go to File - New Project - Web - Web Application
- Specify
UnitConvertor as Project Name
- Specify project's directory
- Select
Sun App server as target server
- Choose J2EE 1.4 as J2EE version and
- Click
Finish
- Go to File - New File - Web services - Web Service Client - Next
- Specify
http://www.webservicex.net/length.asmx?WSDL as Running Web Service WSDL URL
- Optional: if you're behind proxy check its settings via
Set Proxy ... button
- Specify package for the client, eg.
org.netbeans.end2end.unitconvertor.client
- Select
J2EE Container-generated static stub as client type
- Click
Finish
- Rigth-click web module node in project tab
- Invoke New - New File/Folder... - Web Services - Message Handler
- Type
MessageLogger name, specify package, eg.
org.netbeans.end2end.unitconvertor and finish New File wizard
- Open handler's code in editor
- Replace
handleRequest(MessageContext context) and handleResponse(MessageContext context)
implementation with the following line:
return logMessage(context);
- Implement
logMessage method:
private boolean logMessage(MessageContext context) {
SOAPMessageContext smc = (SOAPMessageContext) context;
SOAPMessage msg = smc.getMessage();
try {
msg.writeTo(System.out);
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
} catch (SOAPException se) {
System.out.println(se.getMessage());
}
return true;
}
- Press
Shift+Alt+F - this will fix missing imports
of java.lang.IOException and javax.xml.soap.SOAPException.
- Add handler to the client through
Configure Handlers...
from context menu of LengthUnit client node in project view
- Rigth-click web module node in project tab
- Invoke New - Servlet
- Type
ConvertorServlet name, specify package, eg.
org.netbeans.end2end.unitconvertor and press Next
- Change URL Mapping(s) to
/convert and finish New File wizard
- Open servlet's code in editor
- Right-click inside the
processRequest method of the
servlet class and invoke Web Service Client Resources - Call Web Service Operation
- Select
changeLengthUnit and click OK
button - now IDE generates helper methods (getLengthUnit()
and getLengthUnitSoap()) and also a statement which
calls the web service operation:
try { // This code block invokes the changeLengthUnit operation on web service
getLengthUnitSoap().changeLengthUnit(/* TODO enter operation arguments */);
} catch(java.rmi.RemoteException ex) {
// TODO handle remote exception
} catch(Exception ex) {
// TODO handle custom exceptions here
}
- Add user input handling code into
processRequest method, so
the method implementation is:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String size = request.getParameter("size");
String from = request.getParameter("from");
String to = request.getParameter("to");
double d = 0.0;
try {
d = Double.parseDouble(size);
} catch (NumberFormatException nfe) {
d = Double.MIN_VALUE;
}
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Convertor</title>");
out.println("</head>");
out.println("<body>");
if (d != Double.MIN_VALUE) {
try {
out.println(size);
out.println(" ");
out.println(from);
out.println(" is ");
out.println(getLengthUnitSoap().changeLengthUnit(d,
Lengths.fromString(from), Lengths.fromString(to)));
out.println(" ");
out.println(to);
} catch(java.rmi.RemoteException ex) {
// TODO handle remote exception
ex.printStackTrace(out);
}
} else {
out.println(size + " is not valid number!");
}
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
- Press
Shift+Alt+F - this will fix missing import
of org.netbeans.end2end.unitconvertor.client.Lengths.
- Now we've done our application and we can run it. Here's sample
output:
- And in server output we can see SOAP messages which are comming
from/to our client: