Using JAX-WS: How to Set a User Agent Property

I looked for it and found a few misses. I created a java client to use a web service using JAX-WS. Is there a way to use JAX to set the value of HTTP_USER_AGENT? I would like to have my own web services log when specific clients (mine) gain access to it, so I need an individual value.

I saw options where you set it in the system properties, but this does not seem to work. The generated JAX classes do not seem to have a direct reference to the connection object, so I don’t see how I can manipulate these classes.

Any help would be great. thanks ST

+8
java
source share
3 answers

The solution to this problem in JAX-WS is to implement the SoapMessage handler (Interface: SOAPHandler <SOAPMessageContext>). Inside this handler, you insert your HTTP header into possibly existing headers, then you transfer control to the next handler in the handler chain.

The concept of this chain of handlers is nice, you can have small classes for a specific purpose (security, logging, etc.).

In your client, you configure the chain of handlers before sending any request:

// HandlerChain installieren Binding binding = ((BindingProvider) port).getBinding(); List hchain = binding.getHandlerChain(); if (hchain == null) { hchain = new ArrayList(); } hchain.add(new HTTPUserAgentHandler()); binding.setHandlerChain(hchain); 

And here is the code for HTTPUserAgentHandler:

 public class HTTPUserAgentHandler implements SOAPHandler<SOAPMessageContext> { @Override public boolean handleMessage(SOAPMessageContext context) { boolean request = ((Boolean) context.get(SOAPMessageContext.MESSAGE_OUTBOUND_PROPERTY)).booleanValue(); if (request) { @SuppressWarnings("unchecked") Map<String, List<String>> headers = (Map<String, List<String>>) context .get(MessageContext.HTTP_REQUEST_HEADERS); if (null == headers) { headers = new HashMap<String, List<String>>(); } headers.put("HTTP_USER_AGENT", Collections.singletonList("user_agent")); context.put(MessageContext.HTTP_REQUEST_HEADERS, headers); } return true; } @Override public boolean handleFault(SOAPMessageContext context) { return true; } @Override public void close(MessageContext context) {} @Override public Set<QName> getHeaders() { return null; } } 
+11
source share

Not sure if this is the best / most direct way to do this, but I think you can add custom javax.xml.ws.handler.Handler to the handler chain in the javax.xml.ws.Binding manager. in the handler, you should be able to configure a custom map of additional HTTP headers in the outgoing MessageContext using the MessageContext.HTTP_REQUEST_HEADERS property.

+2
source share

Let me first ask for the idea of ​​an HTTP header.

A more correct (WS-oriented) approach is to set the SOAP header rather than the HTTP header. Consider this: SOAP messages can be delivered not only in HTTP, but also in JMS, SMTP, or user transports. Requiring you to have an HTTP header User-agent, you do not necessarily associate the code with only one transport, although it currently prevails.

This is why BTW is why JAX-WS has no concept of HTTP headers other than handlers.

And (of course) StackOverlow knows how to create SOAP headers.

+2
source share

All Articles