Skip to content

Commit

Permalink
Backed out the jakarta package port and returned to javax for compati…
Browse files Browse the repository at this point in the history
…bility with springboot.
  • Loading branch information
John MacAuley committed Mar 24, 2022
1 parent 6d5595d commit 990ac8a
Show file tree
Hide file tree
Showing 18 changed files with 277 additions and 106 deletions.
74 changes: 27 additions & 47 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@

<groupId>net.es.nsi</groupId>
<artifactId>common-lib</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
<packaging>jar</packaging>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
</parent>

<properties>
<project.scm.id>github</project.scm.id>
<maven.javadoc.skip>true</maven.javadoc.skip>
Expand All @@ -16,54 +22,30 @@
</properties>

<dependencies>
<!-- Needed for our use of JAXB API bindings. -->
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>3.0.0</version>
</dependency>

<!-- Needed for REST API bindings. -->
<!-- For JAXB support. -->
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>3.0.0</version>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>

<!-- For content encoding types. -->
<dependency>
<groupId>jakarta.mail</groupId>
<artifactId>jakarta.mail-api</artifactId>
<version>2.0.0</version>
<scope>provided</scope>
</dependency>

<!-- For priority annotations. -->
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>2.0.0</version>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.6.2</version>
</dependency>

<!-- JAXB Runtime -->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>3.0.0</version>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>

<!-- Helper annotations for Getter/Setters and Builders. -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>[1.18.20,]</version>
</dependency>

<!-- Google JSON parsers perform better than Moxy. -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>[2.8.7,]</version>
</dependency>

<dependency>
Expand All @@ -83,26 +65,25 @@
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>[2.14.1,)</version>
<version>[2.17.0,)</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>[2.14.1,)</version>
<version>[2.17.0,)</version>
</dependency>

<!-- To handle XML dateTime manipulation. -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>[2.10.10,]</version>
<version>[2.10.13,]</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>[4.13.2,)</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -112,7 +93,6 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.5</version>
<configuration>
<generateBackupPoms>false</generateBackupPoms>
</configuration>
Expand All @@ -126,8 +106,9 @@
<!-- Compatible down to java 1.8. Tested 1.8, 11, and 15. -->
<source>1.8</source>
<target>1.8</target>
<!-- <releaase>11</releaase> -->
<!-- <release>11</release> -->
<compilerArgument>-Xlint:deprecation</compilerArgument>
<compilerArgument>-Xlint:unchecked</compilerArgument>
</configuration>
</plugin>

Expand Down Expand Up @@ -177,7 +158,6 @@
<xjbSource>${basedir}/src/main/schema/topology/xjb</xjbSource>
</xjbSources>
<packageName>net.es.nsi.common.jaxb.nml</packageName>
<clearOutputDir>false</clearOutputDir>
</configuration>
</execution>

Expand Down Expand Up @@ -222,21 +202,21 @@
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>3.0.0</version>
<version>2.3.6</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-core</artifactId>
<version>3.0.0</version>
<version>2.3.0.1</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>3.0.0</version>
<version>2.3.6</version>
<scope>compile</scope>
</dependency>
</dependencies>
Expand Down Expand Up @@ -286,7 +266,7 @@
</repository>
<repository>
<id>github</id>
<url>https://maven.pkg.github.com/OWNER/*</url>
<url>https://maven.pkg.github.com/BandwidthOnDemand/*</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
Expand All @@ -296,7 +276,7 @@
<distributionManagement>
<repository>
<id>github</id>
<name>GitHub BanwidthOnDemand nsi-dds-lib Apache Maven Packages</name>
<name>GitHub BanwidthOnDemand nsi-common-lib Apache Maven Packages</name>
<url>https://maven.pkg.github.com/BandwidthOnDemand/nsi-common-lib</url>
</repository>
</distributionManagement>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/es/nsi/common/Nml.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
*/
package net.es.nsi.common;

import jakarta.xml.bind.JAXBException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.xml.bind.JAXBException;
import javax.xml.namespace.QName;
import net.es.nsi.common.jaxb.NmlParser;
import net.es.nsi.common.jaxb.nml.NmlBidirectionalPortType;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/es/nsi/common/NmlEthernet.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.List;
import java.util.Optional;
import jakarta.xml.bind.JAXBElement;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;

/**
Expand Down
69 changes: 43 additions & 26 deletions src/main/java/net/es/nsi/common/jaxb/JaxbParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Optional;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBElement;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;
import jakarta.xml.bind.Unmarshaller;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.io.FileUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

/**
* A singleton to load the very expensive JAXBContext once.
Expand Down Expand Up @@ -191,18 +191,23 @@ public String jaxb2XmlFormatter(JAXBElement<?> jaxbElement) throws JAXBException
* @throws JAXBException Could not parse the specified XML document.
*/
public <T extends Object> T xml2Jaxb(Class<T> xmlClass, String xml) throws JAXBException, IllegalArgumentException {
Optional<JAXBElement<T>> element;
Optional<JAXBElement> element = Optional.empty();
try (StringReader reader = new StringReader(xml)) {
element = Optional.ofNullable((JAXBElement<T>) unmarshaller().unmarshal(reader));
Object unmarshalled = unmarshaller().unmarshal(reader);
if (unmarshalled instanceof JAXBElement) {
element = Optional.ofNullable((JAXBElement) unmarshalled);
}
}

if (!element.isPresent()) {
throw new IllegalArgumentException("Unable to convert string to JAXB, class=" + xmlClass.getName() + ", xml=\n" + xml);
throw new IllegalArgumentException("Unable to convert string to JAXB, class="
+ xmlClass.getName() + ", xml=\n" + xml);
} else if (element.get().getDeclaredType() == xmlClass) {
return xmlClass.cast(element.get().getValue());
}

throw new JAXBException("Expected XML for class " + xmlClass.getCanonicalName() + " but found " + element.get().getDeclaredType().getCanonicalName());
throw new JAXBException("Expected XML for class " + xmlClass.getCanonicalName() + " but found "
+ element.get().getDeclaredType().getCanonicalName());
}

