Skip to content

Commit

Permalink
feat: removed passport class, using jsonnode instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias Brunkow Moser committed Oct 13, 2023
1 parent b16e869 commit ac3c4cc
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
********************************************************************************/

package org.eclipse.tractusx.productpass.http.controllers;
import com.fasterxml.jackson.databind.JsonNode;

import com.fasterxml.jackson.databind.JsonNode;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
Expand All @@ -35,8 +35,6 @@
import org.eclipse.tractusx.productpass.config.DtrConfig;
import org.eclipse.tractusx.productpass.config.ProcessConfig;
import org.eclipse.tractusx.productpass.exceptions.ControllerException;
import org.eclipse.tractusx.productpass.exceptions.DataModelException;
import org.eclipse.tractusx.productpass.exceptions.ServiceException;
import org.eclipse.tractusx.productpass.managers.ProcessManager;
import org.eclipse.tractusx.productpass.models.catenax.Dtr;
import org.eclipse.tractusx.productpass.models.dtregistry.DigitalTwin3;
Expand All @@ -49,7 +47,6 @@
import org.eclipse.tractusx.productpass.models.manager.History;
import org.eclipse.tractusx.productpass.models.manager.SearchStatus;
import org.eclipse.tractusx.productpass.models.manager.Status;
import org.eclipse.tractusx.productpass.models.passports.Passport;
import org.eclipse.tractusx.productpass.services.AasService;
import org.eclipse.tractusx.productpass.services.DataPlaneService;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -291,7 +288,7 @@ public Response endpoint(@RequestBody Object body, @PathVariable String processI
return httpUtil.buildResponse(httpUtil.getNotFound("Process not found!"), httpResponse);
}

