-
Notifications
You must be signed in to change notification settings - Fork 340
/
WadlApplicationContextImpl.java
266 lines (219 loc) · 10.5 KB
/
WadlApplicationContextImpl.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
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
/*
* Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.jersey.server.wadl.internal;
import java.net.URI;
import java.security.AccessController;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;
import javax.inject.Inject;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.namespace.QName;
import org.glassfish.jersey.internal.inject.InjectionManager;
import org.glassfish.jersey.internal.util.ReflectionHelper;
import org.glassfish.jersey.server.ExtendedResourceContext;
import org.glassfish.jersey.server.internal.LocalizationMessages;
import org.glassfish.jersey.server.wadl.WadlApplicationContext;
import org.glassfish.jersey.server.wadl.WadlGenerator;
import org.glassfish.jersey.server.wadl.config.WadlGeneratorConfig;
import org.glassfish.jersey.server.wadl.config.WadlGeneratorConfigLoader;
import com.sun.research.ws.wadl.Application;
import com.sun.research.ws.wadl.Doc;
import com.sun.research.ws.wadl.Grammars;
import com.sun.research.ws.wadl.Include;
import com.sun.research.ws.wadl.Resource;
import com.sun.research.ws.wadl.Resources;
/**
* WADL application context implementation.
*
* @author Paul Sandoz
*/
public final class WadlApplicationContextImpl implements WadlApplicationContext {
private static final Logger LOGGER = Logger.getLogger(WadlApplicationContextImpl.class.getName());
/**
* Jersey WADL extension XML namespace.
*/
static final String WADL_JERSEY_NAMESPACE = "http://jersey.java.net/";
/**
* Jersey WADL extension XML element.
*/
static final JAXBElement EXTENDED_ELEMENT =
new JAXBElement<>(new QName(WADL_JERSEY_NAMESPACE, "extended", "jersey"), String.class, "true");
private final ExtendedResourceContext resourceContext;
private final InjectionManager injectionManager;
private final WadlGeneratorConfig wadlGeneratorConfig;
private final JAXBContext jaxbContext;
private volatile boolean wadlGenerationEnabled = true;
/**
* Injection constructor.
*
* @param injectionManager injection manager.
* @param configuration runtime application configuration.
* @param resourceContext extended resource context.
*/
@Inject
public WadlApplicationContextImpl(
final InjectionManager injectionManager,
final Configuration configuration,
final ExtendedResourceContext resourceContext) {
this.injectionManager = injectionManager;
this.wadlGeneratorConfig = WadlGeneratorConfigLoader.loadWadlGeneratorsFromConfig(configuration.getProperties());
this.resourceContext = resourceContext;
// TODO perhaps this should be done another way for the moment
// create a temporary generator just to do this one task
final WadlGenerator wadlGenerator = wadlGeneratorConfig.createWadlGenerator(injectionManager);
try {
jaxbContext = getJAXBContextFromWadlGenerator(wadlGenerator);
} catch (JAXBException ex) {
throw new ProcessingException(LocalizationMessages.ERROR_WADL_JAXB_CONTEXT(), ex);
}
}
public static JAXBContext getJAXBContextFromWadlGenerator(WadlGenerator wadlGenerator) throws JAXBException {
JAXBContext jaxbContextCandidate = null;
final ClassLoader contextClassLoader = AccessController.doPrivileged(ReflectionHelper.getContextClassLoaderPA());
try {
// Nasty ClassLoader magic. JAXB-API has some strange limitation about what class loader can
// be used in OSGi environment - it must be same as context ClassLoader. Following code just
// workarounds this limitation
// see JERSEY-1818
// see JSR222-46
final ClassLoader jerseyModuleClassLoader =
AccessController.doPrivileged(ReflectionHelper.getClassLoaderPA(wadlGenerator.getClass()));
AccessController.doPrivileged(ReflectionHelper.setContextClassLoaderPA(jerseyModuleClassLoader));
jaxbContextCandidate = JAXBContext.newInstance(wadlGenerator.getRequiredJaxbContextPath(), jerseyModuleClassLoader);
} catch (final JAXBException ex) {
try {
// fallback for glassfish
LOGGER.log(Level.FINE, LocalizationMessages.WADL_JAXB_CONTEXT_FALLBACK(), ex);
jaxbContextCandidate = JAXBContext.newInstance(wadlGenerator.getRequiredJaxbContextPath());
} catch (final JAXBException innerEx) {
throw ex;
}
} finally {
AccessController.doPrivileged(ReflectionHelper.setContextClassLoaderPA(contextClassLoader));
}
return jaxbContextCandidate;
}
@Override
public ApplicationDescription getApplication(final UriInfo uriInfo, final boolean detailedWadl) {
final ApplicationDescription applicationDescription = getWadlBuilder(detailedWadl, uriInfo)
.generate(resourceContext.getResourceModel().getRootResources());
final Application application = applicationDescription.getApplication();
for (final Resources resources : application.getResources()) {
if (resources.getBase() == null) {
resources.setBase(uriInfo.getBaseUri().toString());
}
}
attachExternalGrammar(application, applicationDescription, uriInfo.getRequestUri());
return applicationDescription;
}
@Override
public Application getApplication(final UriInfo info,
final org.glassfish.jersey.server.model.Resource resource, final boolean detailedWadl) {
// Get the root application description
//
final ApplicationDescription description = getApplication(info, detailedWadl);
final WadlGenerator wadlGenerator = wadlGeneratorConfig.createWadlGenerator(injectionManager);
final Application application = new WadlBuilder(wadlGenerator, detailedWadl, info).generate(description, resource);
if (application == null) {
return null;
}
for (final Resources resources : application.getResources()) {
resources.setBase(info.getBaseUri().toString());
}
// Attach any grammar we may have
attachExternalGrammar(application, description,
info.getRequestUri());
for (final Resources resources : application.getResources()) {
final Resource r = resources.getResource().get(0);
r.setPath(info.getBaseUri().relativize(info.getAbsolutePath()).toString());
// remove path params since path is fixed at this point
r.getParam().clear();
}
return application;
}
@Override
public JAXBContext getJAXBContext() {
return jaxbContext;
}
private WadlBuilder getWadlBuilder(final boolean detailedWadl, final UriInfo uriInfo) {
return (this.wadlGenerationEnabled ? new WadlBuilder(wadlGeneratorConfig.createWadlGenerator(injectionManager),
detailedWadl, uriInfo) : null);
}
@Override
public void setWadlGenerationEnabled(final boolean wadlGenerationEnabled) {
this.wadlGenerationEnabled = wadlGenerationEnabled;
}
@Override
public boolean isWadlGenerationEnabled() {
return wadlGenerationEnabled;
}
/**
* Update the application object to include the generated grammar objects.
*/
private void attachExternalGrammar(
final Application application,
final ApplicationDescription applicationDescription,
URI requestURI) {
// Massage the application.wadl URI slightly to get the right effect
//
try {
final String requestURIPath = requestURI.getPath();
if (requestURIPath.endsWith("application.wadl")) {
requestURI = UriBuilder.fromUri(requestURI)
.replacePath(
requestURIPath
.substring(0, requestURIPath.lastIndexOf('/') + 1))
.build();
}
final String root = application.getResources().get(0).getBase();
final UriBuilder extendedPath = root != null
? UriBuilder.fromPath(root).path("/application.wadl/") : UriBuilder.fromPath("./application.wadl/");
final URI rootURI = root != null ? UriBuilder.fromPath(root).build() : null;
// Add a reference to this grammar
//
final Grammars grammars;
if (application.getGrammars() != null) {
LOGGER.info(LocalizationMessages.ERROR_WADL_GRAMMAR_ALREADY_CONTAINS());
grammars = application.getGrammars();
} else {
grammars = new Grammars();
application.setGrammars(grammars);
}
// Create a reference back to the root WADL
//
for (final String path : applicationDescription.getExternalMetadataKeys()) {
final URI schemaURI = extendedPath.clone().path(path).build();
final String schemaPath = rootURI != null ? requestURI.relativize(schemaURI).toString() : schemaURI.toString();
final Include include = new Include();
include.setHref(schemaPath);
final Doc doc = new Doc();
doc.setLang("en");
doc.setTitle("Generated");
include.getDoc().add(doc);
// Finally add to list
grammars.getInclude().add(include);
}
} catch (final Exception e) {
throw new ProcessingException(LocalizationMessages.ERROR_WADL_EXTERNAL_GRAMMAR(), e);
}
}
}