Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Tuckey urlrewrite in spring boot to access service using ?wsdl

My clients wanted to access wsdl using ?wsdl notation, but the springboot service i have created is working with only '.wsdl' format. I need a working example/sample how to configure Tuckey urlrewrite in the springboot application. I have tried using below code, but the application complaints as it cant find urlrewrite.xml (which i have placed in src/main/resources folder.

Q1: How can i make my service to be accessible using url below http://localhost:8080/ws/organisation?wsdl

I have tried using below code, but tuckey cannot find the urlrewrite.xml which is under src/java/resources.

@Bean
public FilterRegistrationBean tuckeyRegistrationBean() {
    final FilterRegistrationBean registrationBean = new ilterRegistrationBean();
    registrationBean.setFilter(new UrlRewriteFilter());
    registrationBean.addInitParameter("confPath", "urlrewrite.xml");
    return registrationBean;
}
like image 333
SPagadala Avatar asked Nov 11 '16 04:11

SPagadala


2 Answers

Finally I could figure out a solution. This is now reading urlrewrite.xml from the src/main/resources folder.

No need to declare above mentioned bean definition in the question post (public FilterRegistrationBean tuckeyRegistrationBean()), as the below code declared as @Component will automatically register with context and url-rewriting is performed.

@Component
public class WsdlUrlRewriteFilter extends UrlRewriteFilter {

    private static final String CONFIG_LOCATION = "classpath:/urlrewrite.xml";

    @Value(CONFIG_LOCATION)
    private Resource resource;

    @Override
    protected void loadUrlRewriter(FilterConfig filterConfig) throws ServletException {
        try {
            Conf conf = new Conf(filterConfig.getServletContext(), resource.getInputStream(), resource.getFilename(), "");
        checkConf(conf);
        } catch (IOException ex) {
            throw new ServletException("Unable to load URL-rewrite configuration file from " + CONFIG_LOCATION, ex);
        }
    }
}
like image 192
SPagadala Avatar answered Oct 13 '22 00:10

SPagadala


Best option is to write your own filter as follows.You can make use of HttpServletRequestWrapper to handle the ?wsdl extension and let the server handle the request.

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.IOException;

@Component
public class WSDLQuestionMarkReplaceFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //put init logs
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        if ("wsdl".equalsIgnoreCase(httpRequest.getQueryString())) {
            HttpServletRequestWrapper requestWrapper = new HttpServletRequestWrapper(httpRequest) {
                @Override
                public String getQueryString() {
                    return null;
                }

                @Override
                public String getRequestURI() {
                    return super.getRequestURI() + ".wsdl";
                }
            };
            chain.doFilter(requestWrapper, response);
        } else {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {
        //put destroy logs
    }
}
like image 40
Javadroider Avatar answered Oct 12 '22 23:10

Javadroider