Benedict cross
Benedict cross

Reputation: 1

Advanced client-side JAX-WS configuration

How can I configure (requestTimeout, responseTimeout, user and pass (optionable) and so on) properties on client's side of web service without using "http:conf" namespace?

Upvotes: 0

Views: 588

Answers (1)

tsolakp
tsolakp

Reputation: 5948

Here is how I have specified those properties using CXF 3.0.3:

timeout:

 public void setTimeout(Integer timeout) {
        setServiceTimeout(timeout);
        setWsTimeout(timeout);
    }

public static void setServiceTimeout(Object port, Integer timeout) {
    if (timeout != null){
        if (timeout <= 0) timeout = -1;
        else timeout *= 1000;  
    } 
    if(port instanceof javax.xml.ws.BindingProvider) {
        Map<String, Object> context = ((BindingProvider)port).getRequestContext();
        context.put("com.sun.xml.ws.request.timeout", timeout);
        context.put("com.sun.xml.ws.connect.timeout", timeout);
        context.put("com.sun.xml.internal.ws.request.timeout", timeout);
        context.put("com.sun.xml.internal.ws.connect.timeout", timeout);
        context.put("org.jboss.ws.timeout", timeout);
    }else {
        throw new RuntimeException( "Cannot set service timeout on object of type " + port.getClass().getName() );
    }
}

public static void setWSTimeout(Object port, Integer timeoutInMilliseconds) {
    if(port instanceof javax.xml.ws.BindingProvider) {
        java.util.Map<String, Object> requestContext = ( (javax.xml.ws.BindingProvider) port ).getRequestContext();
        requestContext.put("javax.xml.ws.client.connectionTimeout", timeoutInMilliseconds);
        requestContext.put("javax.xml.ws.client.receiveTimeout", timeoutInMilliseconds);
    } else {
        throw new RuntimeException( "Cannot set timeout on object of type " + port.getClass().getName() );
    }
}

User/Password:

public static class UserTokenSecurityHandler extends AbstractSoapHeaderHandler{
        private static final String SECURITY_PREFIX = "wsse";
        private static final String SECURITY_ELEMENT_NAME = "Security";
        private static final String SECURITY_URI = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";

        private static final String USERNAME_TOKEN_ELEMENT_NAME = "UsernameToken";
        private static final String USERNAME_TOKEN_URI = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";

        private static final String USERNAME_ELEMENT_NAME = "Username";
        private static final String PASSWORD_ELEMENT_NAME = "Password";

        private static final String TYPE_ELEMENT_NAME = "Type";
        private static final String TYPE_URI = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText";

        private UserTokenSecurityHandler(){             
        }

        private UserTokenSecurityHandler(String propertyPrefix, String propertySuffix){     
            userNameProps().keys(propertyPrefix + "security.usertoken.username" + propertySuffix);
            tokenProps().keys(propertyPrefix + "security.usertoken.token" + propertySuffix);
        }


        @Override
        protected void populateHeader(SOAPHeader header) throws Exception{
            SOAPElement security = header.addChildElement(SECURITY_ELEMENT_NAME, SECURITY_PREFIX, SECURITY_URI);

            SOAPElement usernameToken = security.addChildElement(USERNAME_TOKEN_ELEMENT_NAME, SECURITY_PREFIX);
            usernameToken.addAttribute( new QName("xmlns:wsu"), USERNAME_TOKEN_URI );

            SOAPElement username = usernameToken.addChildElement(USERNAME_ELEMENT_NAME, SECURITY_PREFIX);
            username.addTextNode( username );

            SOAPElement password = usernameToken.addChildElement(PASSWORD_ELEMENT_NAME, SECURITY_PREFIX);
            password.setAttribute(TYPE_ELEMENT_NAME, TYPE_URI);
            password.addTextNode( passowrd );
        }

        @Override
        public Set<QName> getHeaders() {
            QName securityHeader = new QName(SECURITY_URI, SECURITY_ELEMENT_NAME, SECURITY_PREFIX);
            HashSet<QName> headers = new HashSet<QName>();
            headers.add(securityHeader);
            return headers;
        }
    }

Upvotes: 1

Related Questions