
/*
 *  Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
*/
package oracle.jcs.ws.sample.saml.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 oracle.jcs.ws.sample.saml.proxy.HelloWorldPortClient;

public class TestSamlWs extends HttpServlet {
    @SuppressWarnings("compatibility:6483819646109056180")
    private static final long serialVersionUID = 1L;
    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-around
        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 wsdl = request.getParameter("wsdl");
            String address = request.getParameter("address");
            String policy = request.getParameter("policy");
            String issuername = request.getParameter("issuername");
            String name = request.getParameter("name");
            out.println("wsdl=" + wsdl);
            out.println("address=" + address);
            out.println("policy=" + policy);
            out.println("issuer name=" + issuername);
            out.println("name=" + name);

            String ret = null;
            String username = request.getRemoteUser();
            out.println("username=" + username);
            out.println("");
            
            ret = HelloWorldPortClient.callHelloWorld(wsdl, address, policy, issuername, name, username);
            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();
    }
}
