Skip to content

Commit 990ac8a

Browse files
author
John MacAuley
committed
Backed out the jakarta package port and returned to javax for compatibility with springboot.
1 parent 6d5595d commit 990ac8a

18 files changed

Lines changed: 277 additions & 106 deletions

File tree

pom.xml

Lines changed: 27 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@
55

66
<groupId>net.es.nsi</groupId>
77
<artifactId>common-lib</artifactId>
8-
<version>1.0.0</version>
8+
<version>1.0.1</version>
99
<packaging>jar</packaging>
1010

11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>2.6.4</version>
15+
</parent>
16+
1117
<properties>
1218
<project.scm.id>github</project.scm.id>
1319
<maven.javadoc.skip>true</maven.javadoc.skip>
@@ -16,54 +22,30 @@
1622
</properties>
1723

1824
<dependencies>
19-
<!-- Needed for our use of JAXB API bindings. -->
20-
<dependency>
21-
<groupId>jakarta.xml.bind</groupId>
22-
<artifactId>jakarta.xml.bind-api</artifactId>
23-
<version>3.0.0</version>
24-
</dependency>
25-
26-
<!-- Needed for REST API bindings. -->
25+
<!-- For JAXB support. -->
2726
<dependency>
28-
<groupId>jakarta.ws.rs</groupId>
29-
<artifactId>jakarta.ws.rs-api</artifactId>
30-
<version>3.0.0</version>
27+
<groupId>javax.xml.bind</groupId>
28+
<artifactId>jaxb-api</artifactId>
29+
<version>2.3.1</version>
3130
</dependency>
3231

3332
<!-- For content encoding types. -->
3433
<dependency>
35-
<groupId>jakarta.mail</groupId>
36-
<artifactId>jakarta.mail-api</artifactId>
37-
<version>2.0.0</version>
38-
<scope>provided</scope>
39-
</dependency>
40-
41-
<!-- For priority annotations. -->
42-
<dependency>
43-
<groupId>jakarta.annotation</groupId>
44-
<artifactId>jakarta.annotation-api</artifactId>
45-
<version>2.0.0</version>
34+
<groupId>javax.mail</groupId>
35+
<artifactId>javax.mail-api</artifactId>
36+
<version>1.6.2</version>
4637
</dependency>
4738

48-
<!-- JAXB Runtime -->
4939
<dependency>
50-
<groupId>org.glassfish.jaxb</groupId>
51-
<artifactId>jaxb-runtime</artifactId>
52-
<version>3.0.0</version>
40+
<groupId>com.sun.mail</groupId>
41+
<artifactId>javax.mail</artifactId>
42+
<version>1.6.2</version>
5343
</dependency>
5444

5545
<!-- Helper annotations for Getter/Setters and Builders. -->
5646
<dependency>
5747
<groupId>org.projectlombok</groupId>
5848
<artifactId>lombok</artifactId>
59-
<version>[1.18.20,]</version>
60-
</dependency>
61-
62-
<!-- Google JSON parsers perform better than Moxy. -->
63-
<dependency>
64-
<groupId>com.google.code.gson</groupId>
65-
<artifactId>gson</artifactId>
66-
<version>[2.8.7,]</version>
6749
</dependency>
6850

6951
<dependency>
@@ -83,26 +65,25 @@
8365
<dependency>
8466
<groupId>org.apache.logging.log4j</groupId>
8567
<artifactId>log4j-api</artifactId>
86-
<version>[2.14.1,)</version>
68+
<version>[2.17.0,)</version>
8769
</dependency>
8870

8971
<dependency>
9072
<groupId>org.apache.logging.log4j</groupId>
9173
<artifactId>log4j-core</artifactId>
92-
<version>[2.14.1,)</version>
74+
<version>[2.17.0,)</version>
9375
</dependency>
9476

9577
<!-- To handle XML dateTime manipulation. -->
9678
<dependency>
9779
<groupId>joda-time</groupId>
9880
<artifactId>joda-time</artifactId>
99-
<version>[2.10.10,]</version>
81+
<version>[2.10.13,]</version>
10082
</dependency>
10183

