BlueLettuce16
BlueLettuce16

Reputation: 2103

Apache CXF Customization of service list page

I have a webpage which is generated by apache cxf that shows me all available soap webservices in my application with methods. I have also generated some javadocs for my webservices. I would like to customize list of webservices generated by apache cxf in tha way that name of the webservice is a link to javadoc and the same with method name. Is it possible? I wouldn't like to change code of apache cxf library. I'm using CXF version 2.3.7.

Upvotes: 0

Views: 1681

Answers (2)

CHW
CHW

Reputation: 2674

You can use a Servlet Filter to override the response returned by the servlet. Its not the most efficient solution, but you don't need to subclass any CXF classes.

The idea is to pass a custom instance of HttpServletResponseWrapper to the FilterChain.doFilter method. The wrapper prevents that the servlet response (i.e. the service list page) is written directly to the ouptut. So the response can be retrieved later inside the filter and modified before sending definitely to the client.

The following example is inspired from The Essentials of Filters.

public class DocumentationFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        CharResponseWrapper wrapper = new CharResponseWrapper((HttpServletResponse) response);

        chain.doFilter(request, wrapper);

        try (PrintWriter out = response.getWriter()) {
            if(wrapper.getContentType().startsWith("text/html")) {
                CharArrayWriter caw = new CharArrayWriter();
                // get the service list page
                String content = wrapper.toString();

                String newContent = ... // transform the service list page as needed
                caw.write(newContent);

                response.setContentLength(caw.toString().length());

                // send the response to the client
                out.write(caw.toString());
            } else {
                out.write(wrapper.toString());
            }
        }
    }

    @Override
    public void destroy() {
    }

    @Override
    public void init(FilterConfig filterConfig) {
    }

    public class CharResponseWrapper extends HttpServletResponseWrapper {

        private final CharArrayWriter output;

        public CharResponseWrapper(HttpServletResponse response){
            super(response);
            output = new CharArrayWriter();
        }

        @Override
        public PrintWriter getWriter(){
            return new PrintWriter(output);
        }

        @Override
        public String toString() {
            return output.toString();
        }

    }

}

Finally in your web.xml register the filter as follows. You need to adapt the url pattern to hit the service list page.

<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <filter>
        <filter-name>DocumentationFilter</filter-name>
        <filter-class>org.mypackage.DocumentationFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>DocumentationFilter</filter-name>
        <url-pattern>/</url-pattern> <!-- The location of the service list page -->
    </filter-mapping>
...
</web-app>

Upvotes: 0

Willem Jiang
Willem Jiang

Reputation: 3291

You can find the serviceList is generated from the ServiceListGeneratorServlet. But I think you need to create a new CXFNonSpringServlet to replace the ServletController which holds the ServiceListGeneratorServlet.

Upvotes: 1

Related Questions