GenericController.java

  1. /*
  2.  * This software was designed and created by Jason Carroll.
  3.  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
  4.  * The author can be reached at jcarroll@cowsultants.com
  5.  * ITracker website: http://www.cowsultants.com
  6.  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it only under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  */

  18. package org.itracker.web.servlets;

  19. import org.apache.commons.lang.StringUtils;
  20. import org.apache.log4j.Logger;
  21. import org.apache.struts.Globals;
  22. import org.apache.struts.action.ActionErrors;
  23. import org.itracker.model.PermissionType;
  24. import org.itracker.model.User;
  25. import org.itracker.services.ITrackerServices;
  26. import org.itracker.model.util.UserUtilities;
  27. import org.itracker.web.util.Constants;
  28. import org.itracker.web.util.LoginUtilities;
  29. import org.itracker.web.util.RequestHelper;
  30. import org.itracker.web.util.ServletContextUtils;

  31. import javax.servlet.RequestDispatcher;
  32. import javax.servlet.ServletContext;
  33. import javax.servlet.ServletException;
  34. import javax.servlet.http.HttpServlet;
  35. import javax.servlet.http.HttpServletRequest;
  36. import javax.servlet.http.HttpServletResponse;
  37. import javax.servlet.http.HttpSession;
  38. import java.io.IOException;
  39. import java.util.Locale;
  40. import java.util.Map;
  41. import java.util.Set;


  42. /**
  43.  * TODO: Rewrite Javadocs here
  44.  * This needs documentation.
  45.  * Is it still used?
  46.  * What is it used for?
  47.  * How?
  48.  * It is referenced by
  49.  * AttachementDownloadController, (@deprecated Use org.itracker.web.actions.admin.attachment.DownloadAttachmentAction instead.)
  50.  * ReportChartController,
  51.  * ReportDownloadController
  52.  *
  53.  * @author ready
  54.  */
  55. public abstract class GenericController extends HttpServlet {

  56.     /**
  57.      *
  58.      */
  59.     private static final long serialVersionUID = 1L;
  60.     private static final Logger logger = Logger.getLogger(GenericController.class);

  61.     public GenericController() {
  62.     }

  63.     protected static void saveMessages(HttpServletRequest request, ActionErrors errors) {

  64.         if ((errors == null) || errors.isEmpty()) {
  65.             request.removeAttribute(Globals.ERROR_KEY);
  66.             return;
  67.         }
  68.         request.setAttribute(Globals.ERROR_KEY, errors);
  69.     }

  70.     protected static void forward(String url, HttpServletRequest request, HttpServletResponse response)
  71.             throws IOException, ServletException {
  72.         RequestDispatcher rd = request.getRequestDispatcher(url);
  73.         if (rd == null) {
  74.             throw new ServletException("RequestDispatcher is null. URL: " + url);
  75.         }

  76.         rd.forward(request, response);
  77.     }

  78.     protected static void redirect(String url, HttpServletRequest request, HttpServletResponse response)
  79.             throws IOException, ServletException {

  80.         String baseURL = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() +
  81.                 request.getContextPath();

  82.         response.sendRedirect(baseURL + url);
  83.     }

  84.     public static Locale getLocale(HttpServletRequest request) {
  85.         return LoginUtilities.getCurrentLocale(request);
  86.     }


  87. }