Skip to main content

How do i add a soap header to an outbound request

3 replies [Last post]
rmenon13
Offline
Joined: 2010-02-25
Points: 0

Hello

I am trying to add a soap header containing an app id an authorization token to an outbound request and am completely stumped. I have been using setOutboundHeaders, and it seems that unless i explicitly specify my headers using something like 'authHeaderBinding.setOutboundHeaders(Headers.create(new QName("simpleHeader"),"stringValue"))' the entire request seems to get overwritten. HELP!!!! My code is below:

public class getQuote {

public static void main(byte[]RKDToken, String applicationID) {

try { // Call Web Service Operation
com.reuters.webservice.rkd.client.quote.Quotes1 service = new com.reuters.webservice.rkd.client.quote.Quotes1();

// Prepare to plug in the code that allows to read soap messages

com.sun.xml.ws.transport.http.client.HttpTransportPipe.dump = true;
com.sun.xml.ws.transport.http.HttpAdapter.dump = true;

// get the port

com.reuters.webservice.rkd.client.quote.IQuotes1 port = service.getHttpAndRKDTokenIQuotes1HttpAndRKDToken();
WSBindingProvider authHeaderBinding = (WSBindingProvider) port;

// TODO initialize WS operation arguments here

System.out.println("READY TO GENERATE THE REQUEST -----");

com.reuters.webservice.rkd.client.quote.RetrieveItemRequest1 request = new com.reuters.webservice.rkd.client.quote.RetrieveItemRequest1();

System.out.println("REQUEST GENERATED -------");

com.reuters.webservice.rkd.client.quote.ItemRequest quoteRequestContent = new com.reuters.webservice.rkd.client.quote.ItemRequest();

System.out.println("QUOTE REQUEST CONTENT INITIALIZED");

com.reuters.webservice.rkd.client.quote.InstrumentKey quoteInstrumentKey = new com.reuters.webservice.rkd.client.quote.InstrumentKey();
quoteInstrumentKey.setName("MSFT.O");
String quoteInstrument = quoteInstrumentKey.getName();
System.out.println("WE WILL RETRIEVE THE QUOTE FOR THIS INSTRUMENT ---- ");
System.out.println(quoteInstrument);

// Assign the quote instrument key to the item request in preparation to build out the request

quoteRequestContent.getRequestKey().add(quoteInstrumentKey);
request.getItemRequest().add(quoteRequestContent);

System.out.println("INSTRUMENT ASSIGNED TO REQUEST SUCCESSFULLY ------");

// Set the outbound authorization header

System.out.println("SET THE AUTHORIZATION HEADERS");
AuthorizationType authorization = new AuthorizationType();
System.out.println("SET THE APPLICATION ID---");
authorization.setApplicationID(applicationID);
System.out.println(authorization.getApplicationID());
System.out.println("SET THE TOKEN VALUE ------");
authorization.setToken(RKDToken);
System.out.println(authorization.getToken());
//
JAXBContext jaxbContext = null;
try {
// use the package you created your stub classes in
jaxbContext = JAXBContext.newInstance("quote");
} catch (JAXBException e) {
// throw new e(
// "Could not get the JAXB context for the stub package", e);
}

//authHeaderBinding.setOutboundHeaders(Headers.create(new QName("simpleHeader"),"stringValue"));
//Headers.creat
// System.out.println("The AuthHeader Binding:" + authHeaderBinding.toString());

// Initialize the response

com.reuters.webservice.rkd.client.quote.RetrieveItemResponse1 response = null;

// TODO process result here
System.out.println("MAKE THE REQUEST-------");
System.out.println(request.toString());
com.reuters.webservice.rkd.client.quote.RetrieveItemResponse1 result = port.retrieveItem1(request);
System.out.println("Result = "+result);
} catch (Exception ex) {
// TODO handle custom exceptions here
}

}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
bgottlieb
Offline
Joined: 2012-01-11
Points: 0

I just posted a question in this forum with a working example of adding the header to the request. Adding the header works just fine. Mine just fails because of a security issue.

See question: com.sun.xml.ws.message.saaj.SAAJHeader cannot be cast to com.sun.xml.ws.security.opt.impl.outgoing.SecurityHeader

michaleibl
Offline
Joined: 2010-09-07
Points: 0

I think you have an error in the
authHeaderBinding.setOutboundHeaders(Headers.create(new QName("simpleHeader"),"stringValue"));
I use
authHeaderBinding.setOutboundHeaders(new ObjectFactory().createAuthorization(authorization));
The object factory is the one from common package (generated by wsimport) and the method creates an instance of JAXBElement<AuthorizationType>.
.M.

marcos.fraga
Offline
Joined: 2011-12-27
Points: 0

Thank you for your answer it resolve the problem