Skip to content

Commit 47d1794

Browse files
committed
Prevent NPE in AbstractJaxbProvider
Signed-off-by: jansupol <[email protected]>
1 parent d4453e5 commit 47d1794

File tree

3 files changed

+99
-16
lines changed

3 files changed

+99
-16
lines changed

media/jaxb/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright (c) 2015, 2024 Oracle and/or its affiliates. All rights reserved.
4+
Copyright (c) 2015, 2025 Oracle and/or its affiliates. All rights reserved.
55
66
This program and the accompanying materials are made available under the
77
terms of the Eclipse Public License v. 2.0, which is available at
@@ -128,6 +128,12 @@
128128
<artifactId>osgi-resource-locator</artifactId>
129129
</dependency>
130130

131+
<dependency>
132+
<groupId>org.glassfish.jersey.core</groupId>
133+
<artifactId>jersey-client</artifactId>
134+
<version>${jersey.version}</version>
135+
<scope>test</scope>
136+
</dependency>
131137
<dependency>
132138
<groupId>org.junit.jupiter</groupId>
133139
<artifactId>junit-jupiter</artifactId>

media/jaxb/src/main/java/org/glassfish/jersey/jaxb/internal/AbstractJaxbProvider.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2023 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -323,23 +323,25 @@ protected boolean isXmlRootElementProcessing() {
323323
* @param annotations array of annotations that MAY contain a {@code XmlHeader} annotation instance.
324324
*/
325325
protected void setHeader(Marshaller marshaller, Annotation[] annotations) {
326-
for (Annotation a : annotations) {
327-
if (a instanceof XmlHeader) {
328-
try {
329-
// standalone jaxb ri
330-
marshaller.setProperty("com.sun.xml.bind.xmlHeaders", ((XmlHeader) a).value());
331-
} catch (PropertyException e) {
326+
if (annotations != null) {
327+
for (Annotation a : annotations) {
328+
if (a instanceof XmlHeader) {
332329
try {
333-
// jaxb ri from jdk
334-
marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", ((XmlHeader) a).value());
335-
} catch (PropertyException ex) {
336-
// other jaxb implementation
337-
Logger.getLogger(AbstractJaxbProvider.class.getName()).log(
338-
Level.WARNING, "@XmlHeader annotation is not supported with this JAXB implementation."
339-
+ " Please use JAXB RI if you need this feature.");
330+
// standalone jaxb ri
331+
marshaller.setProperty("com.sun.xml.bind.xmlHeaders", ((XmlHeader) a).value());
332+
} catch (PropertyException e) {
333+
try {
334+
// jaxb ri from jdk
335+
marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", ((XmlHeader) a).value());
336+
} catch (PropertyException ex) {
337+
// other jaxb implementation
338+
Logger.getLogger(AbstractJaxbProvider.class.getName()).log(
339+
Level.WARNING, "@XmlHeader annotation is not supported with this JAXB implementation."
340+
+ " Please use JAXB RI if you need this feature.");
341+
}
340342
}
343+
break;
341344
}
342-
break;
343345
}
344346
}
345347
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2025 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.jersey.jaxb.internal;
18+
19+
import org.hamcrest.MatcherAssert;
20+
import org.hamcrest.Matchers;
21+
import org.junit.jupiter.api.Test;
22+
23+
import javax.ws.rs.client.Client;
24+
import javax.ws.rs.client.ClientBuilder;
25+
import javax.ws.rs.client.ClientRequestContext;
26+
import javax.ws.rs.client.ClientRequestFilter;
27+
import javax.ws.rs.core.Response;
28+
import javax.xml.bind.annotation.XmlElement;
29+
import javax.xml.bind.annotation.XmlRootElement;
30+
31+
public class AbortClientTest {
32+
public static final String MESSAGE = "hello";
33+
@Test
34+
void testAbortWithJaxbEntity() {
35+
Client client = ClientBuilder.newBuilder()
36+
.register(AbortRequestFilter.class)
37+
.build();
38+
39+
try {
40+
JaxbEntity entity = client.target("http://localhost:8080")
41+
.request()
42+
.get()
43+
.readEntity(JaxbEntity.class);
44+
MatcherAssert.assertThat(entity.getStr(), Matchers.is(MESSAGE));
45+
} finally {
46+
client.close();
47+
}
48+
}
49+
50+
public static class AbortRequestFilter implements ClientRequestFilter {
51+
52+
@Override
53+
public void filter(ClientRequestContext requestContext) {
54+
requestContext.abortWith(Response.ok(new JaxbEntity(MESSAGE)).build());
55+
}
56+
57+
}
58+
59+
@XmlRootElement
60+
public static class JaxbEntity {
61+
62+
@XmlElement
63+
private String str;
64+
65+
public JaxbEntity() {}
66+
67+
public JaxbEntity(String str) {
68+
this.str = str;
69+
}
70+
71+
public String getStr() {
72+
return str;
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)