gpt4 book ai didi

java - 高级客户端 JAX-WS 配置

转载 作者:行者123 更新时间:2023-12-02 11:50:11 25 4
gpt4 key购买 nike

如何在不使用“http:conf”命名空间的情况下在 Web 服务的客户端配置(requestTimeout、responseTimeout、user 和 pass(可选)等)属性?

最佳答案

以下是我如何使用 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;
}
}

关于java - 高级客户端 JAX-WS 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47931883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com