下面的代码案例来源开源项目,你可以参考使用. 如果觉得好,请点个赞,鼓励一下。
Code Example 1:
From project alphaportal_dev, under directory /sys-src/alphaportal/web/src/main/java/alpha/portal/webapp/filter/.
Source LocaleFilter.java
/** * This method looks for a "locale" request parameter. If it finds one, it sets it as the preferred locale and also configures it to work with JSTL. * @param request the current request * @param response the current response * @param chain the chain * @throws IOException when something goes wrong * @throws ServletException when a communication failure happens */ @Override @SuppressWarnings("unchecked") public void doFilterInternal(HttpServletRequest request,final HttpServletResponse response,final FilterChain chain) throws IOException, ServletException { final String locale=request.getParameter("locale"); Locale preferredLocale=null; if (locale != null) { final int indexOfUnderscore=locale.indexOf('_'); if (indexOfUnderscore != -1) { final String language=locale.substring(0,indexOfUnderscore); final String country=locale.substring(indexOfUnderscore + 1); preferredLocale=new Locale(language,country); } else { preferredLocale=new Locale(locale); } } final HttpSession session=request.getSession(false); if (session != null) { if (preferredLocale == null) { preferredLocale=(Locale)session.getAttribute(Constants.PREFERRED_LOCALE_KEY); } else { session.setAttribute(Constants.PREFERRED_LOCALE_KEY,preferredLocale); Config.set(session,Config.FMT_LOCALE,preferredLocale); } if ((preferredLocale != null) && !(request instanceof LocaleRequestWrapper)) { request=new LocaleRequestWrapper(request,preferredLocale); LocaleContextHolder.setLocale(preferredLocale); } } final String theme=request.getParameter("theme"); if ((theme != null) && request.isUserInRole(Constants.ADMIN_ROLE)) { final Map<String,Object> config=(Map)this.getServletContext().getAttribute(Constants.CONFIG); config.put(Constants.CSS_THEME,theme); } chain.doFilter(request,response); LocaleContextHolder.setLocaleContext(null); }Code Example 2:
From project alphaportal_dev, under directory /sys-src/alphaportal/web/src/test/java/alpha/portal/webapp/filter/.
Source LocaleFilterTest.java
/** * Test jstl locale is set. * @throws Exception the exception */ public void testJstlLocaleIsSet() throws Exception { final MockHttpServletRequest request=new MockHttpServletRequest(); request.addParameter("locale","es"); final MockHttpServletResponse response=new MockHttpServletResponse(); request.setSession(new MockHttpSession(null)); this.filter.doFilter(request,response,new MockFilterChain()); Assert.assertNotNull(Config.get(request.getSession(),Config.FMT_LOCALE)); }
From project appfuse, under directory /web/common/src/main/java/org/appfuse/webapp/filter/.
Source LocaleFilter.java
/** * This method looks for a "locale" request parameter. If it finds one, it sets it as the preferred locale and also configures it to work with JSTL. * @param request the current request * @param response the current response * @param chain the chain * @throws IOException when something goes wrong * @throws ServletException when a communication failure happens */ @SuppressWarnings("unchecked") public void doFilterInternal(HttpServletRequest request,HttpServletResponse response,FilterChain chain) throws IOException, ServletException { String locale=request.getParameter("locale"); Locale preferredLocale=null; if (locale != null) { int indexOfUnderscore=locale.indexOf('_'); if (indexOfUnderscore != -1) { String language=locale.substring(0,indexOfUnderscore); String country=locale.substring(indexOfUnderscore + 1); preferredLocale=new Locale(language,country); } else { preferredLocale=new Locale(locale); } } HttpSession session=request.getSession(false); if (session != null) { if (preferredLocale == null) { preferredLocale=(Locale)session.getAttribute(Constants.PREFERRED_LOCALE_KEY); } else { session.setAttribute(Constants.PREFERRED_LOCALE_KEY,preferredLocale); Config.set(session,Config.FMT_LOCALE,preferredLocale); } if (preferredLocale != null && !(request instanceof LocaleRequestWrapper)) { request=new LocaleRequestWrapper(request,preferredLocale); LocaleContextHolder.setLocale(preferredLocale); } } chain.doFilter(request,response); LocaleContextHolder.setLocaleContext(null); }Code Example 4:
From project appfuse, under directory /web/common/src/test/java/org/appfuse/webapp/filter/.
Source LocaleFilterTest.java
public void testJstlLocaleIsSet() throws Exception { MockHttpServletRequest request=new MockHttpServletRequest(); request.addParameter("locale","es"); MockHttpServletResponse response=new MockHttpServletResponse(); request.setSession(new MockHttpSession(null)); filter.doFilter(request,response,new MockFilterChain()); assertNotNull(Config.get(request.getSession(),Config.FMT_LOCALE)); }Code Example 5:
From project appfuse_1, under directory /web/common/src/main/java/org/appfuse/webapp/filter/.
Source LocaleFilter.java
/** * This method looks for a "locale" request parameter. If it finds one, it sets it as the preferred locale and also configures it to work with JSTL. * @param request the current request * @param response the current response * @param chain the chain * @throws IOException when something goes wrong * @throws ServletException when a communication failure happens */ @SuppressWarnings("unchecked") public void doFilterInternal(HttpServletRequest request,HttpServletResponse response,FilterChain chain) throws IOException, ServletException { String locale=request.getParameter("locale"); Locale preferredLocale=null; if (locale != null) { int indexOfUnderscore=locale.indexOf('_'); if (indexOfUnderscore != -1) { String language=locale.substring(0,indexOfUnderscore); String country=locale.substring(indexOfUnderscore + 1); preferredLocale=new Locale(language,country); } else { preferredLocale=new Locale(locale); } } HttpSession session=request.getSession(false); if (session != null) { if (preferredLocale == null) { preferredLocale=(Locale)session.getAttribute(Constants.PREFERRED_LOCALE_KEY); } else { session.setAttribute(Constants.PREFERRED_LOCALE_KEY,preferredLocale); Config.set(session,Config.FMT_LOCALE,preferredLocale); } if (preferredLocale != null && !(request instanceof LocaleRequestWrapper)) { request=new LocaleRequestWrapper(request,preferredLocale); LocaleContextHolder.setLocale(preferredLocale); } } String theme=request.getParameter("theme"); if (theme != null && request.isUserInRole(Constants.ADMIN_ROLE)) { Map<String,Object> config=(Map)getServletContext().getAttribute(Constants.CONFIG); config.put(Constants.CSS_THEME,theme); } chain.doFilter(request,response); LocaleContextHolder.setLocaleContext(null); }Code Example 6:
From project appfuse_1, under directory /web/common/src/test/java/org/appfuse/webapp/filter/.
Source LocaleFilterTest.java
public void testJstlLocaleIsSet() throws Exception { MockHttpServletRequest request=new MockHttpServletRequest(); request.addParameter("locale","es"); MockHttpServletResponse response=new MockHttpServletResponse(); request.setSession(new MockHttpSession(null)); filter.doFilter(request,response,new MockFilterChain()); assertNotNull(Config.get(request.getSession(),Config.FMT_LOCALE)); }Code Example 7:
From project CaptureJSP, under directory /src/main/java/com/technologicaloddity/capturejsp/util/.
Source SwallowingJspRenderer.java
public String render(String viewName,Map<String,Object> modelMap,Locale locale) throws IOException { String result=null; if (locale == null) { locale=Locale.getDefault(); } StringWriter sout=new StringWriter(); StringBuffer sbuffer=sout.getBuffer(); HttpServletRequest request=new MockIncludedHttpServletRequest(); HttpServletResponse response=new MockHttpServletResponse(); HttpServletResponse swallowingResponse=new SwallowingHttpServletResponse(response,sout,"UTF-8"); LocaleResolver localeResolver=new JspLocaleResolver(); localeResolver.setLocale(request,swallowingResponse,locale); try { addModelAsRequestAttributes(request,modelMap); request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,localeResolver); LocalizationContext localizationContext=new LocalizationContext(null,locale); request.setAttribute(Config.FMT_LOCALIZATION_CONTEXT + ".request",localizationContext); request.setAttribute(Config.FMT_LOCALE,locale); swallowingResponse.setContentType("text/html; charset=utf-8"); RequestDispatcher dispatcher=servletContext.getRequestDispatcher(viewResolver.urlForView(viewName)); dispatcher.include(request,swallowingResponse); result=sbuffer.toString(); } catch ( Exception e) { throw new IOException(e); } return result; }Code Example 8:
From project Corisco, under directory /dspace-1.6.2-src-release-corisco-1/dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/servlet/.
Source LDAPServlet.java
protected void doDSPost(Context context,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException, SQLException, AuthorizeException { String netid=request.getParameter("login_netid"); String password=request.getParameter("login_password"); String jsp=null; int status=AuthenticationManager.authenticate(context,netid,password,null,request); if (status == AuthenticationMethod.SUCCESS) { Authenticate.loggedIn(context,request,context.getCurrentUser()); Locale epersonLocale=I18nUtil.getEPersonLocale(context.getCurrentUser()); context.setCurrentLocale(epersonLocale); Config.set(request.getSession(),Config.FMT_LOCALE,epersonLocale); log.info(LogManager.getHeader(context,"login","type=explicit")); Authenticate.resumeInterruptedRequest(request,response); return; } else if (status == AuthenticationMethod.CERT_REQUIRED) { jsp="/error/require-certificate.jsp"; } else { jsp="/login/incorrect.jsp"; } log.info(LogManager.getHeader(context,"failed_login","netid=" + netid + ", result="+ String.valueOf(status))); JSPManager.showJSP(request,response,jsp); }Code Example 9:
From project Corisco, under directory /dspace-1.6.2-src-release-corisco-1/dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/servlet/.
Source PasswordServlet.java
protected void doDSPost(Context context,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException, SQLException, AuthorizeException { String email=request.getParameter("login_email"); String password=request.getParameter("login_password"); String jsp=null; int status=AuthenticationManager.authenticate(context,email,password,null,request); if (status == AuthenticationMethod.SUCCESS) { Authenticate.loggedIn(context,request,context.getCurrentUser()); Locale epersonLocale=I18nUtil.getEPersonLocale(context.getCurrentUser()); context.setCurrentLocale(epersonLocale); Config.set(request.getSession(),Config.FMT_LOCALE,epersonLocale); log.info(LogManager.getHeader(context,"login","type=explicit")); Authenticate.resumeInterruptedRequest(request,response); return; } else if (status == AuthenticationMethod.CERT_REQUIRED) jsp="/error/require-certificate.jsp"; else jsp="/login/incorrect.jsp"; log.info(LogManager.getHeader(context,"failed_login","email=" + email + ", result="+ String.valueOf(status))); JSPManager.showJSP(request,response,jsp); }Code Example 10:
From project Corisco, under directory /dspace-1.6.2-src-release-corisco-1/dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/util/.
Source Authenticate.java
/** * Store information about the current user in the request and context * @param context DSpace context * @param request HTTP request * @param eperson the eperson logged in */ public static void loggedIn(Context context,HttpServletRequest request,EPerson eperson){ HttpSession session=request.getSession(); if ((!session.isNew()) && (session.getAttribute("dspace.current.user.id") == null)) { Locale sessionLocale=UIUtil.getSessionLocale(request); if (ConfigurationManager.getBooleanProperty("webui.session.invalidate",true)) { session.invalidate(); } session=request.getSession(); if (sessionLocale != null) { Config.set(request.getSession(),Config.FMT_LOCALE,sessionLocale); } } context.setCurrentUser(eperson); boolean isAdmin=false; try { isAdmin=AuthorizeManager.isAdmin(context); } catch ( SQLException se) { log.warn("Unable to use AuthorizeManager " + se); } finally { request.setAttribute("is.admin",new Boolean(isAdmin)); } request.setAttribute("dspace.current.user",eperson); session.setAttribute("dspace.current.user.id",new Integer(eperson.getID())); session.setAttribute("dspace.current.remote.addr",request.getRemoteAddr()); }Code Example 11:
From project Corisco, under directory /dspace-1.6.2-src-release-corisco-1/dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/util/.
Source Authenticate.java
/** * Log the user out * @param context DSpace context * @param request HTTP request */ public static void loggedOut(Context context,HttpServletRequest request){ HttpSession session=request.getSession(); context.setCurrentUser(null); request.removeAttribute("is.admin"); request.removeAttribute("dspace.current.user"); session.removeAttribute("dspace.current.user.id"); Locale sessionLocale=UIUtil.getSessionLocale(request); if (ConfigurationManager.getBooleanProperty("webui.session.invalidate",true)) { session.invalidate(); } if (sessionLocale != null) { Config.set(request.getSession(),Config.FMT_LOCALE,sessionLocale); } }Code Example 12:
From project Corisco, under directory /dspace-1.6.2-src-release-corisco-1/dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/util/.
Source UIUtil.java
/** * Obtain a new context object. If a context object has already been created for this HTTP request, it is re-used, otherwise it is created. If a user has authenticated with the system, the current user of the context is set appropriately. * @param request the HTTP request * @return a context object */ public static Context obtainContext(HttpServletRequest request) throws SQLException { try { if (request.getCharacterEncoding() == null) request.setCharacterEncoding(Constants.DEFAULT_ENCODING); } catch ( Exception e) { log.error("Unable to set encoding to UTF-8.",e); } Context c=(Context)request.getAttribute("dspace.context"); if (c == null) { c=new Context(); HttpSession session=request.getSession(); Integer userID=(Integer)session.getAttribute("dspace.current.user.id"); if (userID != null) { String remAddr=(String)session.getAttribute("dspace.current.remote.addr"); if (remAddr != null && remAddr.equals(request.getRemoteAddr())) { EPerson e=EPerson.find(c,userID.intValue()); Authenticate.loggedIn(c,request,e); } else { log.warn("POSSIBLE HIJACKED SESSION: request from " + request.getRemoteAddr() + " does not match original "+ "session address: "+ remAddr+ ". Authentication rejected."); } } int[] groupIDs=AuthenticationManager.getSpecialGroups(c,request); for (int i=0; i < groupIDs.length; i++) { c.setSpecialGroup(groupIDs[i]); log.debug("Adding Special Group id=" + String.valueOf(groupIDs[i])); } c.setExtraLogInfo("session_id=" + request.getSession().getId() + ":ip_addr="+ request.getRemoteAddr()); request.setAttribute("dspace.context",c); } Locale sessionLocale=getSessionLocale(request); Config.set(request.getSession(),Config.FMT_LOCALE,sessionLocale); c.setCurrentLocale(sessionLocale); return c; }Code Example 13:
From project Corisco, under directory /dspace-1.6.2-src-release-corisco-1/dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/util/.
Source UIUtil.java
/** * Get the Locale for a session according to the user's language selection or language preferences. Order of selection - language selected via UI - language as set by application - language browser default * @param request the request Object * @return supportedLocaleLocale supported by this DSpace Instance for this request */ public static Locale getSessionLocale(HttpServletRequest request){ String paramLocale=request.getParameter("locale"); Locale sessionLocale=null; Locale supportedLocale=null; if (paramLocale != null && paramLocale != "") { sessionLocale=new Locale(paramLocale); } if (sessionLocale == null) { HttpSession session=request.getSession(); sessionLocale=(Locale)Config.get(session,Config.FMT_LOCALE); } if (sessionLocale == null) { sessionLocale=request.getLocale(); } if (sessionLocale == null) { sessionLocale=I18nUtil.DEFAULTLOCALE; } supportedLocale=I18nUtil.getSupportedLocale(sessionLocale); return supportedLocale; }Code Example 14:
From project DICE, under directory /jgossip/src/org/jresearch/gossip/filters/.
Source SetCharacterEncodingFilter.java
/** * Select and set (if specified) the character encoding to be used to interpret request parameters for this request. * @param request The servlet request we are processing * @param result The servlet response we are creating * @param chain The filter chain we are processing * @exception IOException if an input/output error occurs * @exception ServletException if a servlet error occurs */ public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException, ServletException { if (ignore || (request.getCharacterEncoding() == null)) { String encoding=selectEncoding(request); if (encoding != null) { request.setCharacterEncoding(encoding); } } HttpSession session=((HttpServletRequest)request).getSession(true); Locale locale=(Locale)session.getAttribute(Globals.LOCALE_KEY); if (locale == null) { locale=request.getLocale(); } Config.set(session,Config.FMT_LOCALE,locale); chain.doFilter(request,response); }Code Example 15:
From project DSpace, under directory /dspace-jspui/src/main/java/org/dspace/app/webui/servlet/.
Source LDAPServlet.java
protected void doDSPost(Context context,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException, SQLException, AuthorizeException { String netid=request.getParameter("login_netid"); String password=request.getParameter("login_password"); String jsp=null; int status=AuthenticationManager.authenticate(context,netid,password,null,request); if (status == AuthenticationMethod.SUCCESS) { Authenticate.loggedIn(context,request,context.getCurrentUser()); Locale epersonLocale=I18nUtil.getEPersonLocale(context.getCurrentUser()); context.setCurrentLocale(epersonLocale); Config.set(request.getSession(),Config.FMT_LOCALE,epersonLocale); log.info(LogManager.getHeader(context,"login","type=explicit")); Authenticate.resumeInterruptedRequest(request,response); return; } else if (status == AuthenticationMethod.CERT_REQUIRED) { jsp="/error/require-certificate.jsp"; } else { jsp="/login/ldap-incorrect.jsp"; } log.info(LogManager.getHeader(context,"failed_login","netid=" + netid + ", result="+ String.valueOf(status))); JSPManager.showJSP(request,response,jsp); }Code Example 16:
From project DSpace, under directory /dspace-jspui/src/main/java/org/dspace/app/webui/servlet/.
Source PasswordServlet.java
protected void doDSPost(Context context,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException, SQLException, AuthorizeException { String email=request.getParameter("login_email"); String password=request.getParameter("login_password"); String jsp=null; int status=AuthenticationManager.authenticate(context,email,password,null,request); if (status == AuthenticationMethod.SUCCESS) { Authenticate.loggedIn(context,request,context.getCurrentUser()); Locale epersonLocale=I18nUtil.getEPersonLocale(context.getCurrentUser()); context.setCurrentLocale(epersonLocale); Config.set(request.getSession(),Config.FMT_LOCALE,epersonLocale); log.info(LogManager.getHeader(context,"login","type=explicit")); Authenticate.resumeInterruptedRequest(request,response); return; } else if (status == AuthenticationMethod.CERT_REQUIRED) { jsp="/error/require-certificate.jsp"; } else { jsp="/login/incorrect.jsp"; } log.info(LogManager.getHeader(context,"failed_login","email=" + email + ", result="+ String.valueOf(status))); JSPManager.showJSP(request,response,jsp); }Code Example 17:
From project DSpace, under directory /dspace-jspui/src/main/java/org/dspace/app/webui/util/.
Source Authenticate.java
/** * Store information about the current user in the request and context * @param context DSpace context * @param request HTTP request * @param eperson the eperson logged in */ public static void loggedIn(Context context,HttpServletRequest request,EPerson eperson){ HttpSession session=request.getSession(); if ((!session.isNew()) && (session.getAttribute("dspace.current.user.id") == null)) { Locale sessionLocale=UIUtil.getSessionLocale(request); RequestInfo requestInfo=(RequestInfo)session.getAttribute("interrupted.request.info"); String requestUrl=(String)session.getAttribute("interrupted.request.url"); if (ConfigurationManager.getBooleanProperty("webui.session.invalidate",true)) { session.invalidate(); } session=request.getSession(); if (sessionLocale != null) { Config.set(request.getSession(),Config.FMT_LOCALE,sessionLocale); } if (requestInfo != null && requestUrl != null) { session.setAttribute("interrupted.request.info",requestInfo); session.setAttribute("interrupted.request.url",requestUrl); } } context.setCurrentUser(eperson); boolean isAdmin=false; try { isAdmin=AuthorizeManager.isAdmin(context); } catch ( SQLException se) { log.warn("Unable to use AuthorizeManager " + se); } finally { request.setAttribute("is.admin",Boolean.valueOf(isAdmin)); } request.setAttribute("dspace.current.user",eperson); session.setAttribute("dspace.current.user.id",Integer.valueOf(eperson.getID())); session.setAttribute("dspace.current.remote.addr",request.getRemoteAddr()); }Code Example 18:
From project DSpace, under directory /dspace-jspui/src/main/java/org/dspace/app/webui/util/.
Source Authenticate.java
/** * Log the user out * @param context DSpace context * @param request HTTP request */ public static void loggedOut(Context context,HttpServletRequest request){ HttpSession session=request.getSession(); context.setCurrentUser(null); request.removeAttribute("is.admin"); request.removeAttribute("dspace.current.user"); session.removeAttribute("dspace.current.user.id"); Locale sessionLocale=UIUtil.getSessionLocale(request); if (ConfigurationManager.getBooleanProperty("webui.session.invalidate",true)) { session.invalidate(); } if (sessionLocale != null) { Config.set(request.getSession(),Config.FMT_LOCALE,sessionLocale); } }Code Example 19:
From project DSpace, under directory /dspace-jspui/src/main/java/org/dspace/app/webui/util/.
Source UIUtil.java
/** * Get the Locale for a session according to the user's language selection or language preferences. Order of selection - language selected via UI - language as set by application - language browser default * @param request the request Object * @return supportedLocaleLocale supported by this DSpace Instance for this request */ public static Locale getSessionLocale(HttpServletRequest request){ String paramLocale=request.getParameter("locale"); Locale sessionLocale=null; Locale supportedLocale=null; if (!StringUtils.isEmpty(paramLocale)) { sessionLocale=new Locale(paramLocale); } if (sessionLocale == null) { HttpSession session=request.getSession(); sessionLocale=(Locale)Config.get(session,Config.FMT_LOCALE); } if (sessionLocale == null) { sessionLocale=request.getLocale(); } if (sessionLocale == null) { sessionLocale=I18nUtil.DEFAULTLOCALE; } supportedLocale=I18nUtil.getSupportedLocale(sessionLocale); return supportedLocale; }Code Example 20:
From project gisgraphy-mirror, under directory /gisgraphy/src/main/java/com/gisgraphy/webapp/filter/.
Source LocaleFilter.java
/** * This method looks for a "locale" request parameter. If it finds one, it sets it as the preferred locale and also configures it to work with JSTL. * @param request the current request * @param response the current response * @param chain the chain * @throws IOException when something goes wrong * @throws ServletException when a communication failure happens */ @Override @SuppressWarnings("unchecked") public void doFilterInternal(HttpServletRequest request,HttpServletResponse response,FilterChain chain) throws IOException, ServletException { String locale=request.getParameter("locale"); Locale preferredLocale=null; if (locale != null) { int indexOfUnderscore=locale.indexOf('-'); if (indexOfUnderscore != -1) { String language=locale.substring(0,indexOfUnderscore); String country=locale.substring(indexOfUnderscore + 1); preferredLocale=new Locale(language,country); } else { preferredLocale=new Locale(locale); } } HttpSession session=request.getSession(false); if (session != null) { if (preferredLocale == null) { preferredLocale=(Locale)session.getAttribute(Constants.PREFERRED_LOCALE_KEY); } else { session.setAttribute(Constants.PREFERRED_LOCALE_KEY,preferredLocale); Config.set(session,Config.FMT_LOCALE,preferredLocale); } if (preferredLocale != null && !(request instanceof LocaleRequestWrapper)) { request=new LocaleRequestWrapper(request,preferredLocale); LocaleContextHolder.setLocale(preferredLocale); } } String theme=request.getParameter("theme"); if (theme != null && request.isUserInRole(Constants.ADMIN_ROLE)) { Map<String,Object> config=(Map)getServletContext().getAttribute(Constants.CONFIG); config.put(Constants.CSS_THEME,theme); } chain.doFilter(request,response); LocaleContextHolder.setLocaleContext(null); }