1 package gboat2.base.bridge;
2
3 import gboat2.base.bridge.exception.DefaultGboatNestedException;
4 import gboat2.base.bridge.model.UserSession;
5 import gboat2.base.bridge.util.json.JsonUtil;
6
7 import java.io.PrintWriter;
8
9 import javax.servlet.ServletContext;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 import javax.servlet.http.HttpSession;
13
14 import org.apache.commons.io.IOUtils;
15 import org.apache.struts2.ServletActionContext;
16
17
18
19
20
21
22
23 public class GboatAppContext implements GboatAppConstants{
24
25
26
27
28
29
30 public static String getWebRootPath() {
31 String path = System.getProperty(WEBAPP_ROOT_KEY);
32 if (null == path) {
33 path = ServletActionContext.getServletContext().getRealPath("/");
34 }
35 return path;
36 }
37
38
39
40
41 public static ServletContext getServletContext(){
42 return ServletActionContext.getServletContext();
43 }
44
45
46
47
48 public static HttpServletRequest getRequest(){
49 return ServletActionContext.getRequest();
50 }
51
52
53
54
55 public static HttpServletResponse getResponse(){
56 return ServletActionContext.getResponse();
57 }
58
59
60
61
62 public static HttpSession getSession(){
63 return getRequest().getSession();
64 }
65
66
67
68
69
70
71 public static Object getSessionAttr(String name){
72 return getSession().getAttribute(name);
73 }
74
75
76
77
78
79 public static UserSession getUserSession() {
80 return getUserSession(null);
81 }
82
83
84
85
86
87
88 public static UserSession getUserSession(HttpServletRequest request){
89 if(request == null) {
90 request = getRequest();
91 }
92
93 Object userSessionObj = request.getSession().getAttribute(UserSession.USER_SESSION_KEY);
94 if (userSessionObj instanceof UserSession)
95 return (UserSession) userSessionObj;
96
97 return null;
98 }
99
100
101
102
103 public static String getRemoteAddr() {
104 HttpServletRequest request = getRequest();
105 String ip = request.getHeader("x-forwarded-for");
106
107
108 if (ip != null && !"".equals(ip) && !"unknown".equalsIgnoreCase(ip)) {
109 String[] tempArray = ip.split(",");
110 for (int i = 0; i < tempArray.length; i++) {
111 if (!"unknown".equalsIgnoreCase(tempArray[i])) {
112 ip = tempArray[i].replaceAll("\\s", "");
113 break;
114 }
115 }
116 }
117
118 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
119 ip = request.getHeader("Proxy-Client-IP");
120 }
121
122 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
123 ip = request.getHeader("WL-Proxy-Client-IP");
124 }
125
126 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
127 ip = request.getRemoteAddr();
128 }
129
130 return ip;
131 }
132
133
134
135
136
137
138 public static void output(Object obj) {
139 output(obj, null);
140 }
141
142
143
144
145
146
147 public static void output(Object obj, HttpServletResponse response) {
148 if (obj == null)
149 throw new IllegalArgumentException("向前台输出的响应信息不能为 null");
150
151 if(response == null) {
152 response = getResponse();
153 }
154 response.setContentType(CT_PLAIN_UTF8);
155 PrintWriter out = null;
156 try {
157 out = response.getWriter();
158 if (obj instanceof CharSequence) {
159 out.print(obj);
160 } else {
161 String objClass = obj.getClass().getName();
162 if ("net.sf.json.JSONObject".equals(objClass)
163 || "net.sf.json.JSONArray".equals(objClass)) {
164 out.print(obj);
165 } else {
166 JsonUtil.object2Json(out, obj);
167 }
168 }
169 out.flush();
170 } catch (Exception e) {
171 throw new DefaultGboatNestedException("向前台输出响应信息 [" + obj + "] 失败。", e);
172 } finally {
173 IOUtils.closeQuietly(out);
174 out = null;
175 }
176 }
177 }