Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRESOLVER-295] ChecksumUtils should be gone (deprecated) #352

Merged
merged 3 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySelector;
import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySupport;
import org.eclipse.aether.util.ChecksumUtils;
import org.eclipse.aether.util.StringDigestUtil;

import static java.util.stream.Collectors.toList;

Expand Down Expand Up @@ -104,7 +104,7 @@ public void update(final ByteBuffer input) {

@Override
public String checksum() {
return ChecksumUtils.toHexString(messageDigest.digest());
return StringDigestUtil.toHexString(messageDigest.digest());
}
};
} catch (NoSuchAlgorithmException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,13 @@
import javax.inject.Named;
import javax.inject.Singleton;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

import org.eclipse.aether.spi.io.FileProcessor;
import org.eclipse.aether.util.ChecksumUtils;
import org.eclipse.aether.util.FileUtils;

/**
Expand Down Expand Up @@ -142,7 +135,34 @@ public void move(File source, File target) throws IOException {
@Override
public String readChecksum(final File checksumFile) throws IOException {
// for now do exactly same as happened before, but FileProcessor is a component and can be replaced
return ChecksumUtils.read(checksumFile);
String checksum = "";
try (BufferedReader br = new BufferedReader(
new InputStreamReader(Files.newInputStream(checksumFile.toPath()), StandardCharsets.UTF_8), 512)) {
while (true) {
String line = br.readLine();
if (line == null) {
break;
}
line = line.trim();
if (!line.isEmpty()) {
checksum = line;
break;
}
}
}

if (checksum.matches(".+= [0-9A-Fa-f]+")) {
int lastSpacePos = checksum.lastIndexOf(' ');
checksum = checksum.substring(lastSpacePos + 1);
} else {
int spacePos = checksum.indexOf(' ');

if (spacePos != -1) {
checksum = checksum.substring(0, spacePos);
}
}

return checksum;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithm;
import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySupport;
import org.eclipse.aether.util.ChecksumUtils;
import org.eclipse.aether.util.StringDigestUtil;

/**
* Support class to implement {@link ChecksumAlgorithmFactory} based on Java {@link MessageDigest}.
Expand All @@ -49,7 +49,7 @@ public void update(final ByteBuffer input) {

@Override
public String checksum() {
return ChecksumUtils.toHexString(messageDigest.digest());
return StringDigestUtil.toHexString(messageDigest.digest());
}
};
} catch (NoSuchAlgorithmException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,39 @@ void testWriteStream() throws IOException {

target.delete();
}

@Test
void testReadChecksumNonExistentFile() {
assertThrows(IOException.class, () -> fileProcessor.readChecksum(new File("non existent")));
}

@Test
void testReadChecksumEmptyFile() throws IOException {
File emptyFile = TestFileUtils.createTempFile("");
String read = fileProcessor.readChecksum(emptyFile);
assertEquals("", read);
}

@Test
void testReadChecksum() throws IOException {
String checksum = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
File checksumFile = TestFileUtils.createTempFile(checksum);
String read = fileProcessor.readChecksum(checksumFile);
assertEquals(checksum, read);
}

@Test
void testReadChecksumWhitespace() throws IOException {
String checksum = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
File checksumFile;
String read;

checksumFile = TestFileUtils.createTempFile("foobar(alg) = " + checksum);
read = fileProcessor.readChecksum(checksumFile);
assertEquals(checksum, read);

checksumFile = TestFileUtils.createTempFile(checksum + " foobar");
read = fileProcessor.readChecksum(checksumFile);
assertEquals(checksum, read);
}
}
5 changes: 5 additions & 0 deletions maven-resolver-transport-http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@
<artifactId>maven-resolver-test-util</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-impl</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.aether.util.ChecksumUtils;
import org.eclipse.aether.internal.impl.checksum.Sha1ChecksumAlgorithmFactory;
import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmHelper;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.server.Request;
Expand Down Expand Up @@ -334,12 +335,12 @@ public void handle(String target, Request req, HttpServletRequest request, HttpS
"bytes " + offset + "-" + (file.length() - 1L) + "/" + file.length());
}
if (checksumHeader != null) {
Map<String, Object> checksums = ChecksumUtils.calc(file, Collections.singleton("SHA-1"));
Map<String, String> checksums = ChecksumAlgorithmHelper.calculate(
file, Collections.singletonList(new Sha1ChecksumAlgorithmFactory()));
if (checksumHeader == ChecksumHeader.NEXUS) {
response.setHeader(HttpHeader.ETAG.asString(), "{SHA1{" + checksums.get("SHA-1") + "}}");
} else if (checksumHeader == ChecksumHeader.XCHECKSUM) {
response.setHeader(
"x-checksum-sha1", checksums.get("SHA-1").toString());
response.setHeader("x-checksum-sha1", checksums.get(Sha1ChecksumAlgorithmFactory.NAME));
}
}
if (HttpMethod.HEAD.is(req.getMethod())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@

/**
* A utility class to assist in the verification and generation of checksums.
*
* @deprecated The use of class should be avoided, see {@link StringDigestUtil} and file processor in SPI module.
*/
@Deprecated
public final class ChecksumUtils {

private ChecksumUtils() {
Expand Down Expand Up @@ -147,21 +150,7 @@ private static Map<String, Object> calc(InputStream data, Collection<String> alg
*/
@SuppressWarnings("checkstyle:magicnumber")
public static String toHexString(byte[] bytes) {
if (bytes == null) {
return null;
}

StringBuilder buffer = new StringBuilder(bytes.length * 2);

for (byte aByte : bytes) {
int b = aByte & 0xFF;
if (b < 0x10) {
buffer.append('0');
}
buffer.append(Integer.toHexString(b));
}

return buffer.toString();
return StringDigestUtil.toHexString(bytes);
}

/**
Expand All @@ -174,21 +163,6 @@ public static String toHexString(byte[] bytes) {
*/
@SuppressWarnings("checkstyle:magicnumber")
public static byte[] fromHexString(String hexString) {
if (hexString == null) {
return null;
}
if (hexString.isEmpty()) {
return new byte[] {};
}
int len = hexString.length();
if (len % 2 != 0) {
throw new IllegalArgumentException("hexString length not even");
}
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte)
((Character.digit(hexString.charAt(i), 16) << 4) + Character.digit(hexString.charAt(i + 1), 16));
}
return data;
return StringDigestUtil.fromHexString(hexString);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public StringDigestUtil update(String data) {
* @see MessageDigest#digest()
*/
public String digest() {
return ChecksumUtils.toHexString(digest.digest());
return toHexString(digest.digest());
}

/**
Expand All @@ -79,4 +79,59 @@ public static StringDigestUtil sha1() {
public static String sha1(final String string) {
return sha1().update(string).digest();
}

/**
* Creates a hexadecimal representation of the specified bytes. Each byte is converted into a two-digit hex number
* and appended to the result with no separator between consecutive bytes.
*
* @param bytes The bytes to represent in hex notation, may be {@code null}.
* @return The hexadecimal representation of the input or {@code null} if the input was {@code null}.
* @since 2.0.0
*/
@SuppressWarnings("checkstyle:magicnumber")
public static String toHexString(byte[] bytes) {
if (bytes == null) {
return null;
}

StringBuilder buffer = new StringBuilder(bytes.length * 2);

for (byte aByte : bytes) {
int b = aByte & 0xFF;
if (b < 0x10) {
buffer.append('0');
}
buffer.append(Integer.toHexString(b));
}

return buffer.toString();
}

/**
* Creates a byte array out of hexadecimal representation of the specified bytes. If input string is {@code null},
* {@code null} is returned. Input value must have even length (due hex encoding = 2 chars one byte).
*
* @param hexString The hexString to convert to byte array, may be {@code null}.
* @return The byte array of the input or {@code null} if the input was {@code null}.
* @since 2.0.0
*/
@SuppressWarnings("checkstyle:magicnumber")
public static byte[] fromHexString(String hexString) {
if (hexString == null) {
return null;
}
if (hexString.isEmpty()) {
return new byte[] {};
}
int len = hexString.length();
if (len % 2 != 0) {
throw new IllegalArgumentException("hexString length not even");
}
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte)
((Character.digit(hexString.charAt(i), 16) << 4) + Character.digit(hexString.charAt(i + 1), 16));
}
return data;
}
}
Loading
Loading