hjg
2023-10-08 a966b5abe5fb804464fe1cd09053a03fe94877fd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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;
    }
}