Administrator
2023-04-14 cc0cbfc79a34e1b106fdb998450cd2ad03446126
提交 | 用户 | 时间
58d006 1 package com.mandi.common;
A 2
3 import java.io.IOException;
4 import java.util.UUID;
5
6 import javax.faces.context.FacesContext;
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9 import javax.servlet.http.HttpSession;
10 /**
11  * 封装了部分JSF的接口
12  * @author Administrator
13  *
14  */
15 public class JSFMethod {
16     public static HttpSession getSession()
17     {
18         FacesContext context=FacesContext.getCurrentInstance();
19         if(context==null)
20             return null;
21         return (HttpSession)context.getExternalContext().getSession(true);
22     }
23     public static HttpServletRequest getRequest()
24     {
25         FacesContext context=FacesContext.getCurrentInstance();
26         if(context==null)
27             return null;
28         return (HttpServletRequest)context.getExternalContext().getRequest();
29     }
30     public static HttpServletResponse getResponse()
31     {
32         FacesContext context=FacesContext.getCurrentInstance();
33         if(context==null)
34             return null;
35         return (HttpServletResponse)context.getExternalContext().getResponse();
36     }
37     public static void facesexit()
38     {
39         FacesContext context=FacesContext.getCurrentInstance();
40         if(context!=null)
41         {
42             context.responseComplete();
43         }
44     }
45     public static void faceswrite(String data){
46         FacesContext context=FacesContext.getCurrentInstance();
47         if(context!=null)
48         {
49             HttpServletResponse response=(HttpServletResponse)context.getExternalContext().getResponse();
50             try {
51                 response.getWriter().print(data);
52             } catch (IOException e) {
53                 // TODO Auto-generated catch block
54                 e.printStackTrace();
55             }finally{
56                 context.responseComplete();
57             }
58             
59         }
60     }
61     public static void facesredirect(String source)
62     {
63         FacesContext context=FacesContext.getCurrentInstance();
64         if(context!=null)
65         {
66             try {
67                 context.getExternalContext().redirect(source);
68             } catch (IOException e) {
69                 // TODO Auto-generated catch block
70                 e.printStackTrace();
71             }
72             context.responseComplete();
73         }
74     }
75     public static void facesdispatch(String source){
76         FacesContext context=FacesContext.getCurrentInstance();
77         if(context!=null)
78         {
79             try {
80                 context.getExternalContext().dispatch(source);
81             } catch (IOException e) {
82                 // TODO Auto-generated catch block
83                 e.printStackTrace();
84             }
85             context.responseComplete();
86         }
87     }
88     /**
89      *  The token in client is:ctoken
90      * @return
91      */
92     public static boolean createToken()
93     {
94         String ss=UUID.randomUUID().toString();
95         HttpSession session=getSession();
96         if(session!=null)
97         {
98             session.setAttribute("stoken", ss);
99             return true;
100         }
101         return false;
102     }
103     public static boolean checkToken(){
104         HttpServletRequest request=getRequest();
105         HttpSession session=getSession();
106         if(request==null||session==null)
107             return false;
108         String ctoken=request.getParameter("ctoken");
109         Object stoken=session.getAttribute("stoken");
110         String ss=UUID.randomUUID().toString();
111         session.setAttribute("stoken", ss);
112         if(stoken!=null&& ctoken!=null &&ctoken.equals(stoken.toString()))
113         {
114             return true;
115         }
116         return false;
117     }
118     /**
119      * certyfi in client is:
120      * @return
121      */
122     public static boolean checkCertifyCode(String clientname,String servername)
123     {
124         HttpServletRequest request=getRequest();
125         HttpSession session=getSession();
126         if(request==null||session==null)
127             return false;
128         Object obj=session.getAttribute(servername);
129         if(obj==null)
130             return false;
131         Object obj1=request.getParameter(clientname);
132         if(obj1==null)
133             return false;
134         String str1=obj1.toString().toLowerCase();
135         if(obj.equals(str1))
136             return true;
137         return false;
138     }
139 }