1
18
19 package io.undertow.servlet.core;
20
21
22 import java.security.AccessController;
23 import java.security.PrivilegedAction;
24
25 import javax.servlet.ServletContext;
26
27 import io.undertow.server.HttpHandler;
28 import io.undertow.server.session.Session;
29 import io.undertow.servlet.api.Deployment;
30 import io.undertow.servlet.handlers.ServletInitialHandler;
31 import io.undertow.servlet.handlers.ServletPathMatches;
32 import io.undertow.servlet.handlers.ServletRequestContext;
33 import io.undertow.servlet.spec.HttpSessionImpl;
34 import io.undertow.servlet.spec.ServletContextImpl;
35
36 final class SecurityActions {
37
38 private SecurityActions() {
39
40 }
41
42
47 static ClassLoader getContextClassLoader() {
48 if (System.getSecurityManager() == null) {
49 return Thread.currentThread().getContextClassLoader();
50 } else {
51 return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
52 public ClassLoader run() {
53 return Thread.currentThread().getContextClassLoader();
54 }
55 });
56 }
57 }
58
59
65 static void setContextClassLoader(final ClassLoader classLoader) {
66 if (System.getSecurityManager() == null) {
67 Thread.currentThread().setContextClassLoader(classLoader);
68 } else {
69 AccessController.doPrivileged(new PrivilegedAction<Object>() {
70 public Object run() {
71 Thread.currentThread().setContextClassLoader(classLoader);
72 return null;
73 }
74 });
75 }
76 }
77
78 static String getSystemProperty(final String prop) {
79 if (System.getSecurityManager() == null) {
80 return System.getProperty(prop);
81 } else {
82 return (String) AccessController.doPrivileged(new PrivilegedAction<Object>() {
83 public Object run() {
84 return System.getProperty(prop);
85 }
86 });
87 }
88 }
89
90 static HttpSessionImpl forSession(final Session session, final ServletContext servletContext, final boolean newSession) {
91 if (System.getSecurityManager() == null) {
92 return HttpSessionImpl.forSession(session, servletContext, newSession);
93 } else {
94 return AccessController.doPrivileged(new PrivilegedAction<HttpSessionImpl>() {
95 @Override
96 public HttpSessionImpl run() {
97 return HttpSessionImpl.forSession(session, servletContext, newSession);
98 }
99 });
100 }
101 }
102
103 static ServletRequestContext currentServletRequestContext() {
104 if (System.getSecurityManager() == null) {
105 return ServletRequestContext.current();
106 } else {
107 return AccessController.doPrivileged(new PrivilegedAction<ServletRequestContext>() {
108 @Override
109 public ServletRequestContext run() {
110 return ServletRequestContext.current();
111 }
112 });
113 }
114 }
115
116 static void setCurrentRequestContext(final ServletRequestContext servletRequestContext) {
117 if (System.getSecurityManager() == null) {
118 ServletRequestContext.setCurrentRequestContext(servletRequestContext);
119 } else {
120 AccessController.doPrivileged(new PrivilegedAction<Object>() {
121 @Override
122 public Object run() {
123 ServletRequestContext.setCurrentRequestContext(servletRequestContext);
124 return null;
125 }
126 });
127 }
128 }
129
130 static void clearCurrentServletAttachments() {
131 if (System.getSecurityManager() == null) {
132 ServletRequestContext.clearCurrentServletAttachments();
133 } else {
134 AccessController.doPrivileged(new PrivilegedAction<Object>() {
135 @Override
136 public Object run() {
137 ServletRequestContext.clearCurrentServletAttachments();
138 return null;
139 }
140 });
141 }
142 }
143 static ServletRequestContext requireCurrentServletRequestContext() {
144 if (System.getSecurityManager() == null) {
145 return ServletRequestContext.requireCurrent();
146 } else {
147 return AccessController.doPrivileged(new PrivilegedAction<ServletRequestContext>() {
148 @Override
149 public ServletRequestContext run() {
150 return ServletRequestContext.requireCurrent();
151 }
152 });
153 }
154 }
155 static ServletInitialHandler createServletInitialHandler(final ServletPathMatches paths, final HttpHandler next, final Deployment deployment, final ServletContextImpl servletContext) {
156 if (System.getSecurityManager() == null) {
157 return new ServletInitialHandler(paths, next, deployment, servletContext);
158 } else {
159 return AccessController.doPrivileged(new PrivilegedAction<ServletInitialHandler>() {
160 @Override
161 public ServletInitialHandler run() {
162 return new ServletInitialHandler(paths, next, deployment, servletContext);
163 }
164 });
165 }
166 }
167 }
168