/
RegisterBuiltin.java
195 lines (183 loc) · 6.34 KB
/
RegisterBuiltin.java
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
package org.jboss.resteasy.plugins.providers;
import org.jboss.resteasy.core.ThreadLocalResteasyProviderFactory;
import org.jboss.resteasy.core.providerfactory.ResteasyProviderFactoryImpl;
import org.jboss.resteasy.microprofile.config.ResteasyConfigProvider;
import org.jboss.resteasy.plugins.interceptors.GZIPDecodingInterceptor;
import org.jboss.resteasy.plugins.interceptors.GZIPEncodingInterceptor;
import org.jboss.resteasy.resteasy_jaxrs.i18n.LogMessages;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import javax.ws.rs.RuntimeType;
import javax.ws.rs.ext.Providers;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.WeakHashMap;
/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
public class RegisterBuiltin
{
private static final Map<ClassLoader, ResteasyProviderFactory> configuredClientFactories = new WeakHashMap<>();
private static final boolean gzipForCachedFactories = isGZipEnabled();
public static synchronized ResteasyProviderFactory getClientInitializedResteasyProviderFactory(ClassLoader cl)
{
ResteasyProviderFactory rpf = null;
final boolean gzip = isGZipEnabled();
if (gzipForCachedFactories == gzip) {
rpf = configuredClientFactories.get(cl);
}
if (rpf == null) {
rpf = new ResteasyProviderFactoryImpl(RuntimeType.CLIENT) {
@Override
public RuntimeType getRuntimeType()
{
return RuntimeType.CLIENT;
}
};
if (!rpf.isBuiltinsRegistered()) {
register(rpf);
}
if (gzipForCachedFactories == gzip) {
configuredClientFactories.put(cl, rpf);
}
}
return rpf;
}
public static void register(ResteasyProviderFactory factory)
{
final ResteasyProviderFactory monitor = (factory instanceof ThreadLocalResteasyProviderFactory)
? ((ThreadLocalResteasyProviderFactory) factory).getDelegate()
: factory;
synchronized (monitor)
{
if (factory.isBuiltinsRegistered() || !factory.isRegisterBuiltins())
return;
try
{
registerProviders(factory);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
factory.setBuiltinsRegistered(true);
}
}
public static void registerProviders(ResteasyProviderFactory factory) throws Exception
{
Map<String, URL> origins = scanBuiltins();
for (final Entry<String, URL> entry : origins.entrySet())
{
final String line = entry.getKey();
try
{
Class<?> clazz;
if (System.getSecurityManager() == null)
{
clazz = Thread.currentThread().getContextClassLoader().loadClass(line);
}
else
{
clazz = AccessController.doPrivileged(new PrivilegedExceptionAction<Class<?>>()
{
@Override
public Class<?> run() throws ClassNotFoundException
{
return Thread.currentThread().getContextClassLoader().loadClass(line);
}
});
}
factory.registerProvider(clazz, true);
}
catch (NoClassDefFoundError e)
{
LogMessages.LOGGER.noClassDefFoundErrorError(line, entry.getValue(), e);
}
catch (ClassNotFoundException | PrivilegedActionException ex)
{
LogMessages.LOGGER.classNotFoundException(line, entry.getValue(), ex);
}
}
if (isGZipEnabled()) {
factory.registerProvider(GZIPDecodingInterceptor.class, true);
factory.registerProvider(GZIPEncodingInterceptor.class, true);
}
}
public static Map<String, URL> scanBuiltins() throws IOException, PrivilegedActionException {
Enumeration<URL> en;
if (System.getSecurityManager() == null)
{
en = Thread.currentThread().getContextClassLoader().getResources("META-INF/services/" + Providers.class.getName());
}
else
{
en = AccessController.doPrivileged(new PrivilegedExceptionAction<Enumeration<URL>>()
{
@Override
public Enumeration<URL> run() throws IOException
{
return Thread.currentThread().getContextClassLoader().getResources("META-INF/services/" + Providers.class.getName());
}
});
}
Map<String, URL> origins = new HashMap<String, URL>();
while (en.hasMoreElements())
{
final URL url = en.nextElement();
InputStream is;
if (System.getSecurityManager() == null)
{
is = url.openStream();
}
else
{
is = AccessController.doPrivileged(new PrivilegedExceptionAction<InputStream>()
{
@Override
public InputStream run() throws IOException
{
return url.openStream();
}
});
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
String line;
while ((line = reader.readLine()) != null)
{
line = line.trim();
if (line.equals("")) continue;
origins.put(line, url);
}
}
finally
{
is.close();
}
}
return origins;
}
public static boolean isGZipEnabled() {
return AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
@Override
public Boolean run() {
final String value = ResteasyConfigProvider.getConfig().getOptionalValue("resteasy.allowGzip", String.class).orElse(null);
if ("".equals(value)) return Boolean.FALSE;
return Boolean.parseBoolean(value);
}
});
}
}