-
-
Notifications
You must be signed in to change notification settings - Fork 563
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #3509 Allows definition of pURL in component or projects either…
… as string or object to conform to swagger api defintion Signed-off-by: Sebastien Delcoigne <sebastien.delcoigne@gmail.com>
- Loading branch information
Showing
5 changed files
with
205 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/main/java/org/dependencytrack/resources/v1/serializers/CustomPackageURLDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* This file is part of Dependency-Track. | ||
* | ||
* 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) Steve Springett. All Rights Reserved. | ||
*/ | ||
package org.dependencytrack.resources.v1.serializers; | ||
|
||
import alpine.common.logging.Logger; | ||
import com.fasterxml.jackson.core.JacksonException; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.github.packageurl.MalformedPackageURLException; | ||
import com.github.packageurl.PackageURLBuilder; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* This class deserializes a PackageURL from either the canonicalized form | ||
* or the object form. | ||
* | ||
* @author Sebastien Delcoigne | ||
* @since 4.11.0 | ||
*/ | ||
public class CustomPackageURLDeserializer extends JsonDeserializer<String> { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(CustomPackageURLDeserializer.class); | ||
|
||
@Override | ||
public String deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException { | ||
try { | ||
if (jsonParser.getCurrentToken() == JsonToken.START_OBJECT) { | ||
PackageURLBuilder builder = PackageURLBuilder.aPackageURL(); | ||
while (jsonParser.nextToken() != JsonToken.END_OBJECT) { | ||
String name = jsonParser.getCurrentName(); | ||
switch (name) { | ||
case "type" -> { | ||
jsonParser.nextToken(); | ||
builder.withType(jsonParser.getText()); | ||
} | ||
case "namespace" -> { | ||
jsonParser.nextToken(); | ||
builder.withNamespace(jsonParser.getText()); | ||
} | ||
case "name" -> { | ||
jsonParser.nextToken(); | ||
builder.withName(jsonParser.getText()); | ||
} | ||
case "version" -> { | ||
jsonParser.nextToken(); | ||
builder.withVersion(jsonParser.getText()); | ||
} | ||
case "subpath" -> { | ||
jsonParser.nextToken(); | ||
builder.withSubpath(jsonParser.getText()); | ||
} | ||
case "qualifiers" -> { | ||
extractQualifiers(jsonParser, builder); | ||
} | ||
} | ||
} | ||
return builder.build().canonicalize(); | ||
} else if (jsonParser.getCurrentToken() == JsonToken.VALUE_STRING) { | ||
return jsonParser.getValueAsString(); | ||
} | ||
} catch (MalformedPackageURLException e) { | ||
LOGGER.warn("Malformed pURL encountered", e); | ||
} | ||
return null; | ||
} | ||
|
||
private void extractQualifiers(JsonParser jsonParser, PackageURLBuilder builder) throws IOException { | ||
if (jsonParser.getCurrentToken() == JsonToken.START_OBJECT) { | ||
while (jsonParser.nextToken() != JsonToken.END_OBJECT) { | ||
String key = jsonParser.getCurrentName(); | ||
jsonParser.nextToken(); | ||
String value = jsonParser.getText(); | ||
builder.withQualifier(key, value); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters