org.bdware.doip.codec.JsonDoipMessage Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of doip-audit-tool Show documentation
Show all versions of doip-audit-tool Show documentation
doip audit tool developed by bdware
package org.bdware.doip.codec;
import com.google.gson.JsonObject;
import org.apache.commons.codec.binary.Base64;
import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.doip.codec.doipMessage.DoipMessageFactory;
import org.bdware.doip.codec.doipMessage.DoipResponseCode;
import org.bdware.sc.conn.ByteUtil;
import java.nio.charset.StandardCharsets;
public class JsonDoipMessage {
String reqID;
public JsonDoipHeader header;
String body;
public JsonObject sm2KeyPair;
public static JsonDoipMessage fromDoipMessage(DoipMessage doipMessage) {
JsonDoipMessage message = new JsonDoipMessage();
//TODO 需要的按byte数组方式去读取!
if (doipMessage.body != null && doipMessage.body.getEncodedData() != null)
message.body = ByteUtil.encodeBASE64(doipMessage.body.getEncodedData());
if (doipMessage.credential != null) {
message.sm2KeyPair = new JsonObject();
message.sm2KeyPair.addProperty("publicKey", doipMessage.credential.getSigner());
}
if (doipMessage.header != null && doipMessage.header.parameters != null) {
message.header = new JsonDoipHeader();
message.header.identifier = doipMessage.header.parameters.id;
message.header.operation = doipMessage.header.parameters.operation;
message.header.attributes = doipMessage.header.parameters.attributes;
if (doipMessage.header.parameters.response != null)
message.header.response = doipMessage.header.parameters.response.getName();
}
return message;
}
public DoipMessage toResponseDoipMessage(DoipMessage request) {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
builder.createResponse(DoipResponseCode.getDoResponse(header.response), request);
appendHeaderAndBody(builder);
return builder.create();
}
// use
public DoipMessage toRequestDoipMessage() {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
builder.createRequest(header.identifier, header.operation);
appendHeaderAndBody(builder);
return builder.create();
}
private void appendHeaderAndBody(DoipMessageFactory.DoipMessageBuilder builder) {
if (header.attributes != null) {
for (String key : header.attributes.keySet()) {
builder.addAttributes(key, header.attributes.get(key));
}
}
if (body != null) {
if (Base64.isBase64(body)) {
builder.setBody(Base64.decodeBase64(body));
} else
builder.setBody(body.getBytes(StandardCharsets.UTF_8));
}
}
}