Skip to content

Commit

Permalink
Migrate javax.json to jakarta.json (frankframework#3515)
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsm5 authored Jul 20, 2022
1 parent 49a2d6e commit 736e6ae
Show file tree
Hide file tree
Showing 35 changed files with 356 additions and 365 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ public enum HashAlgorithm {
/**
* Algorithm name as defined in {@link java.security.MessageDigest#getInstance(String)}
*/
private @Getter final String algorithm;
private final @Getter String algorithm;

private HashAlgorithm(final String algorithm) {
this.algorithm = algorithm;
}

public String computeHash(Message file) throws IOException {
byte[] fileBytes = file.asByteArray();
if (fileBytes == null) {
if (Message.isEmpty(file)) {
throw new IllegalStateException("unable to compute hash over null message");
}
byte[] fileBytes = file.asByteArray();

byte[] checksum = computeHash(fileBytes, this);
if(checksum != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,50 +97,50 @@ public void setVersion(int version) {

public void mapParameters(ParameterValueList pvl) throws SenderException {
if(action == Action.UPLOAD && pvl.contains(NetStorageSender.FILE_PARAM_KEY)) {
file = pvl.getParameterValue(NetStorageSender.FILE_PARAM_KEY).asMessage();
file = pvl.get(NetStorageSender.FILE_PARAM_KEY).asMessage();
if(Message.isEmpty(file)) {
throw new SenderException("no file specified");
}

if(hashAlgorithm != null) {
try {
generateHash(pvl);
}
catch (IOException e) {
throw new SenderException("error while calculating ["+hashAlgorithm+"] hash", e);
}
generateHash(pvl);
}

setEntity(file);

if(pvl.contains("size")) {
int size = pvl.getParameterValue("size").asIntegerValue(0);
int size = pvl.get("size").asIntegerValue(0);
actionHeader.put("size", size+"");
}
}

if(action == Action.RENAME && pvl.contains(NetStorageSender.DESTINATION_PARAM_KEY)) {
String destination = pvl.getParameterValue(NetStorageSender.DESTINATION_PARAM_KEY).asStringValue(null);
String destination = pvl.get(NetStorageSender.DESTINATION_PARAM_KEY).asStringValue(null);
actionHeader.put("destination", destination);
}

if(pvl.contains(NetStorageSender.MTIME_PARAM_KEY)) {
long mtime = pvl.getParameterValue(NetStorageSender.MTIME_PARAM_KEY).asLongValue(-1L);
long mtime = pvl.get(NetStorageSender.MTIME_PARAM_KEY).asLongValue(-1L);
actionHeader.put("mtime", mtime+"");
}
}

private void generateHash(ParameterValueList pvl) throws IOException {
private void generateHash(ParameterValueList pvl) throws SenderException {
String hash = null;
String algorithm = hashAlgorithm.name().toLowerCase();
if(pvl.contains(NetStorageSender.HASHVALUE_PARAM_KEY)) {
hash = pvl.getParameterValue(NetStorageSender.HASHVALUE_PARAM_KEY).asStringValue(null);
hash = pvl.get(NetStorageSender.HASHVALUE_PARAM_KEY).asStringValue(null);
}
else if(pvl.contains(algorithm)) { //backwards compatibility
hash = pvl.getParameterValue(algorithm).asStringValue(null);
hash = pvl.get(algorithm).asStringValue(null);
}
else {
hash = hashAlgorithm.computeHash(file);
try {
hash = hashAlgorithm.computeHash(file);
}
catch (IOException | IllegalStateException e) {
throw new SenderException("error while calculating ["+hashAlgorithm+"] hash", e);
}
}

if(StringUtils.isNotEmpty(hash)) {
Expand Down
5 changes: 2 additions & 3 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,10 @@
<artifactId>json</artifactId>
<version>20080701</version>
</dependency>
<!-- javax.json is included in java 7SE -->
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.0.4</version>
<artifactId>jakarta.json</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>xalan</groupId>
Expand Down
18 changes: 9 additions & 9 deletions core/src/main/java/nl/nn/adapterframework/align/Json2Xml.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,6 @@
import java.util.Map;
import java.util.Set;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonException;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonString;
import javax.json.JsonStructure;
import javax.json.JsonValue;
import javax.xml.validation.ValidatorHandler;

import org.apache.commons.lang3.StringUtils;
Expand All @@ -41,6 +32,15 @@
import org.apache.xerces.xs.XSModel;
import org.xml.sax.SAXException;

import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonArrayBuilder;
import jakarta.json.JsonException;
import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
import jakarta.json.JsonString;
import jakarta.json.JsonStructure;
import jakarta.json.JsonValue;
import lombok.Getter;
import lombok.Setter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@
import java.util.ArrayList;
import java.util.List;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonStructure;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.logging.log4j.Logger;
Expand All @@ -46,6 +39,12 @@
import org.apache.xerces.xs.XSTypeDefinition;
import org.apache.xerces.xs.XSWildcard;

import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonArrayBuilder;
import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
import jakarta.json.JsonStructure;
import lombok.Getter;
import nl.nn.adapterframework.util.LogUtil;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.Map;
import java.util.Map.Entry;

import javax.json.stream.JsonGenerator;
import jakarta.json.stream.JsonGenerator;

import org.apache.commons.lang3.NotImplementedException;
import org.apache.logging.log4j.Logger;
Expand All @@ -34,7 +34,7 @@
*/
public class JsonDocumentContainer extends TreeContentContainer<JsonElementContainer>{
protected Logger log = LogUtil.getLogger(this.getClass());

private String name;
private boolean skipArrayElementContainers;
private boolean skipRootElement;
Expand All @@ -43,13 +43,13 @@ public class JsonDocumentContainer extends TreeContentContainer<JsonElementConta

private final char[] INDENTOR="\n ".toCharArray();
private final int MAX_INDENT=INDENTOR.length/2;

public JsonDocumentContainer(String name, boolean skipArrayElementContainers, boolean skipRootElement) {
this.name=name;
this.skipArrayElementContainers=skipArrayElementContainers;
this.skipRootElement=skipRootElement;
}

@Override
protected JsonElementContainer createElementContainer(String localName, boolean xmlArrayContainer, boolean repeatedElement, XSTypeDefinition typeDefinition) {
return new JsonElementContainer(localName, xmlArrayContainer, repeatedElement, skipArrayElementContainers, attributePrefix, mixedContentLabel, typeDefinition);
Expand All @@ -61,12 +61,11 @@ protected void addContent(JsonElementContainer parent, JsonElementContainer chil
parent.addContent(child);
}


@Override
public String toString() {
return toString(true);
}

public String toString(boolean indent) {
Object content=getRoot().getContent();
if (content==null) {
Expand All @@ -89,12 +88,12 @@ public String toString(boolean indent) {
// generator.close();
// return writer.toString();
}

protected void toString(StringBuffer sb, Object item, int indentLevel) {
if (item==null) {
sb.append("null");
} else if (item instanceof String) {
sb.append(item);
sb.append(item);
} else if (item instanceof Map) {
sb.append("{");
if (indentLevel>=0) indentLevel++;
Expand Down Expand Up @@ -129,17 +128,17 @@ protected void toString(StringBuffer sb, Object item, int indentLevel) {

protected void generate(JsonGenerator g, String key, Object item) {
if (item==null) {
if (key!=null) g.writeNull(key); else g.writeNull();
if (key!=null) g.writeNull(key); else g.writeNull();
} else if (item instanceof String) {
if (key!=null) g.write(key,(String)item); else g.write((String)item);
if (key!=null) g.write(key,(String)item); else g.write((String)item);
} else if (item instanceof Map) {
if (key!=null) g.writeStartObject(key); else g.writeStartObject();
if (key!=null) g.writeStartObject(key); else g.writeStartObject();
for (Entry<String,Object> entry:((Map<String,Object>)item).entrySet()) {
generate(g, entry.getKey(), entry.getValue());
}
g.writeEnd();
} else if (item instanceof List) {
if (key!=null) g.writeStartArray(key); else g.writeStartArray();
if (key!=null) g.writeStartArray(key); else g.writeStartArray();
for (Object subitem:(List)item) {
generate(g, null, subitem);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
import java.util.List;
import java.util.Map;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonWriter;
import javax.json.JsonWriterFactory;
import javax.json.stream.JsonGenerator;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMultipart;
Expand All @@ -41,6 +36,11 @@

import com.nimbusds.jose.util.JSONObjectUtils;

import jakarta.json.Json;
import jakarta.json.JsonObject;
import jakarta.json.JsonWriter;
import jakarta.json.JsonWriterFactory;
import jakarta.json.stream.JsonGenerator;
import nl.nn.adapterframework.core.PipeLineSession;
import nl.nn.adapterframework.http.HttpSecurityHandler;
import nl.nn.adapterframework.http.HttpServletBase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonValue;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Response.Status;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Logger;
import org.apache.xerces.xs.XSModel;

import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonArrayBuilder;
import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
import jakarta.json.JsonValue;
import nl.nn.adapterframework.align.XmlTypeToJsonSchemaConverter;
import nl.nn.adapterframework.core.IAdapter;
import nl.nn.adapterframework.core.IPipe;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.naming.NamingException;

import org.apache.commons.lang3.StringUtils;
Expand All @@ -47,6 +44,9 @@
import com.mongodb.client.result.UpdateResult;
import com.mongodb.connection.ServerDescription;

import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import lombok.Getter;
import lombok.Lombok;
import lombok.Setter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@
import java.util.List;
import java.util.Map;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonStructure;
import javax.xml.validation.ValidatorHandler;

import org.apache.commons.lang3.StringUtils;
import org.apache.xerces.xs.XSModel;
import org.xml.sax.ContentHandler;
import org.xml.sax.helpers.XMLFilterImpl;

import jakarta.json.Json;
import jakarta.json.JsonObject;
import jakarta.json.JsonStructure;
import lombok.Getter;
import nl.nn.adapterframework.align.Json2Xml;
import nl.nn.adapterframework.align.Xml2Json;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@

import java.io.IOException;

import javax.json.Json;
import javax.json.JsonException;
import javax.json.JsonReader;

import jakarta.json.Json;
import jakarta.json.JsonException;
import jakarta.json.JsonReader;
import nl.nn.adapterframework.core.PipeLineSession;
import nl.nn.adapterframework.core.PipeRunException;
import nl.nn.adapterframework.core.PipeRunResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class JsonTee implements JsonEventHandler {

private JsonEventHandler first;
private JsonEventHandler second;

public JsonTee(JsonEventHandler first, JsonEventHandler second) {
this.first=first;
this.second=second;
Expand Down Expand Up @@ -76,5 +76,5 @@ public void primitive(Object value) throws SAXException {
first.primitive(value);
second.primitive(value);
}

}
Loading

0 comments on commit 736e6ae

Please sign in to comment.