Skip to content

Commit

Permalink
Refactor messages by subclasses
Browse files Browse the repository at this point in the history
merge SelectObjectContentRequest
merge ListVersionsResult
Remove unused CompleteMultipartUploadOutput.java
merge ListMultipartUploadsResult
merge NotificationRecords
merge SseConfiguration
merge DeleteRequest, DeleteResult
merge RestoreRequest
merge NotificationConfiguration
merge LifecycleConfiguration
merge ReplicationConfiguration
merge Filter
merge ObjectLockConfiguration
merge ListAllMyBucketsResult
merge Item
merge ListObjectsResult
Move common fields for ListPartsResult and GetObjectAttributesOutput

Signed-off-by: Bala.FA <[email protected]>
  • Loading branch information
balamurugana committed Feb 15, 2025
1 parent 5e7e074 commit 17a9645
Show file tree
Hide file tree
Showing 194 changed files with 4,722 additions and 6,131 deletions.
40 changes: 19 additions & 21 deletions adminapi/src/main/java/io/minio/admin/MinioAdminClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@
import com.google.common.collect.Multimap;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.minio.Digest;
import io.minio.MinioProperties;
import io.minio.S3Escaper;
import io.minio.Signer;
import io.minio.Time;
import io.minio.Utils;
import io.minio.admin.messages.DataUsageInfo;
import io.minio.admin.messages.info.Message;
import io.minio.credentials.Credentials;
Expand Down Expand Up @@ -115,7 +114,7 @@ public String toString() {
OBJECT_MAPPER.registerModule(new JavaTimeModule());
}

private String userAgent = MinioProperties.INSTANCE.getDefaultUserAgent();
private String userAgent = Utils.getDefaultUserAgent();
private PrintWriter traceStream;

private HttpUrl baseUrl;
Expand Down Expand Up @@ -146,26 +145,26 @@ private Response execute(
this.baseUrl
.newBuilder()
.host(this.baseUrl.host())
.addEncodedPathSegments(S3Escaper.encodePath("minio/admin/v3/" + command.toString()));
.addEncodedPathSegments(Utils.encodePath("minio/admin/v3/" + command.toString()));
if (queryParamMap != null) {
for (Map.Entry<String, String> entry : queryParamMap.entries()) {
urlBuilder.addEncodedQueryParameter(
S3Escaper.encode(entry.getKey()), S3Escaper.encode(entry.getValue()));
Utils.encode(entry.getKey()), Utils.encode(entry.getValue()));
}
}
HttpUrl url = urlBuilder.build();

