5.5. Access HTTP headers in a Handler

HTTP headers can be accessed in a Handler. Here is how you can access the Content-Type HTTP header in a Handler:

5.5.1. From Client side handler

HTTP headers can be accessed in a client side Handler in an incoming response. Here is Handler code that demonstrates how to do this:

ClientHandler.java

public class ClientHandler implements SOAPHandler<SOAPMessageContext> {
    public boolean handleMessage(SOAPMessageContext context) {
        if (!(Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)) {
            Map<String, List<String>> map = (Map<String, List<String>>) context.get(MessageContext.HTTP_RESPONSE_HEADERS);
            List<String> contentType = getHTTPHeader(map, "Content-Type");
            if(contentType != null){
                StringBuffer strBuf = new StringBuffer();
                for(String type : contentType){
                    strBuf.append(type);
                }
                System.out.println("Content-Type:"+strBuf.toString());
            }
        }
        return true;
    }


    private @Nullable
    List<String> getHTTPHeader(Map<String, List<String>> headers, String header){
        for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
            String name = entry.getKey();
            if(name.equalsIgnoreCase(header))
                return entry.getValue();
        }
        return null;
    }
}
            

5.5.2. From Server side handler

HTTP headers can be accessed in a server side Handler on an incoming request. Here is Handler code that demonstrates how to do this:

ServerHandler.java

public class ServerHandler implements SOAPHandler<SOAPMessageContext> {
    public boolean handleMessage(SOAPMessageContext context) {

        if(!(Boolean)context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)){
            Map<String, List<String>> map = (Map<String, List<String>>) context.get(MessageContext.HTTP_REQUEST_HEADERS);
            List<String> contentType = getHTTPHeader(map, "Content-Type");
            if(contentType != null){
                StringBuffer strBuf = new StringBuffer();
                for(String type : contentType){
                    strBuf.append(type);
                }
                System.out.println("Content-Type:"+strBuf.toString());
            }
        }

        return true;
    }

    private @Nullable
        List<String> getHTTPHeader(@NotNull Map<String, List<String>> headers, @NotNull String header){
        for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
            String name = entry.getKey();
            if(name.equalsIgnoreCase(header))
                return entry.getValue();
        }
        return null;
    }
}
        

Terms of Use; Privacy Policy; Copyright ©2013-2014 (revision 20140418.2d69abc)
 
 
Close
loading
Please Confirm
Close