在servlet中转发请求

时间:2015-06-03 09:50:57

标签: java web-services servlets forward

所以我试图创建一个servlet来处理端点上对WSDL的所有请求,然后发送WSDL(现在它只是磁盘上的一个文件,之后,我计划好了)根据客户端的权限稍微更改该文件)。我尝试实现这一点的方法是创建一个WSDLHandlerServlet类,该类在web.xml中映射到/ *。我理解的方式 - 所有请求都应该被截获。

一旦传递给servlet,它基本上检查带有查询字符串的URI是否为 - " / TestProject / pace / soap?wsdl"如果是这样,则返回WSDL。当我打开" localhost:8180 / TestProject / pace / soap?wsdl"这部分工作正常。在浏览器中,它显示正确的WSDL。

如果请求没有查询字符串,那么我将它转发到链接到我的Web服务实现者的cxf servlet。

但是,当我从客户端发送请求(SOAP请求)时,我得到了 -

Exception in thread "main" com.sun.xml.internal.ws.client.ClientTransportException: The server sent HTTP status code 200: OK  

此外,显然没有达到实施类,因为如果确实如此,就会有一个日志。我怀疑我最有可能搞砸了servlet映射。

这是WSDLHandler Servlet -

package servlets;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WSDLHandlerServlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    public void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // This is just a test.. anything without a query string will be sent to
        // cxf servlet... and any long request ending with ?wsdl will yield the
        // wsdl.. need to work on the design...never mind now it is time to
        // begin!!
        String query = new String();
        query = request.getQueryString();
        /*
         * File abc = new File("/home/aneeshb/testing.txt"); FileWriter fw = new
         * FileWriter(abc);
         * 
         * 
         * if(!query.isEmpty()){ fw.write(query); fw.close();}
         */String requestURI = request.getRequestURI();
        File abc = new File("/home/aneeshb/testing.txt");
        FileWriter fw = new FileWriter(abc);
        fw.write(requestURI);
        fw.append("1234");
        fw.append(query);
        fw.flush();

        if (query==null) {
            fw.append("2");
            fw.flush();
            RequestDispatcher dispatch = request.getRequestDispatcher("/pace");
            fw.append("2");
            fw.flush();
            dispatch.forward(request, response);
            fw.append("2");
            fw.flush();
        }

        else if (requestURI.equals("/TestProject/pace/soap")
                && query.equals("wsdl")) {
            fw.append("This is in part1 ");
            fw.flush();
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/xml");
            PrintWriter respWrite = response.getWriter();
            File wsdlFile = new File(
                    "/home/aneeshb/Workspaces/Project1/TestProject/WebContent/WEB-INF/wsdl/stock.wsdl");
            FileReader wsdlRead = new FileReader(wsdlFile);
            BufferedReader wsdlBuffRead = new BufferedReader(wsdlRead);
            int temp = 0;
            while ((temp = wsdlBuffRead.read()) != -1) {
                respWrite.write(temp);
            }
            respWrite.flush();
            wsdlBuffRead.close();
        }

        else {
            throw new IndexOutOfBoundsException();
        }

    }

}

我的web.xml -

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-insance"
    version="2.5"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee           http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>cxf</display-name>
    <servlet>
        <description>Apache CXF Endpoint</description>
        <display-name>cxf</display-name>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
        <description>WSDL Handler</description>
        <display-name>wsdlh</display-name>
        <servlet-name>wsdlh</servlet-name>
        <servlet-class>servlets.WSDLHandlerServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/pace</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>wsdlh</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>
</web-app>

只是为了好的衡量 - cxf servlet配置文件 -

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:soap="http://cxf.apache.org/bindings/soap" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd" xmlns:tns="http://impl/">
    <jaxws:server id="jaxwsService" serviceClass="impl.GatewayImplementor" address="/soap" >
        <jaxws:serviceBean>
            <bean class="impl.GatewayImplementor"/>
        </jaxws:serviceBean>
    </jaxws:server>
</beans>

谢谢,对我所做错的任何帮助或对替代解决方案的想法都会很棒!

0 个答案:

没有答案