Request.Builder requestBuilder = new Request.Builder();
requestBuilder.url(url);
requestBuilder.header("Host", HttpUtils.getHostHeader(url));
requestBuilder.header("Host", Utils.getHostHeader(url));
requestBuilder.header("Accept-Encoding", "identity"); // Disable default gzip compression.
requestBuilder.header("User-Agent", this.userAgent);
requestBuilder.header("x-amz-date", ZonedDateTime.now().format(Time.AMZ_DATE_FORMAT));
if (creds.sessionToken() != null) {
requestBuilder.header("X-Amz-Security-Token", creds.sessionToken());
}
if (body == null && (method != Method.GET && method != Method.HEAD)) {
body = HttpUtils.EMPTY_BODY;
body = Utils.EMPTY_BODY;
}
if (body != null) {
requestBuilder.header("x-amz-content-sha256", Digest.sha256Hash(body, body.length));
Expand Down Expand Up @@ -685,7 +684,7 @@ public Credentials addServiceAccount(
serviceAccount.put("description", description);
}
if (expiration != null) {
serviceAccount.put("expiration", expiration.format(Time.EXPIRATION_DATE_FORMAT));
serviceAccount.put("expiration", expiration.format(Time.ISO8601UTC_FORMAT));
}

Credentials creds = getCredentials();
Expand Down Expand Up @@ -752,7 +751,7 @@ public void updateServiceAccount(
serviceAccount.put("newDescription", newDescription);
}
if (newExpiration != null) {
serviceAccount.put("newExpiration", newExpiration.format(Time.EXPIRATION_DATE_FORMAT));
serviceAccount.put("newExpiration", newExpiration.format(Time.ISO8601UTC_FORMAT));
}

Credentials creds = getCredentials();
Expand Down Expand Up @@ -885,8 +884,7 @@ public void ignoreCertCheck() throws KeyManagementException, NoSuchAlgorithmExce
*/
public void setAppInfo(String name, String version) {
if (name == null || version == null) return;
this.userAgent =
MinioProperties.INSTANCE.getDefaultUserAgent() + " " + name.trim() + "/" + version.trim();
this.userAgent = Utils.getDefaultUserAgent() + " " + name.trim() + "/" + version.trim();
}

/**
Expand Down Expand Up @@ -923,12 +921,12 @@ public static final class Builder {
private OkHttpClient httpClient;

public Builder endpoint(String endpoint) {
this.baseUrl = HttpUtils.getBaseUrl(endpoint);
this.baseUrl = Utils.getBaseUrl(endpoint);
return this;
}

public Builder endpoint(String endpoint, int port, boolean secure) {
HttpUrl url = HttpUtils.getBaseUrl(endpoint);
HttpUrl url = Utils.getBaseUrl(endpoint);
if (port < 1 || port > 65535) {
throw new IllegalArgumentException("port must be in range of 1 to 65535");
}
Expand All @@ -938,20 +936,20 @@ public Builder endpoint(String endpoint, int port, boolean secure) {
}

public Builder endpoint(HttpUrl url) {
HttpUtils.validateNotNull(url, "url");
HttpUtils.validateUrl(url);
Utils.validateNotNull(url, "url");
Utils.validateUrl(url);

this.baseUrl = url;
return this;
}

public Builder endpoint(URL url) {
HttpUtils.validateNotNull(url, "url");
Utils.validateNotNull(url, "url");
return endpoint(HttpUrl.get(url));
}

public Builder region(String region) {
HttpUtils.validateNotNull(region, "region");
Utils.validateNotNull(region, "region");
this.region = region;
return this;
}
Expand All @@ -962,20 +960,20 @@ public Builder credentials(String accessKey, String secretKey) {
}

public Builder credentialsProvider(Provider provider) {
HttpUtils.validateNotNull(provider, "credential provider");
Utils.validateNotNull(provider, "credential provider");
this.provider = provider;
return this;
}

public Builder httpClient(OkHttpClient httpClient) {
HttpUtils.validateNotNull(httpClient, "http client");
Utils.validateNotNull(httpClient, "http client");
this.httpClient = httpClient;
return this;
}

public MinioAdminClient build() {
HttpUtils.validateNotNull(baseUrl, "base url");
HttpUtils.validateNotNull(provider, "credential provider");
Utils.validateNotNull(baseUrl, "base url");
Utils.validateNotNull(provider, "credential provider");
if (httpClient == null) {
httpClient =
HttpUtils.newDefaultHttpClient(
Expand Down
49 changes: 4 additions & 45 deletions api/src/main/java/io/minio/BaseArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,75 +60,34 @@ public abstract static class Builder<B extends Builder<B, A>, A extends BaseArgs

protected abstract void validate(A args);

protected void validateNotNull(Object arg, String argName) {
if (arg == null) {
throw new IllegalArgumentException(argName + " must not be null.");
}
}

protected void validateNotEmptyString(String arg, String argName) {
validateNotNull(arg, argName);
if (arg.isEmpty()) {
throw new IllegalArgumentException(argName + " must be a non-empty string.");
}
}

protected void validateNullOrNotEmptyString(String arg, String argName) {
if (arg != null && arg.isEmpty()) {
throw new IllegalArgumentException(argName + " must be a non-empty string.");
}
}

protected void validateNullOrPositive(Number arg, String argName) {
if (arg != null && arg.longValue() < 0) {
throw new IllegalArgumentException(argName + " cannot be non-negative.");
}
}

public Builder() {
this.operations = new ArrayList<>();
}

protected Multimap<String, String> copyMultimap(Multimap<String, String> multimap) {
Multimap<String, String> multimapCopy = HashMultimap.create();
if (multimap != null) {
multimapCopy.putAll(multimap);
}
return Multimaps.unmodifiableMultimap(multimapCopy);
}

protected Multimap<String, String> toMultimap(Map<String, String> map) {
Multimap<String, String> multimap = HashMultimap.create();
if (map != null) {
multimap.putAll(Multimaps.forMap(map));
}
return Multimaps.unmodifiableMultimap(multimap);
}

@SuppressWarnings("unchecked") // Its safe to type cast to B as B extends this class.
public B extraHeaders(Multimap<String, String> headers) {
final Multimap<String, String> extraHeaders = copyMultimap(headers);
final Multimap<String, String> extraHeaders = Utils.newMultimap(headers);
operations.add(args -> args.extraHeaders = extraHeaders);
return (B) this;
}

@SuppressWarnings("unchecked") // Its safe to type cast to B as B extends this class.
public B extraQueryParams(Multimap<String, String> queryParams) {
final Multimap<String, String> extraQueryParams = copyMultimap(queryParams);
final Multimap<String, String> extraQueryParams = Utils.newMultimap(queryParams);
operations.add(args -> args.extraQueryParams = extraQueryParams);
return (B) this;
}

@SuppressWarnings("unchecked") // Its safe to type cast to B as B extends this class.
public B extraHeaders(Map<String, String> headers) {
final Multimap<String, String> extraHeaders = toMultimap(headers);
final Multimap<String, String> extraHeaders = Utils.newMultimap(headers);
operations.add(args -> args.extraHeaders = extraHeaders);
return (B) this;
}

@SuppressWarnings("unchecked") // Its safe to type cast to B as B extends this class.
public B extraQueryParams(Map<String, String> queryParams) {
final Multimap<String, String> extraQueryParams = toMultimap(queryParams);
final Multimap<String, String> extraQueryParams = Utils.newMultimap(queryParams);
operations.add(args -> args.extraQueryParams = extraQueryParams);
return (B) this;
}
Expand Down
8 changes: 3 additions & 5 deletions api/src/main/java/io/minio/BucketArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package io.minio;

import io.minio.http.HttpUtils;
import io.minio.org.apache.commons.validator.routines.InetAddressValidator;
import java.util.Objects;
import java.util.regex.Pattern;

Expand All @@ -42,7 +40,7 @@ public abstract static class Builder<B extends Builder<B, A>, A extends BucketAr
protected boolean skipValidation = false;

protected void validateBucketName(String name) {
validateNotNull(name, "bucket name");
Utils.validateNotNull(name, "bucket name");
if (skipValidation) {
return;
}
Expand All @@ -55,7 +53,7 @@ protected void validateBucketName(String name) {
+ "https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html");
}

if (InetAddressValidator.getInstance().isValidInet4Address(name)) {
if (Utils.isValidIPv4(name)) {
throw new IllegalArgumentException(
"bucket name '" + name + "' must not be formatted as an IP address");
}
Expand All @@ -67,7 +65,7 @@ protected void validateBucketName(String name) {
}

private void validateRegion(String region) {
if (!skipValidation && region != null && !HttpUtils.REGION_REGEX.matcher(region).find()) {
if (!skipValidation && region != null && !Utils.REGION_REGEX.matcher(region).find()) {
throw new IllegalArgumentException("invalid region " + region);
}
}
Expand Down
4 changes: 2 additions & 2 deletions api/src/main/java/io/minio/CopyObjectArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static final class Builder extends ObjectWriteArgs.Builder<Builder, CopyO
@Override
protected void validate(CopyObjectArgs args) {
super.validate(args);
validateNotNull(args.source, "copy source");
Utils.validateNotNull(args.source, "copy source");
if (args.source.offset() != null || args.source.length() != null) {
if (args.metadataDirective != null && args.metadataDirective == Directive.COPY) {
throw new IllegalArgumentException(
Expand All @@ -83,7 +83,7 @@ protected void validate(CopyObjectArgs args) {
}

public Builder source(CopySource source) {
validateNotNull(source, "copy source");
Utils.validateNotNull(source, "copy source");
operations.add(args -> args.source = source);
return this;
}
Expand Down
101 changes: 0 additions & 101 deletions api/src/main/java/io/minio/Digest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
package io.minio;

import com.google.common.io.BaseEncoding;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -58,100 +53,4 @@ public static String sha256Hash(String string) throws NoSuchAlgorithmException {
byte[] data = string.getBytes(StandardCharsets.UTF_8);
return sha256Hash(data, data.length);
}

/**
* Returns SHA-256 and MD5 hashes of given data and it's length.
*
* @param data must be {@link RandomAccessFile}, {@link BufferedInputStream} or byte array.
* @param len length of data to be read for hash calculation.
* @deprecated This method is no longer supported.
*/
@Deprecated
public static String[] sha256Md5Hashes(Object data, int len)
throws NoSuchAlgorithmException, IOException, InsufficientDataException, InternalException {
MessageDigest sha256Digest = MessageDigest.getInstance("SHA-256");
MessageDigest md5Digest = MessageDigest.getInstance("MD5");

if (data instanceof BufferedInputStream || data instanceof RandomAccessFile) {
updateDigests(data, len, sha256Digest, md5Digest);
} else if (data instanceof byte[]) {
sha256Digest.update((byte[]) data, 0, len);
md5Digest.update((byte[]) data, 0, len);
} else {
throw new InternalException(
"Unknown data source to calculate SHA-256 hash. This should not happen, "
+ "please report this issue at https://github.com/minio/minio-java/issues",
null);
}

return new String[] {
BaseEncoding.base16().encode(sha256Digest.digest()).toLowerCase(Locale.US),
BaseEncoding.base64().encode(md5Digest.digest())
};
}

/** Updated MessageDigest with bytes read from file and stream. */
private static int updateDigests(
Object inputStream, int len, MessageDigest sha256Digest, MessageDigest md5Digest)
throws IOException, InsufficientDataException {
RandomAccessFile file = null;
BufferedInputStream stream = null;
if (inputStream instanceof RandomAccessFile) {
file = (RandomAccessFile) inputStream;
} else if (inputStream instanceof BufferedInputStream) {
stream = (BufferedInputStream) inputStream;
}

// hold current position of file/stream to reset back to this position.
long pos = 0;
if (file != null) {
pos = file.getFilePointer();
} else {
stream.mark(len);
}

// 16KiB buffer for optimization
byte[] buf = new byte[16384];
int bytesToRead = buf.length;
int bytesRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < len) {
if ((len - totalBytesRead) < bytesToRead) {
bytesToRead = len - totalBytesRead;
}

if (file != null) {
bytesRead = file.read(buf, 0, bytesToRead);
} else {
bytesRead = stream.read(buf, 0, bytesToRead);
}

if (bytesRead < 0) {
// reached EOF
throw new InsufficientDataException(
"Insufficient data. bytes read " + totalBytesRead + " expected " + len);
}

if (bytesRead > 0) {
if (sha256Digest != null) {
sha256Digest.update(buf, 0, bytesRead);
}

if (md5Digest != null) {
md5Digest.update(buf, 0, bytesRead);
}

totalBytesRead += bytesRead;
}
}

// reset back to saved position.
if (file != null) {
file.seek(pos);
} else {
stream.reset();
}

return totalBytesRead;
}
}
Loading

0 comments on commit 17a9645

Please sign in to comment.