tôi phải thiết lập một số lĩnh vực tiêu đề http trong một client Apache CXF:Apache CXF - Set HTTP header
Tôi đã thử nó qua 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;
}
}
trong lớp khách hàng năng động của tôi tole:
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);
}
được gọi là trước khi tôi gọi dịch vụ từ xa:
Đối với mỗi gọi userId và xAuthorizeRol es nên thay đổi nhưng khi tôi kiểm tra bằng các cuộc gọi qua tcpdump, tất cả các cuộc gọi có cùng giá trị trong các trường tiêu đề.
Bất kỳ ý tưởng nào?
xem thêm http://stackoverflow.com/q/3165647/55452 –
rằng đã không giải quyết vấn đề của tôi – Alex