Passport passport = dataPlaneService.getPassport(endpointData);
JsonNode passport = dataPlaneService.getPassport(endpointData);
if (passport == null) {
return httpUtil.buildResponse(httpUtil.getNotFound("Passport not found in data plane!"), httpResponse);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

package org.eclipse.tractusx.productpass.http.controllers.api;

import com.fasterxml.jackson.databind.JsonNode;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
Expand All @@ -41,7 +42,6 @@
import org.eclipse.tractusx.productpass.models.manager.Process;
import org.eclipse.tractusx.productpass.models.manager.Status;
import org.eclipse.tractusx.productpass.models.negotiation.Dataset;
import org.eclipse.tractusx.productpass.models.passports.Passport;
import org.eclipse.tractusx.productpass.models.passports.PassportResponse;
import org.eclipse.tractusx.productpass.services.AasService;
import org.eclipse.tractusx.productpass.services.AuthenticationService;
Expand Down Expand Up @@ -105,8 +105,6 @@ Response index() throws Exception {
schema = @Schema(implementation = Response.class))),
@ApiResponse(description = "Content of Data Field in Response", responseCode = "200", content = @Content(mediaType = "application/json",
schema = @Schema(implementation = PassportResponse.class))),
@ApiResponse(description = "Content of Passport Field in Data Field", useReturnTypeSchema = true, content = @Content(mediaType = "application/json",
schema = @Schema(implementation = Passport.class)))
})
public Response getPassport(@Valid @RequestBody TokenRequest tokenRequestBody) {
Response response = httpUtil.getInternalError();
Expand Down Expand Up @@ -181,7 +179,7 @@ public Response getPassport(@Valid @RequestBody TokenRequest tokenRequestBody) {
return httpUtil.buildResponse(response, httpResponse);
}
String semanticId = status.getSemanticId();
Passport passport = processManager.loadPassport(processId);
JsonNode passport = processManager.loadPassport(processId);
if (passport == null) {
response = httpUtil.getNotFound("Failed to load passport!");
return httpUtil.buildResponse(response, httpResponse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package org.eclipse.tractusx.productpass.managers;

import com.fasterxml.jackson.databind.JsonNode;
import jakarta.servlet.http.HttpServletRequest;
import org.eclipse.tractusx.productpass.config.ProcessConfig;
import org.eclipse.tractusx.productpass.exceptions.ManagerException;
Expand All @@ -40,7 +41,6 @@
import org.eclipse.tractusx.productpass.models.manager.SearchStatus;
import org.eclipse.tractusx.productpass.models.manager.Status;
import org.eclipse.tractusx.productpass.models.negotiation.*;
import org.eclipse.tractusx.productpass.models.passports.Passport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -1203,7 +1203,7 @@ public String getContractId(DataPlaneEndpoint endpointData){
* @throws ManagerException
* if unable to load the passport.
*/
public Passport loadPassport(String processId){
public JsonNode loadPassport(String processId){
try {
String path = this.getProcessFilePath(processId, this.passportFileName);
History history = new History(
Expand All @@ -1213,17 +1213,17 @@ public Passport loadPassport(String processId){
if(!fileUtil.pathExists(path)){
throw new ManagerException(this.getClass().getName(), "Passport file ["+path+"] not found!");
}
Passport passport = null;
JsonNode passport = null;
Boolean encrypt = env.getProperty("passport.dataTransfer.encrypt", Boolean.class, true);
if(encrypt){
Status status = this.getStatus(processId);
History negotiationHistory = status.getHistory("negotiation-accepted");
String decryptedPassportJson = CrypUtil.decryptAes(fileUtil.readFile(path), this.generateStatusToken(status, negotiationHistory.getId()));
// Delete passport file

passport = (Passport) jsonUtil.loadJson(decryptedPassportJson, Passport.class);
passport = (JsonNode) jsonUtil.loadJson(decryptedPassportJson, JsonNode.class);
}else{
passport = (Passport) jsonUtil.fromJsonFileToObject(path, Passport.class);
passport = (JsonNode) jsonUtil.fromJsonFileToObject(path, JsonNode.class);
}

if(passport == null){
Expand Down Expand Up @@ -1262,7 +1262,7 @@ public Passport loadPassport(String processId){
* @throws ManagerException
* if unable to save the passport.
*/
public String savePassport(String processId, DataPlaneEndpoint endpointData, Passport passport) {
public String savePassport(String processId, DataPlaneEndpoint endpointData, JsonNode passport) {
try {
// Retrieve the configuration
Boolean prettyPrint = env.getProperty("passport.dataTransfer.indent", Boolean.class, true);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public class PassportResponse {
@JsonProperty("metadata")
Map<String, Object> metadata;
@JsonProperty("passport")
Passport passport;
JsonNode passport;

@SuppressWarnings("Unused")
public PassportResponse(Map<String, Object> metadata, Passport passport) {
public PassportResponse(Map<String, Object> metadata, JsonNode passport) {
this.metadata = metadata;
this.passport = passport;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@

package org.eclipse.tractusx.productpass.services;

import com.fasterxml.jackson.databind.JsonNode;
import org.eclipse.tractusx.productpass.exceptions.ServiceException;
import org.eclipse.tractusx.productpass.exceptions.ServiceInitializationException;
import org.eclipse.tractusx.productpass.models.edc.DataPlaneEndpoint;
import org.eclipse.tractusx.productpass.models.passports.Passport;
import org.eclipse.tractusx.productpass.models.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
Expand Down Expand Up @@ -96,9 +96,9 @@ public Object getTransferData(DataPlaneEndpoint endpointData) {
* @throws ServiceException
* if unable to parse the data to the passport.
*/
public Passport getPassport(DataPlaneEndpoint endpointData) {
public JsonNode getPassport(DataPlaneEndpoint endpointData) {
try {
return (Passport) jsonUtil.bindObject(this.getTransferData(endpointData), Passport.class);
return jsonUtil.toJsonNode(this.getTransferData(endpointData));
}catch (Exception e){
throw new ServiceException(this.getClass().getName()+"."+"getPassport",
e,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.eclipse.tractusx.productpass.models.manager.Status;
import org.eclipse.tractusx.productpass.models.negotiation.*;
import org.eclipse.tractusx.productpass.models.negotiation.Set;
import org.eclipse.tractusx.productpass.models.passports.Passport;
import org.eclipse.tractusx.productpass.models.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
Expand Down Expand Up @@ -724,7 +723,7 @@ public Transfer seeTransfer(String id, String processId, ProcessDataModel dataMo
* if unable to get the passport.
*/
@SuppressWarnings("Unused")
public Passport getPassport(String transferProcessId, String endpoint) {
public JsonNode getPassport(String transferProcessId, String endpoint) {
try {
this.checkEmptyVariables();
Map<String, Object> params = httpUtil.getParams();
Expand All @@ -739,7 +738,7 @@ public Passport getPassport(String transferProcessId, String endpoint) {
throw new ServiceException(this.getClass().getName() + ".getPassport", "It was not possible to get passport with id " + transferProcessId);
}
String responseBody = (String) response.getBody();
return (Passport) jsonUtil.bindJsonNode(jsonUtil.toJsonNode(responseBody), Passport.class);
return (JsonNode) jsonUtil.toJsonNode(responseBody);
} catch (Exception e) {
throw new ServiceException(this.getClass().getName() + "." + "getPassport",
e,
Expand Down
19 changes: 19 additions & 0 deletions consumer-backend/productpass/src/main/java/utils/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,25 @@ public JsonNode toJsonNode(String json){
}
}

/**
* Parses the JSON string object to a JsonNode object.
* <p>
* @param json
* the json object as a Object
*
* @return a {@code JsonObject} object parsed with the json data.
*
* @throws UtilException
* if unable to parse the json string.
*/
public JsonNode toJsonNode(Object json){
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.convertValue(json,JsonNode.class);
} catch (Exception e) {
throw new UtilException(JsonUtil.class, "It was not possible to parse json -> [" + e.getMessage() + "]");
}
}
/**
* Parses the JSON Map object to a JsonNode object.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@

package utils;

import com.fasterxml.jackson.databind.JsonNode;
import org.eclipse.tractusx.productpass.managers.ProcessManager;
import org.eclipse.tractusx.productpass.models.edc.DataPlaneEndpoint;
import org.eclipse.tractusx.productpass.models.passports.Passport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -75,7 +75,7 @@ public PassportUtil(JsonUtil jsonUtil, FileUtil fileUtil,ProcessManager processM
* if unable to save the Passport to a JSON file.
*/
@SuppressWarnings("Unused")
public String savePassport(Passport passport, DataPlaneEndpoint endpointData, Boolean prettyPrint, Boolean encrypted){
public String savePassport(JsonNode passport, DataPlaneEndpoint endpointData, Boolean prettyPrint, Boolean encrypted){
try {
fileUtil.createDir(this.transferDir);
String path = Path.of(this.transferDir, endpointData.getId() + ".json").toAbsolutePath().toString();
Expand Down Expand Up @@ -104,7 +104,7 @@ public String savePassport(Passport passport, DataPlaneEndpoint endpointData, Bo
* @throws UtilException
* if unable to save the Passport to a JSON file.
*/
public String savePassport(Passport passport, DataPlaneEndpoint endpointData, Boolean prettyPrint, Boolean encrypted, String filePath){
public String savePassport(JsonNode passport, DataPlaneEndpoint endpointData, Boolean prettyPrint, Boolean encrypted, String filePath){
try {
if(!encrypted) {
return jsonUtil.toJsonFile(filePath, passport, prettyPrint); // Store the plain JSON
Expand Down

0 comments on commit ac3c4cc

Please sign in to comment.