Skip to content

Commit

Permalink
Polish Code and reduce duplicate code
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Alzate <aalzate@sonatype.com>
  • Loading branch information
mr-zepol committed Mar 21, 2024
1 parent a452407 commit 360b7a0
Show file tree
Hide file tree
Showing 17 changed files with 178 additions and 139 deletions.
1 change: 0 additions & 1 deletion src/main/java/org/cyclonedx/model/ExtensibleElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import org.apache.commons.lang3.StringUtils;
import org.cyclonedx.util.serializer.ExtensibleTypesSerializer;
import org.cyclonedx.util.deserializer.ExtensionDeserializer;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* This file is part of CycloneDX Core (Java).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* Copyright (c) OWASP Foundation. All Rights Reserved.
*/
package org.cyclonedx.util.deserializer;

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import org.cyclonedx.model.formulation.common.AbstractType;
import org.cyclonedx.model.formulation.common.EnvVariableChoice;
import org.cyclonedx.model.formulation.common.ResourceReferenceChoice;

public abstract class AbstractDataTypeDeserializer<T extends AbstractType>
extends JsonDeserializer<T> {

protected final ObjectMapper objectMapper = new ObjectMapper();

protected void setEnvironmentVars(final JsonNode node, AbstractType data) throws JsonProcessingException {
JsonNode nodes = node.get("environmentVars");
List<EnvVariableChoice> environmentVars = new ArrayList<>();

ArrayNode environmentVarsNode = (nodes.isArray() ? (ArrayNode) nodes : new ArrayNode(null).add(nodes));

for (JsonNode envVarNode : environmentVarsNode) {
EnvVariableChoice envVar = objectMapper.treeToValue(envVarNode, EnvVariableChoice.class);
environmentVars.add(envVar);
}
data.setEnvironmentVars(environmentVars);
}

protected void setReference(JsonNode node, String fieldName, AbstractType type)
throws JsonProcessingException
{
if (node.has(fieldName)) {
JsonNode fieldNode = node.get(fieldName);
ResourceReferenceChoice reference = objectMapper.treeToValue(fieldNode, ResourceReferenceChoice.class);

if ("source".equals(fieldName)) {
type.setSource(reference);
} else if ("target".equals(fieldName)) {
type.setTarget(reference);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,20 @@
package org.cyclonedx.util.deserializer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import org.cyclonedx.model.AttachmentText;
import org.cyclonedx.model.Property;
import org.cyclonedx.model.formulation.common.EnvVariableChoice;
import org.cyclonedx.model.formulation.common.InputType;
import org.cyclonedx.model.formulation.common.InputType.Parameter;
import org.cyclonedx.model.formulation.common.ResourceReferenceChoice;

public class InputTypeDeserializer extends JsonDeserializer<InputType> {
private final ObjectMapper objectMapper = new ObjectMapper();
public class InputTypeDeserializer extends AbstractDataTypeDeserializer<InputType> {

@Override
public InputType deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
Expand All @@ -47,17 +41,8 @@ public InputType deserialize(JsonParser jsonParser, DeserializationContext deser
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
InputType inputType = new InputType();

if(node.has("source")) {
JsonNode sourceNode = node.get("source");
ResourceReferenceChoice source = objectMapper.treeToValue(sourceNode, ResourceReferenceChoice.class);
inputType.setSource(source);
}

if(node.has("target")) {
JsonNode targetNode = node.get("target");
ResourceReferenceChoice target = objectMapper.treeToValue(targetNode, ResourceReferenceChoice.class);
inputType.setTarget(target);
}
setReference(node, "source", inputType);
setReference(node, "target", inputType);

createInputDataInfo(node, inputType);

Expand All @@ -80,16 +65,7 @@ private void createInputDataInfo(JsonNode node, InputType inputType ) throws Jso
List<Parameter> parameters = objectMapper.convertValue(parametersNode, new TypeReference<List<Parameter>>() {});
inputType.setParameters(parameters);
} else if (node.has("environmentVars")) {
JsonNode nodes = node.get("environmentVars");
List<EnvVariableChoice> environmentVars = new ArrayList<>();

ArrayNode environmentVarsNode = (nodes.isArray() ? (ArrayNode) nodes : new ArrayNode(null).add(nodes));

for (JsonNode envVarNode : environmentVarsNode) {
EnvVariableChoice envVar = objectMapper.treeToValue(envVarNode, EnvVariableChoice.class);
environmentVars.add(envVar);
}
inputType.setEnvironmentVars(environmentVars);
setEnvironmentVars(node, inputType);
} else if (node.has("data")) {
JsonNode dataNode = node.get("data");
AttachmentText data = objectMapper.treeToValue(dataNode, AttachmentText.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,21 @@
package org.cyclonedx.util.deserializer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import org.cyclonedx.model.AttachmentText;
import org.cyclonedx.model.Property;
import org.cyclonedx.model.formulation.common.EnvVariableChoice;
import org.cyclonedx.model.formulation.common.OutputType;
import org.cyclonedx.model.formulation.common.OutputType.OutputTypeEnum;
import org.cyclonedx.model.formulation.common.ResourceReferenceChoice;

public class OutputTypeDeserializer
extends JsonDeserializer<OutputType> {
private final ObjectMapper objectMapper = new ObjectMapper();
extends AbstractDataTypeDeserializer<OutputType> {

@Override
public OutputType deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
Expand All @@ -48,17 +42,8 @@ public OutputType deserialize(JsonParser jsonParser, DeserializationContext dese
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
OutputType outputType = new OutputType();

if(node.has("source")) {
JsonNode sourceNode = node.get("source");
ResourceReferenceChoice source = objectMapper.treeToValue(sourceNode, ResourceReferenceChoice.class);
outputType.setSource(source);
}

if(node.has("target")) {
JsonNode targetNode = node.get("target");
ResourceReferenceChoice target = objectMapper.treeToValue(targetNode, ResourceReferenceChoice.class);
outputType.setTarget(target);
}
setReference(node, "source", outputType);
setReference(node, "target", outputType);

createOutputDataInfo(node, outputType);

Expand All @@ -82,18 +67,11 @@ private void createOutputDataInfo(JsonNode node, OutputType outputType) throws J
JsonNode resourceNode = node.get("resource");
ResourceReferenceChoice resource = objectMapper.treeToValue(resourceNode, ResourceReferenceChoice.class);
outputType.setResource(resource);
} else if (node.has("environmentVars")) {
JsonNode nodes = node.get("environmentVars");
List<EnvVariableChoice> environmentVars = new ArrayList<>();

ArrayNode environmentVarsNode = (nodes.isArray() ? (ArrayNode) nodes : new ArrayNode(null).add(nodes));

for (JsonNode envVarNode : environmentVarsNode) {
EnvVariableChoice envVar = objectMapper.treeToValue(envVarNode, EnvVariableChoice.class);
environmentVars.add(envVar);
}
outputType.setEnvironmentVars(environmentVars);
} else if (node.has("data")) {
}
else if (node.has("environmentVars")) {
setEnvironmentVars(node, outputType);
}
else if (node.has("data")) {
JsonNode dataNode = node.get("data");
AttachmentText data = objectMapper.treeToValue(dataNode, AttachmentText.class);
outputType.setData(data);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.cyclonedx.util.serializer;

import java.io.IOException;
import java.util.List;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import org.cyclonedx.model.formulation.common.AbstractType;
import org.cyclonedx.model.formulation.common.EnvVariableChoice;


public abstract class AbstractDataTypeSerializer<T extends AbstractType> extends StdSerializer<T>
{

public AbstractDataTypeSerializer(Class<T> t) {
super(t);
}

protected void parseEnvironmentVars(final JsonGenerator jsonGenerator, final List<EnvVariableChoice> vars)
throws IOException
{
jsonGenerator.writeArrayFieldStart("environmentVars");
for (EnvVariableChoice envVarChoice : vars) {
if (envVarChoice.getEnvironmentVar() != null) {
jsonGenerator.writeStartObject();
jsonGenerator.writeObjectField("environmentVar", envVarChoice.getEnvironmentVar());
jsonGenerator.writeEndObject();
}
else if (envVarChoice.getValue() != null) {
jsonGenerator.writeStartObject();
jsonGenerator.writeObjectField("value", envVarChoice.getValue());
jsonGenerator.writeEndObject();
}
}
jsonGenerator.writeEndArray();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
import org.cyclonedx.model.formulation.common.EnvVariableChoice;
import org.cyclonedx.model.formulation.common.InputType;

public class InputTypeSerializer
extends StdSerializer<InputType>
extends AbstractDataTypeSerializer<InputType>
{
private final boolean isXml;

Expand Down Expand Up @@ -48,40 +46,27 @@ else if (input.getParameters() != null && !input.getParameters().isEmpty()) {
jsonGenerator.writeObject( input.getParameters());
}
else if (input.getEnvironmentVars() != null && !input.getEnvironmentVars().isEmpty()) {
jsonGenerator.writeArrayFieldStart("environmentVars");
for (EnvVariableChoice envVarChoice : input.getEnvironmentVars()) {
if (envVarChoice.getEnvironmentVar() != null) {
jsonGenerator.writeStartObject();
jsonGenerator.writeObjectField("environmentVar", envVarChoice.getEnvironmentVar());
jsonGenerator.writeEndObject();
} else if (envVarChoice.getValue() != null) {
jsonGenerator.writeStartObject();
jsonGenerator.writeObjectField("value", envVarChoice.getValue());
jsonGenerator.writeEndObject();
}
}
jsonGenerator.writeEndArray();
parseEnvironmentVars(jsonGenerator, input.getEnvironmentVars());
}
else if (input.getData() != null) {
jsonGenerator.writeFieldName("data");
jsonGenerator.writeObject( input.getData());
}

if (input.getSource() != null) {
jsonGenerator.writeFieldName("source");
jsonGenerator.writeObject(input.getSource());
}
if (input.getTarget() != null) {
jsonGenerator.writeFieldName("target");
jsonGenerator.writeObject(input.getTarget());
}
if (input.getProperties() != null) {
jsonGenerator.writeFieldName("properties");
jsonGenerator.writeObject( input.getProperties());
}
writeField(jsonGenerator, "source", input.getSource());
writeField(jsonGenerator, "target", input.getTarget());
writeField(jsonGenerator, "properties", input.getProperties());

jsonGenerator.writeEndObject();
}

private void writeField(JsonGenerator jsonGenerator, String fieldName, Object fieldValue) throws IOException {
if (fieldValue != null) {
jsonGenerator.writeFieldName(fieldName);
jsonGenerator.writeObject(fieldValue);
}
}

@Override
public Class<InputType> handledType() {
return InputType.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public LicenseChoiceSerializer() {
this(LicenseChoice.class);
}

public LicenseChoiceSerializer(final Class t) {
public LicenseChoiceSerializer(final Class<LicenseChoice> t) {
super(t);
}

Expand Down
Loading

0 comments on commit 360b7a0

Please sign in to comment.