/**
* Created : Anirban
* Date: May 19, 2003
* Modified by : Chandar
*
* File : ClientAuthenticator.java
*
* Overview :
* This file defines the client side handler class of SOAP header
* Web Service. The handler intercepts the SOAP request from
* client, creates a header using client authentication information
* and attaches the header to SOAP message before sending it to
* the Web service.
*/ package oracle.demo.header;
import java.io.*;
import java.util.Map;
// JAX-RPC imports
import javax.xml.namespace.QName;
import javax.xml.rpc.handler.MessageContext;
import javax.xml.rpc.handler.HandlerInfo;
import javax.xml.rpc.handler.soap.SOAPMessageContext;
import javax.xml.soap.*;
import javax.xml.soap.SOAPFactory;
//utility imports
import java.text.DateFormat;
import java.util.Date;
import com.sun.xml.rpc.soap.message.*;
/**
* Handler to insert authentication info on the client
*/ public class ClientAuthenticator implements javax.xml.rpc.handler.Handler
{
// variable holding output stream
private PrintStream out =System.out;
private QName[] headers;
// variable for date
private DateFormat formater = DateFormat.getDateTimeInstance();
// variable used for creating SOAPElements
private static String PREFIX ="ns0";
private static String URI ="http://oracle.j2ee.ws/ejb/Credit";
// client authentication information
private static String VALID_ID="SCOTT";
private static String VALID_PASSWORD="TIGER";
/*
* Constructor definition
*/ public ClientAuthenticator () {
out.println("--- In ClientAuthenticator Constructor.");
}
/*
* This method initializes the handler class
*/ public void init(HandlerInfo info) {
out.println("--- In ClientAuthenticator.init ()");
}
/*
* The handleFault method processes the SOAP faults based
* on the SOAP message processing model.
*/ public boolean handleFault(MessageContext context) {
out.println("--- In ClientAuthenticator.handleFault ()"
+ context);
 
return true;
}
/*
* The handleRequest method initiates the request processing for this handler.
* It creates a SOAPHeader element and add the SOAP header to the SOAP
* message.
*/ public boolean handleRequest (MessageContext context) {
out.println("--- In ClientAuthenticator.handleRequest () at " +formater.format(new Date()));
try {
/* add a header with the authentication info into the SOAP message:
*
* <env:Header>
* <ns0:authHeader1 xmlns:ns0="http://oracle.j2ee.ws/ejb/Credit">
* <ns0:id>SCOTT</ns0:id>
* <ns0:password>TIGER</ns0:password>
* </ns0:authHeader1>
* </env:Header>
*/
 
// get SOAP message context
SOAPMessageContext smc = (SOAPMessageContext) context;
// get SOAP envelope from SOAP message
SOAPEnvelope se = smc.getMessage().getSOAPPart().getEnvelope();
// create instance of SOAP factory
SOAPFactory sFactory = SOAPFactory.newInstance();
// create SOAP elements specifying prefix and URI
SOAPElement sHelem1 = sFactory.createElement("authHeader1",PREFIX,URI);
SOAPElement sCHelem11 = sFactory.createElement("id",PREFIX,URI);
// attach value to id element
sCHelem11.addTextNode("SCOTT");
SOAPElement sCHelem12 = sFactory.createElement("password",PREFIX,URI);
// attach value to password element
sCHelem12.addTextNode("TIGER");
//add child elements to the root element
sHelem1.addChildElement(sCHelem11);
sHelem1.addChildElement(sCHelem12);
// create SOAPHeader instance for SOAP envelope
SOAPHeader sh = se.addHeader();
// add SOAP element for header to SOAP header object
sh.addChildElement(sHelem1);
}
catch (Exception ex) {
ex.printStackTrace();
}
return true;
}
/*
* The handleResponse method initiates the response processing for this handler.
*/ public boolean handleResponse (MessageContext context) {
out.println("--- In ClientAuthenticator.handleResponse ()");
return true;
}
/*
* This method gets the header blocks that can be processed by
* this Handler instance
*/ public javax.xml.namespace.QName[] getHeaders() {
out.println("--- In ClientAuthenticator.getHeaders ()");
return null;
}
/*
* The destroy method indicates the end of lifecycle for a Handler instance
*

*/ public void destroy() {
out.println("--- In ClientAuthenticator.destroy ()");
out.close();
}
}

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy