2012-05-07 104 views
14

我必須設置一些HTTP頭字段中的Apache CXF客戶端:的Apache CXF - 設置HTTP頭

我通過攔截器試了一下:

public class HttpHeaderInterceptor extends AbstractPhaseInterceptor<Message> { 

    private String userId; 
    private String xAuthorizeRoles; 
    private String host; 


    public HttpHeaderInterceptor() { 
     super(Phase.POST_PROTOCOL); 
    } 

    @Override 
    public void handleMessage(Message message) throws Fault { 
     Map<String, List> headers = (Map<String, List>) message.get(Message.PROTOCOL_HEADERS); 
     try { 
      System.out.println("HttpHeaderInterceptor Host: " + host + " UserId: " + userId + " X-AUTHORIZE-roles: " + xAuthorizeRoles); 
      headers.put("Host", Collections.singletonList(host)); 
      headers.put("UserId", Collections.singletonList(userId)); 
      headers.put("X-AUTHORIZE-roles", Collections.singletonList(xAuthorizeRoles)); 
     } catch (Exception ce) { 
      throw new Fault(ce); 
     } 
    } 

    public void setUserId(String userId) { 
     this.userId = userId; 
    } 

    public void setxAuthorizeRoles(String xAuthorizeRoles) { 
     this.xAuthorizeRoles = xAuthorizeRoles; 
    } 

    public void setHost(String host) { 
     this.host = host; 
    } 
} 
在我的動態客戶端類

的梅索德:

每個呼叫的用戶id和xAuthorizeRol:

public void setHttHeaderInterceptor(String userId, String xAuthorizeRoles){ 
    Client cxfClient = ClientProxy.getClient(this.abgWebServicePort); 
    HttpHeaderInterceptor httpHeaderInterceptor = new HttpHeaderInterceptor(); 
    httpHeaderInterceptor.setHost("example.org"); 
    httpHeaderInterceptor.setUserId(userId); 
    httpHeaderInterceptor.setxAuthorizeRoles(xAuthorizeRoles); 
    cxfClient.getOutInterceptors().add(httpHeaderInterceptor); 
} 

之前我調用遠程服務被稱爲es應該有所不同,但是當我通過tcpdump調用來檢查所有調用在頭字段中具有相同的值時。

任何想法?

+0

又見http://stackoverflow.com/q/3165647/55452 –

+0

相信沒有解決不了我的問題 – Alex

回答

12

我已經解決了我的問題:

將通過XML配置的攔截器:

<jaxws:client id="clientBean" serviceClass="org.example.service.ServicePortType" 
       address="example.org/src/service/ServicePort"> 
    <jaxws:outInterceptors> 
     <bean class="org.example.interceptor.HttpHeaderInterceptor"/> 
    </jaxws:outInterceptors> 
    <jaxws:properties> 
     <entry key="mtom-enabled" value="true"/> 
    </jaxws:properties> 
</jaxws:client> 

客戶端類中我改變setHttpHeaderInterceptor到

public void setHttpHeaderInterceptor(String userId, String xAuthorizeRoles){ 
    Client cxfClient = ClientProxy.getClient(this.servicePort); 
    cxfClient.getRequestContext().put("HTTP_HEADER_HOST", "example.org"); 
    cxfClient.getRequestContext().put("HTTP_HEADER_USER_ID", userId); 
    cxfClient.getRequestContext().put("HTTP_HEADER_X_AUTHORIZE-ROLES", xAuthorizeRoles); 
} 

攔截器類

@Override 
    public void handleMessage(Message message) throws Fault { 
     Map<String, List> headers = (Map<String, List>) message.get(Message.PROTOCOL_HEADERS); 
     try { 
      headers.put("Host", Collections.singletonList(message.get("HTTP_HEADER_HOST"))); 
      headers.put("KD_NR", Collections.singletonList(message.get("HTTP_HEADER_KD_NR"))); 
      headers.put("X-AUTHORIZE-roles", Collections.singletonList(message.get("HTTP_HEADER_X_AUTHORIZE-ROLES"))); 
     } catch (Exception ce) { 
      throw new Fault(ce); 
     } 
    } 

現在它的工作。

通過這種方法,我可以在運行時設置HTTP頭字段。

+0

我需要得到CXF頭SESSION_ID並通過Axis2的發送。那麼該怎麼做。? -thanks –

1

您應該使用:Phase.POST_LOGICAL而不是Phase.POST。這對我工作

+0

根據文檔,它似乎不是這樣做的階段;這是說,這種修改確實解決了CXF似乎隨機忽略標題的問題。 –

1

這是一個代碼片段複製自定義HTTP標頭(從請求)的響應在一個單一的CXF出攔截器。

public void handleMessage(SoapMessage message) throws Fault { 
    // Get request HTTP headers 
    Map<String, List<String>> inHeaders = (Map<String, List<String>>) message.getExchange().getInMessage().get(Message.PROTOCOL_HEADERS); 
    // Get response HTTP headers 
    Map<String, List<String>> outHeaders = (Map<String, List<String>>) message.get(Message.PROTOCOL_HEADERS); 
    if (outHeaders == null) { 
     outHeaders = new HashMap<>(); 
     message.put(Message.PROTOCOL_HEADERS, outHeaders); 
    } 
    // Copy Custom HTTP header on the response 
    outHeaders.put("myCustomHTTPHeader", inHeaders.get("myCustomHTTPHeader")); 
} 
0

如果需要設置標準的HTTP頭,那麼也可以使用http管道完成。

<http-conf:conduit 
     name="*.http-conduit"> 
<http-conf:client AllowChunking="false" AcceptEncoding="gzip,deflate" Connection="Keep-Alive" 
Host="myhost.com"/> 
</http-conf:conduit>