Skip to content

Commit 8a32991

Browse files
committed
Drop dependency on Guava
1 parent d80813f commit 8a32991

File tree

7 files changed

+46
-115
lines changed

7 files changed

+46
-115
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ See https://github.com/bozaro/git-lfs-java/blob/master/gitlfs-server/src/test/ja
5656

5757
Version 0.14.0 (Unreleased)
5858

59+
* Drop dependency on Guava
60+
5961
Version 0.13.0
6062

6163
* [LFS locking](https://github.com/git-lfs/git-lfs/blob/master/docs/api/locking.md) support
@@ -67,7 +69,6 @@ Version 0.12.1
6769
Version 0.12.0
6870

6971
* Update dependencies
70-
* Drop dependency on Guava
7172
* Do not output \r in JSON on Windows
7273

7374
Version 0.11.1

gitlfs-client/src/main/java/ru/bozaro/gitlfs/client/AuthHelper.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,12 @@ public static AuthProvider create(@NotNull String gitURL) throws MalformedURLExc
5353
}
5454

5555
@NotNull
56-
public static URI join(@NotNull URI href, @NotNull String path) {
56+
public static URI join(@NotNull URI href, @NotNull String... path) {
5757
try {
58-
return new URI(href.getScheme(), href.getAuthority(), href.getPath() + (href.getPath().endsWith("/") ? "" : "/"), null, null).resolve(path);
58+
URI uri = new URI(href.getScheme(), href.getAuthority(), href.getPath() + (href.getPath().endsWith("/") ? "" : "/"), null, null);
59+
for (String fragment : path)
60+
uri = uri.resolve(fragment);
61+
return uri;
5962
} catch (URISyntaxException e) {
6063
throw new IllegalStateException(e);
6164
}

gitlfs-client/src/main/java/ru/bozaro/gitlfs/client/Client.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package ru.bozaro.gitlfs.client;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
4-
import com.google.common.base.Strings;
5-
import com.google.common.net.UrlEscapers;
64
import org.apache.commons.codec.binary.Hex;
75
import org.apache.http.HttpResponse;
86
import org.apache.http.HttpStatus;
@@ -28,6 +26,7 @@
2826
import java.io.IOException;
2927
import java.io.InputStream;
3028
import java.net.URI;
29+
import java.net.URLEncoder;
3130
import java.security.MessageDigest;
3231
import java.security.NoSuchAlgorithmException;
3332
import java.util.ArrayList;
@@ -87,7 +86,7 @@ public ObjectRes getMeta(@NotNull final String hash) throws IOException {
8786
return doWork(auth -> doRequest(
8887
auth,
8988
new MetaGet(),
90-
AuthHelper.join(auth.getHref(), PATH_OBJECTS + "/" + UrlEscapers.urlPathSegmentEscaper().escape(hash))
89+
AuthHelper.join(auth.getHref(), PATH_OBJECTS + "/", hash)
9190
), Operation.Download);
9291
}
9392

@@ -153,7 +152,7 @@ public <T> T getObject(@NotNull final String hash, @NotNull final StreamHandler<
153152
final ObjectRes links = doRequest(
154153
auth,
155154
new MetaGet(),
156-
AuthHelper.join(auth.getHref(), PATH_OBJECTS + "/" + UrlEscapers.urlPathSegmentEscaper().escape(hash))
155+
AuthHelper.join(auth.getHref(), PATH_OBJECTS + "/", hash)
157156
);
158157
if (links == null) {
159158
throw new FileNotFoundException();
@@ -384,7 +383,7 @@ public Lock unlock(@NotNull String lockId, boolean force, @Nullable Ref ref) thr
384383
return doWork(auth -> doRequest(
385384
auth,
386385
new LockDelete(force, ref),
387-
AuthHelper.join(auth.getHref(), PATH_LOCKS + "/" + UrlEscapers.urlPathSegmentEscaper().escape(lockId) + "/unlock")),
386+
AuthHelper.join(auth.getHref(), PATH_LOCKS + "/", lockId + "/unlock")),
388387
Operation.Upload
389388
);
390389
}
@@ -398,9 +397,9 @@ public List<Lock> listLocks(@Nullable String path, @Nullable String id, @Nullabl
398397
if (id == null)
399398
id = "";
400399

401-
final String params = "?path=" + UrlEscapers.urlFormParameterEscaper().escape(path)
402-
+ "&id=" + UrlEscapers.urlFormParameterEscaper().escape(id)
403-
+ "&refspec=" + UrlEscapers.urlFormParameterEscaper().escape(ref == null ? "" : ref.getName());
400+
final String params = "?path=" + URLEncoder.encode(path, "UTF-8")
401+
+ "&id=" + URLEncoder.encode(id, "UTF-8")
402+
+ "&refspec=" + URLEncoder.encode(ref == null ? "" : ref.getName(), "UTF-8");
404403

405404
String cursor = "";
406405
do {
@@ -410,13 +409,13 @@ public List<Lock> listLocks(@Nullable String path, @Nullable String id, @Nullabl
410409
new LocksList(),
411410
AuthHelper.join(
412411
auth.getHref(),
413-
PATH_LOCKS + params + "&cursor=" + UrlEscapers.urlFormParameterEscaper().escape(cursorFinal))
412+
PATH_LOCKS + params + "&cursor=" + URLEncoder.encode(cursorFinal, "UTF-8"))
414413
),
415414
Operation.Download
416415
);
417416
result.addAll(res.getLocks());
418417
cursor = res.getNextCursor();
419-
} while (!Strings.isNullOrEmpty(cursor));
418+
} while (cursor != null && !cursor.isEmpty());
420419

