Apache CXF - Set HTTP header

后端 未结 4 856
时光取名叫无心
时光取名叫无心 2021-01-04 00:01

I have to set some http header fields in a Apache CXF client:

I tried it via Interceptor:

    public class HttpHeaderInterceptor extends AbstractPhas         


        
相关标签:
4条回答
  • 2021-01-04 00:12

    You should have used :Phase.POST_LOGICAL instead of Phase.POST. This worked for me

    0 讨论(0)
  • 2021-01-04 00:13

    If required to set standard HTTP header then it can be done using http conduit also.

    <http-conf:conduit
            name="*.http-conduit">
    <http-conf:client AllowChunking="false" AcceptEncoding="gzip,deflate" Connection="Keep-Alive"
     Host="myhost.com"/>
    </http-conf:conduit>
    
    0 讨论(0)
  • 2021-01-04 00:21

    Here is a code snippet to copy a custom HTTP header (from the request) on the response in a single CXF out interceptor.

    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 讨论(0)
  • 2021-01-04 00:34

    I Have solved my problem:

    adding the interceptor via xml configuration:

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

    in the client class I altered setHttpHeaderInterceptor to

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

    the interceptor class

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

    and now it work's.

    With this approach I can set HTTP-Header fields at runtime.

    0 讨论(0)
提交回复
热议问题