2014-01-28 131 views
0

我期待使用CXF構建獨立的ExactTarget SOAP客戶端。使用CXF的ExactTarget SOAP客戶端

我能夠使用Glassfish Metro創建客戶端,但由於未來的支持考慮,我們希望使用CXF。我發現了一箇舊的例子和相關的項目,但它太舊而無法使用。

目前我想了解如何在存根/端口對象上設置處理程序,並將動態用戶名和密碼傳遞給它。動態我的意思是:應用程序在運行時從用戶獲取用戶名和密碼。這裏是我目前有地鐵實現的代碼:

PartnerAPI service = new PartnerAPI(); 
Soap stub = service.getSoap();  
Map<String, Object> outProperties = new HashMap<String, Object>();   
Map ctx = ((BindingProvider) stub).getRequestContext(); 

requestContext.put(BindingProvider.USERNAME_PROPERTY, user); 
requestContext.put(BindingProvider.PASSWORD_PROPERTY, password); 

List<Handler> chain = new ArrayList<Handler>(); 
chain.add(new SecurityHandler()); 
((BindingProvider) stub).getBinding().setHandlerChain(chain); 

我想重用第4-6行的CXF實現,但由於它們依賴於com.sun.xml.wss.XWSSProcessor我不能用我有處理程序。

回答

0

這裏是代碼,做一切:

private static Soap createApiStub() { 
    PartnerAPI service = new PartnerAPI(); 
    Soap stub = service.getSoap();   
    Client client = org.apache.cxf.frontend.ClientProxy.getClient(stub);  

    Map<String, Object> outProps = new HashMap<String, Object>();   
    outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN); 
    outProps.put(WSHandlerConstants.USER, username);  
    outProps.put(WSHandlerConstants.PASSWORD_TYPE,WSConstants.PW_TEXT);   
    // Automatically adds a Base64 encoded message nonce and a created timestamp 
    outProps.put(WSHandlerConstants.ADD_UT_ELEMENTS,WSConstants.NONCE_LN + " " + WSConstants.CREATED_LN);  
    outProps.put(WSHandlerConstants.PW_CALLBACK_REF, new ClientPasswordCallback(username, password)); 
    WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps); 
    client.getOutInterceptors().add(wssOut); 

    //Enable GZip compression 
    Map<String, java.util.List<String>> httpHeaders = new HashMap<String, java.util.List<String>>(); 
    httpHeaders.put("Content-Encoding",Collections.singletonList("gzip")); 
    httpHeaders.put("Accept-Encoding",Collections.singletonList("gzip")); 
    Map<String, Object> reqContext = client.getRequestContext(); 
    reqContext.put(MessageContext.HTTP_REQUEST_HEADERS,httpHeaders); 

    return stub; 
} 

這裏是處理程序實現:

public class ClientPasswordCallback implements CallbackHandler { 

    private String username; 
    private String password; 

    public ClientPasswordCallback(String username, String password) { 
     this.username = username; 
     this.password = password; 
    } 

    public void handle(Callback[] callbacks) throws IOException, 
    UnsupportedCallbackException { 
     for (Callback callback: callbacks){ 
      if (callback instanceof WSPasswordCallback){ 
       WSPasswordCallback pc = (WSPasswordCallback) callback;    
       if (username.equals(pc.getIdentifier())) {     
        pc.setPassword(password);     
       } 
      } else if (callback instanceof NameCallback){ 
       throw new UnsupportedCallbackException(callback); 
      } else { 
       throw new UnsupportedCallbackException(callback); 
      }   
     } 
    } 
} 

answer幫我dynamiclly傳遞密碼。