421420
return result;
422421
}
@@ -437,7 +436,7 @@ public VerifyLocksResult verifyLocks(@Nullable Ref ref) throws IOException {
437436
result.getOurLocks().addAll(res.getOurs());
438437
result.getTheirLocks().addAll(res.getTheirs());
439438
cursor = res.getNextCursor();
440-
} while (!Strings.isNullOrEmpty(cursor));
439+
} while (cursor != null && !cursor.isEmpty());
441440

442441
return result;
443442
}
Lines changed: 2 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
package ru.bozaro.gitlfs.client.exceptions;
22

3-
import org.apache.http.*;
4-
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
3+
import org.apache.http.HttpResponse;
4+
import org.apache.http.StatusLine;
55
import org.apache.http.client.methods.HttpUriRequest;
66
import org.jetbrains.annotations.NotNull;
7-
import org.jetbrains.annotations.Nullable;
87

98
import java.io.IOException;
10-
import java.io.InputStream;
119

1210
/**
1311
* Simple HTTP exception.
1412
*
1513
* @author Artem V. Navrotskiy
1614
*/
1715
public class RequestException extends IOException {
18-
@NotNull
19-
private static final char[] HEX = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
2016
@NotNull
2117
private final HttpUriRequest request;
2218
@NotNull
@@ -36,85 +32,4 @@ public String getMessage() {
3632
final StatusLine statusLine = response.getStatusLine();
3733
return request.getURI().toString() + " - " + statusLine.getStatusCode() + " (" + statusLine.getReasonPhrase() + ")";
3834
}
39-
40-
@NotNull
41-
public String getRequestInfo() {
42-
final StringBuilder sb = new StringBuilder();
43-
sb.append("Request:\n");
44-
sb.append(" ").append(request.getMethod()).append(" ").append(request.getURI().toString()).append("\n");
45-
buildMessageInfo(sb, request);
46-
if (request instanceof HttpEntityEnclosingRequestBase) {
47-
builEntityInfo(sb, ((HttpEntityEnclosingRequestBase) request).getEntity());
48-
}
49-
50-
final StatusLine statusLine = response.getStatusLine();
51-
sb.append("Response: ").append(statusLine.getStatusCode()).append(" ").append(statusLine.getReasonPhrase()).append("\n");
52-
buildMessageInfo(sb, response);
53-
builEntityInfo(sb, response.getEntity());
54-
return sb.toString();
55-
}
56-
57-
private static void builEntityInfo(@NotNull StringBuilder sb, @Nullable HttpEntity entity) {
58-
if (entity == null) return;
59-
try {
60-
try (InputStream content = entity.getContent()) {
61-
byte[] buffer = new byte[1024];
62-
int size = 0;
63-
while (size < buffer.length) {
64-
int read = content.read(buffer, size, buffer.length - size);
65-
if (read <= 0) break;
66-
size += read;
67-
}
68-
int block = 32;
69-
for (int offset = 0; offset < size; offset += block) {
70-
buildHexLine(sb, buffer, offset, Math.min(block, size - offset), block);
71-
sb.append(" ");
72-
buildSafeLine(sb, buffer, offset, Math.min(block, size - offset));
73-
sb.append("\n");
74-
}
75-
}
76-
} catch (IOException ignored) {
77-
}
78-
}
79-
80-
private static void buildHexLine(@NotNull StringBuilder sb, byte[] buffer, int offset, int size, int line) {
81-
for (int i = 0; i < size; ++i) {
82-
if ((i == 0) && (i % 8 == 0)) sb.append(' ');
83-
byte b = buffer[offset + i];
84-
sb.append(HEX[0x0F & (b >> 4)]);
85-
sb.append(HEX[0x0F & b]);
86-
sb.append(' ');
87-
}
88-
for (int i = size; i < line; ++i) {
89-
if ((i == 0) && (i % 8 == 0)) sb.append(' ');
90-
sb.append(" ");
91-
}
92-
}
93-
94-
private static void buildSafeLine(@NotNull StringBuilder sb, byte[] buffer, int offset, int size) {
95-
for (int i = 0; i < size; ++i) {
96-
int b = 0xFF & (int) buffer[offset + i];
97-
if ((b >= 0x20) && (b < 0x80)) {
98-
sb.append((char) b);
99-
} else {
100-
sb.append(' ');
101-
}
102-
}
103-
}
104-
105-
private static void buildMessageInfo(@NotNull StringBuilder sb, @NotNull HttpMessage message) {
106-
for (Header header : message.getAllHeaders()) {
107-
sb.append(" ").append(header.getName()).append(": ");
108-
if (!header.getName().equals("Authorization")) {
109-
sb.append(header.getValue());
110-
} else {
111-
int space = header.getValue().indexOf(' ');
112-
if (space > 0) {
113-
sb.append(header.getValue().substring(0, space + 1));
114-
}
115-
sb.append("*****");
116-
}
117-
sb.append("\n");
118-
}
119-
}
12035
}

