Skip to content

Commit

Permalink
Prevent NPE in AbstractJaxbProvider
Browse files Browse the repository at this point in the history
Signed-off-by: jansupol <[email protected]>
  • Loading branch information
jansupol committed Jan 22, 2025
1 parent d9658aa commit 557faf5
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 16 deletions.
8 changes: 7 additions & 1 deletion media/jaxb/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2015, 2024 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2015, 2025 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
Expand Down Expand Up @@ -128,6 +128,12 @@
<artifactId>osgi-resource-locator</artifactId>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2025 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
Expand Down Expand Up @@ -323,23 +323,25 @@ protected boolean isXmlRootElementProcessing() {
* @param annotations array of annotations that MAY contain a {@code XmlHeader} annotation instance.
*/
protected void setHeader(Marshaller marshaller, Annotation[] annotations) {
for (Annotation a : annotations) {
if (a instanceof XmlHeader) {
try {
// standalone jaxb ri
marshaller.setProperty("com.sun.xml.bind.xmlHeaders", ((XmlHeader) a).value());
} catch (PropertyException e) {
if (annotations != null) {
for (Annotation a : annotations) {
if (a instanceof XmlHeader) {
try {
// jaxb ri from jdk
marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", ((XmlHeader) a).value());
} catch (PropertyException ex) {
// other jaxb implementation
Logger.getLogger(AbstractJaxbProvider.class.getName()).log(
Level.WARNING, "@XmlHeader annotation is not supported with this JAXB implementation."
+ " Please use JAXB RI if you need this feature.");
// standalone jaxb ri
marshaller.setProperty("com.sun.xml.bind.xmlHeaders", ((XmlHeader) a).value());
} catch (PropertyException e) {
try {
// jaxb ri from jdk
marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", ((XmlHeader) a).value());
} catch (PropertyException ex) {
// other jaxb implementation
Logger.getLogger(AbstractJaxbProvider.class.getName()).log(
Level.WARNING, "@XmlHeader annotation is not supported with this JAXB implementation."
+ " Please use JAXB RI if you need this feature.");
}
}
break;
}
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2025 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.jaxb.internal;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.core.Response;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

public class AbortClientTest {
public static final String MESSAGE = "hello";
@Test
void testAbortWithJaxbEntity() {
Client client = ClientBuilder.newBuilder()
.register(AbortRequestFilter.class)
.build();

try {
JaxbEntity entity = client.target("http://localhost:8080")
.request()
.get()
.readEntity(JaxbEntity.class);
MatcherAssert.assertThat(entity.getStr(), Matchers.is(MESSAGE));
} finally {
client.close();
}
}

public static class AbortRequestFilter implements ClientRequestFilter {

@Override
public void filter(ClientRequestContext requestContext) {
requestContext.abortWith(Response.ok(new JaxbEntity(MESSAGE)).build());
}

}

@XmlRootElement
public static class JaxbEntity {

@XmlElement
private String str;

public JaxbEntity() {}

public JaxbEntity(String str) {
this.str = str;
}

public String getStr() {
return str;
}
}
}

0 comments on commit 557faf5

Please sign in to comment.