10284
<dependency>
10385
<groupId>junit</groupId>
10486
<artifactId>junit</artifactId>
105-
<version>[4.13.2,)</version>
10687
<scope>test</scope>
10788
</dependency>
10889
</dependencies>
@@ -112,7 +93,6 @@
11293
<plugin>
11394
<groupId>org.codehaus.mojo</groupId>
11495
<artifactId>versions-maven-plugin</artifactId>
115-
<version>2.5</version>
11696
<configuration>
11797
<generateBackupPoms>false</generateBackupPoms>
11898
</configuration>
@@ -126,8 +106,9 @@
126106
<!-- Compatible down to java 1.8. Tested 1.8, 11, and 15. -->
127107
<source>1.8</source>
128108
<target>1.8</target>
129-
<!-- <releaase>11</releaase> -->
109+
<!-- <release>11</release> -->
130110
<compilerArgument>-Xlint:deprecation</compilerArgument>
111+
<compilerArgument>-Xlint:unchecked</compilerArgument>
131112
</configuration>
132113
</plugin>
133114

@@ -177,7 +158,6 @@
177158
<xjbSource>${basedir}/src/main/schema/topology/xjb</xjbSource>
178159
</xjbSources>
179160
<packageName>net.es.nsi.common.jaxb.nml</packageName>
180-
<clearOutputDir>false</clearOutputDir>
181161
</configuration>
182162
</execution>
183163

@@ -222,21 +202,21 @@
222202
<dependency>
223203
<groupId>org.glassfish.jaxb</groupId>
224204
<artifactId>jaxb-xjc</artifactId>
225-
<version>3.0.0</version>
205+
<version>2.3.6</version>
226206
<scope>compile</scope>
227207
</dependency>
228208

229209
<dependency>
230210
<groupId>org.glassfish.jaxb</groupId>
231211
<artifactId>jaxb-core</artifactId>
232-
<version>3.0.0</version>
212+
<version>2.3.0.1</version>
233213
<scope>compile</scope>
234214
</dependency>
235215

236216
<dependency>
237217
<groupId>org.glassfish.jaxb</groupId>
238218
<artifactId>jaxb-runtime</artifactId>
239-
<version>3.0.0</version>
219+
<version>2.3.6</version>
240220
<scope>compile</scope>
241221
</dependency>
242222
</dependencies>
@@ -286,7 +266,7 @@
286266
</repository>
287267
<repository>
288268
<id>github</id>
289-
<url>https://maven.pkg.github.com/OWNER/*</url>
269+
<url>https://maven.pkg.github.com/BandwidthOnDemand/*</url>
290270
<snapshots>
291271
<enabled>true</enabled>
292272
</snapshots>
@@ -296,7 +276,7 @@
296276
<distributionManagement>
297277
<repository>
298278
<id>github</id>
299-
<name>GitHub BanwidthOnDemand nsi-dds-lib Apache Maven Packages</name>
279+
<name>GitHub BanwidthOnDemand nsi-common-lib Apache Maven Packages</name>
300280
<url>https://maven.pkg.github.com/BandwidthOnDemand/nsi-common-lib</url>
301281
</repository>
302282
</distributionManagement>

src/main/java/net/es/nsi/common/Nml.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
*/
2020
package net.es.nsi.common;
2121

22-
import jakarta.xml.bind.JAXBException;
2322
import java.io.IOException;
2423
import java.util.ArrayList;
2524
import java.util.HashMap;
2625
import java.util.List;
2726
import java.util.Map;
2827
import java.util.Optional;
2928
import java.util.stream.Collectors;
29+
import javax.xml.bind.JAXBException;
3030
import javax.xml.namespace.QName;
3131
import net.es.nsi.common.jaxb.NmlParser;
3232
import net.es.nsi.common.jaxb.nml.NmlBidirectionalPortType;

src/main/java/net/es/nsi/common/NmlEthernet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import java.util.List;
44
import java.util.Optional;
5-
import jakarta.xml.bind.JAXBElement;
5+
import javax.xml.bind.JAXBElement;
66
import javax.xml.namespace.QName;
77

