0

CXF を使用してスタンドアロンの ExactTarget SOAP クライアントを構築しようとしています。

Glassfish Metro を使用してクライアントを作成できましたが、今後のサポートを考慮して、CXF を使用したいと考えています。古い例と関連するプロジェクトを見つけましたが、古すぎて役に立ちません。

現在、スタブ/ポート オブジェクトにハンドラーを設定し、それに動的なユーザー名とパスワードを渡す方法を理解しようとしています。動的とは、アプリが実行時にユーザーからユーザー名とパスワードを取得することを意味します。これは、Metro 実装用に現在持っているコードです。

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);

CXF 実装の最初の 4 ~ 6 行を再利用しようとしていますが、に依存しているため、持っているハンドラーを使用できませんcom.sun.xml.wss.XWSSProcessor

4

1 に答える 1

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);
            }           
        }
    }
}

この回答は、パスワードを動的に渡すのに役立ちました。

于 2014-01-28T19:07:46.767 に答える