gitlfs-common/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ description = "Java Git-LFS common structures"
22

33
dependencies {
44
compile("com.fasterxml.jackson.core:jackson-databind:2.9.8")
5-
compile("com.google.guava:guava:27.1-jre")
65
}

gitlfs-common/src/main/java/ru/bozaro/gitlfs/common/io/InputStreamValidator.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package ru.bozaro.gitlfs.common.io;
22

3-
import com.google.common.hash.Hasher;
4-
import com.google.common.hash.Hashing;
53
import org.jetbrains.annotations.NotNull;
64
import ru.bozaro.gitlfs.common.data.Meta;
75

86
import java.io.IOException;
97
import java.io.InputStream;
8+
import java.security.MessageDigest;
9+
import java.security.NoSuchAlgorithmException;
1010

1111
/**
1212
* Wrapper for validating hash and size of uploading object.
@@ -15,17 +15,22 @@
1515
*/
1616
public class InputStreamValidator extends InputStream {
1717
@NotNull
18-
private final Hasher hasher;
18+
private static final char[] hexDigits = "0123456789abcdef".toCharArray();
19+
@NotNull
20+
private final MessageDigest digest;
1921
@NotNull
2022
private final InputStream stream;
2123
@NotNull
2224
private final Meta meta;
23-
2425
private boolean eof;
2526
private long totalSize;
2627

27-
public InputStreamValidator(@NotNull InputStream stream, @NotNull Meta meta) {
28-
this.hasher = Hashing.sha256().newHasher();
28+
public InputStreamValidator(@NotNull InputStream stream, @NotNull Meta meta) throws IOException {
29+
try {
30+
this.digest = MessageDigest.getInstance("SHA-256");
31+
} catch (NoSuchAlgorithmException e) {
32+
throw new IOException(e);
33+
}
2934
this.stream = stream;
3035
this.meta = meta;
3136
this.eof = false;
@@ -39,7 +44,7 @@ public int read() throws IOException {
3944
}
4045
final int data = stream.read();
4146
if (data >= 0) {
42-
hasher.putByte((byte) data);
47+
digest.update((byte) data);
4348
checkSize(1);
4449
} else {
4550
checkSize(-1);
@@ -59,21 +64,30 @@ private void checkSize(int size) throws IOException {
5964
if ((meta.getSize() >= 0) && (totalSize != meta.getSize())) {
6065
throw new IOException("Unexpected end of stream");
6166
}
62-
final String hash = hasher.hash().toString();
67+
final String hash = toHexString(digest.digest());
6368
if (!meta.getOid().equals(hash)) {
6469
throw new IOException("Invalid stream hash");
6570
}
6671
}
6772
}
6873

74+
@NotNull
75+
private static String toHexString(@NotNull byte[] bytes) {
76+
StringBuilder sb = new StringBuilder(2 * bytes.length);
77+
for (byte b : bytes) {
78+
sb.append(hexDigits[(b >> 4) & 0xf]).append(hexDigits[b & 0xf]);
79+
}
80+
return sb.toString();
81+
}
82+
6983
@Override
7084
public int read(@NotNull byte[] buffer, int off, int len) throws IOException {
7185
if (eof) {
7286
return -1;
7387
}
7488
final int size = stream.read(buffer, off, len);
7589
if (size > 0) {
76-
hasher.putBytes(buffer, off, size);
90+
digest.update(buffer, off, size);
7791
}
7892
checkSize(size);
7993
return size;

gitlfs-server/src/main/java/ru/bozaro/gitlfs/server/LocalPointerManager.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package ru.bozaro.gitlfs.server;
22

3-
import com.google.common.collect.ImmutableMap;
43
import org.jetbrains.annotations.NotNull;
54
import org.jetbrains.annotations.Nullable;
65
import ru.bozaro.gitlfs.common.Constants;
7-
import ru.bozaro.gitlfs.common.data.*;
86
import ru.bozaro.gitlfs.common.data.Error;
7+
import ru.bozaro.gitlfs.common.data.*;
98

109
import javax.servlet.http.HttpServletRequest;
1110
import java.io.IOException;
1211
import java.net.URI;
12+
import java.util.Collections;
1313
import java.util.HashMap;
1414
import java.util.Map;
1515

@@ -75,11 +75,11 @@ public BatchItem[] getLocations(@NotNull Meta[] metas) throws IOException {
7575
public BatchItem getLocation(@Nullable Map<String, String> header, @NotNull URI selfUrl, @NotNull Meta meta) throws IOException {
7676
final Meta storageMeta = manager.getMetadata(meta.getOid());
7777
if (storageMeta == null) {
78-
return new BatchItem(meta, ImmutableMap.of(LinkType.Upload, new Link(selfUrl.resolve(contentLocation).resolve(meta.getOid()), header, null)));
78+
return new BatchItem(meta, Collections.singletonMap(LinkType.Upload, new Link(selfUrl.resolve(contentLocation).resolve(meta.getOid()), header, null)));
7979
} else if ((meta.getSize() >= 0) && (storageMeta.getSize() != meta.getSize())) {
8080
return new BatchItem(meta, new Error(422, "Invalid object size"));
8181
} else {
82-
return new BatchItem(storageMeta, ImmutableMap.of(LinkType.Download, new Link(selfUrl.resolve(contentLocation).resolve(storageMeta.getOid()), header, null)));
82+
return new BatchItem(storageMeta, Collections.singletonMap(LinkType.Download, new Link(selfUrl.resolve(contentLocation).resolve(storageMeta.getOid()), header, null)));
8383
}
8484
}
8585
};

0 commit comments

Comments
 (0)