88
/**

src/main/java/net/es/nsi/common/jaxb/JaxbParser.java

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@
1010
import java.io.StringReader;
1111
import java.io.StringWriter;
1212
import java.util.Optional;
13-
import jakarta.xml.bind.JAXBContext;
14-
import jakarta.xml.bind.JAXBElement;
15-
import jakarta.xml.bind.JAXBException;
16-
import jakarta.xml.bind.Marshaller;
17-
import jakarta.xml.bind.Unmarshaller;
13+
import javax.xml.bind.JAXBContext;
14+
import javax.xml.bind.JAXBElement;
15+
import javax.xml.bind.JAXBException;
16+
import javax.xml.bind.Marshaller;
17+
import javax.xml.bind.Unmarshaller;
1818
import javax.xml.namespace.QName;
1919
import javax.xml.parsers.DocumentBuilder;
2020
import javax.xml.parsers.DocumentBuilderFactory;
2121
import javax.xml.parsers.ParserConfigurationException;
2222
import org.apache.commons.io.FileUtils;
23-
import org.w3c.dom.Document;
24-
import org.w3c.dom.Node;
2523
import org.apache.logging.log4j.LogManager;
2624
import org.apache.logging.log4j.Logger;
25+
import org.w3c.dom.Document;
26+
import org.w3c.dom.Node;
2727

2828
/**
2929
* A singleton to load the very expensive JAXBContext once.
@@ -191,18 +191,23 @@ public String jaxb2XmlFormatter(JAXBElement<?> jaxbElement) throws JAXBException
191191
* @throws JAXBException Could not parse the specified XML document.
192192
*/
193193
public <T extends Object> T xml2Jaxb(Class<T> xmlClass, String xml) throws JAXBException, IllegalArgumentException {
194-
Optional<JAXBElement<T>> element;
194+
Optional<JAXBElement> element = Optional.empty();
195195
try (StringReader reader = new StringReader(xml)) {
196-
element = Optional.ofNullable((JAXBElement<T>) unmarshaller().unmarshal(reader));
196+
Object unmarshalled = unmarshaller().unmarshal(reader);
197+
if (unmarshalled instanceof JAXBElement) {
198+
element = Optional.ofNullable((JAXBElement) unmarshalled);
199+
}
197200
}
198201

199202
if (!element.isPresent()) {
200-
throw new IllegalArgumentException("Unable to convert string to JAXB, class=" + xmlClass.getName() + ", xml=\n" + xml);
203+
throw new IllegalArgumentException("Unable to convert string to JAXB, class="
204+
+ xmlClass.getName() + ", xml=\n" + xml);
201205
} else if (element.get().getDeclaredType() == xmlClass) {
202206
return xmlClass.cast(element.get().getValue());
203207
}
204208

205-
throw new JAXBException("Expected XML for class " + xmlClass.getCanonicalName() + " but found " + element.get().getDeclaredType().getCanonicalName());
209+
throw new JAXBException("Expected XML for class " + xmlClass.getCanonicalName() + " but found "
210+
+ element.get().getDeclaredType().getCanonicalName());
206211
}
207212

