
/*
 *  Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
*/
package wssunt.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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

import wssunt.HelloWorld;


public class TestWebService extends HttpServlet {
    private static final String CONTENT_TYPE = "text/html; charset=ISO-8859-1";

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //JDK 1.7 U51 - work-arund
        System.setProperty("javax.xml.transform.TransformerFactory","com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
        response.setContentType(CONTENT_TYPE);
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>Test Web Service</title></head>");
        out.println("<body>");
        out.println("<a href=\"javascript:history.back()\">Back</a><br/><pre>");
        try {
            String address = request.getParameter("address");
            String wsdl = request.getParameter("wsdl");
            String name = request.getParameter("name");
            boolean usekey = Boolean.valueOf(request.getParameter("usecsfkey"));
            String csfkey = request.getParameter("csfkey");
            String pwd = request.getParameter("pwd");
            String user = request.getParameter("user");
            out.println("address=" + address);
            out.println("wsdl=" + wsdl);
            if (usekey) {
                out.println("csf-key=" + csfkey);
            } else {
                out.println("user=" + user);
                out.println("pwd=*****");
            }
            out.println("name=" + name);
            out.println("");

            String ret = null;
            if (usekey) {
                ret = HelloWorld.callUtilHello(address, wsdl, name, user, pwd, csfkey);
            } else {
                ret = HelloWorld.callUtilHello(address, wsdl, name, user, pwd, null);
            }
            out.println("Service Returned:<font color='green'>" + ret + "</font>");


        } catch (Exception e) {
            out.println("<font color='red'>Test failed.</font>\n");
            e.printStackTrace(out);
        }

        out.println("</pre></body></html>");
        out.close();
    }
}
