adding ws security to dynamic webservice call
patrik.jetzer Nov 19, 2010 9:56 AMI'm looking for any kind of documentation, examples for adding a Username Password Token to the webservice call.
The generation of client artefacts with wsimport or wsconsume is not an option, since the callback references are not defined until runtime.
This is the way the webservice is called tried to access an example reliable messaging enabled webservice deployed on localhost:
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.soap.SOAPBinding;
// -------------
public void sendRM() {
String namespace = "http://ws.example.com/";
String serviceName = "SimpleService";
String portName = "SimpleServicePort";
String methodName = "echoRequest";
try {
// Qnames for service as defined in wsdl.
QName serviceQName = new QName(namespace, serviceName);
// QName for Port As defined in wsdl.
QName portQName = new QName(namespace, portName);
// Endpoint Address
String endpointAddress = "http://localhost:8080/SimpleWS/SimpleService";
// Create a dynamic Service instance
Service service = Service.create(serviceQName);
// Add a port to the Service
service.addPort(portQName, SOAPBinding.SOAP11HTTP_BINDING,
endpointAddress);
// Create a dispatch instance
Dispatch<SOAPMessage> dispatch = service.createDispatch(portQName,
SOAPMessage.class, Service.Mode.MESSAGE);
// Use Dispatch as BindingProvider
BindingProvider bp = (BindingProvider) dispatch;
// Optionally Configure RequestContext to send SOAPAction HTTP Header
Map<String, Object> rc = bp.getRequestContext();
rc.put(BindingProvider.SOAPACTION_USE_PROPERTY, Boolean.TRUE);
rc.put(BindingProvider.SOAPACTION_URI_PROPERTY, methodName);
// --- username, password (is NOT submitted)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
rc.put(BindingProvider.USERNAME_PROPERTY, "myUsername");
rc.put(BindingProvider.PASSWORD_PROPERTY, "myPassword");
// Obtain a preconfigured SAAJ MessageFactory
MessageFactory factory = ((SOAPBinding) bp.getBinding()).getMessageFactory();
// Create SOAPMessage Request
SOAPMessage request = factory.createMessage();
// Request Header
SOAPHeader header = request.getSOAPHeader();
// Request Body
SOAPBody body = request.getSOAPBody();
// Compose the soap:Body payload
QName payloadName = new QName(namespace, methodName, "ns1");
SOAPBodyElement payload = body.addBodyElement(payloadName);
SOAPElement message = payload.addChildElement("input");
message.addTextNode("dummyHelloWorld");
// Invoke the endpoint synchronously
SOAPMessage reply = null;
try {
// Invoke Endpoint Operation and read response
reply = dispatch.invoke(request);
} catch (WebServiceException wse) {
wse.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
Env Setting:
JBossAS 4.2.2 JRE 1.5.0_19
Webservice stack jbossws-metro-3.0.5.GA
I'm suppose to support multiple different security protocols but i can't find any examples not even in the test classes
for any of the different Tokens (Username, X509, SAML, RSA, etc)
any hints are welcome