Servlet filter for ColdFusion SOAP web service works for WSDL, but fails on SOAP requests

六月ゝ 毕业季﹏ 提交于 2019-12-12 18:26:21

问题


I'm trying to create a workaround for a terrible ColdFusion bug that results in my SOAP web service returning inconsistent results. What I want to do is replace "remote_api.tafkan" with "remote_api" in the output of any call to a CFC.

I've created a Java servlet filter to do this, and it works when I call the WSDL, but it breaks when I make a SOAP call to one of the service's methods.

Here's the code for my filter:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public final class PFAPICorrector implements Filter {
  private FilterConfig filterConfig = null;
  public void init(FilterConfig filterConfig) 
    throws ServletException {
    this.filterConfig = filterConfig;
  }
  public void destroy() {
    this.filterConfig = null;
  }
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
    throws IOException, ServletException {

    CharResponseWrapper wrapper = new CharResponseWrapper((HttpServletResponse)response);
    chain.doFilter(request, wrapper);

    System.out.println("ORIGINAL (" + wrapper.toString().length() + "): " + wrapper.toString());

    String correctedResponse = wrapper.toString().replace("remote_api.tafkan", "remote_api");
    response.setContentLength(correctedResponse.length());

    System.out.println("CORRECTED (" + correctedResponse.length() + "): " + correctedResponse);

    PrintWriter out = response.getWriter();
    out.write(correctedResponse);
    out.close();
  }
}

As I mentioned, calling the WSDL that includes the offending "remote_api.tafkan" string in its wsdl:documentation results in properly filtered output, and the ORIGINAL and CORRECTED debug statements print out the expected content.

However, when I make a SOAP request (using SoapUI) to one of the methods described by the WSDL, the filtering doesn't work (the string is not replaced), and I get the following in my CF log:

ORIGINAL (0): 
CORRECTED (0): 
08/05 11:00:11 error 
java.lang.IllegalStateException
    at jrun.servlet.JRunResponse.getWriter(JRunResponse.java:198)
    at PFAPICorrector.doFilter(PFAPICorrector.java:51)
    at jrun.servlet.FilterChain.doFilter(FilterChain.java:94)
    at jrun.servlet.FilterChain.service(FilterChain.java:101)
    at jrun.servlet.ServletInvoker.invoke(ServletInvoker.java:106)
    at jrun.servlet.JRunInvokerChain.invokeNext(JRunInvokerChain.java:42)
    at jrun.servlet.JRunRequestDispatcher.invoke(JRunRequestDispatcher.java:249)
    at jrun.servlet.ServletEngineService.dispatch(ServletEngineService.java:543)
    at jrun.servlet.jrpp.JRunProxyService.invokeRunnable(JRunProxyService.java:203)
    at jrunx.scheduler.ThreadPool$ThreadThrottle.invokeRunnable(ThreadPool.java:428)
    at jrunx.scheduler.WorkerThread.run(WorkerThread.java:66)

It seems like one of the filters prior to mine is outputting the SOAP response and terminating the filter chain, so my filter never sees the response, hence the lack of data in ORIGINAL in my debug output. (I may be running into this issue, but the hotfix there didn't change anything.)

Without being able to see what's going on inside ColdFusion's servlets and filters, how can I modify the SOAP response?

来源:https://stackoverflow.com/questions/6958459/servlet-filter-for-coldfusion-soap-web-service-works-for-wsdl-but-fails-on-soap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!