diff --git a/README.md b/README.md index 2883006..94031a8 100644 --- a/README.md +++ b/README.md @@ -22,9 +22,9 @@ neo-go wallet export -w -d ```java import info.FrostFS.sdk.enums.BasicAcl; -import info.FrostFS.sdk.jdo.Container; -import info.FrostFS.sdk.jdo.netmap.PlacementPolicy; -import info.FrostFS.sdk.jdo.netmap.Replica; +import info.FrostFS.sdk.dto.container.Container; +import info.FrostFS.sdk.dto.netmap.PlacementPolicy; +import info.FrostFS.sdk.dto.netmap.Replica; import info.FrostFS.sdk.services.FrostFSClient; public class ContainerExample { @@ -36,16 +36,16 @@ public class ContainerExample { // Create container var placementPolicy = new PlacementPolicy(true, new Replica[]{new Replica(1)}); - var containerId = frostFSClient.createContainerAsync(new Container(BasicAcl.PUBLIC_RW, placementPolicy)); + var containerId = frostFSClient.createContainer(new Container(BasicAcl.PUBLIC_RW, placementPolicy)); // Get container - var container = frostFSClient.getContainerAsync(containerId); + var container = frostFSClient.getContainer(containerId); // List containers - var containerIds = frostFSClient.listContainersAsync(); + var containerIds = frostFSClient.listContainers(); // Delete container - frostFSClient.deleteContainerAsync(containerId); + frostFSClient.deleteContainer(containerId); } } ``` @@ -54,10 +54,10 @@ public class ContainerExample { ```java import info.FrostFS.sdk.enums.ObjectType; -import info.FrostFS.sdk.jdo.ContainerId; -import info.FrostFS.sdk.jdo.ObjectAttribute; -import info.FrostFS.sdk.jdo.ObjectFilter; -import info.FrostFS.sdk.jdo.ObjectHeader; +import info.FrostFS.sdk.dto.container.ContainerId; +import info.FrostFS.sdk.dto.object.ObjectAttribute; +import info.FrostFS.sdk.dto.object.ObjectFilter; +import info.FrostFS.sdk.dto.object.ObjectHeader; import info.FrostFS.sdk.services.FrostFSClient; import java.io.FileInputStream; @@ -71,24 +71,24 @@ public class ObjectExample { FrostFSClient frostFSClient = new FrostFSClient(grpcClient, client); // Put object - info.FrostFS.sdk.jdo.ObjectId objectId; + info.FrostFS.sdk.dto.object.ObjectId objectId; try (FileInputStream fis = new FileInputStream("cat.jpg")) { var cat = new ObjectHeader( containerId, ObjectType.REGULAR, new ObjectAttribute[]{new ObjectAttribute("Filename", "cat.jpg")} ); - objectId = frostFSClient.putObjectAsync(cat, fis); + objectId = frostFSClient.putObject(cat, fis); } catch (IOException e) { throw new RuntimeException(e); } // Get object - var obj = frostFSClient.getObjectAsync(containerId, objectId); + var obj = frostFSClient.getObject(containerId, objectId); // Get object header - var objectHeader = frostFSClient.getObjectHeadAsync(containerId, objectId); + var objectHeader = frostFSClient.getObjectHead(containerId, objectId); // Search regular objects - var objectIds = frostFSClient.searchObjectsAsync(containerId, ObjectFilter.RootFilter()); + var objectIds = frostFSClient.searchObjects(containerId, ObjectFilter.RootFilter()); } } ``` \ No newline at end of file diff --git a/client/pom.xml b/client/pom.xml index b416ee4..3d3426e 100644 --- a/client/pom.xml +++ b/client/pom.xml @@ -25,9 +25,19 @@ info.FrostFS.sdk - modelsV2 + models 0.1.0 + + org.apache.commons + commons-collections4 + 4.4 + + + commons-codec + commons-codec + 1.17.0 + \ No newline at end of file diff --git a/client/src/main/java/info/FrostFS/sdk/Main.java b/client/src/main/java/info/FrostFS/sdk/Main.java index d16f06e..4c854fe 100644 --- a/client/src/main/java/info/FrostFS/sdk/Main.java +++ b/client/src/main/java/info/FrostFS/sdk/Main.java @@ -1,14 +1,22 @@ package info.FrostFS.sdk; +import info.FrostFS.sdk.dto.container.Container; +import info.FrostFS.sdk.dto.container.ContainerId; import info.FrostFS.sdk.enums.BasicAcl; import info.FrostFS.sdk.enums.ObjectType; -import info.FrostFS.sdk.jdo.*; -import info.FrostFS.sdk.jdo.netmap.PlacementPolicy; -import info.FrostFS.sdk.jdo.netmap.Replica; +import info.FrostFS.sdk.dto.netmap.PlacementPolicy; +import info.FrostFS.sdk.dto.netmap.Replica; +import info.FrostFS.sdk.dto.object.ObjectAttribute; +import info.FrostFS.sdk.dto.object.ObjectFrostFS; +import info.FrostFS.sdk.dto.object.ObjectHeader; +import info.FrostFS.sdk.dto.object.ObjectId; import info.FrostFS.sdk.services.FrostFSClient; +import info.FrostFS.sdk.tools.ClientSettings; +import info.FrostFS.sdk.tools.PutObjectParameters; import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.util.Collections; import java.util.List; public class Main { @@ -18,37 +26,42 @@ public class Main { /* var res2 = frostFSClient.searchObjectsAsync(new ContainerId("EQGx2QeYHJb53uRwYGzcQaW191sZpdNrjutk6veUSV2R")); */ - - ObjectFrostFs res3 = frostFSClient - .getObjectAsync(new ContainerId("EQGx2QeYHJb53uRwYGzcQaW191sZpdNrjutk6veUSV2R"), new ObjectId("85orCLKSu3X1jGiTFmwmTUsBU88RBARNwuRwrEy5pyww")); - - - var ttttt = frostFSClient.getNetworkSettingsAsync(); - Container container2 = frostFSClient.getContainerAsync(new ContainerId("EQGx2QeYHJb53uRwYGzcQaW191sZpdNrjutk6veUSV2R")); - - var placementPolicy = new PlacementPolicy(true, new Replica[]{new Replica(1)}); - var containerId = frostFSClient.createContainerAsync(new Container(BasicAcl.PUBLIC_RW, placementPolicy)); + var containerId = frostFSClient.createContainer(new Container(BasicAcl.PUBLIC_RW, placementPolicy)); Thread.sleep(1000); FileInputStream file = null; try { file = new FileInputStream("/home/ori/Desktop/cat.jpg"); - var cat = new ObjectHeader(containerId, ObjectType.REGULAR, new ObjectAttribute[]{new ObjectAttribute("Filename", "cat.jpg")}); - frostFSClient.putObjectAsync(cat, file); + var cat = new ObjectHeader(containerId, ObjectType.REGULAR, Collections.singletonList(new ObjectAttribute("Filename", "cat3.jpg"))); + + var params = new PutObjectParameters(cat, file, false, 1024); + var tty = frostFSClient.putObject(params); + + System.out.println(1); } catch (FileNotFoundException e) { throw new RuntimeException(e); } - var res = frostFSClient.searchObjectsAsync(containerId); + + var res = frostFSClient.searchObjects(containerId); + + ObjectFrostFS res3 = frostFSClient + .getObject(new ContainerId("EQGx2QeYHJb53uRwYGzcQaW191sZpdNrjutk6veUSV2R"), new ObjectId("85orCLKSu3X1jGiTFmwmTUsBU88RBARNwuRwrEy5pyww")); - Container container = frostFSClient.getContainerAsync(containerId); - List containerIds = frostFSClient.listContainersAsync(); + var ttttt = frostFSClient.getNetworkSettings(); + Container container2 = frostFSClient.getContainer(new ContainerId("EQGx2QeYHJb53uRwYGzcQaW191sZpdNrjutk6veUSV2R")); + + + + + Container container = frostFSClient.getContainer(containerId); + List containerIds = frostFSClient.listContainers(); /* frostFSClient.deleteContainerAsync(containerId); */ - containerIds = frostFSClient.listContainersAsync(); + containerIds = frostFSClient.listContainers(); System.out.println(); } } \ No newline at end of file diff --git a/client/src/main/java/info/FrostFS/sdk/RequestConstructor.java b/client/src/main/java/info/FrostFS/sdk/RequestConstructor.java index 304a878..28daecc 100644 --- a/client/src/main/java/info/FrostFS/sdk/RequestConstructor.java +++ b/client/src/main/java/info/FrostFS/sdk/RequestConstructor.java @@ -2,8 +2,9 @@ package info.FrostFS.sdk; import com.google.protobuf.AbstractMessage; import frostFS.session.Types; -import info.FrostFS.sdk.jdo.MetaHeader; +import info.FrostFS.sdk.dto.MetaHeader; import info.FrostFS.sdk.mappers.MetaHeaderMapper; +import info.FrostFS.sdk.tools.ECDsa; import static info.FrostFS.sdk.RequestSigner.signMessagePart; import static java.util.Objects.isNull; @@ -27,7 +28,7 @@ public class RequestConstructor { frostFS.refs.Types.ContainerID cid, frostFS.refs.Types.ObjectID oid, Types.ObjectSessionContext.Verb verb, - byte[] publicKey, byte[] privateKey) { + ECDsa key) { var headerField = request.getDescriptorForType().findFieldByName("meta_header"); var header = (Types.RequestMetaHeader) request.getField(headerField); if (header.getSessionToken().getSerializedSize() > 0) { @@ -41,7 +42,7 @@ public class RequestConstructor { var body = sessionToken.getBody().toBuilder().setObject(ctx).build(); sessionToken = sessionToken.toBuilder() - .setSignature(signMessagePart(publicKey, privateKey, body)) + .setSignature(signMessagePart(key, body)) .setBody(body) .build(); diff --git a/client/src/main/java/info/FrostFS/sdk/RequestSigner.java b/client/src/main/java/info/FrostFS/sdk/RequestSigner.java index 962a421..a26dadf 100644 --- a/client/src/main/java/info/FrostFS/sdk/RequestSigner.java +++ b/client/src/main/java/info/FrostFS/sdk/RequestSigner.java @@ -3,6 +3,7 @@ package info.FrostFS.sdk; import com.google.protobuf.AbstractMessage; import com.google.protobuf.ByteString; import frostFS.session.Types; +import info.FrostFS.sdk.tools.ECDsa; import org.apache.commons.codec.digest.DigestUtils; import org.bouncycastle.asn1.sec.SECNamedCurves; import org.bouncycastle.asn1.sec.SECObjectIdentifiers; @@ -14,19 +15,18 @@ import org.bouncycastle.crypto.signers.HMacDSAKCalculator; import java.math.BigInteger; import java.security.Signature; -import static info.FrostFS.sdk.KeyExtension.loadPrivateKey; import static org.bouncycastle.crypto.util.DigestFactory.createSHA256; import static org.bouncycastle.util.BigIntegers.asUnsignedByteArray; public class RequestSigner { public static final int RFC6979_SIGNATURE_SIZE = 64; - public static byte[] signData(byte[] privateKey, byte[] data) { + public static byte[] signData(ECDsa key, byte[] data) { var hash = new byte[65]; hash[0] = 0x04; try { Signature signature = Signature.getInstance("NONEwithECDSAinP1363Format"); - signature.initSign(loadPrivateKey(privateKey)); + signature.initSign(key.getPrivateKey()); signature.update(DigestUtils.sha512(data)); byte[] sig = signature.sign(); System.arraycopy(sig, 0, hash, 1, sig.length); @@ -37,12 +37,12 @@ public class RequestSigner { return hash; } - public static byte[] signRFC6979(byte[] privateKey, byte[] data) { + public static byte[] signRFC6979(ECDsa key, byte[] data) { var digest = createSHA256(); var secp256R1 = SECNamedCurves.getByOID(SECObjectIdentifiers.secp256r1); var ecParameters = new ECDomainParameters(secp256R1.getCurve(), secp256R1.getG(), secp256R1.getN()); - var ecPrivateKey = new ECPrivateKeyParameters(new BigInteger(1, privateKey), ecParameters); + var ecPrivateKey = new ECPrivateKeyParameters(new BigInteger(1, key.getPrivateKeyByte()), ecParameters); var signer = new ECDSASigner(new HMacDSAKCalculator(digest)); var hash = new byte[digest.getDigestSize()]; @@ -62,36 +62,29 @@ public class RequestSigner { return signature; } - public static frostFS.refs.Types.SignatureRFC6979 signRFC6979(byte[] publicKey, - byte[] privateKey, - AbstractMessage message) { + public static frostFS.refs.Types.SignatureRFC6979 signRFC6979(ECDsa key, AbstractMessage message) { return frostFS.refs.Types.SignatureRFC6979.newBuilder() - .setKey(ByteString.copyFrom(publicKey)) - .setSign(ByteString.copyFrom(signRFC6979(privateKey, message.toByteArray()))) + .setKey(ByteString.copyFrom(key.getPublicKeyByte())) + .setSign(ByteString.copyFrom(signRFC6979(key, message.toByteArray()))) .build(); } - public static frostFS.refs.Types.SignatureRFC6979 signRFC6979(byte[] publicKey, - byte[] privateKey, - ByteString data) { + public static frostFS.refs.Types.SignatureRFC6979 signRFC6979(ECDsa key, ByteString data) { return frostFS.refs.Types.SignatureRFC6979.newBuilder() - .setKey(ByteString.copyFrom(publicKey)) - .setSign(ByteString.copyFrom(signRFC6979(privateKey, data.toByteArray()))) + .setKey(ByteString.copyFrom(key.getPublicKeyByte())) + .setSign(ByteString.copyFrom(signRFC6979(key, data.toByteArray()))) .build(); } - public static frostFS.refs.Types.Signature signMessagePart(byte[] publicKey, - byte[] privateKey, - AbstractMessage data) { + public static frostFS.refs.Types.Signature signMessagePart(ECDsa key, AbstractMessage data) { var data2Sign = data.getSerializedSize() == 0 ? new byte[]{} : data.toByteArray(); - return frostFS.refs.Types.Signature.newBuilder() - .setKey(ByteString.copyFrom(publicKey)) - .setSign(ByteString.copyFrom(signData(privateKey, data2Sign))) + .setKey(ByteString.copyFrom(key.getPublicKeyByte())) + .setSign(ByteString.copyFrom(signData(key, data2Sign))) .build(); } - public static void sign(AbstractMessage.Builder request, byte[] publicKey, byte[] privateKey) { + public static void sign(AbstractMessage.Builder request, ECDsa key) { var meta = (AbstractMessage) request.getField(request.getDescriptorForType().findFieldByName("meta_header")); var body = (AbstractMessage) request.getField(request.getDescriptorForType().findFieldByName("body")); var verify = (AbstractMessage) request.getField(request.getDescriptorForType().findFieldByName("verify_header")); @@ -109,7 +102,7 @@ public class RequestSigner { if (verifyOrigin.getSerializedSize() == 0) { verifyBuilder.setField( verifyBuilder.getDescriptorForType().findFieldByName("body_signature"), - signMessagePart(publicKey, privateKey, body) + signMessagePart(key, body) ); } else { verifyBuilder.setField(verifyBuilder.getDescriptorForType().findFieldByName("origin"), verifyOrigin); @@ -117,11 +110,11 @@ public class RequestSigner { verifyBuilder.setField( verifyBuilder.getDescriptorForType().findFieldByName("meta_signature"), - signMessagePart(publicKey, privateKey, meta) + signMessagePart(key, meta) ); verifyBuilder.setField( verifyBuilder.getDescriptorForType().findFieldByName("origin_signature"), - signMessagePart(publicKey, privateKey, verifyOrigin) + signMessagePart(key, verifyOrigin) ); request.setField(request.getDescriptorForType().findFieldByName("verify_header"), verifyBuilder.build()); diff --git a/client/src/main/java/info/FrostFS/sdk/Verifier.java b/client/src/main/java/info/FrostFS/sdk/Verifier.java index 7a1b136..e0c37ec 100644 --- a/client/src/main/java/info/FrostFS/sdk/Verifier.java +++ b/client/src/main/java/info/FrostFS/sdk/Verifier.java @@ -16,7 +16,7 @@ import java.security.PublicKey; import java.security.Signature; import java.util.Arrays; -import static info.FrostFS.sdk.KeyExtension.getPublicKeyByPublic; +import static info.FrostFS.sdk.KeyExtension.getPublicKeyFromBytes; import static java.util.Objects.isNull; import static org.bouncycastle.crypto.util.DigestFactory.createSHA256; import static org.bouncycastle.util.BigIntegers.fromUnsignedByteArray; @@ -99,7 +99,7 @@ public class Verifier { public static boolean verifyMessagePart(Types.Signature sig, AbstractMessage data) { if (sig.getSerializedSize() == 0 || sig.getKey().isEmpty() || sig.getSign().isEmpty()) return false; - var publicKey = getPublicKeyByPublic(sig.getKey().toByteArray()); + var publicKey = getPublicKeyFromBytes(sig.getKey().toByteArray()); var data2Verify = data.getSerializedSize() == 0 ? new byte[]{} : data.toByteArray(); return verifyData(publicKey, data2Verify, sig.getSign().toByteArray()); diff --git a/client/src/main/java/info/FrostFS/sdk/services/ContainerClient.java b/client/src/main/java/info/FrostFS/sdk/services/ContainerClient.java index 3cdb8c2..385f616 100644 --- a/client/src/main/java/info/FrostFS/sdk/services/ContainerClient.java +++ b/client/src/main/java/info/FrostFS/sdk/services/ContainerClient.java @@ -1,16 +1,16 @@ package info.FrostFS.sdk.services; -import info.FrostFS.sdk.jdo.Container; -import info.FrostFS.sdk.jdo.ContainerId; +import info.FrostFS.sdk.dto.container.Container; +import info.FrostFS.sdk.dto.container.ContainerId; import java.util.List; public interface ContainerClient { - Container getContainerAsync(ContainerId cid); + Container getContainer(ContainerId cid); - List listContainersAsync(); + List listContainers(); - ContainerId createContainerAsync(Container container); + ContainerId createContainer(Container container); - void deleteContainerAsync(ContainerId cid); + void deleteContainer(ContainerId cid); } diff --git a/client/src/main/java/info/FrostFS/sdk/services/ContextAccessor.java b/client/src/main/java/info/FrostFS/sdk/services/ContextAccessor.java index 6985310..d3880a9 100644 --- a/client/src/main/java/info/FrostFS/sdk/services/ContextAccessor.java +++ b/client/src/main/java/info/FrostFS/sdk/services/ContextAccessor.java @@ -3,7 +3,7 @@ package info.FrostFS.sdk.services; import info.FrostFS.sdk.tools.ClientEnvironment; public class ContextAccessor { - protected ClientEnvironment context; + private final ClientEnvironment context; public ContextAccessor(ClientEnvironment context) { this.context = context; @@ -12,8 +12,4 @@ public class ContextAccessor { public ClientEnvironment getContext() { return context; } - - public void setContext(ClientEnvironment context) { - this.context = context; - } } diff --git a/client/src/main/java/info/FrostFS/sdk/services/FrostFSClient.java b/client/src/main/java/info/FrostFS/sdk/services/FrostFSClient.java index 44a0388..64ba93c 100644 --- a/client/src/main/java/info/FrostFS/sdk/services/FrostFSClient.java +++ b/client/src/main/java/info/FrostFS/sdk/services/FrostFSClient.java @@ -1,19 +1,23 @@ package info.FrostFS.sdk.services; import frostFS.session.Types; -import info.FrostFS.sdk.ClientSettings; -import info.FrostFS.sdk.jdo.*; -import info.FrostFS.sdk.jdo.netmap.NetmapSnapshot; -import info.FrostFS.sdk.jdo.netmap.NodeInfo; -import info.FrostFS.sdk.services.impl.ContainerService; -import info.FrostFS.sdk.services.impl.NetmapService; -import info.FrostFS.sdk.services.impl.ObjectService; -import info.FrostFS.sdk.services.impl.SessionService; +import info.FrostFS.sdk.tools.ClientSettings; +import info.FrostFS.sdk.tools.PutObjectParameters; +import info.FrostFS.sdk.dto.container.Container; +import info.FrostFS.sdk.dto.container.ContainerId; +import info.FrostFS.sdk.dto.SessionToken; +import info.FrostFS.sdk.dto.Version; +import info.FrostFS.sdk.dto.netmap.NetmapSnapshot; +import info.FrostFS.sdk.dto.netmap.NodeInfo; +import info.FrostFS.sdk.dto.object.ObjectFilter; +import info.FrostFS.sdk.dto.object.ObjectFrostFS; +import info.FrostFS.sdk.dto.object.ObjectHeader; +import info.FrostFS.sdk.dto.object.ObjectId; +import info.FrostFS.sdk.services.impl.*; import info.FrostFS.sdk.tools.ClientEnvironment; import info.FrostFS.sdk.tools.NetworkSettings; import io.grpc.Channel; -import java.io.FileInputStream; import java.util.List; import static info.FrostFS.sdk.GrpcClient.initGrpcChannel; @@ -24,7 +28,7 @@ public class FrostFSClient implements ContainerClient, ObjectClient, NetmapClien private final NetmapService netmapService; private final ObjectService objectService; private final SessionService sessionService; - private final ClientEnvironment clientEnvironment; + private final ObjectTools objectTools; public FrostFSClient(ClientSettings clientSettings) { if (isNull(clientSettings)) { @@ -32,20 +36,22 @@ public class FrostFSClient implements ContainerClient, ObjectClient, NetmapClien } clientSettings.validate(); + Channel channel = initGrpcChannel(clientSettings.getHost(), clientSettings.getCreds()); - this.clientEnvironment = + ClientEnvironment clientEnvironment = new ClientEnvironment(clientSettings.getKey(), channel, new Version(2, 13), this); this.containerService = new ContainerService(clientEnvironment); this.netmapService = new NetmapService(clientEnvironment); this.sessionService = new SessionService(clientEnvironment); this.objectService = new ObjectService(clientEnvironment); + this.objectTools = new ObjectTools(clientEnvironment); checkFrostFsVersionSupport(clientEnvironment.getVersion()); } private void checkFrostFsVersionSupport(Version version) { - var localNodeInfo = netmapService.getLocalNodeInfoAsync(); + var localNodeInfo = netmapService.getLocalNodeInfo(); if (!localNodeInfo.getVersion().isSupported(version)) { var msg = String.format("FrostFS %s is not supported.", localNodeInfo.getVersion()); System.out.println(msg); @@ -54,81 +60,76 @@ public class FrostFSClient implements ContainerClient, ObjectClient, NetmapClien } @Override - public Container getContainerAsync(ContainerId cid) { - return containerService.getContainerAsync(cid); + public Container getContainer(ContainerId cid) { + return containerService.getContainer(cid); } @Override - public List listContainersAsync() { - return containerService.listContainersAsync(); + public List listContainers() { + return containerService.listContainers(); } @Override - public ContainerId createContainerAsync(Container container) { - return containerService.createContainerAsync(container); + public ContainerId createContainer(Container container) { + return containerService.createContainer(container); } @Override - public void deleteContainerAsync(ContainerId cid) { - containerService.deleteContainerAsync(cid); + public void deleteContainer(ContainerId cid) { + containerService.deleteContainer(cid); } @Override - public ObjectHeader getObjectHeadAsync(ContainerId containerId, ObjectId objectId) { - return objectService.getObjectHeadAsync(containerId, objectId); + public ObjectHeader getObjectHead(ContainerId containerId, ObjectId objectId) { + return objectService.getObjectHead(containerId, objectId); } @Override - public ObjectFrostFs getObjectAsync(ContainerId containerId, ObjectId objectId) { - return objectService.getObjectAsync(containerId, objectId); + public ObjectFrostFS getObject(ContainerId containerId, ObjectId objectId) { + return objectService.getObject(containerId, objectId); } @Override - public ObjectId putObjectAsync(ObjectHeader header, FileInputStream payload) { - return objectService.putObjectAsync(header, payload); + public ObjectId putObject(PutObjectParameters parameters) { + return objectService.putObject(parameters); } @Override - public ObjectId putObjectAsync(ObjectHeader header, byte[] payload) { - return objectService.putObjectAsync(header, payload); + public void deleteObject(ContainerId containerId, ObjectId objectId) { + objectService.deleteObject(containerId, objectId); } @Override - public void deleteObjectAsync(ContainerId containerId, ObjectId objectId) { - objectService.deleteObjectAsync(containerId, objectId); + public Iterable searchObjects(ContainerId cid, ObjectFilter... filters) { + return objectService.searchObjects(cid, filters); } @Override - public Iterable searchObjectsAsync(ContainerId cid, ObjectFilter... filters) { - return objectService.searchObjectsAsync(cid, filters); + public NetmapSnapshot getNetmapSnapshot() { + return netmapService.getNetmapSnapshot(); } @Override - public NetmapSnapshot getNetmapSnapshotAsync() { - return netmapService.getNetmapSnapshotAsync(); + public NodeInfo getLocalNodeInfo() { + return netmapService.getLocalNodeInfo(); } @Override - public NodeInfo getLocalNodeInfoAsync() { - return netmapService.getLocalNodeInfoAsync(); + public NetworkSettings getNetworkSettings() { + return netmapService.getNetworkSettings(); } @Override - public NetworkSettings getNetworkSettingsAsync() { - return netmapService.getNetworkSettingsAsync(); + public SessionToken createSession(long expiration) { + return sessionService.createSession(expiration); + } + + public Types.SessionToken createSessionInternal(long expiration) { + return sessionService.createSessionInternal(expiration); } @Override - public SessionToken createSessionAsync(long expiration) { - return sessionService.createSessionAsync(expiration); - } - - public Types.SessionToken createSessionInternalAsync(long expiration) { - return sessionService.createSessionInternalAsync(expiration); - } - - @Override - public ObjectId CalculateObjectId(ObjectHeader header) { - return null; + public ObjectId calculateObjectId(ObjectHeader header) { + return objectTools.calculateObjectId(header); } } diff --git a/client/src/main/java/info/FrostFS/sdk/services/NetmapClient.java b/client/src/main/java/info/FrostFS/sdk/services/NetmapClient.java index f6e9401..80bbf1e 100644 --- a/client/src/main/java/info/FrostFS/sdk/services/NetmapClient.java +++ b/client/src/main/java/info/FrostFS/sdk/services/NetmapClient.java @@ -1,14 +1,13 @@ package info.FrostFS.sdk.services; -import info.FrostFS.sdk.jdo.netmap.NetmapSnapshot; -import info.FrostFS.sdk.jdo.netmap.NodeInfo; +import info.FrostFS.sdk.dto.netmap.NetmapSnapshot; +import info.FrostFS.sdk.dto.netmap.NodeInfo; import info.FrostFS.sdk.tools.NetworkSettings; public interface NetmapClient { + NetmapSnapshot getNetmapSnapshot(); - NetmapSnapshot getNetmapSnapshotAsync(); + NodeInfo getLocalNodeInfo(); - NodeInfo getLocalNodeInfoAsync(); - - NetworkSettings getNetworkSettingsAsync(); + NetworkSettings getNetworkSettings(); } diff --git a/client/src/main/java/info/FrostFS/sdk/services/ObjectClient.java b/client/src/main/java/info/FrostFS/sdk/services/ObjectClient.java index 682d52a..f19fb61 100644 --- a/client/src/main/java/info/FrostFS/sdk/services/ObjectClient.java +++ b/client/src/main/java/info/FrostFS/sdk/services/ObjectClient.java @@ -1,19 +1,20 @@ package info.FrostFS.sdk.services; -import info.FrostFS.sdk.jdo.*; - -import java.io.FileInputStream; +import info.FrostFS.sdk.dto.container.ContainerId; +import info.FrostFS.sdk.tools.PutObjectParameters; +import info.FrostFS.sdk.dto.object.ObjectFilter; +import info.FrostFS.sdk.dto.object.ObjectFrostFS; +import info.FrostFS.sdk.dto.object.ObjectHeader; +import info.FrostFS.sdk.dto.object.ObjectId; public interface ObjectClient { - ObjectHeader getObjectHeadAsync(ContainerId containerId, ObjectId objectId); + ObjectHeader getObjectHead(ContainerId containerId, ObjectId objectId); - ObjectFrostFs getObjectAsync(ContainerId containerId, ObjectId objectId); + ObjectFrostFS getObject(ContainerId containerId, ObjectId objectId); - ObjectId putObjectAsync(ObjectHeader header, FileInputStream payload); + ObjectId putObject(PutObjectParameters parameters); - ObjectId putObjectAsync(ObjectHeader header, byte[] payload); + void deleteObject(ContainerId containerId, ObjectId objectId); - void deleteObjectAsync(ContainerId containerId, ObjectId objectId); - - Iterable searchObjectsAsync(ContainerId cid, ObjectFilter... filters); + Iterable searchObjects(ContainerId cid, ObjectFilter... filters); } diff --git a/client/src/main/java/info/FrostFS/sdk/services/SessionClient.java b/client/src/main/java/info/FrostFS/sdk/services/SessionClient.java index ffd1f0a..6fd7f4f 100644 --- a/client/src/main/java/info/FrostFS/sdk/services/SessionClient.java +++ b/client/src/main/java/info/FrostFS/sdk/services/SessionClient.java @@ -1,8 +1,7 @@ package info.FrostFS.sdk.services; -import info.FrostFS.sdk.jdo.SessionToken; +import info.FrostFS.sdk.dto.SessionToken; public interface SessionClient { - - SessionToken createSessionAsync(long expiration); + SessionToken createSession(long expiration); } diff --git a/client/src/main/java/info/FrostFS/sdk/services/ToolsClient.java b/client/src/main/java/info/FrostFS/sdk/services/ToolsClient.java index 62dffc2..fb673a2 100644 --- a/client/src/main/java/info/FrostFS/sdk/services/ToolsClient.java +++ b/client/src/main/java/info/FrostFS/sdk/services/ToolsClient.java @@ -1,8 +1,8 @@ package info.FrostFS.sdk.services; -import info.FrostFS.sdk.jdo.ObjectHeader; -import info.FrostFS.sdk.jdo.ObjectId; +import info.FrostFS.sdk.dto.object.ObjectHeader; +import info.FrostFS.sdk.dto.object.ObjectId; public interface ToolsClient { - ObjectId CalculateObjectId(ObjectHeader header); + ObjectId calculateObjectId(ObjectHeader header); } diff --git a/client/src/main/java/info/FrostFS/sdk/services/impl/ContainerService.java b/client/src/main/java/info/FrostFS/sdk/services/impl/ContainerService.java index 824f967..4383579 100644 --- a/client/src/main/java/info/FrostFS/sdk/services/impl/ContainerService.java +++ b/client/src/main/java/info/FrostFS/sdk/services/impl/ContainerService.java @@ -3,10 +3,10 @@ package info.FrostFS.sdk.services.impl; import frostFS.container.ContainerServiceGrpc; import frostFS.container.Service; import info.FrostFS.sdk.Verifier; -import info.FrostFS.sdk.jdo.Container; -import info.FrostFS.sdk.jdo.ContainerId; -import info.FrostFS.sdk.mappers.ContainerIdMapper; -import info.FrostFS.sdk.mappers.ContainerMapper; +import info.FrostFS.sdk.dto.container.Container; +import info.FrostFS.sdk.dto.container.ContainerId; +import info.FrostFS.sdk.mappers.container.ContainerIdMapper; +import info.FrostFS.sdk.mappers.container.ContainerMapper; import info.FrostFS.sdk.mappers.OwnerIdMapper; import info.FrostFS.sdk.mappers.VersionMapper; import info.FrostFS.sdk.services.ContainerClient; @@ -21,14 +21,14 @@ import static info.FrostFS.sdk.RequestSigner.sign; import static info.FrostFS.sdk.RequestSigner.signRFC6979; public class ContainerService extends ContextAccessor implements ContainerClient { - private final ContainerServiceGrpc.ContainerServiceBlockingStub containerServiceAsyncClient; + private final ContainerServiceGrpc.ContainerServiceBlockingStub serviceBlockingStub; public ContainerService(ClientEnvironment clientEnvironment) { super(clientEnvironment); - this.containerServiceAsyncClient = ContainerServiceGrpc.newBlockingStub(clientEnvironment.getChannel()); + this.serviceBlockingStub = ContainerServiceGrpc.newBlockingStub(clientEnvironment.getChannel()); } - public Container getContainerAsync(ContainerId cid) { + public Container getContainer(ContainerId cid) { var request = Service.GetRequest.newBuilder() .setBody( Service.GetRequest.Body.newBuilder() @@ -37,26 +37,26 @@ public class ContainerService extends ContextAccessor implements ContainerClient ); addMetaHeader(request); - sign(request, context.getPublicKey(), context.getPrivateKey()); + sign(request, getContext().getKey()); - var response = containerServiceAsyncClient.get(request.build()); + var response = serviceBlockingStub.get(request.build()); Verifier.checkResponse(response); return ContainerMapper.toModel(response.getBody().getContainer()); } - public List listContainersAsync() { + public List listContainers() { var request = Service.ListRequest.newBuilder() .setBody( Service.ListRequest.Body.newBuilder() - .setOwnerId(OwnerIdMapper.toGrpcMessage(context.getOwnerId())) + .setOwnerId(OwnerIdMapper.toGrpcMessage(getContext().getOwnerId())) .build() ); addMetaHeader(request); - sign(request, context.getPublicKey(), context.getPrivateKey()); + sign(request, getContext().getKey()); - var response = containerServiceAsyncClient.list(request.build()); + var response = serviceBlockingStub.list(request.build()); Verifier.checkResponse(response); @@ -65,12 +65,12 @@ public class ContainerService extends ContextAccessor implements ContainerClient .collect(Collectors.toList()); } - public ContainerId createContainerAsync(Container container) { + public ContainerId createContainer(Container container) { var grpcContainer = ContainerMapper.toGrpcMessage(container); grpcContainer = grpcContainer.toBuilder() - .setOwnerId(OwnerIdMapper.toGrpcMessage(context.getOwnerId())) - .setVersion(VersionMapper.toGrpcMessage(context.getVersion())) + .setOwnerId(OwnerIdMapper.toGrpcMessage(getContext().getOwnerId())) + .setVersion(VersionMapper.toGrpcMessage(getContext().getVersion())) .build(); var request = Service.PutRequest.newBuilder() @@ -78,21 +78,21 @@ public class ContainerService extends ContextAccessor implements ContainerClient Service.PutRequest.Body.newBuilder() .setContainer(grpcContainer) .setSignature( - signRFC6979(context.getPublicKey(), context.getPrivateKey(), grpcContainer) + signRFC6979(getContext().getKey(), grpcContainer) ) .build() ); addMetaHeader(request); - sign(request, context.getPublicKey(), context.getPrivateKey()); + sign(request, getContext().getKey()); - var response = containerServiceAsyncClient.put(request.build()); + var response = serviceBlockingStub.put(request.build()); Verifier.checkResponse(response); return ContainerId.fromHash(response.getBody().getContainerId().getValue().toByteArray()); } - public void deleteContainerAsync(ContainerId cid) { + public void deleteContainer(ContainerId cid) { var grpcContainerId = ContainerIdMapper.toGrpcMessage(cid); var request = Service.DeleteRequest.newBuilder() @@ -100,15 +100,15 @@ public class ContainerService extends ContextAccessor implements ContainerClient Service.DeleteRequest.Body.newBuilder() .setContainerId(grpcContainerId) .setSignature(signRFC6979( - context.getPublicKey(), context.getPrivateKey(), grpcContainerId.getValue() + getContext().getKey(), grpcContainerId.getValue() )) .build() ); addMetaHeader(request); - sign(request, context.getPublicKey(), context.getPrivateKey()); + sign(request, getContext().getKey()); - var response = containerServiceAsyncClient.delete(request.build()); + var response = serviceBlockingStub.delete(request.build()); Verifier.checkResponse(response); } diff --git a/client/src/main/java/info/FrostFS/sdk/services/impl/NetmapService.java b/client/src/main/java/info/FrostFS/sdk/services/impl/NetmapService.java index b2ccf23..e55c655 100644 --- a/client/src/main/java/info/FrostFS/sdk/services/impl/NetmapService.java +++ b/client/src/main/java/info/FrostFS/sdk/services/impl/NetmapService.java @@ -4,8 +4,8 @@ import frostFS.netmap.NetmapServiceGrpc; import frostFS.netmap.Service; import frostFS.netmap.Types; import info.FrostFS.sdk.Verifier; -import info.FrostFS.sdk.jdo.netmap.NetmapSnapshot; -import info.FrostFS.sdk.jdo.netmap.NodeInfo; +import info.FrostFS.sdk.dto.netmap.NetmapSnapshot; +import info.FrostFS.sdk.dto.netmap.NodeInfo; import info.FrostFS.sdk.mappers.netmap.NetmapSnapshotMapper; import info.FrostFS.sdk.mappers.netmap.NodeInfoMapper; import info.FrostFS.sdk.services.ContextAccessor; @@ -24,7 +24,7 @@ public class NetmapService extends ContextAccessor implements NetmapClient { public NetmapService(ClientEnvironment clientEnvironment) { super(clientEnvironment); - this.netmapServiceClient = NetmapServiceGrpc.newBlockingStub(context.getChannel()); + this.netmapServiceClient = NetmapServiceGrpc.newBlockingStub(getContext().getChannel()); } private static boolean getBoolValue(byte[] bytes) { @@ -92,12 +92,12 @@ public class NetmapService extends ContextAccessor implements NetmapClient { } @Override - public NetworkSettings getNetworkSettingsAsync() { - if (nonNull(context.getNetworkSettings())) { - return context.getNetworkSettings(); + public NetworkSettings getNetworkSettings() { + if (nonNull(getContext().getNetworkSettings())) { + return getContext().getNetworkSettings(); } - var info = getNetworkInfoAsync(); + var info = getNetworkInfo(); var settings = new NetworkSettings(); @@ -105,18 +105,18 @@ public class NetmapService extends ContextAccessor implements NetmapClient { setNetworksParam(param, settings); } - context.setNetworkSettings(settings); + getContext().setNetworkSettings(settings); return settings; } @Override - public NodeInfo getLocalNodeInfoAsync() { + public NodeInfo getLocalNodeInfo() { var request = Service.LocalNodeInfoRequest.newBuilder() .setBody(Service.LocalNodeInfoRequest.Body.newBuilder().build()); addMetaHeader(request); - sign(request, context.getPublicKey(), context.getPrivateKey()); + sign(request, getContext().getKey()); var response = netmapServiceClient.localNodeInfo(request.build()); Verifier.checkResponse(response); @@ -124,12 +124,12 @@ public class NetmapService extends ContextAccessor implements NetmapClient { return NodeInfoMapper.toModel(response.getBody()); } - public Service.NetworkInfoResponse getNetworkInfoAsync() { + public Service.NetworkInfoResponse getNetworkInfo() { var request = Service.NetworkInfoRequest.newBuilder() .setBody(Service.NetworkInfoRequest.Body.newBuilder().build()); addMetaHeader(request); - sign(request, context.getPublicKey(), context.getPrivateKey()); + sign(request, getContext().getKey()); var response = netmapServiceClient.networkInfo(request.build()); @@ -139,12 +139,12 @@ public class NetmapService extends ContextAccessor implements NetmapClient { } @Override - public NetmapSnapshot getNetmapSnapshotAsync() { + public NetmapSnapshot getNetmapSnapshot() { var request = Service.NetmapSnapshotRequest.newBuilder() .setBody(Service.NetmapSnapshotRequest.Body.newBuilder().build()); addMetaHeader(request); - sign(request, context.getPublicKey(), context.getPrivateKey()); + sign(request, getContext().getKey()); var response = netmapServiceClient.netmapSnapshot(request.build()); diff --git a/client/src/main/java/info/FrostFS/sdk/services/impl/ObjectService.java b/client/src/main/java/info/FrostFS/sdk/services/impl/ObjectService.java index 9ef6a6d..4fbe78d 100644 --- a/client/src/main/java/info/FrostFS/sdk/services/impl/ObjectService.java +++ b/client/src/main/java/info/FrostFS/sdk/services/impl/ObjectService.java @@ -6,16 +6,24 @@ import frostFS.object.ObjectServiceGrpc; import frostFS.object.Service; import frostFS.refs.Types; import info.FrostFS.sdk.Verifier; -import info.FrostFS.sdk.jdo.*; +import info.FrostFS.sdk.constants.AppConst; +import info.FrostFS.sdk.dto.container.ContainerId; +import info.FrostFS.sdk.dto.Split; +import info.FrostFS.sdk.dto.object.*; import info.FrostFS.sdk.mappers.*; +import info.FrostFS.sdk.mappers.container.ContainerIdMapper; +import info.FrostFS.sdk.mappers.object.ObjectFilterMapper; +import info.FrostFS.sdk.mappers.object.ObjectHeaderMapper; +import info.FrostFS.sdk.mappers.object.ObjectIdMapper; +import info.FrostFS.sdk.mappers.object.ObjectFrostFSMapper; import info.FrostFS.sdk.services.ContextAccessor; import info.FrostFS.sdk.services.ObjectClient; import info.FrostFS.sdk.tools.ClientEnvironment; +import info.FrostFS.sdk.tools.PutObjectParameters; +import org.apache.commons.collections4.CollectionUtils; -import java.io.ByteArrayInputStream; import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -29,15 +37,17 @@ import static java.util.Objects.nonNull; public class ObjectService extends ContextAccessor implements ObjectClient { private final ObjectServiceGrpc.ObjectServiceBlockingStub objectServiceBlockingClient; private final ObjectServiceGrpc.ObjectServiceStub objectServiceClient; + private final ObjectTools objectTools; public ObjectService(ClientEnvironment clientEnvironment) { super(clientEnvironment); - this.objectServiceBlockingClient = ObjectServiceGrpc.newBlockingStub(context.getChannel()); - this.objectServiceClient = ObjectServiceGrpc.newStub(context.getChannel()); + this.objectServiceBlockingClient = ObjectServiceGrpc.newBlockingStub(getContext().getChannel()); + this.objectServiceClient = ObjectServiceGrpc.newStub(getContext().getChannel()); + this.objectTools = new ObjectTools(clientEnvironment); } @Override - public ObjectHeader getObjectHeadAsync(ContainerId cid, ObjectId oid) { + public ObjectHeader getObjectHead(ContainerId cid, ObjectId oid) { var request = Service.HeadRequest.newBuilder() .setBody( Service.HeadRequest.Body.newBuilder() @@ -50,7 +60,7 @@ public class ObjectService extends ContextAccessor implements ObjectClient { ); addMetaHeader(request); - sign(request, context.getPublicKey(), context.getPrivateKey()); + sign(request, getContext().getKey()); var response = objectServiceBlockingClient.head(request.build()); Verifier.checkResponse(response); @@ -59,8 +69,8 @@ public class ObjectService extends ContextAccessor implements ObjectClient { } @Override - public ObjectFrostFs getObjectAsync(ContainerId cid, ObjectId oid) { - var sessionToken = context.getFrostFSClient().createSessionInternalAsync(-1); + public ObjectFrostFS getObject(ContainerId cid, ObjectId oid) { + var sessionToken = getContext().getFrostFSClient().createSessionInternal(-1); var request = Service.GetRequest.newBuilder() .setBody( @@ -71,24 +81,23 @@ public class ObjectService extends ContextAccessor implements ObjectClient { .setObjectId(ObjectIdMapper.toGrpcMessage(oid)) .build() ) - .setRaw(false) .build() ); addMetaHeader(request); addObjectSessionToken( request, sessionToken, ContainerIdMapper.toGrpcMessage(cid), ObjectIdMapper.toGrpcMessage(oid), - frostFS.session.Types.ObjectSessionContext.Verb.GET, context.getPublicKey(), context.getPrivateKey() + frostFS.session.Types.ObjectSessionContext.Verb.GET, getContext().getKey() ); - sign(request, context.getPublicKey(), context.getPrivateKey()); + sign(request, getContext().getKey()); var obj = getObject(request.build()); - - return ObjectMapper.toModel(obj); + return ObjectFrostFSMapper.toModel(obj); } + @Override - public void deleteObjectAsync(ContainerId cid, ObjectId oid) { + public void deleteObject(ContainerId cid, ObjectId oid) { var request = Service.DeleteRequest.newBuilder() .setBody( Service.DeleteRequest.Body.newBuilder() @@ -101,27 +110,17 @@ public class ObjectService extends ContextAccessor implements ObjectClient { .build()); addMetaHeader(request); - sign(request, context.getPublicKey(), context.getPrivateKey()); + sign(request, getContext().getKey()); var response = objectServiceBlockingClient.delete(request.build()); Verifier.checkResponse(response); } @Override - public ObjectId putObjectAsync(ObjectHeader header, FileInputStream payload) { - return putObject(header, payload); - } - - @Override - public ObjectId putObjectAsync(ObjectHeader header, byte[] payload) { - return putObject(header, new ByteArrayInputStream(payload)); - } - - @Override - public Iterable searchObjectsAsync(ContainerId cid, ObjectFilter... filters) { + public Iterable searchObjects(ContainerId cid, ObjectFilter... filters) { var body = Service.SearchRequest.Body.newBuilder() .setContainerId(ContainerIdMapper.toGrpcMessage(cid)) - .setVersion(1); + .setVersion(1);// TODO: clarify this param for (ObjectFilter filter : filters) { body.addFilters(ObjectFilterMapper.toGrpcMessage(filter)); @@ -131,13 +130,43 @@ public class ObjectService extends ContextAccessor implements ObjectClient { .setBody(body.build()); addMetaHeader(request); - sign(request, context.getPublicKey(), context.getPrivateKey()); + sign(request, getContext().getKey()); var objectsIds = searchObjects(request.build()); return Iterables.transform(objectsIds, input -> ObjectId.fromHash(input.getValue().toByteArray())); } + @Override + public ObjectId putObject(PutObjectParameters parameters) { + parameters.validate(); + + return parameters.clientCut ? putClientCutObject(parameters) : putStreamObject(parameters); + } + + public ObjectId putSingleObject(ObjectFrostFS modelObject) { + var sessionToken = getContext().getFrostFSClient().createSessionInternal(-1); + + var grpcObject = objectTools.createObject(modelObject); + + var request = Service.PutSingleRequest.newBuilder() + .setBody(Service.PutSingleRequest.Body.newBuilder().setObject(grpcObject).build()); + + + addMetaHeader(request); + addObjectSessionToken( + request, sessionToken, grpcObject.getHeader().getContainerId(), grpcObject.getObjectId(), + frostFS.session.Types.ObjectSessionContext.Verb.PUT, getContext().getKey() + ); + sign(request, getContext().getKey()); + + var response = objectServiceBlockingClient.putSingle(request.build()); + + Verifier.checkResponse(response); + + return ObjectId.fromHash(grpcObject.getObjectId().getValue().toByteArray()); + } + private frostFS.object.Types.Object getObject(Service.GetRequest request) { var iterator = getObjectInit(request); var obj = iterator.readHeader(); @@ -162,18 +191,20 @@ public class ObjectService extends ContextAccessor implements ObjectClient { return new ObjectReader(objectServiceBlockingClient.get(initRequest)); } - private ObjectId putObject(ObjectHeader header, InputStream payload) { - var sessionToken = context.getFrostFSClient().createSessionInternalAsync(-1); - var hdr = ObjectHeaderMapper.toGrpcMessage(header); + private ObjectId putStreamObject(PutObjectParameters parameters) { + var header = parameters.getHeader(); + var sessionToken = getContext().getFrostFSClient().createSessionInternal(-1); + + var hdr = ObjectHeaderMapper.toGrpcMessage(header); hdr = hdr.toBuilder() - .setOwnerId(OwnerIdMapper.toGrpcMessage(context.getOwnerId())) - .setVersion(VersionMapper.toGrpcMessage(context.getVersion())) + .setOwnerId(OwnerIdMapper.toGrpcMessage(getContext().getOwnerId())) + .setVersion(VersionMapper.toGrpcMessage(getContext().getVersion())) .build(); var oid = Types.ObjectID.newBuilder().setValue(getSha256(hdr)).build(); - var request = Service.PutRequest.newBuilder() + var initRequest = Service.PutRequest.newBuilder() .setBody( Service.PutRequest.Body.newBuilder() .setInit( @@ -181,34 +212,35 @@ public class ObjectService extends ContextAccessor implements ObjectClient { ).build() ); - addMetaHeader(request); + addMetaHeader(initRequest); addObjectSessionToken( - request, sessionToken, hdr.getContainerId(), oid, frostFS.session.Types.ObjectSessionContext.Verb.PUT, - context.getPublicKey(), context.getPrivateKey() + initRequest, sessionToken, hdr.getContainerId(), oid, + frostFS.session.Types.ObjectSessionContext.Verb.PUT, getContext().getKey() ); - sign(request, context.getPublicKey(), context.getPrivateKey()); + sign(initRequest, getContext().getKey()); - var writer = putObjectInit(request.build()); + var writer = putObjectInit(initRequest.build()); - var buffer = new byte[Constants.OBJECT_CHUNK_SIZE]; - int bufferLength = 0; - try { - bufferLength = payload.readNBytes(buffer, 0, Constants.OBJECT_CHUNK_SIZE); - while (bufferLength > 0) { - request.setBody( - Service.PutRequest.Body.newBuilder() - .setChunk(ByteString.copyFrom(Arrays.copyOfRange(buffer, 0, bufferLength))) - .build() - ) - .clearVerifyHeader(); - sign(request, context.getPublicKey(), context.getPrivateKey()); - writer.write(request.build()); - bufferLength = payload.readNBytes(buffer, 0, Constants.OBJECT_CHUNK_SIZE); - } - } catch ( - IOException e) { - throw new RuntimeException(e); + var bufferSize = parameters.getBufferMaxSize() > 0 ? parameters.getBufferMaxSize() : AppConst.OBJECT_CHUNK_SIZE; + bufferSize = (int) Math.min(getStreamSize(parameters.getPayload()), bufferSize); + bufferSize = header.getPayloadLength() > 0 ? (int) Math.min(header.getPayloadLength(), bufferSize) : bufferSize; + + var buffer = new byte[bufferSize]; + while (true) { + var bytesCount = readNBytes(parameters.getPayload(), buffer, bufferSize); + if (bytesCount <= 0) + break; + + var chunkRequest = Service.PutRequest.newBuilder(initRequest.build()) + .setBody( + Service.PutRequest.Body.newBuilder() + .setChunk(ByteString.copyFrom(Arrays.copyOfRange(buffer, 0, bytesCount))) + .build() + ) + .clearVerifyHeader(); + sign(chunkRequest, getContext().getKey()); + writer.write(chunkRequest.build()); } var response = writer.complete(); @@ -217,6 +249,71 @@ public class ObjectService extends ContextAccessor implements ObjectClient { return ObjectId.fromHash(response.getBody().getObjectId().getValue().toByteArray()); } + private ObjectId putClientCutObject(PutObjectParameters parameters) { + var header = parameters.getHeader(); + + var networkSettings = getContext().getFrostFSClient().getNetworkSettings(); + var payloadSize = getStreamSize(parameters.getPayload()); + var objectSize = (int) Math.min(payloadSize, networkSettings.getMaxObjectSize()); + var fullLength = header.getPayloadLength() == 0 ? payloadSize : header.getPayloadLength(); + if (fullLength == 0) { + throw new IllegalArgumentException("Payload stream must be able to seek or PayloadLength must be specified"); + } + + var buffer = new byte[objectSize]; + var largeObject = new LargeObject(header.getContainerId()); + var split = new Split(); + + ObjectId objectId; + List sentObjectIds = new ArrayList<>(); + ObjectFrostFS currentObject; + + while (true) { + var bytesCount = readNBytes(parameters.getPayload(), buffer, objectSize); + if (CollectionUtils.isNotEmpty(sentObjectIds)) { + split.setPrevious(sentObjectIds.get(sentObjectIds.size() - 1)); + } + + largeObject.appendBlock(buffer, bytesCount); + + currentObject = new ObjectFrostFS( + header.getContainerId(), + bytesCount < objectSize ? Arrays.copyOfRange(buffer, 0, bytesCount) : buffer + ); + currentObject.setSplit(split); + + if (largeObject.getPayloadLength() == fullLength) + break; + + objectId = putSingleObject(currentObject); + + sentObjectIds.add(objectId); + } + + if (CollectionUtils.isEmpty(sentObjectIds)) { + currentObject.addAttributes(parameters.getHeader().getAttributes()); + return putSingleObject(currentObject); + } + + largeObject.addAttributes(parameters.getHeader().getAttributes()); + largeObject.calculateHash(); + + currentObject.setParent(largeObject); + + objectId = putSingleObject(currentObject); + + sentObjectIds.add(objectId); + + var linkObject = new LinkObject(header.getContainerId(), split.getSplitId(), largeObject); + linkObject.addChildren(sentObjectIds); + + linkObject.getHeader().getAttributes().clear(); + + putSingleObject(linkObject); + + return objectTools.calculateObjectId(largeObject.getHeader()); + } + private ObjectWriter putObjectInit(Service.PutRequest initRequest) { if (initRequest.getSerializedSize() == 0) { throw new IllegalArgumentException(initRequest.getClass().getName()); @@ -248,4 +345,19 @@ public class ObjectService extends ContextAccessor implements ObjectClient { return new SearchReader(objectServiceBlockingClient.search(initRequest)); } + private int readNBytes(FileInputStream fileInputStream, byte[] buffer, int size) { + try { + return fileInputStream.readNBytes(buffer, 0, size); + } catch (IOException exp) { + throw new IllegalArgumentException(exp.getMessage()); + } + } + + private long getStreamSize(FileInputStream fileInputStream) { + try { + return fileInputStream.getChannel().size(); + } catch (IOException exp) { + throw new IllegalArgumentException(exp.getMessage()); + } + } } diff --git a/client/src/main/java/info/FrostFS/sdk/services/impl/ObjectTools.java b/client/src/main/java/info/FrostFS/sdk/services/impl/ObjectTools.java new file mode 100644 index 0000000..31fbebd --- /dev/null +++ b/client/src/main/java/info/FrostFS/sdk/services/impl/ObjectTools.java @@ -0,0 +1,106 @@ +package info.FrostFS.sdk.services.impl; + +import com.google.protobuf.ByteString; +import frostFS.object.Types; +import info.FrostFS.sdk.dto.object.ObjectFrostFS; +import info.FrostFS.sdk.dto.object.ObjectHeader; +import info.FrostFS.sdk.dto.object.ObjectId; +import info.FrostFS.sdk.mappers.object.ObjectHeaderMapper; +import info.FrostFS.sdk.mappers.object.ObjectIdMapper; +import info.FrostFS.sdk.mappers.OwnerIdMapper; +import info.FrostFS.sdk.mappers.VersionMapper; +import info.FrostFS.sdk.services.ContextAccessor; +import info.FrostFS.sdk.services.ToolsClient; +import info.FrostFS.sdk.tools.ClientEnvironment; +import org.apache.commons.collections4.ListUtils; + +import static info.FrostFS.sdk.Helper.getSha256; +import static info.FrostFS.sdk.RequestSigner.signData; +import static java.util.Objects.nonNull; + +public class ObjectTools extends ContextAccessor implements ToolsClient { + public ObjectTools(ClientEnvironment context) { + super(context); + } + + private static frostFS.refs.Types.Checksum sha256Checksum(byte[] data) { + return frostFS.refs.Types.Checksum.newBuilder() + .setType(frostFS.refs.Types.ChecksumType.SHA256) + .setSum(ByteString.copyFrom(getSha256(data))) + .build(); + } + + @Override + public ObjectId calculateObjectId(ObjectHeader header) { + var grpcHeader = createHeader(header, new byte[]{}); + + return ObjectIdMapper.toModel( + frostFS.refs.Types.ObjectID.newBuilder().setValue(getSha256(grpcHeader)).build() + ); + } + + public Types.Object createObject(ObjectFrostFS objectFrostFs) { + var grpcHeaderBuilder = ObjectHeaderMapper.toGrpcMessage(objectFrostFs.getHeader()).toBuilder() + .setOwnerId(OwnerIdMapper.toGrpcMessage(getContext().getOwnerId())) + .setVersion(VersionMapper.toGrpcMessage(getContext().getVersion())) + .setPayloadLength(objectFrostFs.getPayload().length) + .setPayloadHash(sha256Checksum(objectFrostFs.getPayload())); + + var split = objectFrostFs.getHeader().getSplit(); + if (nonNull(split)) { + var splitGrpc = Types.Header.Split.newBuilder() + .setSplitId(nonNull(split.getSplitId()) ? ByteString.copyFrom(split.getSplitId().toBinary()) : null); + + ListUtils.emptyIfNull(split.getChildren()).stream() + .map(ObjectIdMapper::toGrpcMessage) + .forEach(splitGrpc::addChildren); + + + if (nonNull(split.getParentHeader())) { + var grpcParentHeader = createHeader(split.getParentHeader(), new byte[]{}); + var parent = frostFS.refs.Types.ObjectID.newBuilder().setValue(getSha256(grpcParentHeader)).build(); + + splitGrpc + .setParent(parent) + .setParentHeader(grpcParentHeader) + .setParentSignature( + frostFS.refs.Types.Signature.newBuilder() + .setKey(ByteString.copyFrom(getContext().getKey().getPublicKeyByte())) + .setSign(ByteString.copyFrom(signData(getContext().getKey(), parent.toByteArray()))) + ); + + split.setParent(ObjectIdMapper.toModel(parent)); + } + if (nonNull(split.getPrevious())) { + splitGrpc.setPrevious(ObjectIdMapper.toGrpcMessage(split.getPrevious())); + } + grpcHeaderBuilder.setSplit(splitGrpc); + } + + var grpcHeader = grpcHeaderBuilder.build(); + var objectId = frostFS.refs.Types.ObjectID.newBuilder().setValue(getSha256(grpcHeader)).build(); + return Types.Object.newBuilder() + .setHeader(grpcHeader) + .setObjectId(objectId) + .setPayload(ByteString.copyFrom(objectFrostFs.getPayload())) + .setSignature( + frostFS.refs.Types.Signature.newBuilder() + .setKey(ByteString.copyFrom(getContext().getKey().getPublicKeyByte())) + .setSign(ByteString.copyFrom(signData(getContext().getKey(), objectId.toByteArray()))) + ) + .build(); + } + + private Types.Header createHeader(ObjectHeader header, byte[] payload) { + var grpcHeader = ObjectHeaderMapper.toGrpcMessage(header).toBuilder() + .setOwnerId(OwnerIdMapper.toGrpcMessage(getContext().getOwnerId())) + .setVersion(VersionMapper.toGrpcMessage(getContext().getVersion())); + + if (header.getPayloadCheckSum() != null) + grpcHeader.setPayloadHash(sha256Checksum(header.getPayloadCheckSum())); + else if (payload != null) + grpcHeader.setPayloadHash(sha256Checksum(payload)); + + return grpcHeader.build(); + } +} diff --git a/client/src/main/java/info/FrostFS/sdk/services/impl/SessionService.java b/client/src/main/java/info/FrostFS/sdk/services/impl/SessionService.java index 3406acf..cae3cb6 100644 --- a/client/src/main/java/info/FrostFS/sdk/services/impl/SessionService.java +++ b/client/src/main/java/info/FrostFS/sdk/services/impl/SessionService.java @@ -3,7 +3,7 @@ package info.FrostFS.sdk.services.impl; import frostFS.session.Service; import frostFS.session.SessionServiceGrpc; import frostFS.session.Types; -import info.FrostFS.sdk.jdo.SessionToken; +import info.FrostFS.sdk.dto.SessionToken; import info.FrostFS.sdk.mappers.OwnerIdMapper; import info.FrostFS.sdk.mappers.SessionMapper; import info.FrostFS.sdk.services.ContextAccessor; @@ -14,36 +14,36 @@ import static info.FrostFS.sdk.RequestConstructor.addMetaHeader; import static info.FrostFS.sdk.RequestSigner.sign; public class SessionService extends ContextAccessor implements SessionClient { - private final SessionServiceGrpc.SessionServiceBlockingStub sessionServiceAsyncClient; + private final SessionServiceGrpc.SessionServiceBlockingStub serviceBlockingStub; public SessionService(ClientEnvironment clientEnvironment) { super(clientEnvironment); - this.sessionServiceAsyncClient = SessionServiceGrpc.newBlockingStub(context.getChannel()); + this.serviceBlockingStub = SessionServiceGrpc.newBlockingStub(getContext().getChannel()); } @Override - public SessionToken createSessionAsync(long expiration) { - var sessionToken = createSessionInternalAsync(expiration); + public SessionToken createSession(long expiration) { + var sessionToken = createSessionInternal(expiration); var token = SessionMapper.serialize(sessionToken); return new SessionToken(new byte[]{}, token); } - public Types.SessionToken createSessionInternalAsync(long expiration) { + public Types.SessionToken createSessionInternal(long expiration) { var request = Service.CreateRequest.newBuilder() .setBody( Service.CreateRequest.Body.newBuilder() - .setOwnerId(OwnerIdMapper.toGrpcMessage(context.getOwnerId())) + .setOwnerId(OwnerIdMapper.toGrpcMessage(getContext().getOwnerId())) .setExpiration(expiration).build() ); addMetaHeader(request); - sign(request, context.getPublicKey(), context.getPrivateKey()); + sign(request, getContext().getKey()); return createSession(request.build()); } private Types.SessionToken createSession(Service.CreateRequest request) { - var resp = sessionServiceAsyncClient.create(request); + var resp = serviceBlockingStub.create(request); var lifetime = Types.SessionToken.Body.TokenLifetime.newBuilder() .setExp(request.getBody().getExpiration()) diff --git a/client/src/main/java/info/FrostFS/sdk/tools/ClientEnvironment.java b/client/src/main/java/info/FrostFS/sdk/tools/ClientEnvironment.java index 93019ca..ee1d8d4 100644 --- a/client/src/main/java/info/FrostFS/sdk/tools/ClientEnvironment.java +++ b/client/src/main/java/info/FrostFS/sdk/tools/ClientEnvironment.java @@ -1,26 +1,21 @@ package info.FrostFS.sdk.tools; -import info.FrostFS.sdk.jdo.OwnerId; -import info.FrostFS.sdk.jdo.Version; +import info.FrostFS.sdk.dto.OwnerId; +import info.FrostFS.sdk.dto.Version; import info.FrostFS.sdk.services.FrostFSClient; import io.grpc.Channel; -import static info.FrostFS.sdk.KeyExtension.getPrivateKeyFromWIF; -import static info.FrostFS.sdk.KeyExtension.loadPublicKey; - public class ClientEnvironment { private final OwnerId ownerId; private final Version version; - private final byte[] privateKey; - private final byte[] publicKey; + private final ECDsa key; private final Channel channel; private final FrostFSClient frostFSClient; private NetworkSettings networkSettings; - public ClientEnvironment(String key, Channel channel, Version version, FrostFSClient frostFSClient) { - this.privateKey = getPrivateKeyFromWIF(key); - this.publicKey = loadPublicKey(privateKey); - this.ownerId = OwnerId.fromKey(publicKey); + public ClientEnvironment(String wif, Channel channel, Version version, FrostFSClient frostFSClient) { + this.key = new ECDsa(wif); + this.ownerId = OwnerId.fromKey(key.getPublicKeyByte()); this.version = version; this.channel = channel; this.frostFSClient = frostFSClient; @@ -50,11 +45,7 @@ public class ClientEnvironment { return version; } - public byte[] getPrivateKey() { - return privateKey; - } - - public byte[] getPublicKey() { - return publicKey; + public ECDsa getKey() { + return key; } } diff --git a/client/src/main/java/info/FrostFS/sdk/ClientSettings.java b/client/src/main/java/info/FrostFS/sdk/tools/ClientSettings.java similarity index 97% rename from client/src/main/java/info/FrostFS/sdk/ClientSettings.java rename to client/src/main/java/info/FrostFS/sdk/tools/ClientSettings.java index 3b09497..2b25083 100644 --- a/client/src/main/java/info/FrostFS/sdk/ClientSettings.java +++ b/client/src/main/java/info/FrostFS/sdk/tools/ClientSettings.java @@ -1,4 +1,4 @@ -package info.FrostFS.sdk; +package info.FrostFS.sdk.tools; import io.grpc.ChannelCredentials; import org.apache.commons.lang3.StringUtils; diff --git a/client/src/main/java/info/FrostFS/sdk/tools/ECDsa.java b/client/src/main/java/info/FrostFS/sdk/tools/ECDsa.java new file mode 100644 index 0000000..98ccfc0 --- /dev/null +++ b/client/src/main/java/info/FrostFS/sdk/tools/ECDsa.java @@ -0,0 +1,29 @@ +package info.FrostFS.sdk.tools; + +import java.security.PrivateKey; + +import static info.FrostFS.sdk.KeyExtension.*; + +public class ECDsa { + private final byte[] publicKeyByte; + private final byte[] privateKeyByte; + private final PrivateKey privateKey; + + public ECDsa(String wif) { + this.privateKeyByte = getPrivateKeyFromWIF(wif); + this.publicKeyByte = loadPublicKey(privateKeyByte); + this.privateKey = loadPrivateKey(privateKeyByte); + } + + public byte[] getPublicKeyByte() { + return publicKeyByte; + } + + public byte[] getPrivateKeyByte() { + return privateKeyByte; + } + + public PrivateKey getPrivateKey() { + return privateKey; + } +} diff --git a/client/src/main/java/info/FrostFS/sdk/tools/PutObjectParameters.java b/client/src/main/java/info/FrostFS/sdk/tools/PutObjectParameters.java new file mode 100644 index 0000000..ca62737 --- /dev/null +++ b/client/src/main/java/info/FrostFS/sdk/tools/PutObjectParameters.java @@ -0,0 +1,76 @@ +package info.FrostFS.sdk.tools; + +import info.FrostFS.sdk.dto.object.ObjectHeader; + +import java.io.FileInputStream; + +import static java.util.Objects.isNull; + +public class PutObjectParameters { + private static final String ERROR_TEMPLATE = "%s value cannot be null."; + + public ObjectHeader header; + public FileInputStream payload; + public boolean clientCut; + public int bufferMaxSize; + + public PutObjectParameters(ObjectHeader header, FileInputStream payload, boolean clientCut, int bufferMaxSize) { + this.header = header; + this.payload = payload; + this.clientCut = clientCut; + this.bufferMaxSize = bufferMaxSize; + } + + public PutObjectParameters(ObjectHeader header, FileInputStream payload) { + this.header = header; + this.payload = payload; + } + + public ObjectHeader getHeader() { + return header; + } + + public void setHeader(ObjectHeader header) { + this.header = header; + } + + public FileInputStream getPayload() { + return payload; + } + + public void setPayload(FileInputStream payload) { + this.payload = payload; + } + + public boolean isClientCut() { + return clientCut; + } + + public void setClientCut(boolean clientCut) { + this.clientCut = clientCut; + } + + public int getBufferMaxSize() { + return bufferMaxSize; + } + + public void setBufferMaxSize(int bufferMaxSize) { + this.bufferMaxSize = bufferMaxSize; + } + + public void validate() { + StringBuilder errorMessage = new StringBuilder(); + + if (isNull(header)) { + errorMessage.append(String.format(ERROR_TEMPLATE, "Header")).append(System.lineSeparator()); + } + + if (isNull(payload)) { + errorMessage.append(String.format(ERROR_TEMPLATE, "Payload")).append(System.lineSeparator()); + } + + if (errorMessage.length() != 0) { + throw new IllegalArgumentException(errorMessage.toString()); + } + } +} diff --git a/cryptography/pom.xml b/cryptography/pom.xml index 9af2880..9c18d69 100644 --- a/cryptography/pom.xml +++ b/cryptography/pom.xml @@ -19,11 +19,6 @@ - - commons-codec - commons-codec - 1.17.0 - com.google.protobuf protobuf-java @@ -34,11 +29,6 @@ bcprov-jdk18on 1.78.1 - - org.bouncycastle - bcpkix-jdk18on - 1.78.1 - \ No newline at end of file diff --git a/cryptography/src/main/java/info/FrostFS/sdk/Helper.java b/cryptography/src/main/java/info/FrostFS/sdk/Helper.java index e4e1460..fb11ac2 100644 --- a/cryptography/src/main/java/info/FrostFS/sdk/Helper.java +++ b/cryptography/src/main/java/info/FrostFS/sdk/Helper.java @@ -18,19 +18,23 @@ public class Helper { return hash; } - public static byte[] getSha256(byte[] value) { + public static MessageDigest getSha256Instance() { try { - return MessageDigest.getInstance("SHA-256").digest(value); + return MessageDigest.getInstance("SHA-256"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } } + public static byte[] getSha256(byte[] value) { + return getSha256Instance().digest(value); + } + public static ByteString getSha256(AbstractMessage value) { return ByteString.copyFrom(getSha256(value.toByteArray())); } - public static String пуеHexString(byte[] array) { + public static String getHexString(byte[] array) { return String.format("%0" + (array.length << 1) + "x", new BigInteger(1, array)); } } diff --git a/cryptography/src/main/java/info/FrostFS/sdk/KeyExtension.java b/cryptography/src/main/java/info/FrostFS/sdk/KeyExtension.java index b8c5ab6..d057a04 100644 --- a/cryptography/src/main/java/info/FrostFS/sdk/KeyExtension.java +++ b/cryptography/src/main/java/info/FrostFS/sdk/KeyExtension.java @@ -57,7 +57,9 @@ public class KeyExtension { public static byte[] loadPublicKey(byte[] privateKey) { X9ECParameters params = SECNamedCurves.getByOID(SECObjectIdentifiers.secp256r1); - ECDomainParameters domain = new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH()); + ECDomainParameters domain = new ECDomainParameters( + params.getCurve(), params.getG(), params.getN(), params.getH() + ); ECPoint q = domain.getG().multiply(new BigInteger(1, privateKey)); ECPublicKeyParameters publicParams = new ECPublicKeyParameters(q, domain); return publicParams.getQ().getEncoded(true); @@ -65,7 +67,9 @@ public class KeyExtension { public static PrivateKey loadPrivateKey(byte[] privateKey) { X9ECParameters params = SECNamedCurves.getByOID(SECObjectIdentifiers.secp256r1); - ECDomainParameters domain = new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH()); + ECDomainParameters domain = new ECDomainParameters( + params.getCurve(), params.getG(), params.getN(), params.getH() + ); ECPrivateKeyParameters ecParams = new ECPrivateKeyParameters(fromUnsignedByteArray(privateKey), domain); ECParameterSpec ecParameterSpec = new ECNamedCurveSpec( @@ -80,7 +84,7 @@ public class KeyExtension { } } - public static PublicKey getPublicKeyByPublic(byte[] publicKey) { + public static PublicKey getPublicKeyFromBytes(byte[] publicKey) { if (publicKey.length != COMPRESSED_PUBLIC_KEY_LENGTH) { throw new IllegalArgumentException( String.format("Decompress argument isn't compressed public key. Expected length=%s, actual=%s", @@ -89,14 +93,17 @@ public class KeyExtension { } X9ECParameters secp256R1 = SECNamedCurves.getByOID(SECObjectIdentifiers.secp256r1); - ECDomainParameters domain = new ECDomainParameters(secp256R1.getCurve(), secp256R1.getG(), secp256R1.getN(), secp256R1.getH()); + ECDomainParameters domain = new ECDomainParameters( + secp256R1.getCurve(), secp256R1.getG(), secp256R1.getN(), secp256R1.getH() + ); var ecPoint = secp256R1.getCurve().decodePoint(publicKey); var publicParams = new ECPublicKeyParameters(ecPoint, domain); java.security.spec.ECPoint point = new java.security.spec.ECPoint( publicParams.getQ().getRawXCoord().toBigInteger(), publicParams.getQ().getRawYCoord().toBigInteger() ); ECParameterSpec ecParameterSpec = new ECNamedCurveSpec( - "secp256r1", secp256R1.getCurve(), secp256R1.getG(), secp256R1.getN(), secp256R1.getH(), secp256R1.getSeed() + "secp256r1", secp256R1.getCurve(), secp256R1.getG(), secp256R1.getN(), + secp256R1.getH(), secp256R1.getSeed() ); ECPublicKeySpec publicKeySpec = new ECPublicKeySpec(point, ecParameterSpec); @@ -108,7 +115,6 @@ public class KeyExtension { } } - public static byte[] getScriptHash(byte[] publicKey) { var script = createSignatureRedeemScript(publicKey); diff --git a/modelsV2/pom.xml b/models/pom.xml similarity index 93% rename from modelsV2/pom.xml rename to models/pom.xml index 9093e71..7f9af60 100644 --- a/modelsV2/pom.xml +++ b/models/pom.xml @@ -9,7 +9,7 @@ 0.1.0 - modelsV2 + models 11 @@ -30,7 +30,7 @@ info.FrostFS.sdk - protosV2 + protos 0.1.0 diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/UUIDExtension.java b/models/src/main/java/info/FrostFS/sdk/UUIDExtension.java similarity index 100% rename from modelsV2/src/main/java/info/FrostFS/sdk/UUIDExtension.java rename to models/src/main/java/info/FrostFS/sdk/UUIDExtension.java diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/Constants.java b/models/src/main/java/info/FrostFS/sdk/constants/AppConst.java similarity index 66% rename from modelsV2/src/main/java/info/FrostFS/sdk/jdo/Constants.java rename to models/src/main/java/info/FrostFS/sdk/constants/AppConst.java index 2c01d9e..8880eb0 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/Constants.java +++ b/models/src/main/java/info/FrostFS/sdk/constants/AppConst.java @@ -1,6 +1,6 @@ -package info.FrostFS.sdk.jdo; +package info.FrostFS.sdk.constants; -public class Constants { +public class AppConst { public static final int OBJECT_CHUNK_SIZE = 3 * (1 << 20); public static final int SHA256_HASH_LENGTH = 32; } diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/constants/XHeaderConst.java b/models/src/main/java/info/FrostFS/sdk/constants/XHeaderConst.java similarity index 100% rename from modelsV2/src/main/java/info/FrostFS/sdk/constants/XHeaderConst.java rename to models/src/main/java/info/FrostFS/sdk/constants/XHeaderConst.java diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/MetaHeader.java b/models/src/main/java/info/FrostFS/sdk/dto/MetaHeader.java similarity index 86% rename from modelsV2/src/main/java/info/FrostFS/sdk/jdo/MetaHeader.java rename to models/src/main/java/info/FrostFS/sdk/dto/MetaHeader.java index 5f428b4..b0eead0 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/MetaHeader.java +++ b/models/src/main/java/info/FrostFS/sdk/dto/MetaHeader.java @@ -1,9 +1,9 @@ -package info.FrostFS.sdk.jdo; +package info.FrostFS.sdk.dto; public class MetaHeader { - public Version version; - public int epoch; - public int ttl; + private Version version; + private int epoch; + private int ttl; public MetaHeader(Version version, int epoch, int ttl) { this.version = version; diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/OwnerId.java b/models/src/main/java/info/FrostFS/sdk/dto/OwnerId.java similarity index 94% rename from modelsV2/src/main/java/info/FrostFS/sdk/jdo/OwnerId.java rename to models/src/main/java/info/FrostFS/sdk/dto/OwnerId.java index 723e234..40b4f04 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/OwnerId.java +++ b/models/src/main/java/info/FrostFS/sdk/dto/OwnerId.java @@ -1,4 +1,4 @@ -package info.FrostFS.sdk.jdo; +package info.FrostFS.sdk.dto; import info.FrostFS.sdk.Base58; diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/SessionToken.java b/models/src/main/java/info/FrostFS/sdk/dto/SessionToken.java similarity index 74% rename from modelsV2/src/main/java/info/FrostFS/sdk/jdo/SessionToken.java rename to models/src/main/java/info/FrostFS/sdk/dto/SessionToken.java index bd4f7b0..5980875 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/SessionToken.java +++ b/models/src/main/java/info/FrostFS/sdk/dto/SessionToken.java @@ -1,8 +1,8 @@ -package info.FrostFS.sdk.jdo; +package info.FrostFS.sdk.dto; public class SessionToken { - public byte[] id; - public byte[] sessionKey; + private final byte[] id; + private final byte[] sessionKey; public SessionToken(byte[] id, byte[] sessionKey) { this.id = id; diff --git a/models/src/main/java/info/FrostFS/sdk/dto/Signature.java b/models/src/main/java/info/FrostFS/sdk/dto/Signature.java new file mode 100644 index 0000000..0ab5b3e --- /dev/null +++ b/models/src/main/java/info/FrostFS/sdk/dto/Signature.java @@ -0,0 +1,33 @@ +package info.FrostFS.sdk.dto; + +import info.FrostFS.sdk.enums.SignatureScheme; + +public class Signature { + private byte[] key; + private byte[] sign; + private SignatureScheme scheme; + + public byte[] getKey() { + return key; + } + + public void setKey(byte[] key) { + this.key = key; + } + + public byte[] getSign() { + return sign; + } + + public void setSign(byte[] sign) { + this.sign = sign; + } + + public SignatureScheme getScheme() { + return scheme; + } + + public void setScheme(SignatureScheme scheme) { + this.scheme = scheme; + } +} diff --git a/models/src/main/java/info/FrostFS/sdk/dto/Split.java b/models/src/main/java/info/FrostFS/sdk/dto/Split.java new file mode 100644 index 0000000..be51331 --- /dev/null +++ b/models/src/main/java/info/FrostFS/sdk/dto/Split.java @@ -0,0 +1,69 @@ +package info.FrostFS.sdk.dto; + +import info.FrostFS.sdk.dto.object.ObjectHeader; +import info.FrostFS.sdk.dto.object.ObjectId; + +import java.util.ArrayList; +import java.util.List; + +public class Split { + private final List children; + private SplitId splitId; + private ObjectId parent; + private ObjectId previous; + private Signature parentSignature; + private ObjectHeader parentHeader; + + public Split() { + this(new SplitId()); + } + + public Split(SplitId splitId) { + this.splitId = splitId; + this.children = new ArrayList<>(); + } + + public SplitId getSplitId() { + return splitId; + } + + private void setSplitId(SplitId splitId) { + this.splitId = splitId; + } + + public ObjectId getParent() { + return parent; + } + + public void setParent(ObjectId parent) { + this.parent = parent; + } + + public ObjectId getPrevious() { + return previous; + } + + public void setPrevious(ObjectId previous) { + this.previous = previous; + } + + public Signature getParentSignature() { + return parentSignature; + } + + public void setParentSignature(Signature parentSignature) { + this.parentSignature = parentSignature; + } + + public ObjectHeader getParentHeader() { + return parentHeader; + } + + public void setParentHeader(ObjectHeader parentHeader) { + this.parentHeader = parentHeader; + } + + public List getChildren() { + return children; + } +} diff --git a/models/src/main/java/info/FrostFS/sdk/dto/SplitId.java b/models/src/main/java/info/FrostFS/sdk/dto/SplitId.java new file mode 100644 index 0000000..b01b8d0 --- /dev/null +++ b/models/src/main/java/info/FrostFS/sdk/dto/SplitId.java @@ -0,0 +1,44 @@ +package info.FrostFS.sdk.dto; + +import java.util.UUID; + +import static info.FrostFS.sdk.UUIDExtension.asBytes; +import static info.FrostFS.sdk.UUIDExtension.asUuid; +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) { + this.id = asUuid(binary); + } + + private 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(); + } + + public byte[] toBinary() { + return isNull(id) ? null : asBytes(id); + } +} diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/Status.java b/models/src/main/java/info/FrostFS/sdk/dto/Status.java similarity index 71% rename from modelsV2/src/main/java/info/FrostFS/sdk/jdo/Status.java rename to models/src/main/java/info/FrostFS/sdk/dto/Status.java index bcb6f08..d766546 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/Status.java +++ b/models/src/main/java/info/FrostFS/sdk/dto/Status.java @@ -1,16 +1,16 @@ -package info.FrostFS.sdk.jdo; +package info.FrostFS.sdk.dto; import info.FrostFS.sdk.enums.StatusCode; -import java.util.Optional; +import static java.util.Objects.isNull; public class Status { - public StatusCode code; - public String message; + private StatusCode code; + private String message; public Status(StatusCode code, String message) { this.code = code; - this.message = Optional.ofNullable(message).orElse(""); + this.message = isNull(message) ? "" : message; } public Status(StatusCode code) { @@ -36,7 +36,7 @@ public class Status { @Override public String toString() { - return "Response status: " + code + ". Message: " + message + "."; + return String.format("Response status: %s. Message: %s.", code, message); } public boolean isSuccess() { diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/Version.java b/models/src/main/java/info/FrostFS/sdk/dto/Version.java similarity index 88% rename from modelsV2/src/main/java/info/FrostFS/sdk/jdo/Version.java rename to models/src/main/java/info/FrostFS/sdk/dto/Version.java index 22cd8c7..c498b49 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/Version.java +++ b/models/src/main/java/info/FrostFS/sdk/dto/Version.java @@ -1,8 +1,13 @@ -package info.FrostFS.sdk.jdo; +package info.FrostFS.sdk.dto; public class Version { - public int major; - public int minor; + private int major; + private int minor; + + public Version(int major, int minor) { + this.major = major; + this.minor = minor; + } public int getMajor() { return major; @@ -25,11 +30,6 @@ public class Version { return "v" + major + "." + minor; } - public Version(int major, int minor) { - this.major = major; - this.minor = minor; - } - public boolean isSupported(Version version) { return major == version.getMajor(); } diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/Container.java b/models/src/main/java/info/FrostFS/sdk/dto/container/Container.java similarity index 64% rename from modelsV2/src/main/java/info/FrostFS/sdk/jdo/Container.java rename to models/src/main/java/info/FrostFS/sdk/dto/container/Container.java index 38df197..b952fcf 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/Container.java +++ b/models/src/main/java/info/FrostFS/sdk/dto/container/Container.java @@ -1,15 +1,22 @@ -package info.FrostFS.sdk.jdo; +package info.FrostFS.sdk.dto.container; +import info.FrostFS.sdk.dto.Version; +import info.FrostFS.sdk.dto.netmap.PlacementPolicy; import info.FrostFS.sdk.enums.BasicAcl; -import info.FrostFS.sdk.jdo.netmap.PlacementPolicy; import java.util.UUID; public class Container { - public UUID nonce; - public BasicAcl basicAcl; - public PlacementPolicy placementPolicy; - public Version version; + private UUID nonce; + private BasicAcl basicAcl; + private PlacementPolicy placementPolicy; + private Version version; + + public Container(BasicAcl basicAcl, PlacementPolicy placementPolicy) { + this.nonce = UUID.randomUUID(); + this.basicAcl = basicAcl; + this.placementPolicy = placementPolicy; + } public UUID getNonce() { return nonce; @@ -42,17 +49,4 @@ public class Container { public void setVersion(Version version) { this.version = version; } - - public Container(BasicAcl basicAcl, PlacementPolicy placementPolicy) { - nonce = UUID.randomUUID(); - this.basicAcl = basicAcl; - this.placementPolicy = placementPolicy; - } - - public Container(BasicAcl basicAcl, PlacementPolicy placementPolicy, UUID nonce, Version version) { - this.nonce = nonce; - this.basicAcl = basicAcl; - this.placementPolicy = placementPolicy; - this.version = version; - } } diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/ContainerId.java b/models/src/main/java/info/FrostFS/sdk/dto/container/ContainerId.java similarity index 78% rename from modelsV2/src/main/java/info/FrostFS/sdk/jdo/ContainerId.java rename to models/src/main/java/info/FrostFS/sdk/dto/container/ContainerId.java index 309f25b..fd8d529 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/ContainerId.java +++ b/models/src/main/java/info/FrostFS/sdk/dto/container/ContainerId.java @@ -1,16 +1,17 @@ -package info.FrostFS.sdk.jdo; +package info.FrostFS.sdk.dto.container; import info.FrostFS.sdk.Base58; +import info.FrostFS.sdk.constants.AppConst; public class ContainerId { - public String value; + private String value; public ContainerId(String value) { this.value = value; } public static ContainerId fromHash(byte[] hash) { - if (hash.length != Constants.SHA256_HASH_LENGTH) { + if (hash.length != AppConst.SHA256_HASH_LENGTH) { throw new IllegalArgumentException("ContainerID must be a sha256 hash."); } return new ContainerId(Base58.encode(hash)); diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/netmap/NetmapSnapshot.java b/models/src/main/java/info/FrostFS/sdk/dto/netmap/NetmapSnapshot.java similarity index 75% rename from modelsV2/src/main/java/info/FrostFS/sdk/jdo/netmap/NetmapSnapshot.java rename to models/src/main/java/info/FrostFS/sdk/dto/netmap/NetmapSnapshot.java index 7fd344e..8619f00 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/netmap/NetmapSnapshot.java +++ b/models/src/main/java/info/FrostFS/sdk/dto/netmap/NetmapSnapshot.java @@ -1,10 +1,10 @@ -package info.FrostFS.sdk.jdo.netmap; +package info.FrostFS.sdk.dto.netmap; import java.util.List; public class NetmapSnapshot { - public Long epoch; - public List nodeInfoCollection; + private final Long epoch; + private final List nodeInfoCollection; public NetmapSnapshot(Long epoch, List nodeInfoCollection) { this.epoch = epoch; diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/netmap/NodeInfo.java b/models/src/main/java/info/FrostFS/sdk/dto/netmap/NodeInfo.java similarity index 66% rename from modelsV2/src/main/java/info/FrostFS/sdk/jdo/netmap/NodeInfo.java rename to models/src/main/java/info/FrostFS/sdk/dto/netmap/NodeInfo.java index 5b060f1..bd78146 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/netmap/NodeInfo.java +++ b/models/src/main/java/info/FrostFS/sdk/dto/netmap/NodeInfo.java @@ -1,17 +1,17 @@ -package info.FrostFS.sdk.jdo.netmap; +package info.FrostFS.sdk.dto.netmap; +import info.FrostFS.sdk.dto.Version; import info.FrostFS.sdk.enums.NodeState; -import info.FrostFS.sdk.jdo.Version; import java.util.List; import java.util.Map; public class NodeInfo { - public NodeState state; - public Version version; - public List addresses; - public Map attributes; - public byte[] publicKey; + private final NodeState state; + private final Version version; + private final List addresses; + private final Map attributes; + private final byte[] publicKey; public NodeInfo(NodeState state, Version version, List addresses, Map attributes, byte[] publicKey) { @@ -26,18 +26,10 @@ public class NodeInfo { return state; } - public void setState(NodeState state) { - this.state = state; - } - public Version getVersion() { return version; } - public void setVersion(Version version) { - this.version = version; - } - public byte[] getPublicKey() { return publicKey; } diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/netmap/PlacementPolicy.java b/models/src/main/java/info/FrostFS/sdk/dto/netmap/PlacementPolicy.java similarity index 52% rename from modelsV2/src/main/java/info/FrostFS/sdk/jdo/netmap/PlacementPolicy.java rename to models/src/main/java/info/FrostFS/sdk/dto/netmap/PlacementPolicy.java index d32b585..9e92a63 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/netmap/PlacementPolicy.java +++ b/models/src/main/java/info/FrostFS/sdk/dto/netmap/PlacementPolicy.java @@ -1,8 +1,8 @@ -package info.FrostFS.sdk.jdo.netmap; +package info.FrostFS.sdk.dto.netmap; public class PlacementPolicy { - public Replica[] replicas; - public boolean unique; + private final Replica[] replicas; + private final boolean unique; public PlacementPolicy(boolean unique, Replica[] replicas) { this.replicas = replicas; @@ -13,15 +13,7 @@ public class PlacementPolicy { return replicas; } - public void setReplicas(Replica[] replicas) { - this.replicas = replicas; - } - public boolean isUnique() { return unique; } - - public void setUnique(boolean unique) { - this.unique = unique; - } } diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/netmap/Replica.java b/models/src/main/java/info/FrostFS/sdk/dto/netmap/Replica.java similarity index 72% rename from modelsV2/src/main/java/info/FrostFS/sdk/jdo/netmap/Replica.java rename to models/src/main/java/info/FrostFS/sdk/dto/netmap/Replica.java index 80d1e89..5189df7 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/netmap/Replica.java +++ b/models/src/main/java/info/FrostFS/sdk/dto/netmap/Replica.java @@ -1,14 +1,14 @@ -package info.FrostFS.sdk.jdo.netmap; +package info.FrostFS.sdk.dto.netmap; -import java.util.Optional; +import static java.util.Objects.isNull; public class Replica { - public int count; - public String selector; + private int count; + private String selector; public Replica(int count, String selector) { this.count = count; - this.selector = Optional.ofNullable(selector).orElse(""); + this.selector = isNull(selector) ? "" : selector; } public Replica(int count) { diff --git a/models/src/main/java/info/FrostFS/sdk/dto/object/LargeObject.java b/models/src/main/java/info/FrostFS/sdk/dto/object/LargeObject.java new file mode 100644 index 0000000..66d9ad0 --- /dev/null +++ b/models/src/main/java/info/FrostFS/sdk/dto/object/LargeObject.java @@ -0,0 +1,29 @@ +package info.FrostFS.sdk.dto.object; + +import info.FrostFS.sdk.dto.container.ContainerId; + +import java.security.MessageDigest; + +import static info.FrostFS.sdk.Helper.getSha256Instance; + +public class LargeObject extends ObjectFrostFS { + private final MessageDigest payloadHash; + + public LargeObject(ContainerId cid) { + super(cid, new byte[]{}); + this.payloadHash = getSha256Instance(); + } + + public void appendBlock(byte[] bytes, int count) { + this.getHeader().increasePayloadLength(count); + this.payloadHash.update(bytes, 0, count); + } + + public void calculateHash() { + this.getHeader().setPayloadCheckSum(this.payloadHash.digest()); + } + + public long getPayloadLength() { + return getHeader().getPayloadLength(); + } +} diff --git a/models/src/main/java/info/FrostFS/sdk/dto/object/LinkObject.java b/models/src/main/java/info/FrostFS/sdk/dto/object/LinkObject.java new file mode 100644 index 0000000..e7b8ea4 --- /dev/null +++ b/models/src/main/java/info/FrostFS/sdk/dto/object/LinkObject.java @@ -0,0 +1,21 @@ +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 java.util.List; + +public class LinkObject extends ObjectFrostFS { + + public LinkObject(ContainerId cid, SplitId splitId, LargeObject largeObject) { + super(cid, new byte[]{}); + var split = new Split(splitId); + split.setParentHeader(largeObject.getHeader()); + this.getHeader().setSplit(split); + } + + public void addChildren(List objectIds) { + this.getHeader().getSplit().getChildren().addAll(objectIds); + } +} diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/ObjectAttribute.java b/models/src/main/java/info/FrostFS/sdk/dto/object/ObjectAttribute.java similarity index 82% rename from modelsV2/src/main/java/info/FrostFS/sdk/jdo/ObjectAttribute.java rename to models/src/main/java/info/FrostFS/sdk/dto/object/ObjectAttribute.java index 1a122f4..9729f01 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/ObjectAttribute.java +++ b/models/src/main/java/info/FrostFS/sdk/dto/object/ObjectAttribute.java @@ -1,8 +1,8 @@ -package info.FrostFS.sdk.jdo; +package info.FrostFS.sdk.dto.object; public class ObjectAttribute { - public String key; - public String value; + private String key; + private String value; public ObjectAttribute(String key, String value) { this.key = key; diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/ObjectFilter.java b/models/src/main/java/info/FrostFS/sdk/dto/object/ObjectFilter.java similarity index 82% rename from modelsV2/src/main/java/info/FrostFS/sdk/jdo/ObjectFilter.java rename to models/src/main/java/info/FrostFS/sdk/dto/object/ObjectFilter.java index 0fb1db3..0dae796 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/ObjectFilter.java +++ b/models/src/main/java/info/FrostFS/sdk/dto/object/ObjectFilter.java @@ -1,13 +1,15 @@ -package info.FrostFS.sdk.jdo; +package info.FrostFS.sdk.dto.object; +import info.FrostFS.sdk.dto.OwnerId; +import info.FrostFS.sdk.dto.Version; import info.FrostFS.sdk.enums.ObjectMatchType; public class ObjectFilter { - private static final String HEADER_PREFIX = "Object:"; + private static final String HEADER_PREFIX = "$Object:"; - public ObjectMatchType matchType; - public String key; - public String value; + private ObjectMatchType matchType; + private String key; + private String value; public ObjectFilter(ObjectMatchType matchType, String key, String value) { @@ -17,7 +19,7 @@ public class ObjectFilter { } public static ObjectFilter ObjectIdFilter(ObjectMatchType matchType, ObjectId objectId) { - return new ObjectFilter(matchType, HEADER_PREFIX + "objectID", objectId.value); + return new ObjectFilter(matchType, HEADER_PREFIX + "objectID", objectId.getValue()); } public static ObjectFilter OwnerFilter(ObjectMatchType matchType, OwnerId ownerId) { diff --git a/models/src/main/java/info/FrostFS/sdk/dto/object/ObjectFrostFS.java b/models/src/main/java/info/FrostFS/sdk/dto/object/ObjectFrostFS.java new file mode 100644 index 0000000..3b9d3c9 --- /dev/null +++ b/models/src/main/java/info/FrostFS/sdk/dto/object/ObjectFrostFS.java @@ -0,0 +1,80 @@ +package info.FrostFS.sdk.dto.object; + +import info.FrostFS.sdk.dto.Split; +import info.FrostFS.sdk.dto.container.ContainerId; +import info.FrostFS.sdk.enums.ObjectType; + +import java.util.ArrayList; +import java.util.List; + +import static java.util.Objects.isNull; + +public class ObjectFrostFS { + private ObjectHeader header; + private ObjectId objectId; + private byte[] payload; + + public ObjectFrostFS(ObjectHeader header, ObjectId objectId, byte[] payload) { + this.header = header; + this.objectId = objectId; + this.payload = payload; + } + + public ObjectFrostFS(ContainerId container, byte[] payload) { + this.payload = payload; + this.header = new ObjectHeader(container, new ArrayList<>()); + } + + public ObjectFrostFS(ContainerId container, byte[] payload, ObjectType objectType) { + this.payload = payload; + this.header = new ObjectHeader(container, objectType, new ArrayList<>()); + } + + public ObjectHeader getHeader() { + return header; + } + + public void setHeader(ObjectHeader header) { + this.header = header; + } + + public ObjectId getObjectId() { + return objectId; + } + + private void setObjectId(ObjectId objectId) { + this.objectId = objectId; + } + + public byte[] getPayload() { + return payload; + } + + public void setPayload(byte[] payload) { + this.payload = payload; + } + + public void setSplit(Split split) { + header.setSplit(split); + } + + public void addAttribute(String key, String value) { + header.getAttributes().add(new ObjectAttribute(key, value)); + } + + public void addAttribute(ObjectAttribute attribute) { + header.getAttributes().add(attribute); + } + + public void addAttributes(List attributes) { + header.getAttributes().addAll(attributes); + } + + public void setParent(LargeObject largeObject) { + if (isNull(header.getSplit())) { + throw new IllegalArgumentException("The object is not initialized properly"); + } + + header.getSplit().setParentHeader(largeObject.getHeader()); + } +} diff --git a/models/src/main/java/info/FrostFS/sdk/dto/object/ObjectHeader.java b/models/src/main/java/info/FrostFS/sdk/dto/object/ObjectHeader.java new file mode 100644 index 0000000..401ed41 --- /dev/null +++ b/models/src/main/java/info/FrostFS/sdk/dto/object/ObjectHeader.java @@ -0,0 +1,118 @@ +package info.FrostFS.sdk.dto.object; + +import info.FrostFS.sdk.dto.OwnerId; +import info.FrostFS.sdk.dto.Split; +import info.FrostFS.sdk.dto.Version; +import info.FrostFS.sdk.dto.container.ContainerId; +import info.FrostFS.sdk.enums.ObjectType; + +import java.util.List; + +public class ObjectHeader { + private List attributes; + private ContainerId containerId; + private long size; + private ObjectType objectType; + private Version version; + private OwnerId ownerId; + private long payloadLength; + private byte[] payloadCheckSum; + private Split split; + + public ObjectHeader(ContainerId containerId, ObjectType objectType, + List attributes, long size, Version version) { + this.attributes = attributes; + this.containerId = containerId; + this.size = size; + this.objectType = objectType; + this.version = version; + } + + public ObjectHeader(ContainerId containerId, ObjectType type, List attributes) { + this.attributes = attributes; + this.containerId = containerId; + this.objectType = type; + } + + public ObjectHeader(ContainerId containerId, List attributes) { + this.attributes = attributes; + this.containerId = containerId; + this.objectType = ObjectType.REGULAR; + } + + public OwnerId getOwnerId() { + return ownerId; + } + + public void setOwnerId(OwnerId ownerId) { + this.ownerId = ownerId; + } + + public long getPayloadLength() { + return payloadLength; + } + + public void setPayloadLength(long payloadLength) { + this.payloadLength = payloadLength; + } + + public void increasePayloadLength(long payloadLength) { + this.payloadLength += payloadLength; + } + + public byte[] getPayloadCheckSum() { + return payloadCheckSum; + } + + public void setPayloadCheckSum(byte[] payloadCheckSum) { + this.payloadCheckSum = payloadCheckSum; + } + + public Split getSplit() { + return split; + } + + public void setSplit(Split split) { + this.split = split; + } + + public List getAttributes() { + return attributes; + } + + public void setAttributes(List attributes) { + this.attributes = attributes; + } + + public ContainerId getContainerId() { + return containerId; + } + + public void setContainerId(ContainerId containerId) { + this.containerId = containerId; + } + + public long getSize() { + return size; + } + + public void setSize(long size) { + this.size = size; + } + + public ObjectType getObjectType() { + return objectType; + } + + public void setObjectType(ObjectType objectType) { + this.objectType = objectType; + } + + public Version getVersion() { + return version; + } + + public void setVersion(Version version) { + this.version = version; + } +} diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/ObjectId.java b/models/src/main/java/info/FrostFS/sdk/dto/object/ObjectId.java similarity index 75% rename from modelsV2/src/main/java/info/FrostFS/sdk/jdo/ObjectId.java rename to models/src/main/java/info/FrostFS/sdk/dto/object/ObjectId.java index 438d027..4b36d80 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/ObjectId.java +++ b/models/src/main/java/info/FrostFS/sdk/dto/object/ObjectId.java @@ -1,16 +1,17 @@ -package info.FrostFS.sdk.jdo; +package info.FrostFS.sdk.dto.object; import info.FrostFS.sdk.Base58; +import info.FrostFS.sdk.constants.AppConst; public class ObjectId { - public String value; + private final String value; public ObjectId(String id) { this.value = id; } public static ObjectId fromHash(byte[] hash) { - if (hash.length != Constants.SHA256_HASH_LENGTH) { + if (hash.length != AppConst.SHA256_HASH_LENGTH) { throw new IllegalArgumentException("ObjectId must be a sha256 hash."); } return new ObjectId(Base58.encode(hash)); diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/enums/BasicAcl.java b/models/src/main/java/info/FrostFS/sdk/enums/BasicAcl.java similarity index 93% rename from modelsV2/src/main/java/info/FrostFS/sdk/enums/BasicAcl.java rename to models/src/main/java/info/FrostFS/sdk/enums/BasicAcl.java index b454a68..7ebf7f7 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/enums/BasicAcl.java +++ b/models/src/main/java/info/FrostFS/sdk/enums/BasicAcl.java @@ -28,6 +28,6 @@ public enum BasicAcl { } public static BasicAcl get(int value) { - return ENUM_MAP_BY_VALUE.get(value); + return ENUM_MAP_BY_VALUE.get(value); } } diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/enums/NodeState.java b/models/src/main/java/info/FrostFS/sdk/enums/NodeState.java similarity index 100% rename from modelsV2/src/main/java/info/FrostFS/sdk/enums/NodeState.java rename to models/src/main/java/info/FrostFS/sdk/enums/NodeState.java diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/enums/ObjectMatchType.java b/models/src/main/java/info/FrostFS/sdk/enums/ObjectMatchType.java similarity index 100% rename from modelsV2/src/main/java/info/FrostFS/sdk/enums/ObjectMatchType.java rename to models/src/main/java/info/FrostFS/sdk/enums/ObjectMatchType.java diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/enums/ObjectType.java b/models/src/main/java/info/FrostFS/sdk/enums/ObjectType.java similarity index 100% rename from modelsV2/src/main/java/info/FrostFS/sdk/enums/ObjectType.java rename to models/src/main/java/info/FrostFS/sdk/enums/ObjectType.java diff --git a/models/src/main/java/info/FrostFS/sdk/enums/SignatureScheme.java b/models/src/main/java/info/FrostFS/sdk/enums/SignatureScheme.java new file mode 100644 index 0000000..362c75a --- /dev/null +++ b/models/src/main/java/info/FrostFS/sdk/enums/SignatureScheme.java @@ -0,0 +1,14 @@ +package info.FrostFS.sdk.enums; + +public enum SignatureScheme { + ECDSA_SHA512(0), + ECDSA_RFC6979_SHA256(1), + ECDSA_RFC6979_SHA256_WALLET_CONNECT(2), + ; + + public final int value; + + SignatureScheme(int value) { + this.value = value; + } +} diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/enums/StatusCode.java b/models/src/main/java/info/FrostFS/sdk/enums/StatusCode.java similarity index 100% rename from modelsV2/src/main/java/info/FrostFS/sdk/enums/StatusCode.java rename to models/src/main/java/info/FrostFS/sdk/enums/StatusCode.java diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/MetaHeaderMapper.java b/models/src/main/java/info/FrostFS/sdk/mappers/MetaHeaderMapper.java similarity index 91% rename from modelsV2/src/main/java/info/FrostFS/sdk/mappers/MetaHeaderMapper.java rename to models/src/main/java/info/FrostFS/sdk/mappers/MetaHeaderMapper.java index 2d493c4..96371d7 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/MetaHeaderMapper.java +++ b/models/src/main/java/info/FrostFS/sdk/mappers/MetaHeaderMapper.java @@ -1,7 +1,7 @@ package info.FrostFS.sdk.mappers; import frostFS.session.Types; -import info.FrostFS.sdk.jdo.MetaHeader; +import info.FrostFS.sdk.dto.MetaHeader; public class MetaHeaderMapper { diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/OwnerIdMapper.java b/models/src/main/java/info/FrostFS/sdk/mappers/OwnerIdMapper.java similarity index 89% rename from modelsV2/src/main/java/info/FrostFS/sdk/mappers/OwnerIdMapper.java rename to models/src/main/java/info/FrostFS/sdk/mappers/OwnerIdMapper.java index bc7027c..edf9f1c 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/OwnerIdMapper.java +++ b/models/src/main/java/info/FrostFS/sdk/mappers/OwnerIdMapper.java @@ -2,9 +2,10 @@ package info.FrostFS.sdk.mappers; import com.google.protobuf.ByteString; import frostFS.refs.Types; -import info.FrostFS.sdk.jdo.OwnerId; +import info.FrostFS.sdk.dto.OwnerId; public class OwnerIdMapper { + public static Types.OwnerID toGrpcMessage(OwnerId ownerId) { return Types.OwnerID.newBuilder() .setValue(ByteString.copyFrom(ownerId.toHash())) diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/SessionMapper.java b/models/src/main/java/info/FrostFS/sdk/mappers/SessionMapper.java similarity index 73% rename from modelsV2/src/main/java/info/FrostFS/sdk/mappers/SessionMapper.java rename to models/src/main/java/info/FrostFS/sdk/mappers/SessionMapper.java index f9d7ecd..a8d329d 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/SessionMapper.java +++ b/models/src/main/java/info/FrostFS/sdk/mappers/SessionMapper.java @@ -8,24 +8,22 @@ import java.io.IOException; public class SessionMapper { - public static byte[] serialize(Types.SessionToken token) - { + public static byte[] serialize(Types.SessionToken token) { try { byte[] bytes = new byte[token.getSerializedSize()]; CodedOutputStream stream = CodedOutputStream.newInstance(bytes); token.writeTo(stream); return bytes; - } catch (IOException e) { - throw new IllegalArgumentException(e); + } catch (IOException exp) { + throw new IllegalArgumentException(exp.getMessage()); } } - public static Types.SessionToken deserializeSessionToken(byte[] bytes) - { + public static Types.SessionToken deserializeSessionToken(byte[] bytes) { try { return Types.SessionToken.newBuilder().mergeFrom(bytes).build(); } catch (InvalidProtocolBufferException exp) { - throw new IllegalArgumentException(); + throw new IllegalArgumentException(exp.getMessage()); } } } diff --git a/models/src/main/java/info/FrostFS/sdk/mappers/SignatureMapper.java b/models/src/main/java/info/FrostFS/sdk/mappers/SignatureMapper.java new file mode 100644 index 0000000..3a388d0 --- /dev/null +++ b/models/src/main/java/info/FrostFS/sdk/mappers/SignatureMapper.java @@ -0,0 +1,25 @@ +package info.FrostFS.sdk.mappers; + +import com.google.protobuf.ByteString; +import frostFS.refs.Types; +import info.FrostFS.sdk.dto.Signature; + +import static java.util.Objects.isNull; + +public class SignatureMapper { + + public static Types.Signature ToGrpcMessage(Signature signature) { + var scheme = Types.SignatureScheme.forNumber(signature.getScheme().value); + if (isNull(scheme)) { + throw new IllegalArgumentException( + String.format("Unknown SignatureScheme. Value: %s.", signature.getScheme().name()) + ); + } + + return Types.Signature.newBuilder() + .setKey(ByteString.copyFrom(signature.getKey())) + .setSign(ByteString.copyFrom(signature.getSign())) + .setScheme(scheme) + .build(); + } +} diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/StatusMapper.java b/models/src/main/java/info/FrostFS/sdk/mappers/StatusMapper.java similarity index 94% rename from modelsV2/src/main/java/info/FrostFS/sdk/mappers/StatusMapper.java rename to models/src/main/java/info/FrostFS/sdk/mappers/StatusMapper.java index 6cb68d7..3817f7d 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/StatusMapper.java +++ b/models/src/main/java/info/FrostFS/sdk/mappers/StatusMapper.java @@ -1,12 +1,13 @@ package info.FrostFS.sdk.mappers; import frostFS.status.Types; +import info.FrostFS.sdk.dto.Status; import info.FrostFS.sdk.enums.StatusCode; -import info.FrostFS.sdk.jdo.Status; import static java.util.Objects.isNull; public class StatusMapper { + public static Status toModel(Types.Status status) { if (isNull(status)) return new Status(StatusCode.SUCCESS); @@ -15,7 +16,6 @@ public class StatusMapper { throw new IllegalArgumentException( String.format("Unknown StatusCode. Value: %s.", status.getCode()) ); - } return new Status(statusCode, status.getMessage()); diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/VersionMapper.java b/models/src/main/java/info/FrostFS/sdk/mappers/VersionMapper.java similarity index 92% rename from modelsV2/src/main/java/info/FrostFS/sdk/mappers/VersionMapper.java rename to models/src/main/java/info/FrostFS/sdk/mappers/VersionMapper.java index 339746a..fefb7ba 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/VersionMapper.java +++ b/models/src/main/java/info/FrostFS/sdk/mappers/VersionMapper.java @@ -1,7 +1,7 @@ package info.FrostFS.sdk.mappers; import frostFS.refs.Types; -import info.FrostFS.sdk.jdo.Version; +import info.FrostFS.sdk.dto.Version; public class VersionMapper { diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/ContainerIdMapper.java b/models/src/main/java/info/FrostFS/sdk/mappers/container/ContainerIdMapper.java similarity index 73% rename from modelsV2/src/main/java/info/FrostFS/sdk/mappers/ContainerIdMapper.java rename to models/src/main/java/info/FrostFS/sdk/mappers/container/ContainerIdMapper.java index bab5b61..b661e71 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/ContainerIdMapper.java +++ b/models/src/main/java/info/FrostFS/sdk/mappers/container/ContainerIdMapper.java @@ -1,14 +1,12 @@ -package info.FrostFS.sdk.mappers; +package info.FrostFS.sdk.mappers.container; import com.google.protobuf.ByteString; import frostFS.refs.Types; -import info.FrostFS.sdk.jdo.ContainerId; +import info.FrostFS.sdk.dto.container.ContainerId; public class ContainerIdMapper { public static Types.ContainerID toGrpcMessage(ContainerId containerId) { - var test = containerId.toHash(); - return Types.ContainerID.newBuilder() .setValue(ByteString.copyFrom(containerId.toHash())) .build(); diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/ContainerMapper.java b/models/src/main/java/info/FrostFS/sdk/mappers/container/ContainerMapper.java similarity index 60% rename from modelsV2/src/main/java/info/FrostFS/sdk/mappers/ContainerMapper.java rename to models/src/main/java/info/FrostFS/sdk/mappers/container/ContainerMapper.java index 4f1d886..9895eee 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/ContainerMapper.java +++ b/models/src/main/java/info/FrostFS/sdk/mappers/container/ContainerMapper.java @@ -1,10 +1,10 @@ -package info.FrostFS.sdk.mappers; - +package info.FrostFS.sdk.mappers.container; import com.google.protobuf.ByteString; import frostFS.container.Types; +import info.FrostFS.sdk.dto.container.Container; import info.FrostFS.sdk.enums.BasicAcl; -import info.FrostFS.sdk.jdo.Container; +import info.FrostFS.sdk.mappers.VersionMapper; import info.FrostFS.sdk.mappers.netmap.PlacementPolicyMapper; import static info.FrostFS.sdk.UUIDExtension.asBytes; @@ -21,19 +21,17 @@ public class ContainerMapper { .build(); } - public static Container toModel(Types.Container container) { - var basicAcl = BasicAcl.get(container.getBasicAcl()); + public static Container toModel(Types.Container containerGrpc) { + var basicAcl = BasicAcl.get(containerGrpc.getBasicAcl()); if (isNull(basicAcl)) { throw new IllegalArgumentException( - String.format("Unknown BasicACL rule. Value: %s.", container.getBasicAcl()) + String.format("Unknown BasicACL rule. Value: %s.", containerGrpc.getBasicAcl()) ); } - return new Container( - basicAcl, - PlacementPolicyMapper.toModel(container.getPlacementPolicy()), - asUuid(container.getNonce().toByteArray()), - VersionMapper.toModel(container.getVersion()) - ); + var container = new Container(basicAcl, PlacementPolicyMapper.toModel(containerGrpc.getPlacementPolicy())); + container.setNonce(asUuid(containerGrpc.getNonce().toByteArray())); + container.setVersion(VersionMapper.toModel(containerGrpc.getVersion())); + return container; } } diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/netmap/NetmapSnapshotMapper.java b/models/src/main/java/info/FrostFS/sdk/mappers/netmap/NetmapSnapshotMapper.java similarity index 74% rename from modelsV2/src/main/java/info/FrostFS/sdk/mappers/netmap/NetmapSnapshotMapper.java rename to models/src/main/java/info/FrostFS/sdk/mappers/netmap/NetmapSnapshotMapper.java index 9d9eb27..d18fc32 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/netmap/NetmapSnapshotMapper.java +++ b/models/src/main/java/info/FrostFS/sdk/mappers/netmap/NetmapSnapshotMapper.java @@ -1,7 +1,7 @@ package info.FrostFS.sdk.mappers.netmap; import frostFS.netmap.Service; -import info.FrostFS.sdk.jdo.netmap.NetmapSnapshot; +import info.FrostFS.sdk.dto.netmap.NetmapSnapshot; import java.util.stream.Collectors; @@ -11,7 +11,7 @@ public class NetmapSnapshotMapper { return new NetmapSnapshot( netmap.getBody().getNetmap().getEpoch(), netmap.getBody().getNetmap().getNodesList().stream() - .map(t -> NodeInfoMapper.toModel(t, netmap.getMetaHeader().getVersion())) + .map(node -> NodeInfoMapper.toModel(node, netmap.getMetaHeader().getVersion())) .collect(Collectors.toList()) ); } diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/netmap/NodeInfoMapper.java b/models/src/main/java/info/FrostFS/sdk/mappers/netmap/NodeInfoMapper.java similarity index 96% rename from modelsV2/src/main/java/info/FrostFS/sdk/mappers/netmap/NodeInfoMapper.java rename to models/src/main/java/info/FrostFS/sdk/mappers/netmap/NodeInfoMapper.java index 6c9e25c..53eca35 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/netmap/NodeInfoMapper.java +++ b/models/src/main/java/info/FrostFS/sdk/mappers/netmap/NodeInfoMapper.java @@ -3,8 +3,8 @@ package info.FrostFS.sdk.mappers.netmap; import frostFS.netmap.Service; import frostFS.netmap.Types.NodeInfo.Attribute; import frostFS.refs.Types; +import info.FrostFS.sdk.dto.netmap.NodeInfo; import info.FrostFS.sdk.enums.NodeState; -import info.FrostFS.sdk.jdo.netmap.NodeInfo; import info.FrostFS.sdk.mappers.VersionMapper; import java.util.stream.Collectors; @@ -12,13 +12,13 @@ import java.util.stream.Collectors; import static java.util.Objects.isNull; public class NodeInfoMapper { + public static NodeInfo toModel(Service.LocalNodeInfoResponse.Body nodeInfo) { return toModel(nodeInfo.getNodeInfo(), nodeInfo.getVersion()); } public static NodeInfo toModel(frostFS.netmap.Types.NodeInfo nodeInfo, Types.Version version) { NodeState nodeState = NodeState.get(nodeInfo.getState().getNumber()); - if (isNull(nodeState)) { throw new IllegalArgumentException( String.format("Unknown NodeState. Value: %s.", nodeInfo.getState()) diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/netmap/PlacementPolicyMapper.java b/models/src/main/java/info/FrostFS/sdk/mappers/netmap/PlacementPolicyMapper.java similarity index 88% rename from modelsV2/src/main/java/info/FrostFS/sdk/mappers/netmap/PlacementPolicyMapper.java rename to models/src/main/java/info/FrostFS/sdk/mappers/netmap/PlacementPolicyMapper.java index 871dfcb..e2803d6 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/netmap/PlacementPolicyMapper.java +++ b/models/src/main/java/info/FrostFS/sdk/mappers/netmap/PlacementPolicyMapper.java @@ -1,10 +1,11 @@ package info.FrostFS.sdk.mappers.netmap; import frostFS.netmap.Types; -import info.FrostFS.sdk.jdo.netmap.PlacementPolicy; -import info.FrostFS.sdk.jdo.netmap.Replica; +import info.FrostFS.sdk.dto.netmap.PlacementPolicy; +import info.FrostFS.sdk.dto.netmap.Replica; public class PlacementPolicyMapper { + public static Types.PlacementPolicy toGrpcMessage(PlacementPolicy placementPolicy) { var pp = Types.PlacementPolicy.newBuilder() .setUnique(placementPolicy.isUnique()); diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/netmap/ReplicaMapper.java b/models/src/main/java/info/FrostFS/sdk/mappers/netmap/ReplicaMapper.java similarity index 91% rename from modelsV2/src/main/java/info/FrostFS/sdk/mappers/netmap/ReplicaMapper.java rename to models/src/main/java/info/FrostFS/sdk/mappers/netmap/ReplicaMapper.java index 93735ff..b2929ba 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/netmap/ReplicaMapper.java +++ b/models/src/main/java/info/FrostFS/sdk/mappers/netmap/ReplicaMapper.java @@ -1,9 +1,10 @@ package info.FrostFS.sdk.mappers.netmap; import frostFS.netmap.Types; -import info.FrostFS.sdk.jdo.netmap.Replica; +import info.FrostFS.sdk.dto.netmap.Replica; public class ReplicaMapper { + public static Types.Replica toGrpcMessage(Replica replica) { return Types.Replica.newBuilder() .setCount(replica.getCount()) diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/ObjectAttributeMapper.java b/models/src/main/java/info/FrostFS/sdk/mappers/object/ObjectAttributeMapper.java similarity index 83% rename from modelsV2/src/main/java/info/FrostFS/sdk/mappers/ObjectAttributeMapper.java rename to models/src/main/java/info/FrostFS/sdk/mappers/object/ObjectAttributeMapper.java index 45489a5..7604e0f 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/ObjectAttributeMapper.java +++ b/models/src/main/java/info/FrostFS/sdk/mappers/object/ObjectAttributeMapper.java @@ -1,9 +1,10 @@ -package info.FrostFS.sdk.mappers; +package info.FrostFS.sdk.mappers.object; import frostFS.object.Types; -import info.FrostFS.sdk.jdo.ObjectAttribute; +import info.FrostFS.sdk.dto.object.ObjectAttribute; public class ObjectAttributeMapper { + public static Types.Header.Attribute toGrpcMessage(ObjectAttribute attribute) { return Types.Header.Attribute.newBuilder() .setKey(attribute.getKey()) diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/ObjectFilterMapper.java b/models/src/main/java/info/FrostFS/sdk/mappers/object/ObjectFilterMapper.java similarity index 86% rename from modelsV2/src/main/java/info/FrostFS/sdk/mappers/ObjectFilterMapper.java rename to models/src/main/java/info/FrostFS/sdk/mappers/object/ObjectFilterMapper.java index 328de81..52adf8d 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/ObjectFilterMapper.java +++ b/models/src/main/java/info/FrostFS/sdk/mappers/object/ObjectFilterMapper.java @@ -1,13 +1,13 @@ -package info.FrostFS.sdk.mappers; +package info.FrostFS.sdk.mappers.object; import frostFS.object.Service; import frostFS.object.Types; -import info.FrostFS.sdk.jdo.ObjectFilter; -import org.apache.commons.lang3.EnumUtils; +import info.FrostFS.sdk.dto.object.ObjectFilter; import static java.util.Objects.isNull; public class ObjectFilterMapper { + public static Service.SearchRequest.Body.Filter toGrpcMessage(ObjectFilter filter) { var objectMatchType = Types.MatchType.forNumber(filter.getMatchType().value); if (isNull(objectMatchType)) { diff --git a/models/src/main/java/info/FrostFS/sdk/mappers/object/ObjectFrostFSMapper.java b/models/src/main/java/info/FrostFS/sdk/mappers/object/ObjectFrostFSMapper.java new file mode 100644 index 0000000..22cc305 --- /dev/null +++ b/models/src/main/java/info/FrostFS/sdk/mappers/object/ObjectFrostFSMapper.java @@ -0,0 +1,16 @@ +package info.FrostFS.sdk.mappers.object; + +import frostFS.object.Types; +import info.FrostFS.sdk.dto.object.ObjectFrostFS; +import info.FrostFS.sdk.dto.object.ObjectId; + +public class ObjectFrostFSMapper { + + public static ObjectFrostFS toModel(Types.Object obj) { + return new ObjectFrostFS( + ObjectHeaderMapper.toModel(obj.getHeader()), + ObjectId.fromHash(obj.getObjectId().getValue().toByteArray()), + obj.getPayload().toByteArray() + ); + } +} diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/ObjectHeaderMapper.java b/models/src/main/java/info/FrostFS/sdk/mappers/object/ObjectHeaderMapper.java similarity index 75% rename from modelsV2/src/main/java/info/FrostFS/sdk/mappers/ObjectHeaderMapper.java rename to models/src/main/java/info/FrostFS/sdk/mappers/object/ObjectHeaderMapper.java index ba38b14..d47dc55 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/ObjectHeaderMapper.java +++ b/models/src/main/java/info/FrostFS/sdk/mappers/object/ObjectHeaderMapper.java @@ -1,17 +1,21 @@ -package info.FrostFS.sdk.mappers; +package info.FrostFS.sdk.mappers.object; import frostFS.object.Types; +import info.FrostFS.sdk.dto.container.ContainerId; +import info.FrostFS.sdk.dto.object.ObjectAttribute; +import info.FrostFS.sdk.dto.object.ObjectHeader; import info.FrostFS.sdk.enums.ObjectType; -import info.FrostFS.sdk.jdo.ContainerId; -import info.FrostFS.sdk.jdo.ObjectAttribute; -import info.FrostFS.sdk.jdo.ObjectHeader; -import org.apache.commons.lang3.EnumUtils; +import info.FrostFS.sdk.mappers.VersionMapper; +import info.FrostFS.sdk.mappers.container.ContainerIdMapper; + +import java.util.stream.Collectors; import static java.util.Objects.isNull; public class ObjectHeaderMapper { + public static Types.Header toGrpcMessage(ObjectHeader header) { - var objectType = EnumUtils.getEnum(Types.ObjectType.class, header.getObjectType().name()); + var objectType = Types.ObjectType.forNumber(header.getObjectType().value); if (isNull(objectType)) { throw new IllegalArgumentException( String.format("Unknown ObjectType. Value: %s.", header.getObjectType().name()) @@ -40,7 +44,7 @@ public class ObjectHeaderMapper { return new ObjectHeader( ContainerId.fromHash(header.getContainerId().getValue().toByteArray()), objectType, - header.getAttributesList().stream().map(ObjectAttributeMapper::toModel).toArray(ObjectAttribute[]::new), + header.getAttributesList().stream().map(ObjectAttributeMapper::toModel).collect(Collectors.toList()), header.getPayloadLength(), VersionMapper.toModel(header.getVersion()) ); diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/ObjectIdMapper.java b/models/src/main/java/info/FrostFS/sdk/mappers/object/ObjectIdMapper.java similarity index 57% rename from modelsV2/src/main/java/info/FrostFS/sdk/mappers/ObjectIdMapper.java rename to models/src/main/java/info/FrostFS/sdk/mappers/object/ObjectIdMapper.java index 42200e2..437b4bb 100644 --- a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/ObjectIdMapper.java +++ b/models/src/main/java/info/FrostFS/sdk/mappers/object/ObjectIdMapper.java @@ -1,13 +1,18 @@ -package info.FrostFS.sdk.mappers; +package info.FrostFS.sdk.mappers.object; import com.google.protobuf.ByteString; import frostFS.refs.Types; -import info.FrostFS.sdk.jdo.ObjectId; +import info.FrostFS.sdk.dto.object.ObjectId; public class ObjectIdMapper { + public static Types.ObjectID toGrpcMessage(ObjectId objectId) { return Types.ObjectID.newBuilder() .setValue(ByteString.copyFrom(objectId.toHash())) .build(); } + + public static ObjectId toModel(Types.ObjectID objectId) { + return ObjectId.fromHash(objectId.getValue().toByteArray()); + } } diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/ObjectFrostFs.java b/modelsV2/src/main/java/info/FrostFS/sdk/jdo/ObjectFrostFs.java deleted file mode 100644 index c7cc689..0000000 --- a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/ObjectFrostFs.java +++ /dev/null @@ -1,37 +0,0 @@ -package info.FrostFS.sdk.jdo; - -public class ObjectFrostFs { - public ObjectHeader header; - public ObjectId objectId; - public byte[] payload; - - public ObjectFrostFs(ObjectHeader header, ObjectId objectId, byte[] payload) { - this.header = header; - this.objectId = objectId; - this.payload = payload; - } - - public ObjectHeader getHeader() { - return header; - } - - public void setHeader(ObjectHeader header) { - this.header = header; - } - - public ObjectId getObjectId() { - return objectId; - } - - public void setObjectId(ObjectId objectId) { - this.objectId = objectId; - } - - public byte[] getPayload() { - return payload; - } - - public void setPayload(byte[] payload) { - this.payload = payload; - } -} diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/ObjectHeader.java b/modelsV2/src/main/java/info/FrostFS/sdk/jdo/ObjectHeader.java deleted file mode 100644 index 7dc795c..0000000 --- a/modelsV2/src/main/java/info/FrostFS/sdk/jdo/ObjectHeader.java +++ /dev/null @@ -1,71 +0,0 @@ -package info.FrostFS.sdk.jdo; - -import info.FrostFS.sdk.enums.ObjectType; - -public class ObjectHeader { - public ObjectAttribute[] attributes; - public ContainerId containerId; - public long size; - public ObjectType objectType; - public Version version; - - public ObjectHeader(ContainerId containerId, ObjectType objectType, ObjectAttribute[] attributes, long size, Version version) { - this.attributes = attributes; - this.containerId = containerId; - this.size = size; - this.objectType = objectType; - this.version = version; - } - - public ObjectHeader(ContainerId containerId, ObjectType type, ObjectAttribute[] attributes) { - this.attributes = attributes; - this.containerId = containerId; - this.objectType = type; - } - - public ObjectHeader(ContainerId containerId, ObjectAttribute[] attributes) { - this.attributes = attributes; - this.containerId = containerId; - this.objectType = ObjectType.REGULAR; - } - - public ObjectAttribute[] getAttributes() { - return attributes; - } - - public void setAttributes(ObjectAttribute[] attributes) { - this.attributes = attributes; - } - - public ContainerId getContainerId() { - return containerId; - } - - public void setContainerId(ContainerId containerId) { - this.containerId = containerId; - } - - public long getSize() { - return size; - } - - public void setSize(long size) { - this.size = size; - } - - public ObjectType getObjectType() { - return objectType; - } - - public void setObjectType(ObjectType objectType) { - this.objectType = objectType; - } - - public Version getVersion() { - return version; - } - - public void setVersion(Version version) { - this.version = version; - } -} diff --git a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/ObjectMapper.java b/modelsV2/src/main/java/info/FrostFS/sdk/mappers/ObjectMapper.java deleted file mode 100644 index b970456..0000000 --- a/modelsV2/src/main/java/info/FrostFS/sdk/mappers/ObjectMapper.java +++ /dev/null @@ -1,15 +0,0 @@ -package info.FrostFS.sdk.mappers; - -import frostFS.object.Types; -import info.FrostFS.sdk.jdo.ObjectFrostFs; -import info.FrostFS.sdk.jdo.ObjectId; - -public class ObjectMapper { - public static ObjectFrostFs toModel(Types.Object obj) { - return new ObjectFrostFs( - ObjectHeaderMapper.toModel(obj.getHeader()), - ObjectId.fromHash(obj.getObjectId().getValue().toByteArray()), - obj.getPayload().toByteArray() - ); - } -} diff --git a/pom.xml b/pom.xml index 5c16e71..8423cfb 100644 --- a/pom.xml +++ b/pom.xml @@ -9,10 +9,10 @@ 0.1.0 pom - protosV2 client cryptography - modelsV2 + models + protos diff --git a/protosV2/pom.xml b/protos/pom.xml similarity index 98% rename from protosV2/pom.xml rename to protos/pom.xml index d7d8630..cee5fb0 100644 --- a/protosV2/pom.xml +++ b/protos/pom.xml @@ -9,13 +9,13 @@ 0.1.0 - protosV2 + protos 11 11 3.23.0 - 1.62.2 + 1.65.1 UTF-8 diff --git a/protosV2/src/main/proto/accounting/service.proto b/protos/src/main/proto/accounting/service.proto similarity index 100% rename from protosV2/src/main/proto/accounting/service.proto rename to protos/src/main/proto/accounting/service.proto diff --git a/protosV2/src/main/proto/accounting/types.proto b/protos/src/main/proto/accounting/types.proto similarity index 100% rename from protosV2/src/main/proto/accounting/types.proto rename to protos/src/main/proto/accounting/types.proto diff --git a/protosV2/src/main/proto/acl/types.proto b/protos/src/main/proto/acl/types.proto similarity index 100% rename from protosV2/src/main/proto/acl/types.proto rename to protos/src/main/proto/acl/types.proto diff --git a/protosV2/src/main/proto/apemanager/service.proto b/protos/src/main/proto/apemanager/service.proto similarity index 100% rename from protosV2/src/main/proto/apemanager/service.proto rename to protos/src/main/proto/apemanager/service.proto diff --git a/protosV2/src/main/proto/apemanager/types.proto b/protos/src/main/proto/apemanager/types.proto similarity index 100% rename from protosV2/src/main/proto/apemanager/types.proto rename to protos/src/main/proto/apemanager/types.proto diff --git a/protosV2/src/main/proto/container/service.proto b/protos/src/main/proto/container/service.proto similarity index 100% rename from protosV2/src/main/proto/container/service.proto rename to protos/src/main/proto/container/service.proto diff --git a/protosV2/src/main/proto/container/types.proto b/protos/src/main/proto/container/types.proto similarity index 100% rename from protosV2/src/main/proto/container/types.proto rename to protos/src/main/proto/container/types.proto diff --git a/protosV2/src/main/proto/lock/types.proto b/protos/src/main/proto/lock/types.proto similarity index 100% rename from protosV2/src/main/proto/lock/types.proto rename to protos/src/main/proto/lock/types.proto diff --git a/protosV2/src/main/proto/netmap/service.proto b/protos/src/main/proto/netmap/service.proto similarity index 100% rename from protosV2/src/main/proto/netmap/service.proto rename to protos/src/main/proto/netmap/service.proto diff --git a/protosV2/src/main/proto/netmap/types.proto b/protos/src/main/proto/netmap/types.proto similarity index 100% rename from protosV2/src/main/proto/netmap/types.proto rename to protos/src/main/proto/netmap/types.proto diff --git a/protosV2/src/main/proto/object/service.proto b/protos/src/main/proto/object/service.proto similarity index 100% rename from protosV2/src/main/proto/object/service.proto rename to protos/src/main/proto/object/service.proto diff --git a/protosV2/src/main/proto/object/types.proto b/protos/src/main/proto/object/types.proto similarity index 100% rename from protosV2/src/main/proto/object/types.proto rename to protos/src/main/proto/object/types.proto diff --git a/protosV2/src/main/proto/refs/types.proto b/protos/src/main/proto/refs/types.proto similarity index 100% rename from protosV2/src/main/proto/refs/types.proto rename to protos/src/main/proto/refs/types.proto diff --git a/protosV2/src/main/proto/session/service.proto b/protos/src/main/proto/session/service.proto similarity index 100% rename from protosV2/src/main/proto/session/service.proto rename to protos/src/main/proto/session/service.proto diff --git a/protosV2/src/main/proto/session/types.proto b/protos/src/main/proto/session/types.proto similarity index 100% rename from protosV2/src/main/proto/session/types.proto rename to protos/src/main/proto/session/types.proto diff --git a/protosV2/src/main/proto/status/types.proto b/protos/src/main/proto/status/types.proto similarity index 100% rename from protosV2/src/main/proto/status/types.proto rename to protos/src/main/proto/status/types.proto diff --git a/protosV2/src/main/proto/tombstone/types.proto b/protos/src/main/proto/tombstone/types.proto similarity index 100% rename from protosV2/src/main/proto/tombstone/types.proto rename to protos/src/main/proto/tombstone/types.proto