package com.example.servlet;

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

import java.io.IOException;
import java.net.URLEncoder;


/**
 * The saasid value is the GUID associated with the application. This is displayed in parentheses below the 
 * application name on the My Applications page.
 * You will need to replace the saasid value in "${saasId}" in the sample below with the GUID for your application. 
 * For example: final String saasId = URLEncoder.encode("18a6ada7-8f37-4d77-86f4-173046796193", "UTF-8");
 */

public class SaasExampleRedirectServlet
    extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException
    {
    
        final String saasId = URLEncoder.encode("${saasId}", "UTF-8");
        String idpId = URLEncoder.encode(getIdpIdFromRequest(req), "UTF-8");
        String location = String.format("https://sso.connect.pingidentity.com/sso/sp/initsso?saasid=%s&idpid=%s",
                                        saasId,
                                        idpId);
        resp.sendRedirect(location);
    }

    /**
     * Implement me!
     */
    public String getIdpIdFromRequest(HttpServletRequest req)
    {
        return "testidp.admin.pingidentity.com";           
    }
}