-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoogleHttpClientMain.java
69 lines (60 loc) · 3.24 KB
/
GoogleHttpClientMain.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpContent;
import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.apache.v2.ApacheHttpTransport;
import com.google.common.io.ByteStreams;
import com.google.common.net.MediaType;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GoogleHttpClientMain {
private static class RandoContent implements HttpContent {
private int megaBytes;
private RandoContent(int megaBytes) { this.megaBytes = megaBytes; }
@Override public long getLength() throws IOException { return megaBytes * 1024 * 1024; }
@Override public String getType() { return MediaType.OCTET_STREAM.toString(); }
@Override public boolean retrySupported() { return true; }
@Override public void writeTo(OutputStream out) throws IOException {
byte[] rando = new byte[1024];
for (int i = 0; i < megaBytes * 1024; i++) out.write(rando);
out.flush();
}
}
private static HttpRequestFactory reqFactory = new ApacheHttpTransport().createRequestFactory();
public static void main(String[] args) throws IOException {
String username = YOUR DOCKER HUB ACCOUNT
String password = YOUR DOCKER HUB PASSWORD
String dockerHubRepo = YOUR DOCKER HUB TEST REPO (e.g., myaccount/myrepo)
String authToken = getAuthToken(dockerHubRepo, username, password), // don't show this in public
uploadUrl = getUploadUrl(dockerHubRepo, authToken);
System.out.println("start timing uploading 40MB to Docker Hub...");
long started = System.nanoTime();
reqFactory.buildPatchRequest(new GenericUrl(uploadUrl), new RandoContent(40))
.setHeaders(new HttpHeaders().setAuthorization("Bearer " + authToken))
.execute();
System.out.println("elapsed (s): " + (System.nanoTime() - started) / 100000000L / 10.);
}
private static String getAuthToken(String repository, String username, String password) throws IOException {
System.out.println("requesting bearer auth token from Docker Hub...");
String authUrl = "https://auth.docker.io/token?service=registry.docker.io&scope=repository:" + repository + ":pull,push";
HttpResponse res = reqFactory.buildGetRequest(new GenericUrl(authUrl))
.setHeaders(new HttpHeaders().setBasicAuthentication(username, password))
.execute();
String json = new String(ByteStreams.toByteArray(res.getContent()), StandardCharsets.UTF_8);
Matcher m = Pattern.compile("\"token\"\\s*:\\s*\"([^\"]+)\"").matcher(json);
m.find();
return m.group(1); // don't show this in public
}
private static String getUploadUrl(String repository, String authToken) throws IOException {
System.out.println("get upload URL...");
String uploadInitUrl = "https://registry-1.docker.io/v2/" + repository + "/blobs/uploads/";
HttpResponse res = reqFactory.buildPostRequest(new GenericUrl(uploadInitUrl), new RandoContent(0))
.setHeaders(new HttpHeaders().setAuthorization("Bearer " + authToken))
.execute();
return res.getHeaders().getLocation();
}
}