package com.mandi.common; import java.io.IOException; import java.util.UUID; import javax.faces.context.FacesContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * 封装了部分JSF的接口 * @author Administrator * */ public class JSFMethod { public static HttpSession getSession() { FacesContext context=FacesContext.getCurrentInstance(); if(context==null) return null; return (HttpSession)context.getExternalContext().getSession(true); } public static HttpServletRequest getRequest() { FacesContext context=FacesContext.getCurrentInstance(); if(context==null) return null; return (HttpServletRequest)context.getExternalContext().getRequest(); } public static HttpServletResponse getResponse() { FacesContext context=FacesContext.getCurrentInstance(); if(context==null) return null; return (HttpServletResponse)context.getExternalContext().getResponse(); } public static void facesexit() { FacesContext context=FacesContext.getCurrentInstance(); if(context!=null) { context.responseComplete(); } } public static void faceswrite(String data){ FacesContext context=FacesContext.getCurrentInstance(); if(context!=null) { HttpServletResponse response=(HttpServletResponse)context.getExternalContext().getResponse(); try { response.getWriter().print(data); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ context.responseComplete(); } } } public static void facesredirect(String source) { FacesContext context=FacesContext.getCurrentInstance(); if(context!=null) { try { context.getExternalContext().redirect(source); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } context.responseComplete(); } } public static void facesdispatch(String source){ FacesContext context=FacesContext.getCurrentInstance(); if(context!=null) { try { context.getExternalContext().dispatch(source); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } context.responseComplete(); } } /** * The token in client is:ctoken * @return */ public static boolean createToken() { String ss=UUID.randomUUID().toString(); HttpSession session=getSession(); if(session!=null) { session.setAttribute("stoken", ss); return true; } return false; } public static boolean checkToken(){ HttpServletRequest request=getRequest(); HttpSession session=getSession(); if(request==null||session==null) return false; String ctoken=request.getParameter("ctoken"); Object stoken=session.getAttribute("stoken"); String ss=UUID.randomUUID().toString(); session.setAttribute("stoken", ss); if(stoken!=null&& ctoken!=null &&ctoken.equals(stoken.toString())) { return true; } return false; } /** * certyfi in client is: * @return */ public static boolean checkCertifyCode(String clientname,String servername) { HttpServletRequest request=getRequest(); HttpSession session=getSession(); if(request==null||session==null) return false; Object obj=session.getAttribute(servername); if(obj==null) return false; Object obj1=request.getParameter(clientname); if(obj1==null) return false; String str1=obj1.toString().toLowerCase(); if(obj.equals(str1)) return true; return false; } }