如何在不使用“http:conf”命名空间的情况下在Web服务的客户端配置(requestTimeout,responseTimeout,user和pass(可选)等)属性?
答案 0 :(得分:1)
以下是我使用CXF 3.0.3指定这些属性的方法:
<强>超时:强>
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() );
}
}
用户/密码:强>
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;
}
}