-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApacheHttpClientMain.java
82 lines (75 loc) · 3.79 KB
/
ApacheHttpClientMain.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
70
71
72
73
74
75
76
77
78
79
80
81
82
import com.google.api.client.util.Base64;
import com.google.api.client.util.StringUtils;
import com.google.common.io.ByteStreams;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.AbstractHttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public class ApacheHttpClientMain {
private static class RandoContent extends AbstractHttpEntity {
private int megaBytes;
private RandoContent(int megaBytes) {
this.megaBytes = megaBytes;
setContentType(ContentType.APPLICATION_OCTET_STREAM.toString());
}
@Override public boolean isRepeatable() { return false; }
@Override public boolean isStreaming() { return false; }
@Override public long getContentLength() { return megaBytes * 1024 * 1024; }
@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();
}
@Override public InputStream getContent() throws IOException, UnsupportedOperationException {
throw new UnsupportedOperationException();
}
}
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();
HttpPatch req = new HttpPatch(uploadUrl);
req.setHeader("Authorization", "Bearer " + authToken);
req.setEntity(new RandoContent(40));
try (CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse res = client.execute(req)) { }
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";
HttpGet req = new HttpGet(authUrl);
req.setHeader("Authorization", "Basic " + Base64.encodeBase64String(StringUtils.getBytesUtf8(username + ':' + password)));
try (CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse res = client.execute(req)) {
String json = new String(ByteStreams.toByteArray(res.getEntity().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/";
HttpPost req = new HttpPost(uploadInitUrl);
req.setHeader("Authorization", "Bearer " + authToken);
try (CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse res = client.execute(req)) {
return res.getFirstHeader("Location").getValue();
}
}
}