/**
Expand All @@ -216,14 +221,20 @@ public <T extends Object> T xml2Jaxb(Class<T> xmlClass, String xml) throws JAXBE
* @throws IOException InputStream could not be read.
*/
public <T extends Object> T xml2Jaxb(Class<T> xmlClass, InputStream is) throws JAXBException, IOException {
JAXBElement<T> element = (JAXBElement<T>) unmarshaller().unmarshal(is);
if (element == null) {
Optional<JAXBElement> element = Optional.empty();
Object unmarshalled = unmarshaller().unmarshal(is);
if (unmarshalled instanceof JAXBElement) {
element = Optional.ofNullable((JAXBElement) unmarshalled);
}

if (!element.isPresent()) {
throw new IllegalArgumentException("Unable to convert stream to JAXB, class=" + xmlClass.getName());
} else if (element.getDeclaredType() == xmlClass) {
return xmlClass.cast(element.getValue());
} else if (element.get().getDeclaredType() == xmlClass) {
return xmlClass.cast(element.get().getValue());
}

throw new JAXBException("Expected XML for class " + xmlClass.getCanonicalName() + " but found " + element.getDeclaredType().getCanonicalName());
throw new JAXBException("Expected XML for class " + xmlClass.getCanonicalName() + " but found "
+ element.get().getDeclaredType().getCanonicalName());
}

/**
Expand All @@ -237,22 +248,31 @@ public <T extends Object> T xml2Jaxb(Class<T> xmlClass, InputStream is) throws J
* @throws IOException BufferedInputStream could not be read.
*/
public <T extends Object> T xml2Jaxb(Class<T> xmlClass, BufferedInputStream is) throws JAXBException, IOException {
JAXBElement<T> element = (JAXBElement<T>) unmarshaller().unmarshal(is);
if (element.getDeclaredType() == xmlClass) {
return xmlClass.cast(element.getValue());
Optional<JAXBElement> element = Optional.empty();
Object unmarshalled = unmarshaller().unmarshal(is);
if (unmarshalled instanceof JAXBElement) {
element = Optional.ofNullable((JAXBElement) unmarshalled);
}

throw new JAXBException("Expected XML for class " + xmlClass.getCanonicalName() + " but found " + element.getDeclaredType().getCanonicalName());
if (!element.isPresent()) {
throw new IllegalArgumentException("Unable to convert stream to JAXB, class=" + xmlClass.getName());
} else if (element.get().getDeclaredType() == xmlClass) {
return xmlClass.cast(element.get().getValue());
}

throw new JAXBException("Expected XML for class " + xmlClass.getCanonicalName() + " but found "
+ element.get().getDeclaredType().getCanonicalName());
}

/**
* Utility method to marshal a JAXB annotated java object to an XML string.
*
* @param <T>
* @param messageClass The class of the object to marshal.
* @param message The object to marshal.
* @return String containing the XML encoded object.
*/
public static String jaxb2String(Class<?> messageClass, Object message) {
public static <T extends Object> String jaxb2String(Class<T> messageClass, Object message) {

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

try {
// We will use JAXB to marshal the java objects.
final JAXBContext jaxbContext = JAXBContext.newInstance(messageClass);

// We do not have @XmlRootElement annotations on the classes so
// we need to manually create the JAXBElement.
JAXBElement<?> element = new JAXBElement(new QName("uri", "local"), messageClass, message);
JAXBElement<T> element = new JAXBElement<T>(new QName("uri", "local"), messageClass, messageClass.cast(message));

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

// Return the XML string.
return writer.toString();
}
}
4 changes: 2 additions & 2 deletions src/main/java/net/es/nsi/common/jaxb/NmlParser.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package net.es.nsi.common.jaxb;

import jakarta.xml.bind.JAXBElement;
import jakarta.xml.bind.JAXBException;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import net.es.nsi.common.jaxb.nml.NmlTopologyType;
import net.es.nsi.common.jaxb.nml.ObjectFactory;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/es/nsi/common/jaxb/NsaParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import java.io.IOException;
import java.io.InputStream;
import jakarta.xml.bind.JAXBElement;
import jakarta.xml.bind.JAXBException;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import net.es.nsi.common.jaxb.nsa.NsaType;
import net.es.nsi.common.jaxb.nsa.ObjectFactory;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/es/nsi/common/signing/KeyStoreHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

/**
* A Java keystore handler.
*
*
* @author hacksaw
*/
public class KeyStoreHandler {
Expand Down Expand Up @@ -70,7 +70,7 @@ public KeyInfo getKeyInfo(String alias, XMLSignatureFactory fac) throws NoSuchAl
// Create the KeyInfo containing the X509Data.
KeyInfoFactory kif = fac.getKeyInfoFactory();
X509Certificate x509Certificate = getX509Certificate(alias);
List x509Content = new ArrayList();
List<Object> x509Content = new ArrayList<>();
x509Content.add(x509Certificate.getSubjectX500Principal().getName());
x509Content.add(x509Certificate);
X509Data xd = kif.newX509Data(x509Content);
Expand Down
Loading

0 comments on commit 990ac8a

Please sign in to comment.