CRXContext.java
9.62 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.crx.i18n.Dictionary
* com.day.crx.i18n.LanguageManager
* javax.jcr.Credentials
* javax.jcr.Item
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.Repository
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Workspace
* javax.servlet.ServletContext
* javax.servlet.ServletException
* javax.servlet.http.HttpServletRequest
* javax.servlet.http.HttpSession
* javax.servlet.jsp.PageContext
* org.apache.jackrabbit.api.JackrabbitSession
* org.apache.jackrabbit.api.security.user.Authorizable
* org.apache.jackrabbit.api.security.user.User
* org.apache.jackrabbit.api.security.user.UserManager
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.crx.explorer.impl.j2ee;
import com.day.crx.explorer.impl.j2ee.CRXSessionCache;
import com.day.crx.explorer.impl.j2ee.JCRExplorerServlet;
import com.day.crx.explorer.impl.j2ee.LoginServlet;
import com.day.crx.explorer.impl.util.JSR283Helper;
import com.day.crx.explorer.impl.util.RequestData;
import com.day.crx.i18n.Dictionary;
import com.day.crx.i18n.LanguageManager;
import java.io.File;
import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Locale;
import javax.jcr.Credentials;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.security.auth.Subject;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.PageContext;
import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CRXContext {
private static Logger log = LoggerFactory.getLogger(CRXContext.class);
public static final String ATTR_NAME = "CRXContext";
private static final String S_ATTR_LOCALE = "CRXLocale";
public static final String REQ_PARAM_PATH = "Path";
public static final String REQ_PARAM_PATH2 = "path";
public static final String REQ_PARAM_ACTION = "CRXAction";
public static final String REQ_PARAM_ACTION_ARG = "CRXActionArg";
private final ServletContext servletCtx;
private final Repository repository;
private final boolean ownSession;
private final RequestData data;
private final String docroot;
private final HttpServletRequest request;
private boolean initializedSession;
private Session repSession;
private CRXSessionCache.CRXSessionId sessionId;
public static CRXContext getInstance(PageContext pCtx, HttpServletRequest request) {
return CRXContext.getInstance(pCtx.getServletContext(), request);
}
public static CRXContext getInstance(PageContext pCtx, HttpServletRequest request, boolean ownSession) {
return CRXContext.getInstance(pCtx.getServletContext(), request, ownSession);
}
public static CRXContext getInstance(ServletContext sCtx, HttpServletRequest request) {
return CRXContext.getInstance(sCtx, request, false);
}
public static CRXContext getInstance(ServletContext sCtx, HttpServletRequest request, boolean ownSession) {
CRXContext ctx = (CRXContext)request.getAttribute("CRXContext");
if (ctx == null) {
ctx = new CRXContext(sCtx, request, ownSession);
request.setAttribute("CRXContext", (Object)ctx);
}
return ctx;
}
private CRXContext(ServletContext ctx, HttpServletRequest request, boolean ownSession) {
this.data = new RequestData(request, JCRExplorerServlet.getTempDirectory(ctx));
this.docroot = JCRExplorerServlet.getDocrootPrefix(request);
this.repository = JCRExplorerServlet.getInstance() == null ? null : JCRExplorerServlet.getInstance().getRepository();
this.request = request;
this.servletCtx = ctx;
this.ownSession = ownSession;
this.getCurrentDictionary();
}
public Item getItem() {
String path = this.getPath();
try {
Session session = this.getSession();
return path == null || session == null ? null : JSR283Helper.getItem(session, path);
}
catch (RepositoryException e) {
return null;
}
}
public String getPath() {
String path = this.data.getParameter("Path");
if (path == null) {
path = this.data.getParameter("path");
}
return "".equals(path) ? "/" : path;
}
public Node getNode() {
String path = this.getPath();
try {
Session session = this.getSession();
return path == null || session == null ? null : JSR283Helper.getNode(session, path);
}
catch (RepositoryException e) {
return null;
}
}
public Property getProperty() {
String path = this.getPath();
try {
Session session = this.getSession();
return path == null || session == null ? null : JSR283Helper.getProperty(session, path);
}
catch (RepositoryException e) {
return null;
}
}
public String getDocroot() {
return this.docroot;
}
public boolean isAdmin() {
Session session = this.getSession();
if (session instanceof JackrabbitSession) {
try {
UserManager manager = ((JackrabbitSession)session).getUserManager();
Authorizable authorizable = manager.getAuthorizable(session.getUserID());
if (authorizable instanceof User) {
return ((User)authorizable).isAdmin();
}
}
catch (Exception e) {
// empty catch block
}
}
return "admin".equals(session.getUserID());
}
public String getUsersPath() {
return "/home/users";
}
public String getGroupsPath() {
return "/home/groups";
}
public Session getSession() {
if (!this.initializedSession) {
if (this.ownSession) {
try {
this.repSession = LoginServlet.login(this, null);
}
catch (RepositoryException e) {
log.error("Unable to retrieve session: " + e.toString());
}
catch (ServletException e) {
log.error("Unable to retrieve session: " + e.toString());
}
} else {
this.repSession = JCRExplorerServlet.getDefaultSession(this.request);
}
this.initializedSession = true;
}
return this.repSession;
}
public static Session createSession(Subject subject, final Session session) throws RepositoryException {
Session s = (Session)Subject.doAsPrivileged(subject, new PrivilegedAction<Session>(){
@Override
public Session run() {
try {
return session.getRepository().login(null, session.getWorkspace().getName());
}
catch (RepositoryException e) {
log.error("Unable to create session: " + (Object)e);
return null;
}
}
}, AccessController.getContext());
if (s == null) {
throw new RepositoryException("Unable to create session from subject");
}
return s;
}
public Repository getRepository() {
return this.repository;
}
public RequestData getRequestData() {
return this.data;
}
public HttpServletRequest getRequest() {
return this.request;
}
public ServletContext getServletContext() {
return this.servletCtx;
}
public void lockSession() {
this.getSession();
if (this.sessionId == null && !this.ownSession && this.repSession != null) {
this.sessionId = JCRExplorerServlet.getInstance().getSessionCache().getSessionId(this.request);
if (this.sessionId != null) {
this.sessionId.acquire();
}
}
}
public void close() {
try {
this.data.close();
}
catch (IOException e) {
// empty catch block
}
this.request.removeAttribute("CRXContext");
if (this.ownSession && this.repSession != null) {
this.repSession.logout();
}
if (this.sessionId != null) {
this.sessionId.release();
}
}
public Dictionary getCurrentDictionary() {
HttpSession s = this.request.getSession(false);
Dictionary dict = Dictionary.getCurrent();
if (s == null) {
return dict;
}
Locale locale = (Locale)s.getAttribute("CRXLocale");
if (locale == null) {
return dict;
}
if (!locale.equals(dict.getLocale())) {
dict = Dictionary.setCurrent((String)null, (Locale)locale);
}
return dict;
}
public void setCurrentLocale(String localeStr) {
this.setCurrentLocale(LanguageManager.getLocale((String)localeStr));
}
public void setCurrentLocale(Locale locale) {
HttpSession s = this.request.getSession();
if (s == null) {
throw new IllegalStateException("Session must not be null at this time.");
}
s.setAttribute("CRXLocale", (Object)locale);
}
}