Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache CXF - Set HTTP header

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

I tried it via Interceptor:

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

in my dynamic client class the methode:

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

is called before I invoke the remote service:

For each call the userId and the xAuthorizeRoles should vary but when I inspect by calls via tcpdump all calls have the same values in the header fields.

Any ideas?

like image 497
Alex Avatar asked May 07 '12 09:05

Alex


People also ask

How do I generate a class from WSDL using Apache CXF?

You will have to make sure that you create an appropriate directory structure for your project and add the earlier shown hello. wsdl file to the specified folder. The wsdl2java plugin will compile this wsdl and create Apache CXF classes in a pre-defined folder.

What is CXF interceptor?

Interceptors and Phases Interceptors are the fundamental processing unit inside CXF. When a service is invoked, an InterceptorChain is created and invoked. Each interceptor gets a chance to do what they want with the message. This can include reading it, transforming it, processing headers, validating the message, etc.

What is http Conf conduit?

public abstract class HTTPConduit extends AbstractConduit implements Configurable, Assertor, PropertyChangeListener. This Conduit handles the "http" and "https" transport protocols. An instance is governed by policies either explicitly set or by configuration.

What is HTTP header?

An HTTP header is a field of an HTTP request or response that passes additional context and metadata about the request or response. For example, a request message can use headers to indicate it's preferred media formats, while a response can use header to indicate the media format of the returned body.


2 Answers

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.

like image 78
Alex Avatar answered Sep 28 '22 00:09

Alex


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

like image 37
NVN Avatar answered Sep 27 '22 23:09

NVN