forked from TrueCloudLab/frostfs-sdk-java
[#1] Add additional security
Signed-off-by: Ori Bruk <o.bruk@yadro.com>
This commit is contained in:
parent
bf2f19f08d
commit
1be65c63ae
62 changed files with 670 additions and 281 deletions
|
@ -18,11 +18,6 @@
|
|||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.14.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>info.frostfs.sdk</groupId>
|
||||
<artifactId>cryptography</artifactId>
|
||||
|
@ -33,6 +28,16 @@
|
|||
<artifactId>protos</artifactId>
|
||||
<version>0.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.14.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>4.4</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -3,19 +3,33 @@ package info.frostfs.sdk;
|
|||
import java.nio.ByteBuffer;
|
||||
import java.util.UUID;
|
||||
|
||||
import static java.util.Objects.isNull;
|
||||
|
||||
public class UUIDExtension {
|
||||
private static final int UUID_BYTE_ARRAY_LENGTH = 16;
|
||||
|
||||
private UUIDExtension() {
|
||||
}
|
||||
|
||||
public static UUID asUuid(byte[] bytes) {
|
||||
if (isNull(bytes) || bytes.length != UUID_BYTE_ARRAY_LENGTH) {
|
||||
throw new IllegalArgumentException("Uuid byte array length must be " + UUID_BYTE_ARRAY_LENGTH);
|
||||
}
|
||||
|
||||
ByteBuffer bb = ByteBuffer.wrap(bytes);
|
||||
long firstLong = bb.getLong();
|
||||
long secondLong = bb.getLong();
|
||||
return new UUID(firstLong, secondLong);
|
||||
}
|
||||
|
||||
public static byte[] asBytes(UUID id) {
|
||||
public static byte[] asBytes(UUID uuid) {
|
||||
if (isNull(uuid)) {
|
||||
throw new IllegalArgumentException("Uuid is not present");
|
||||
}
|
||||
|
||||
ByteBuffer bb = ByteBuffer.allocate(16);
|
||||
bb.putLong(id.getMostSignificantBits());
|
||||
bb.putLong(id.getLeastSignificantBits());
|
||||
bb.putLong(uuid.getMostSignificantBits());
|
||||
bb.putLong(uuid.getLeastSignificantBits());
|
||||
return bb.array();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,11 @@
|
|||
package info.frostfs.sdk.constants;
|
||||
|
||||
public class AppConst {
|
||||
public static final int DEFAULT_MAJOR_VERSION = 2;
|
||||
public static final int DEFAULT_MINOR_VERSION = 13;
|
||||
public static final int OBJECT_CHUNK_SIZE = 3 * (1 << 20);
|
||||
public static final int SHA256_HASH_LENGTH = 32;
|
||||
|
||||
private AppConst() {
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,4 +8,8 @@ public class FieldConst {
|
|||
public static final String ORIGIN_FIELD_NAME = "origin";
|
||||
public static final String ORIGIN_SIGNATURE_FIELD_NAME = "origin_signature";
|
||||
public static final String VERIFY_HEADER_FIELD_NAME = "verify_header";
|
||||
public static final String EMPTY_STRING = "";
|
||||
|
||||
private FieldConst() {
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,4 +4,7 @@ public class XHeaderConst {
|
|||
public static final String RESERVED_XHEADER_PREFIX = "__SYSTEM__";
|
||||
public static final String XHEADER_NETMAP_EPOCH = RESERVED_XHEADER_PREFIX + "NETMAP_EPOCH";
|
||||
public static final String XHEADER_NETMAP_LOOKUP_DEPTH = RESERVED_XHEADER_PREFIX + "NETMAP_LOOKUP_DEPTH";
|
||||
|
||||
private XHeaderConst() {
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,18 +1,28 @@
|
|||
package info.frostfs.sdk.dto;
|
||||
|
||||
import static info.frostfs.sdk.constants.AppConst.DEFAULT_MAJOR_VERSION;
|
||||
import static info.frostfs.sdk.constants.AppConst.DEFAULT_MINOR_VERSION;
|
||||
import static java.util.Objects.isNull;
|
||||
|
||||
public class MetaHeader {
|
||||
private Version version;
|
||||
private int epoch;
|
||||
private int ttl;
|
||||
|
||||
public MetaHeader(Version version, int epoch, int ttl) {
|
||||
if (isNull(version) || epoch < 0 || ttl <= 0) {
|
||||
throw new IllegalArgumentException("One of the input attributes is invalid or missing");
|
||||
}
|
||||
|
||||
this.version = version;
|
||||
this.epoch = epoch;
|
||||
this.ttl = ttl;
|
||||
}
|
||||
|
||||
public static MetaHeader getDefault() {
|
||||
return new MetaHeader(new Version(2, 13), 0, 2);
|
||||
public MetaHeader() {
|
||||
this.version = new Version(DEFAULT_MAJOR_VERSION, DEFAULT_MINOR_VERSION);
|
||||
this.epoch = 0;
|
||||
this.ttl = 2;
|
||||
}
|
||||
|
||||
public Version getVersion() {
|
||||
|
@ -20,6 +30,10 @@ public class MetaHeader {
|
|||
}
|
||||
|
||||
public void setVersion(Version version) {
|
||||
if (isNull(version)) {
|
||||
throw new IllegalArgumentException("Version is missing.");
|
||||
}
|
||||
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
|
@ -28,6 +42,10 @@ public class MetaHeader {
|
|||
}
|
||||
|
||||
public void setEpoch(int epoch) {
|
||||
if (epoch < 0) {
|
||||
throw new IllegalArgumentException("The epoch must be greater than or equal to zero.");
|
||||
}
|
||||
|
||||
this.epoch = epoch;
|
||||
}
|
||||
|
||||
|
@ -36,6 +54,10 @@ public class MetaHeader {
|
|||
}
|
||||
|
||||
public void setTtl(int ttl) {
|
||||
if (ttl <= 0) {
|
||||
throw new IllegalArgumentException("The ttl must be greater than zero.");
|
||||
}
|
||||
|
||||
this.ttl = ttl;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package info.frostfs.sdk.dto;
|
|||
import info.frostfs.sdk.Base58;
|
||||
|
||||
import static info.frostfs.sdk.KeyExtension.publicKeyToAddress;
|
||||
import static java.util.Objects.isNull;
|
||||
|
||||
public class OwnerId {
|
||||
private final String value;
|
||||
|
@ -11,8 +12,12 @@ public class OwnerId {
|
|||
this.value = value;
|
||||
}
|
||||
|
||||
public static OwnerId fromKey(byte[] publicKey) {
|
||||
return new OwnerId(publicKeyToAddress(publicKey));
|
||||
public OwnerId(byte[] publicKey) {
|
||||
if (isNull(publicKey) || publicKey.length == 0) {
|
||||
throw new IllegalArgumentException("PublicKey is invalid");
|
||||
}
|
||||
|
||||
this.value = publicKeyToAddress(publicKey);
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
|
|
|
@ -6,9 +6,11 @@ import info.frostfs.sdk.dto.object.ObjectId;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.Objects.isNull;
|
||||
|
||||
public class Split {
|
||||
private final List<ObjectId> children;
|
||||
private SplitId splitId;
|
||||
private final SplitId splitId;
|
||||
private ObjectId parent;
|
||||
private ObjectId previous;
|
||||
private Signature parentSignature;
|
||||
|
@ -19,6 +21,10 @@ public class Split {
|
|||
}
|
||||
|
||||
public Split(SplitId splitId) {
|
||||
if (isNull(splitId)) {
|
||||
throw new IllegalArgumentException("SplitId is not present");
|
||||
}
|
||||
|
||||
this.splitId = splitId;
|
||||
this.children = new ArrayList<>();
|
||||
}
|
||||
|
@ -27,10 +33,6 @@ public class Split {
|
|||
return splitId;
|
||||
}
|
||||
|
||||
private void setSplitId(SplitId splitId) {
|
||||
this.splitId = splitId;
|
||||
}
|
||||
|
||||
public ObjectId getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
|
|
@ -9,30 +9,22 @@ import static java.util.Objects.isNull;
|
|||
public class SplitId {
|
||||
private final UUID id;
|
||||
|
||||
public SplitId(UUID uuid) {
|
||||
this.id = uuid;
|
||||
}
|
||||
|
||||
public SplitId() {
|
||||
this.id = UUID.randomUUID();
|
||||
}
|
||||
|
||||
private SplitId(byte[] binary) {
|
||||
public SplitId(UUID uuid) {
|
||||
this.id = uuid;
|
||||
}
|
||||
|
||||
public SplitId(byte[] binary) {
|
||||
this.id = asUuid(binary);
|
||||
}
|
||||
|
||||
private SplitId(String str) {
|
||||
public SplitId(String str) {
|
||||
this.id = UUID.fromString(str);
|
||||
}
|
||||
|
||||
public static SplitId createFromBinary(byte[] binaryData) {
|
||||
return new SplitId(binaryData);
|
||||
}
|
||||
|
||||
public static SplitId createFromString(String stringData) {
|
||||
return new SplitId(stringData);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return id.toString();
|
||||
|
|
|
@ -2,6 +2,7 @@ package info.frostfs.sdk.dto;
|
|||
|
||||
import info.frostfs.sdk.enums.StatusCode;
|
||||
|
||||
import static info.frostfs.sdk.constants.FieldConst.EMPTY_STRING;
|
||||
import static java.util.Objects.isNull;
|
||||
|
||||
public class Status {
|
||||
|
@ -10,12 +11,12 @@ public class Status {
|
|||
|
||||
public Status(StatusCode code, String message) {
|
||||
this.code = code;
|
||||
this.message = isNull(message) ? "" : message;
|
||||
this.message = isNull(message) ? EMPTY_STRING : message;
|
||||
}
|
||||
|
||||
public Status(StatusCode code) {
|
||||
this.code = code;
|
||||
this.message = "";
|
||||
this.message = EMPTY_STRING;
|
||||
}
|
||||
|
||||
public StatusCode getCode() {
|
||||
|
|
|
@ -1,36 +1,41 @@
|
|||
package info.frostfs.sdk.dto;
|
||||
|
||||
import static info.frostfs.sdk.constants.AppConst.DEFAULT_MAJOR_VERSION;
|
||||
import static info.frostfs.sdk.constants.AppConst.DEFAULT_MINOR_VERSION;
|
||||
import static java.util.Objects.isNull;
|
||||
|
||||
public class Version {
|
||||
private int major;
|
||||
private int minor;
|
||||
private final int major;
|
||||
private final int minor;
|
||||
|
||||
public Version(int major, int minor) {
|
||||
this.major = major;
|
||||
this.minor = minor;
|
||||
}
|
||||
|
||||
public Version() {
|
||||
this.major = DEFAULT_MAJOR_VERSION;
|
||||
this.minor = DEFAULT_MINOR_VERSION;
|
||||
}
|
||||
|
||||
public int getMajor() {
|
||||
return major;
|
||||
}
|
||||
|
||||
public void setMajor(int major) {
|
||||
this.major = major;
|
||||
}
|
||||
|
||||
public int getMinor() {
|
||||
return minor;
|
||||
}
|
||||
|
||||
public void setMinor(int minor) {
|
||||
this.minor = minor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "v" + major + "." + minor;
|
||||
}
|
||||
|
||||
public boolean isSupported(Version version) {
|
||||
if (isNull(version)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return major == version.getMajor();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,29 +2,33 @@ package info.frostfs.sdk.dto.container;
|
|||
|
||||
import info.frostfs.sdk.Base58;
|
||||
import info.frostfs.sdk.constants.AppConst;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import static java.util.Objects.isNull;
|
||||
|
||||
public class ContainerId {
|
||||
private String value;
|
||||
private final String value;
|
||||
|
||||
public ContainerId(String value) {
|
||||
if (StringUtils.isEmpty(value)) {
|
||||
throw new IllegalArgumentException("ContainerId value is missing");
|
||||
}
|
||||
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static ContainerId fromHash(byte[] hash) {
|
||||
if (hash.length != AppConst.SHA256_HASH_LENGTH) {
|
||||
throw new IllegalArgumentException("ContainerID must be a sha256 hash.");
|
||||
public ContainerId(byte[] hash) {
|
||||
if (isNull(hash) || hash.length != AppConst.SHA256_HASH_LENGTH) {
|
||||
throw new IllegalArgumentException("ContainerId must be a sha256 hash.");
|
||||
}
|
||||
return new ContainerId(Base58.encode(hash));
|
||||
|
||||
this.value = Base58.encode(hash);
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return value;
|
||||
|
|
|
@ -1,34 +1,36 @@
|
|||
package info.frostfs.sdk.dto.netmap;
|
||||
|
||||
import static java.util.Objects.isNull;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import static info.frostfs.sdk.constants.FieldConst.EMPTY_STRING;
|
||||
|
||||
public class Replica {
|
||||
private int count;
|
||||
private String selector;
|
||||
private final int count;
|
||||
private final String selector;
|
||||
|
||||
public Replica(int count, String selector) {
|
||||
if (count <= 0) {
|
||||
throw new IllegalArgumentException("Replica count must be positive");
|
||||
}
|
||||
|
||||
this.count = count;
|
||||
this.selector = isNull(selector) ? "" : selector;
|
||||
this.selector = StringUtils.isEmpty(selector) ? EMPTY_STRING : selector;
|
||||
}
|
||||
|
||||
public Replica(int count) {
|
||||
if (count <= 0) {
|
||||
throw new IllegalArgumentException("Replica count must be positive");
|
||||
}
|
||||
|
||||
this.count = count;
|
||||
this.selector = "";
|
||||
this.selector = EMPTY_STRING;
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public void setCount(int count) {
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
public String getSelector() {
|
||||
return selector;
|
||||
}
|
||||
|
||||
public void setSelector(String selector) {
|
||||
this.selector = selector;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import info.frostfs.sdk.dto.container.ContainerId;
|
|||
import java.security.MessageDigest;
|
||||
|
||||
import static info.frostfs.sdk.Helper.getSha256Instance;
|
||||
import static java.util.Objects.isNull;
|
||||
|
||||
public class LargeObject extends ObjectFrostFS {
|
||||
private final MessageDigest payloadHash;
|
||||
|
@ -15,6 +16,10 @@ public class LargeObject extends ObjectFrostFS {
|
|||
}
|
||||
|
||||
public void appendBlock(byte[] bytes, int count) {
|
||||
if (count == 0 || isNull(bytes) || bytes.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.getHeader().increasePayloadLength(count);
|
||||
this.payloadHash.update(bytes, 0, count);
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package info.frostfs.sdk.dto.object;
|
|||
import info.frostfs.sdk.dto.Split;
|
||||
import info.frostfs.sdk.dto.SplitId;
|
||||
import info.frostfs.sdk.dto.container.ContainerId;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -16,6 +17,10 @@ public class LinkObject extends ObjectFrostFS {
|
|||
}
|
||||
|
||||
public void addChildren(List<ObjectId> objectIds) {
|
||||
if (CollectionUtils.isEmpty(objectIds)) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.getHeader().getSplit().getChildren().addAll(objectIds);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,16 @@
|
|||
package info.frostfs.sdk.dto.object;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class ObjectAttribute {
|
||||
private String key;
|
||||
private String value;
|
||||
private final String key;
|
||||
private final String value;
|
||||
|
||||
public ObjectAttribute(String key, String value) {
|
||||
if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) {
|
||||
throw new IllegalArgumentException("One of the input attributes is missing");
|
||||
}
|
||||
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
@ -13,15 +19,7 @@ public class ObjectAttribute {
|
|||
return key;
|
||||
}
|
||||
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,34 +10,34 @@ import java.util.List;
|
|||
import static java.util.Objects.isNull;
|
||||
|
||||
public class ObjectFrostFS {
|
||||
private ObjectHeader header;
|
||||
private final ObjectHeader header;
|
||||
private ObjectId objectId;
|
||||
private byte[] payload;
|
||||
|
||||
public ObjectFrostFS(ObjectHeader header, ObjectId objectId, byte[] payload) {
|
||||
if (isNull(header)) {
|
||||
throw new IllegalArgumentException("Object header is missing");
|
||||
}
|
||||
|
||||
this.header = header;
|
||||
this.objectId = objectId;
|
||||
this.payload = payload;
|
||||
}
|
||||
|
||||
public ObjectFrostFS(ContainerId container, byte[] payload) {
|
||||
public ObjectFrostFS(ContainerId containerId, byte[] payload) {
|
||||
this.payload = payload;
|
||||
this.header = new ObjectHeader(container, new ArrayList<>());
|
||||
this.header = new ObjectHeader(containerId, new ArrayList<>());
|
||||
}
|
||||
|
||||
public ObjectFrostFS(ContainerId container, byte[] payload, ObjectType objectType) {
|
||||
public ObjectFrostFS(ContainerId containerId, byte[] payload, ObjectType objectType) {
|
||||
this.payload = payload;
|
||||
this.header = new ObjectHeader(container, objectType, new ArrayList<>());
|
||||
this.header = new ObjectHeader(containerId, objectType, new ArrayList<>());
|
||||
}
|
||||
|
||||
public ObjectHeader getHeader() {
|
||||
return header;
|
||||
}
|
||||
|
||||
public void setHeader(ObjectHeader header) {
|
||||
this.header = header;
|
||||
}
|
||||
|
||||
public ObjectId getObjectId() {
|
||||
return objectId;
|
||||
}
|
||||
|
|
|
@ -8,11 +8,13 @@ import info.frostfs.sdk.enums.ObjectType;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.Objects.isNull;
|
||||
|
||||
public class ObjectHeader {
|
||||
private final ContainerId containerId;
|
||||
private final ObjectType objectType;
|
||||
private List<ObjectAttribute> attributes;
|
||||
private ContainerId containerId;
|
||||
private long size;
|
||||
private ObjectType objectType;
|
||||
private Version version;
|
||||
private OwnerId ownerId;
|
||||
private long payloadLength;
|
||||
|
@ -21,6 +23,10 @@ public class ObjectHeader {
|
|||
|
||||
public ObjectHeader(ContainerId containerId, ObjectType objectType,
|
||||
List<ObjectAttribute> attributes, long size, Version version) {
|
||||
if (isNull(containerId) || isNull(objectType)) {
|
||||
throw new IllegalArgumentException("ContainerId or objectType is not present");
|
||||
}
|
||||
|
||||
this.attributes = attributes;
|
||||
this.containerId = containerId;
|
||||
this.size = size;
|
||||
|
@ -28,13 +34,21 @@ public class ObjectHeader {
|
|||
this.version = version;
|
||||
}
|
||||
|
||||
public ObjectHeader(ContainerId containerId, ObjectType type, List<ObjectAttribute> attributes) {
|
||||
public ObjectHeader(ContainerId containerId, ObjectType objectType, List<ObjectAttribute> attributes) {
|
||||
if (isNull(containerId) || isNull(objectType)) {
|
||||
throw new IllegalArgumentException("ContainerId or objectType is not present");
|
||||
}
|
||||
|
||||
this.attributes = attributes;
|
||||
this.containerId = containerId;
|
||||
this.objectType = type;
|
||||
this.objectType = objectType;
|
||||
}
|
||||
|
||||
public ObjectHeader(ContainerId containerId, List<ObjectAttribute> attributes) {
|
||||
if (isNull(containerId)) {
|
||||
throw new IllegalArgumentException("ContainerId is not present");
|
||||
}
|
||||
|
||||
this.attributes = attributes;
|
||||
this.containerId = containerId;
|
||||
this.objectType = ObjectType.REGULAR;
|
||||
|
@ -73,6 +87,10 @@ public class ObjectHeader {
|
|||
}
|
||||
|
||||
public void setSplit(Split split) {
|
||||
if (isNull(split)) {
|
||||
throw new IllegalArgumentException("Split is not present");
|
||||
}
|
||||
|
||||
this.split = split;
|
||||
}
|
||||
|
||||
|
@ -88,10 +106,6 @@ public class ObjectHeader {
|
|||
return containerId;
|
||||
}
|
||||
|
||||
public void setContainerId(ContainerId containerId) {
|
||||
this.containerId = containerId;
|
||||
}
|
||||
|
||||
public long getSize() {
|
||||
return size;
|
||||
}
|
||||
|
@ -104,10 +118,6 @@ public class ObjectHeader {
|
|||
return objectType;
|
||||
}
|
||||
|
||||
public void setObjectType(ObjectType objectType) {
|
||||
this.objectType = objectType;
|
||||
}
|
||||
|
||||
public Version getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
|
|
@ -2,19 +2,27 @@ package info.frostfs.sdk.dto.object;
|
|||
|
||||
import info.frostfs.sdk.Base58;
|
||||
import info.frostfs.sdk.constants.AppConst;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import static java.util.Objects.isNull;
|
||||
|
||||
public class ObjectId {
|
||||
private final String value;
|
||||
|
||||
public ObjectId(String id) {
|
||||
this.value = id;
|
||||
public ObjectId(String value) {
|
||||
if (StringUtils.isEmpty(value)) {
|
||||
throw new IllegalArgumentException("ObjectId value is missing");
|
||||
}
|
||||
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static ObjectId fromHash(byte[] hash) {
|
||||
if (hash.length != AppConst.SHA256_HASH_LENGTH) {
|
||||
public ObjectId(byte[] hash) {
|
||||
if (isNull(hash) || hash.length != AppConst.SHA256_HASH_LENGTH) {
|
||||
throw new IllegalArgumentException("ObjectId must be a sha256 hash.");
|
||||
}
|
||||
return new ObjectId(Base58.encode(hash));
|
||||
|
||||
this.value = Base58.encode(hash);
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
|
|
|
@ -3,9 +3,18 @@ package info.frostfs.sdk.mappers;
|
|||
import frostfs.session.Types;
|
||||
import info.frostfs.sdk.dto.MetaHeader;
|
||||
|
||||
import static java.util.Objects.isNull;
|
||||
|
||||
public class MetaHeaderMapper {
|
||||
|
||||
private MetaHeaderMapper() {
|
||||
}
|
||||
|
||||
public static Types.RequestMetaHeader toGrpcMessage(MetaHeader metaHeader) {
|
||||
if (isNull(metaHeader)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Types.RequestMetaHeader.newBuilder()
|
||||
.setVersion(VersionMapper.toGrpcMessage(metaHeader.getVersion()))
|
||||
.setEpoch(metaHeader.getEpoch())
|
||||
|
|
|
@ -4,9 +4,18 @@ import com.google.protobuf.ByteString;
|
|||
import frostfs.refs.Types;
|
||||
import info.frostfs.sdk.dto.OwnerId;
|
||||
|
||||
import static java.util.Objects.isNull;
|
||||
|
||||
public class OwnerIdMapper {
|
||||
|
||||
private OwnerIdMapper() {
|
||||
}
|
||||
|
||||
public static Types.OwnerID toGrpcMessage(OwnerId ownerId) {
|
||||
if (isNull(ownerId)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Types.OwnerID.newBuilder()
|
||||
.setValue(ByteString.copyFrom(ownerId.toHash()))
|
||||
.build();
|
||||
|
|
|
@ -6,9 +6,18 @@ import frostfs.session.Types;
|
|||
|
||||
import java.io.IOException;
|
||||
|
||||
import static java.util.Objects.isNull;
|
||||
|
||||
public class SessionMapper {
|
||||
|
||||
private SessionMapper() {
|
||||
}
|
||||
|
||||
public static byte[] serialize(Types.SessionToken token) {
|
||||
if (isNull(token)) {
|
||||
throw new IllegalArgumentException("Token is not present");
|
||||
}
|
||||
|
||||
try {
|
||||
byte[] bytes = new byte[token.getSerializedSize()];
|
||||
CodedOutputStream stream = CodedOutputStream.newInstance(bytes);
|
||||
|
@ -20,6 +29,10 @@ public class SessionMapper {
|
|||
}
|
||||
|
||||
public static Types.SessionToken deserializeSessionToken(byte[] bytes) {
|
||||
if (isNull(bytes) || bytes.length == 0) {
|
||||
throw new IllegalArgumentException("Token is not present");
|
||||
}
|
||||
|
||||
try {
|
||||
return Types.SessionToken.newBuilder().mergeFrom(bytes).build();
|
||||
} catch (InvalidProtocolBufferException exp) {
|
||||
|
|
|
@ -8,7 +8,14 @@ import static java.util.Objects.isNull;
|
|||
|
||||
public class SignatureMapper {
|
||||
|
||||
public static Types.Signature ToGrpcMessage(Signature signature) {
|
||||
private SignatureMapper() {
|
||||
}
|
||||
|
||||
public static Types.Signature toGrpcMessage(Signature signature) {
|
||||
if (isNull(signature)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var scheme = Types.SignatureScheme.forNumber(signature.getScheme().value);
|
||||
if (isNull(scheme)) {
|
||||
throw new IllegalArgumentException(
|
||||
|
|
|
@ -8,8 +8,13 @@ import static java.util.Objects.isNull;
|
|||
|
||||
public class StatusMapper {
|
||||
|
||||
private StatusMapper() {
|
||||
}
|
||||
|
||||
public static Status toModel(Types.Status status) {
|
||||
if (isNull(status)) return new Status(StatusCode.SUCCESS);
|
||||
if (isNull(status)) {
|
||||
return new Status(StatusCode.SUCCESS);
|
||||
}
|
||||
|
||||
var statusCode = StatusCode.get(status.getCode());
|
||||
if (isNull(statusCode)) {
|
||||
|
|
|
@ -3,9 +3,18 @@ package info.frostfs.sdk.mappers;
|
|||
import frostfs.refs.Types;
|
||||
import info.frostfs.sdk.dto.Version;
|
||||
|
||||
import static java.util.Objects.isNull;
|
||||
|
||||
public class VersionMapper {
|
||||
|
||||
private VersionMapper() {
|
||||
}
|
||||
|
||||
public static Types.Version toGrpcMessage(Version version) {
|
||||
if (isNull(version)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Types.Version.newBuilder()
|
||||
.setMajor(version.getMajor())
|
||||
.setMinor(version.getMinor())
|
||||
|
@ -13,6 +22,10 @@ public class VersionMapper {
|
|||
}
|
||||
|
||||
public static Version toModel(Types.Version version) {
|
||||
if (isNull(version)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new Version(version.getMajor(), version.getMinor());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,9 +4,18 @@ import com.google.protobuf.ByteString;
|
|||
import frostfs.refs.Types;
|
||||
import info.frostfs.sdk.dto.container.ContainerId;
|
||||
|
||||
import static java.util.Objects.isNull;
|
||||
|
||||
public class ContainerIdMapper {
|
||||
|
||||
private ContainerIdMapper() {
|
||||
}
|
||||
|
||||
public static Types.ContainerID toGrpcMessage(ContainerId containerId) {
|
||||
if (isNull(containerId)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Types.ContainerID.newBuilder()
|
||||
.setValue(ByteString.copyFrom(containerId.toHash()))
|
||||
.build();
|
||||
|
|
|
@ -13,7 +13,14 @@ import static java.util.Objects.isNull;
|
|||
|
||||
public class ContainerMapper {
|
||||
|
||||
private ContainerMapper() {
|
||||
}
|
||||
|
||||
public static Types.Container toGrpcMessage(Container container) {
|
||||
if (isNull(container)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Types.Container.newBuilder()
|
||||
.setBasicAcl(container.getBasicAcl().value)
|
||||
.setPlacementPolicy(PlacementPolicyMapper.toGrpcMessage(container.getPlacementPolicy()))
|
||||
|
@ -22,6 +29,10 @@ public class ContainerMapper {
|
|||
}
|
||||
|
||||
public static Container toModel(Types.Container containerGrpc) {
|
||||
if (isNull(containerGrpc)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var basicAcl = BasicAcl.get(containerGrpc.getBasicAcl());
|
||||
if (isNull(basicAcl)) {
|
||||
throw new IllegalArgumentException(
|
||||
|
|
|
@ -5,9 +5,18 @@ import info.frostfs.sdk.dto.netmap.NetmapSnapshot;
|
|||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static java.util.Objects.isNull;
|
||||
|
||||
public class NetmapSnapshotMapper {
|
||||
|
||||
private NetmapSnapshotMapper() {
|
||||
}
|
||||
|
||||
public static NetmapSnapshot toModel(Service.NetmapSnapshotResponse netmap) {
|
||||
if (isNull(netmap)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new NetmapSnapshot(
|
||||
netmap.getBody().getNetmap().getEpoch(),
|
||||
netmap.getBody().getNetmap().getNodesList().stream()
|
||||
|
|
|
@ -13,11 +13,22 @@ import static java.util.Objects.isNull;
|
|||
|
||||
public class NodeInfoMapper {
|
||||
|
||||
private NodeInfoMapper() {
|
||||
}
|
||||
|
||||
public static NodeInfo toModel(Service.LocalNodeInfoResponse.Body nodeInfo) {
|
||||
if (isNull(nodeInfo)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return toModel(nodeInfo.getNodeInfo(), nodeInfo.getVersion());
|
||||
}
|
||||
|
||||
public static NodeInfo toModel(frostfs.netmap.Types.NodeInfo nodeInfo, Types.Version version) {
|
||||
if (isNull(nodeInfo)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
NodeState nodeState = NodeState.get(nodeInfo.getState().getNumber());
|
||||
if (isNull(nodeState)) {
|
||||
throw new IllegalArgumentException(
|
||||
|
|
|
@ -4,9 +4,18 @@ import frostfs.netmap.Types;
|
|||
import info.frostfs.sdk.dto.netmap.PlacementPolicy;
|
||||
import info.frostfs.sdk.dto.netmap.Replica;
|
||||
|
||||
import static java.util.Objects.isNull;
|
||||
|
||||
public class PlacementPolicyMapper {
|
||||
|
||||
private PlacementPolicyMapper() {
|
||||
}
|
||||
|
||||
public static Types.PlacementPolicy toGrpcMessage(PlacementPolicy placementPolicy) {
|
||||
if (isNull(placementPolicy)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var pp = Types.PlacementPolicy.newBuilder()
|
||||
.setUnique(placementPolicy.isUnique());
|
||||
|
||||
|
@ -18,6 +27,10 @@ public class PlacementPolicyMapper {
|
|||
}
|
||||
|
||||
public static PlacementPolicy toModel(Types.PlacementPolicy placementPolicy) {
|
||||
if (isNull(placementPolicy)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new PlacementPolicy(
|
||||
placementPolicy.getUnique(),
|
||||
placementPolicy.getReplicasList().stream().map(ReplicaMapper::toModel).toArray(Replica[]::new)
|
||||
|
|
|
@ -3,9 +3,18 @@ package info.frostfs.sdk.mappers.netmap;
|
|||
import frostfs.netmap.Types;
|
||||
import info.frostfs.sdk.dto.netmap.Replica;
|
||||
|
||||
import static java.util.Objects.isNull;
|
||||
|
||||
public class ReplicaMapper {
|
||||
|
||||
private ReplicaMapper() {
|
||||
}
|
||||
|
||||
public static Types.Replica toGrpcMessage(Replica replica) {
|
||||
if (isNull(replica)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Types.Replica.newBuilder()
|
||||
.setCount(replica.getCount())
|
||||
.setSelector(replica.getSelector())
|
||||
|
@ -13,6 +22,10 @@ public class ReplicaMapper {
|
|||
}
|
||||
|
||||
public static Replica toModel(Types.Replica replica) {
|
||||
if (isNull(replica)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new Replica(replica.getCount(), replica.getSelector());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,9 +3,18 @@ package info.frostfs.sdk.mappers.object;
|
|||
import frostfs.object.Types;
|
||||
import info.frostfs.sdk.dto.object.ObjectAttribute;
|
||||
|
||||
import static java.util.Objects.isNull;
|
||||
|
||||
public class ObjectAttributeMapper {
|
||||
|
||||
private ObjectAttributeMapper() {
|
||||
}
|
||||
|
||||
public static Types.Header.Attribute toGrpcMessage(ObjectAttribute attribute) {
|
||||
if (isNull(attribute)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Types.Header.Attribute.newBuilder()
|
||||
.setKey(attribute.getKey())
|
||||
.setValue(attribute.getValue())
|
||||
|
@ -13,6 +22,10 @@ public class ObjectAttributeMapper {
|
|||
}
|
||||
|
||||
public static ObjectAttribute toModel(Types.Header.Attribute attribute) {
|
||||
if (isNull(attribute)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new ObjectAttribute(attribute.getKey(), attribute.getValue());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,14 @@ import static java.util.Objects.isNull;
|
|||
|
||||
public class ObjectFilterMapper {
|
||||
|
||||
private ObjectFilterMapper() {
|
||||
}
|
||||
|
||||
public static Service.SearchRequest.Body.Filter toGrpcMessage(ObjectFilter filter) {
|
||||
if (isNull(filter)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var objectMatchType = Types.MatchType.forNumber(filter.getMatchType().value);
|
||||
if (isNull(objectMatchType)) {
|
||||
throw new IllegalArgumentException(
|
||||
|
|
|
@ -4,13 +4,22 @@ import frostfs.object.Types;
|
|||
import info.frostfs.sdk.dto.object.ObjectFrostFS;
|
||||
import info.frostfs.sdk.dto.object.ObjectId;
|
||||
|
||||
import static java.util.Objects.isNull;
|
||||
|
||||
public class ObjectFrostFSMapper {
|
||||
|
||||
public static ObjectFrostFS toModel(Types.Object obj) {
|
||||
private ObjectFrostFSMapper() {
|
||||
}
|
||||
|
||||
public static ObjectFrostFS toModel(Types.Object object) {
|
||||
if (isNull(object)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new ObjectFrostFS(
|
||||
ObjectHeaderMapper.toModel(obj.getHeader()),
|
||||
ObjectId.fromHash(obj.getObjectId().getValue().toByteArray()),
|
||||
obj.getPayload().toByteArray()
|
||||
ObjectHeaderMapper.toModel(object.getHeader()),
|
||||
new ObjectId(object.getObjectId().getValue().toByteArray()),
|
||||
object.getPayload().toByteArray()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,7 +14,14 @@ import static java.util.Objects.isNull;
|
|||
|
||||
public class ObjectHeaderMapper {
|
||||
|
||||
private ObjectHeaderMapper() {
|
||||
}
|
||||
|
||||
public static Types.Header toGrpcMessage(ObjectHeader header) {
|
||||
if (isNull(header)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var objectType = Types.ObjectType.forNumber(header.getObjectType().value);
|
||||
if (isNull(objectType)) {
|
||||
throw new IllegalArgumentException(
|
||||
|
@ -34,6 +41,10 @@ public class ObjectHeaderMapper {
|
|||
}
|
||||
|
||||
public static ObjectHeader toModel(Types.Header header) {
|
||||
if (isNull(header)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var objectType = ObjectType.get(header.getObjectTypeValue());
|
||||
if (isNull(objectType)) {
|
||||
throw new IllegalArgumentException(
|
||||
|
@ -42,7 +53,7 @@ public class ObjectHeaderMapper {
|
|||
}
|
||||
|
||||
return new ObjectHeader(
|
||||
ContainerId.fromHash(header.getContainerId().getValue().toByteArray()),
|
||||
new ContainerId(header.getContainerId().getValue().toByteArray()),
|
||||
objectType,
|
||||
header.getAttributesList().stream().map(ObjectAttributeMapper::toModel).collect(Collectors.toList()),
|
||||
header.getPayloadLength(),
|
||||
|
|
|
@ -4,15 +4,24 @@ import com.google.protobuf.ByteString;
|
|||
import frostfs.refs.Types;
|
||||
import info.frostfs.sdk.dto.object.ObjectId;
|
||||
|
||||
import static java.util.Objects.isNull;
|
||||
|
||||
public class ObjectIdMapper {
|
||||
|
||||
private ObjectIdMapper() {
|
||||
}
|
||||
|
||||
public static Types.ObjectID toGrpcMessage(ObjectId objectId) {
|
||||
if (isNull(objectId)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Types.ObjectID.newBuilder()
|
||||
.setValue(ByteString.copyFrom(objectId.toHash()))
|
||||
.build();
|
||||
}
|
||||
|
||||
public static ObjectId toModel(Types.ObjectID objectId) {
|
||||
return ObjectId.fromHash(objectId.getValue().toByteArray());
|
||||
return new ObjectId(objectId.getValue().toByteArray());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue