All Downloads are FREE. Search and download functionalities are using the official Maven repository.

id.unum.converter.DtoToProto Maven / Gradle / Ivy

The newest version!
package id.unum.converter;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.util.JsonFormat;
import id.unum.protos.credential.v1.CredentialRequest;
import id.unum.protos.crypto.v1.EncryptedData;
import id.unum.protos.crypto.v1.PublicKeyInfo;
import id.unum.protos.didDocument.v1.DidDocument;
import id.unum.protos.presentationRequest.v1.PresentationRequest;

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

public class DtoToProto {
    public static CredentialRequest convertCredentialToProto(id.unum.types.dto.CredentialRequest input) throws JsonProcessingException, InvalidProtocolBufferException {

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(input);

        CredentialRequest.Builder builder = CredentialRequest.newBuilder();
        JsonFormat.parser().ignoringUnknownFields().merge(json,
                builder);

        CredentialRequest result = builder.build();

        return result;
    }

    public static List convertCredentialsToProtos(List input) throws InvalidProtocolBufferException, JsonProcessingException {
        List result = new ArrayList<>();
        for (id.unum.types.dto.CredentialRequest request : input) {
            result.add(convertCredentialToProto(request));
        }

        return result;
    }

    public static PresentationRequest convertPresentationRequestToProto(id.unum.types.dto.PresentationRequest input) throws JsonProcessingException, InvalidProtocolBufferException {
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(input);

        PresentationRequest.Builder builder = PresentationRequest.newBuilder();
        JsonFormat.parser().ignoringUnknownFields().merge(json,
                builder);

        PresentationRequest result = builder.build();

        return result;
    }

    public static EncryptedData convertEncryptedDataToProto(id.unum.types.EncryptedData input) throws JsonProcessingException, InvalidProtocolBufferException {
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(input);

        EncryptedData.Builder builder = EncryptedData.newBuilder();
        JsonFormat.parser().ignoringUnknownFields().merge(json,
                builder);

        EncryptedData result = builder.build();

        return result;

    }

    public static DidDocument convertDidDocumentToProto(id.unum.dto.DidDocument input) throws JsonProcessingException, InvalidProtocolBufferException {
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(input);

        DidDocument.Builder builder = DidDocument.newBuilder();
        JsonFormat.parser().ignoringUnknownFields().merge(json,
                builder);

        DidDocument result = builder.build();

        return result;
    }

    public static List convertPublicKeyInfoListToProto(List input) throws InvalidProtocolBufferException, JsonProcessingException {
        List result = new ArrayList<>();
        for (id.unum.types.PublicKeyInfo key : input) {
            result.add(convertPublicKeyInfoToProto(key));
        }

        return result;
    }

    private static PublicKeyInfo convertPublicKeyInfoToProto(id.unum.types.PublicKeyInfo input) throws JsonProcessingException, InvalidProtocolBufferException {
//        ObjectMapper mapper = new ObjectMapper();
//        String json = mapper.writeValueAsString(input);

        Gson gson = new GsonBuilder().create();
        String json = gson.toJson(input);

        PublicKeyInfo.Builder builder = PublicKeyInfo.newBuilder();
        JsonFormat.parser().ignoringUnknownFields().merge(json,
                builder);

        PublicKeyInfo result = builder.build();

        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy