Home » Java » java – Apache CXF 2.7 context passing issues-Exceptionshub

java – Apache CXF 2.7 context passing issues-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I wrote a logging filter and pass around certain information from ContainerRequestContext to WriterInterceptorContext using the setProperty() function of ContainerRequestContext.

I am able to retrieve the property I set using getProperty when using with a Jersey implementation.

But the same does not work with the Apache CXF 2.7. Any inputs?

public class LoggingFilter implements ContainerRequestFilter, ContainerResponseFilter,WriterInterceptor {

@Override
public void filter(ContainerRequestContext reqContext) throws IOException {
  reqContext.setProperty("sampleProperty", "sampleValue");
}

@Override
public void aroundWriteTo(WriterInterceptorContext context)
    throws IOException, WebApplicationException {
 if (context.getProperty("sampleProperty") != null) { -> passes for jersey, fails for Apache CXF 2.0
 }
}
How to&Answers:

I found the solution to get the context.

public class LoggingFilter implements ContainerRequestFilter, ContainerResponseFilter,WriterInterceptor {

@Override
public void filter(ContainerRequestContext reqContext) throws IOException {
  reqContext.setProperty("sampleProperty", "sampleValue");
}

@Override
public void aroundWriteTo(WriterInterceptorContext context)
    throws IOException, WebApplicationException {
    Message message = JAXRSUtils.getCurrentMessage();
    ContainerRequestContext reqContext = new ContainerRequestContextImpl(message.getExchange().getInMessage(),
            false, true);
 if (reqContext.getProperty("sampleProperty") != null) { 
 }
}