a4544a5a0e622ef69e38641f87ab1b5685e05911Phill Cunnington/*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: LoginLogoutMapping.java,v 1.11 2009/06/03 20:46:50 veiming Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
a4544a5a0e622ef69e38641f87ab1b5685e05911Phill Cunnington * Portions Copyrighted 2011-2015 ForgeRock AS.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.sun.identity.authentication.UI;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.authentication.service.AuthD;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.common.ISLocaleContext;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.common.RequestUtils;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.shared.locale.L10NMessageImpl;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.shared.debug.Debug;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Locale;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.ResourceBundle;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.servlet.*;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.servlet.http.*;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Servlet mapping to forward
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * '/login' to '/UI/Login' and
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * '/logout' to '/UI/Logout'
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class LoginLogoutMapping extends HttpServlet {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static boolean isProductInitialize = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static final String bundleName = "amAuthUI";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static final String CANNOT_INIT_AUTH = "cannotInitAuth";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServletConfig config = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Initializes the servlet.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param config servlet config
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws ServletException if it fails to get servlet context.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void init(ServletConfig config) throws ServletException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster super.init(config);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster this.config = config;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster init(config.getServletContext());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Initializes the servlet.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param servletCtx servlet config
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws ServletException if it fails to get servlet context.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void init(ServletContext servletCtx) throws ServletException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (isProductInitialize) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean initialized = initializeAuth(servletCtx);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!initialized) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Locale locale = java.util.Locale.getDefault();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ResourceBundle rb = ResourceBundle.getBundle(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster bundleName, locale);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new ServletException(rb.getString(CANNOT_INIT_AUTH));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Set product initialize flag.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param initialized <code>true</code> if product is initialized.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static void setProductInitialized(boolean initialized) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster isProductInitialize = initialized;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8d3140b524c0e28c0a49dc7c7d481123ef3cfe11Chris Lee * Initializes OpenAM.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster **
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param servletCtx Servlet Context.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public boolean initializeAuth(ServletContext servletCtx) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster AuthD authD = AuthD.getAuth();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (authD == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster authD.setServletContext(servletCtx);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Intialize AdminTokenAction
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (Debug.getInstance("amLoginLogoutMapping").messageEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Debug.getInstance("amLoginLogoutMapping").message(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "LoginLogoutMapping.initializeAuth: " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "Initializing AdminTokenAction to use AuthN");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster com.sun.identity.security.AdminTokenAction
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .getInstance().authenticationInitialized();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return true;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Destroys the servlet.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void destroy() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * methods.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param request servlet request
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param response servlet response
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws ServletException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws java.io.IOException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster protected void processRequest(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster HttpServletRequest request,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster HttpServletResponse response
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ) throws ServletException, java.io.IOException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Check content length
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster RequestUtils.checkContentLength(request);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (L10NMessageImpl e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ISLocaleContext localeContext = new ISLocaleContext();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster localeContext.setLocale(request);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster java.util.Locale locale = localeContext.getLocale();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (Debug.getInstance("amLoginLogoutMapping").messageEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Debug.getInstance("amLoginLogoutMapping").message(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "LoginLogoutMapping: " + e.getL10NMessage(locale));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new ServletException(e.getL10NMessage(locale));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String servletPath = request.getServletPath();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String forwardUrl = "";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (servletPath.equals("/login")) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster forwardUrl = "/UI/Login";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else if (servletPath.equals("/logout")) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster forwardUrl = "/UI/Logout";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster RequestDispatcher dispatcher =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster config.getServletContext().getRequestDispatcher(forwardUrl);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster dispatcher.forward(request, response);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Handles the HTTP <code>GET</code> method.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param request servlet request.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param response servlet response.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws ServletException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws java.io.IOException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster protected void doGet(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster HttpServletRequest request,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster HttpServletResponse response
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ) throws ServletException, java.io.IOException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster processRequest(request, response);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Handles the HTTP <code>POST</code> method.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param request servlet request
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param response servlet response
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws ServletException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws java.io.IOException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster protected void doPost(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster HttpServletRequest request,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster HttpServletResponse response
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ) throws ServletException, java.io.IOException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster processRequest(request, response);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}