⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 headerfilter.java

📁 噶额外噶外骨骼感广泛高热感 就 啊啊
💻 JAVA
字号:
/* * @author                        : Neelesh * @Version                       : 1.0 * * Development Environment        : Oracle9i JDeveloper * Name of the Application        : HeaderFilter.java * Creation/Modification History  : * * Neelesh      17-March-2003      Created * */package oracle.otnsamples.vsm;import java.io.IOException;import java.security.Principal;import java.util.Hashtable;import java.util.Locale;import java.util.ResourceBundle;import javax.naming.InitialContext;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import oracle.otnsamples.util.Utilities;import oracle.otnsamples.util.ServiceLocator;import oracle.otnsamples.vsm.services.DataPopulator;import oracle.otnsamples.vsm.services.DataPopulatorHome;import oracle.otnsamples.vsm.services.MallNavigation;import oracle.otnsamples.vsm.services.MallNavigationHome;import oracle.otnsamples.vsm.services.data.Country;import org.apache.struts.action.Action;/** * This class is a filter for all requests. The filter is used to set the * header and footer for the requested page, based on the role of the user The * filter stores the user id in session for use by other actions. The filter * sets the Struts locale, based on user selection/browser locale. * @author Neelesh * @version 1.0 */public class HeaderFilter implements Filter {  // Cache of locales  private static Hashtable localeTable = new Hashtable();  // Filter config  private FilterConfig filterConfig = null;  // Bundle containing header/footer info  private ResourceBundle rb;  // The character encoding for the application  private String encoding;  /**   * Loads the resource bundle with name "uiresources"   *   * @param <b>filterConfig</b> Filter configuration   */  public void init(FilterConfig filterConfig) throws ServletException {    this.filterConfig   = filterConfig;    rb                  = ResourceBundle.getBundle("uiresources");    encoding=  filterConfig.getInitParameter("encoding");    // Populate data    MallNavigationHome mallHome     = null;    MallNavigation navigation       = null;    DataPopulator populator         = null;    DataPopulatorHome populatorHome = null;        try {      InitialContext ctx = new InitialContext();      java.util.Properties props = Utilities.loadParams("Misc");      JAASManager userManager =(JAASManager)Class.forName(props.                         getProperty("jaas.manager",                         "oracle.otnsamples.vsm.OC4JJAASManager")).newInstance();      userManager.init(filterConfig.getServletContext());      ctx.rebind("java:/comp/env/UserManager",userManager);      mallHome =        (MallNavigationHome) ServiceLocator.getLocator().getService("MallNavigation");      navigation = mallHome.create();      // Check for presence of test data, by getting countries      Country[] countries = navigation.getCountries();      // if no countries, populate data      if(countries == null || countries.length == 0) {        populatorHome =          (DataPopulatorHome) ServiceLocator.getLocator().getService("DataPopulator");        populator = populatorHome.create();        populator.populate();      }    } catch(Exception ex) {      ex.printStackTrace();    } finally {      // Cleanup service beans      try {        if(navigation != null) {          navigation.remove();        }        if(populator != null) {          populator.remove();        }      } catch(Exception e) {      }    }  }  /**   * Filter lifecycle method. Sets the config to null   */  public void destroy() {    this.filterConfig = null;  }  /**   * The method does the actual filtering work If there is a user logged in,   * and the user is shopowner or shopuser, the corresponding header/footer   * JSPs are set in request If the user has requested a locale change, the   * session wide locale is changed to the requested locale If the user has   * logged in, the user name is stored in session for use by other modules   *   * @param <b>request</b> -ServletRequest   * @param <b>response</b> -ServletResponse   * @param <b>chain</b> - Filter chain   *   * @throws <b>IOExcpetion</b>   * @throws <b>ServletExcpetion</b>   */  public void doFilter(ServletRequest request, ServletResponse response,                       FilterChain chain) throws IOException,                                                  ServletException {    // Set the character encoding    request.setCharacterEncoding("UTF8");    HttpSession session = ((HttpServletRequest) request).getSession();    // Find logged in user    HttpServletRequest req = (HttpServletRequest) request;    Principal principal    = req.getUserPrincipal();    if(principal != null) {      String userName = principal.getName();      // Remove realm name and retain only user name      userName =        userName.substring(userName.indexOf('/') + 1, userName.length());      // set appropriate header/footers, based on roles      if(req.isUserInRole("shopuser")) {        request.setAttribute("Header", rb.getString("user.HEADER"));        request.setAttribute("Footer", rb.getString("user.FOOTER"));        // set the user name in session        if(session.getAttribute("userName") == null) {          session.setAttribute("userName", userName);        }      } else if(req.isUserInRole("shopowner")) {        request.setAttribute("Header", rb.getString("owner.HEADER"));        request.setAttribute("Footer", rb.getString("owner.FOOTER"));        // set the user name in session        if(session.getAttribute("userName") == null) {          session.setAttribute("userName", userName);        }      } else {        // unknown user .So use default headers and footers        request.setAttribute("Header", rb.getString("user.HEADER"));        request.setAttribute("Footer", rb.getString("user.FOOTER"));      }    } else {      // nobody has logged in. So use default header/footer      request.setAttribute("Header", rb.getString("user.HEADER"));      request.setAttribute("Footer", rb.getString("user.FOOTER"));    }    // Handle locales    Locale requestedLocale = null;    String lang = request.getParameter("lang");        // If the user has requested for a specific language    if(lang != null) {      // check if the locale for the requested language is in the localetable      requestedLocale = (Locale) localeTable.get(lang);            // if not present, create a new locale and store it in the table      if(requestedLocale == null) {        // Locale is created with no country        requestedLocale = new Locale(lang, "");        localeTable.put(lang, requestedLocale);      }      // change current locale      session.setAttribute(Action.LOCALE_KEY, requestedLocale);          } else {      if(session.getAttribute(Action.LOCALE_KEY) == null) {        session.setAttribute(Action.LOCALE_KEY, request.getLocale());              }    }    // forward to next filter/servlet in the chain    chain.doFilter(request, response);  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -