package at.gv.egovernment.moa.id.auth.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import at.gv.egovernment.moa.id.auth.AuthenticationServer;
import at.gv.egovernment.moa.id.auth.MOAIDAuthInitializer;
import at.gv.egovernment.moa.id.auth.WrongParametersException;
import at.gv.egovernment.moa.id.config.auth.AuthConfigurationProvider;
import at.gv.egovernment.moa.id.util.MOAIDMessageProvider;
import at.gv.egovernment.moa.logging.Logger;
/**
* Servlet requested for selecting a BKU.
*
In case of {@link AuthConfigurationProvider#getBKUSelectionType}==HTMLComplete,
* the browser is redirected to the configured "BKU-Auswahl-URL".
*
In case of {@link AuthConfigurationProvider#getBKUSelectionType}==HTMLSelect,
* the list of available BKU's is fetched from a BKU-Auswahl server, and presented
* to the user in an HTML form.
*
* @author Paul Ivancsics
* @version $Id$
*/
public class SelectBKUServlet extends AuthServlet {
/**
* Calls the web application initializer.
*
* @see javax.servlet.Servlet#init(ServletConfig)
*/
public void init(ServletConfig servletConfig) throws ServletException {
try {
super.init(servletConfig);
MOAIDAuthInitializer.initialize();
Logger.info(MOAIDMessageProvider.getInstance().getMessage("init.00", null));
}
catch (Exception ex) {
Logger.fatal(MOAIDMessageProvider.getInstance().getMessage("init.02", null), ex);
throw new ServletException(ex);
}
}
/**
* Responds with an HTML form which requests the user to choose a BKU.
*/
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Logger.debug("GET SelectBKU");
String authURL =
req.getScheme() + "://" +
req.getServerName() + ":" +
req.getServerPort() +
req.getContextPath() + "/";
String target = req.getParameter(PARAM_TARGET);
String oaURL = req.getParameter(PARAM_OA);
String bkuSelectionTemplateURL = req.getParameter(PARAM_BKUTEMPLATE);
String templateURL = req.getParameter(PARAM_TEMPLATE);
resp.setHeader(HEADER_EXPIRES,HEADER_VALUE_EXPIRES);
resp.setHeader(HEADER_PRAGMA,HEADER_VALUE_PRAGMA);
resp.setHeader(HEADER_CACHE_CONTROL,HEADER_VALUE_CACHE_CONTROL);
resp.addHeader(HEADER_CACHE_CONTROL,HEADER_VALUE_CACHE_CONTROL_IE);
try {
String returnValue = AuthenticationServer.getInstance().selectBKU(
authURL, target, oaURL, bkuSelectionTemplateURL, templateURL);
String bkuSelectionType = AuthConfigurationProvider.getInstance().getBKUSelectionType();
if (bkuSelectionType.equals(AuthConfigurationProvider.BKU_SELECTION_TYPE_HTMLCOMPLETE)) {
// bkuSelectionType==HTMLComplete
String redirectURL = returnValue;
resp.sendRedirect(redirectURL);
Logger.info("REDIRECT TO: " + redirectURL);
}
else {
// bkuSelectionType==HTMLSelect
String htmlForm = returnValue;
resp.setContentType("text/html");
PrintWriter out = new PrintWriter(resp.getOutputStream());
out.print(htmlForm);
out.flush();
Logger.debug("Finished GET SelectBKU");
}
}
catch (WrongParametersException ex) {
handleWrongParameters(ex, req, resp);
}
catch (Throwable ex) {
handleError(null, ex, req, resp);
}
}
}