从Web服务响应中提取Http Content-length

时间:2012-09-12 08:44:45

标签: web-services http soap cxf

我想从响应中提取HTTP标头,更准确地说是响应的内容长度,以便在超过特定大小时引发异常。我一直试图这样做两天,但没有任何成功。我怎么能这样做,为此使用拦截器或处理程序更干净。

这是一个用于记录标题的处理程序,但它不返回任何内容

public class HeaderHandler implements SOAPHandler<SOAPMessageContext> {

private final static Logger log = Logger.getLogger(HeaderHandler.class);
private PrintStream out;

public HeaderHandler() {
    setLogStream(System.out);
}

protected final void setLogStream(PrintStream ps) {
    out = ps;
}

public void init(Map c) {
    System.out.println("HeaderHandler : init() Called....");
}

public Set<QName> getHeaders() {
    return null;
}

public boolean handleMessage(SOAPMessageContext smc) {
    System.out.println("HeaderHandler : handleMessage Called....");
    logToSystemOut(smc);
    return true;
}

public boolean handleFault(SOAPMessageContext smc) {
    System.out.println("HeaderHandler : handleFault Called....");
    logToSystemOut(smc);
    return true;
}

// nothing to clean up
public void close(MessageContext messageContext) {
    System.out.println("HeaderHandler : close() Called....");
}

// nothing to clean up
public void destroy() {
    System.out.println("HeaderHandler : destroy() Called....");
}

/*
 * Check the MESSAGE_OUTBOUND_PROPERTY in the context
 * to see if this is an outgoing or incoming message.
 * Write a brief message to the print stream and
 * output the message. The writeTo() method can throw
 * SOAPException or IOException
 */
protected void logToSystemOut(SOAPMessageContext smc) {
    Boolean outboundProperty = (Boolean)
            smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

    if (outboundProperty.booleanValue()) {
        out.println("\nOutbound message:");
    } else {
        out.println("\nInbound message:");
    }
    out.println("\n***********************************************************************************");
    final Object o = smc.get(MessageContext.HTTP_RESPONSE_HEADERS);
    if (o != null) {
        out.println(o);
    } else {
        out.println("o null");
    }
}

}

0 个答案:

没有答案