208213
/**
@@ -216,14 +221,20 @@ public <T extends Object> T xml2Jaxb(Class<T> xmlClass, String xml) throws JAXBE
216221
* @throws IOException InputStream could not be read.
217222
*/
218223
public <T extends Object> T xml2Jaxb(Class<T> xmlClass, InputStream is) throws JAXBException, IOException {
219-
JAXBElement<T> element = (JAXBElement<T>) unmarshaller().unmarshal(is);
220-
if (element == null) {
224+
Optional<JAXBElement> element = Optional.empty();
225+
Object unmarshalled = unmarshaller().unmarshal(is);
226+
if (unmarshalled instanceof JAXBElement) {
227+
element = Optional.ofNullable((JAXBElement) unmarshalled);
228+
}
229+
230+
if (!element.isPresent()) {
221231
throw new IllegalArgumentException("Unable to convert stream to JAXB, class=" + xmlClass.getName());
222-
} else if (element.getDeclaredType() == xmlClass) {
223-
return xmlClass.cast(element.getValue());
232+
} else if (element.get().getDeclaredType() == xmlClass) {
233+
return xmlClass.cast(element.get().getValue());
224234
}
225235

226-
throw new JAXBException("Expected XML for class " + xmlClass.getCanonicalName() + " but found " + element.getDeclaredType().getCanonicalName());
236+
throw new JAXBException("Expected XML for class " + xmlClass.getCanonicalName() + " but found "
237+
+ element.get().getDeclaredType().getCanonicalName());
227238
}
228239

229240
/**
@@ -237,22 +248,31 @@ public <T extends Object> T xml2Jaxb(Class<T> xmlClass, InputStream is) throws J
237248
* @throws IOException BufferedInputStream could not be read.
238249
*/
239250
public <T extends Object> T xml2Jaxb(Class<T> xmlClass, BufferedInputStream is) throws JAXBException, IOException {
240-
JAXBElement<T> element = (JAXBElement<T>) unmarshaller().unmarshal(is);
241-
if (element.getDeclaredType() == xmlClass) {
242-
return xmlClass.cast(element.getValue());
251+
Optional<JAXBElement> element = Optional.empty();
252+
Object unmarshalled = unmarshaller().unmarshal(is);
253+
if (unmarshalled instanceof JAXBElement) {
254+
element = Optional.ofNullable((JAXBElement) unmarshalled);
243255
}
244256

245-
throw new JAXBException("Expected XML for class " + xmlClass.getCanonicalName() + " but found " + element.getDeclaredType().getCanonicalName());
257+
if (!element.isPresent()) {
258+
throw new IllegalArgumentException("Unable to convert stream to JAXB, class=" + xmlClass.getName());
259+
} else if (element.get().getDeclaredType() == xmlClass) {
260+
return xmlClass.cast(element.get().getValue());
261+
}
262+
263+
throw new JAXBException("Expected XML for class " + xmlClass.getCanonicalName() + " but found "
264+
+ element.get().getDeclaredType().getCanonicalName());
246265
}
247266

248267
/**
249268
* Utility method to marshal a JAXB annotated java object to an XML string.
250269
*
270+
* @param <T>
251271
* @param messageClass The class of the object to marshal.
252272
* @param message The object to marshal.
253273
* @return String containing the XML encoded object.
254274
*/
255-
public static String jaxb2String(Class<?> messageClass, Object message) {
275+
public static <T extends Object> String jaxb2String(Class<T> messageClass, Object message) {
256276

257277
// Make sure we are given the correct input.
258278
if (messageClass == null || message == null) {
@@ -263,14 +283,12 @@ public static String jaxb2String(Class<?> messageClass, Object message) {
263283
StringWriter writer = new StringWriter();
264284

265285
try {
266-
// We will use JAXB to marshal the java objects.
267-
final JAXBContext jaxbContext = JAXBContext.newInstance(messageClass);
268-
269286
// We do not have @XmlRootElement annotations on the classes so
270287
// we need to manually create the JAXBElement.
271-
JAXBElement<?> element = new JAXBElement(new QName("uri", "local"), messageClass, message);
288+
JAXBElement<T> element = new JAXBElement<T>(new QName("uri", "local"), messageClass, messageClass.cast(message));
272289

273290
// Marshal the object.
291+
final JAXBContext jaxbContext = JAXBContext.newInstance(messageClass);
274292
jaxbContext.createMarshaller().marshal(element, writer);
275293
} catch (JAXBException e) {
276294
// Something went wrong so get out of here.
@@ -279,7 +297,6 @@ public static String jaxb2String(Class<?> messageClass, Object message) {
279297
return null;
280298
}
281299

282-
// Return the XML string.
283300
return writer.toString();
284301
}
285302
}

src/main/java/net/es/nsi/common/jaxb/NmlParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package net.es.nsi.common.jaxb;
22

3-
import jakarta.xml.bind.JAXBElement;
4-
import jakarta.xml.bind.JAXBException;
53
import java.io.IOException;
64
import java.io.InputStream;
5+
import javax.xml.bind.JAXBElement;
6+
import javax.xml.bind.JAXBException;
77
import net.es.nsi.common.jaxb.nml.NmlTopologyType;
88
import net.es.nsi.common.jaxb.nml.ObjectFactory;
99

src/main/java/net/es/nsi/common/jaxb/NsaParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import java.io.IOException;
44
import java.io.InputStream;
5-
import jakarta.xml.bind.JAXBElement;
6-
import jakarta.xml.bind.JAXBException;
5+
import javax.xml.bind.JAXBElement;
6+
import javax.xml.bind.JAXBException;
77
import net.es.nsi.common.jaxb.nsa.NsaType;
88
import net.es.nsi.common.jaxb.nsa.ObjectFactory;
99

src/main/java/net/es/nsi/common/signing/KeyStoreHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
/**
2323
* A Java keystore handler.
24-
*
24+
*
2525
* @author hacksaw
2626
*/
2727
public class KeyStoreHandler {
@@ -70,7 +70,7 @@ public KeyInfo getKeyInfo(String alias, XMLSignatureFactory fac) throws NoSuchAl
7070
// Create the KeyInfo containing the X509Data.
7171
KeyInfoFactory kif = fac.getKeyInfoFactory();
7272
X509Certificate x509Certificate = getX509Certificate(alias);
73-
List x509Content = new ArrayList();
73+
List<Object> x509Content = new ArrayList<>();
7474
x509Content.add(x509Certificate.getSubjectX500Principal().getName());
7575
x509Content.add(x509Certificate);
7676
X509Data xd = kif.newX509Data(x509Content);

0 commit comments

Comments
 (0)