docker registry api java impl
程序员文章站
2022-04-19 21:27:11
...
class DockerRegistryApiClient{
private OkHttpClient okHttpClient;
/**
* 检查清单存在
*
* @param url url
* @param headers headers
* @return CallResult
*/
public CallResult<String> headManifest(String url, Map<String, String> headers) {
try {
Response response = baseReq(url, HttpMethod.HEAD, null, headers);
if (ObjectUtils.isEmpty(response)) {
return CallResult.failure("调用DockerRegistry未知异常", StringUtils.EMPTY);
}
if (response.code() == 200) {
return CallResult.success(response.header("content-type"));
}
return CallResult.failure(String.valueOf(response.code()), response.message(), response.body().string());
} catch (IOException ex) {
return CallResult.failure(String.valueOf(ex.getMessage()), ex.getMessage(), StringUtils.EMPTY);
}
}
/**
* 获取清单
*
* @param url url
* @param headers headers
* @return CallResult
*/
public CallResult<String> getManifest(String url, Map<String, String> headers) {
try {
Response response = baseReq(url, HttpMethod.GET, null, headers);
if (ObjectUtils.isEmpty(response)) {
return CallResult.failure("调用DockerRegistry未知异常", StringUtils.EMPTY);
}
if (response.code() == 200) {
return CallResult.success(response.body().string());
}
return CallResult.failure(String.valueOf(response.code()), response.message(),
new String(response.body().bytes(), StandardCharsets.UTF_8));
} catch (IOException ex) {
return CallResult.failure(String.valueOf(ex.getMessage()), ex.getMessage(), StringUtils.EMPTY);
}
}
/**
* 获取blob
*
* @param url url
* @param headers headers
* @return CallResult
*/
public CallResult<byte[]> getBlob(String url, Map<String, String> headers) {
try {
Response response = baseReq(url, HttpMethod.GET, null, headers);
if (ObjectUtils.isEmpty(response)) {
return CallResult.failure("调用DockerRegistry未知异常", null);
}
if (response.code() == 200) {
return CallResult.success(response.body().bytes());
}
return CallResult.failure(String.valueOf(response.code()), response.message(), response.body().bytes());
} catch (IOException ex) {
return CallResult.failure(String.valueOf(ex.getMessage()), ex.getMessage(), new byte[0]);
}
}
/**
* 检查blob存在
*
* @param url url
* @param headers headers
* @return CallResult
*/
public CallResult<String> headBlob(String url, Map<String, String> headers) {
try {
Response response = baseReq(url, HttpMethod.HEAD, null, headers);
if (ObjectUtils.isEmpty(response)) {
return CallResult.failure("调用DockerRegistry未知异常", StringUtils.EMPTY);
}
if (response.code() == 200) {
return CallResult.success();
}
return CallResult.failure(String.valueOf(response.code()), response.message(), response.body().string());
} catch (IOException ex) {
return CallResult.failure(String.valueOf(ex.getMessage()), ex.getMessage(), StringUtils.EMPTY);
}
}
/**
* 上传清单
*
* @param url url
* @param body body
* @param headers headers
* @return CallResult
*/
public CallResult<String> putManifest(String url, String body, Map<String, String> headers) {
try {
RequestBody requestBody =
RequestBody.create(MediaType.parse("application/vnd.docker.distribution.manifest.v2+json"), body);
Response response = baseReq(url, HttpMethod.PUT, requestBody, headers);
if (ObjectUtils.isEmpty(response)) {
return CallResult.failure("调用DockerRegistry未知异常", StringUtils.EMPTY);
}
if (response.code() == 201) {
return CallResult.success(response.body().string());
}
return CallResult.failure(String.valueOf(response.code()), response.message(), response.body().string());
} catch (IOException ex) {
return CallResult.failure(ex.getMessage(), StringUtils.EMPTY);
}
}
/**
* 上传blob
*
* @param url url
* @param body body
* @param headers headers
* @return CallResult
*/
public CallResult<String> putBlob(String url, byte[] body, Map<String, String> headers) {
RequestBody requestBody = RequestBody.create(MediaType.parse("application/octet-stream"), body);
if (ObjectUtils.isEmpty(headers)) {
headers = new HashMap<>();
}
headers.put(HttpHeaders.CONTENT_TYPE, "application/octet-stream");
headers.put(HttpHeaders.CONTENT_LENGTH, String.valueOf(body.length));
try {
Response response = baseReq(url, HttpMethod.PUT, requestBody, headers);
if (ObjectUtils.isEmpty(response)) {
return CallResult.failure("调用DockerRegistry未知异常", StringUtils.EMPTY);
}
if (response.code() == 201) {
return CallResult.success(response.header("Location"));
}
return CallResult.failure(String.valueOf(response.code()), response.message(), response.body().string());
} catch (Exception ex) {
return CallResult.failure(ex.getMessage(), StringUtils.EMPTY);
}
}
/**
* 创建上传空间
*
* @param url url
* @param body body
* @param headers headers
* @return CallResult
*/
public CallResult<String> createUploads(String url, String body, Map<String, String> headers) {
RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), Objects.isNull(body) ? "" : body);
try {
Response response = baseReq(url, HttpMethod.POST, requestBody, headers);
if (ObjectUtils.isEmpty(response)) {
return CallResult.failure("调用DockerRegistry未知异常", StringUtils.EMPTY);
}
if (response.code() == 202 || response.code() == 201) {
return CallResult.success(response.header("Location"));
}
return CallResult.failure(String.valueOf(response.code()), response.message(), response.body().string());
} catch (IOException ex) {
return CallResult.failure(String.valueOf(ex.getMessage()), ex.getMessage(), StringUtils.EMPTY);
}
}
/**
* 删除上传空间
*
* @param url url
* @param headers headers
* @return CallResult
*/
public CallResult<String> deleteUploads(String url, Map<String, String> headers) {
try {
Response response = baseReq(url, HttpMethod.DELETE, null, headers);
if (ObjectUtils.isEmpty(response)) {
return CallResult.failure("调用DockerRegistry未知异常", StringUtils.EMPTY);
}
if (response.code() == 204) {
return CallResult.success();
}
return CallResult.failure(String.valueOf(response.code()), response.message(), response.body().string());
} catch (IOException ex) {
return CallResult.failure(String.valueOf(ex.getMessage()), ex.getMessage(), StringUtils.EMPTY);
}
}
/**
* baseReq
*
* @param url url
* @param method method
* @param body body
* @param headers headers
* @return Response
* @throws IOException IOException
*/
private Response baseReq(String url, HttpMethod method, RequestBody body, Map<String, String> headers)
throws IOException {
// 兼容私仓
if (StringUtils.contains(url, "5000")) {
url = StringUtils.replace(url, HttpConstants.HTTPS_PROTOCOL, HttpConstants.HTTP_PROTOCOL);
}
Request.Builder builder = new Request.Builder().url(url).method(method.name(), body);
buildHeaders(builder, headers);
Request request = builder.build();
try {
return okHttpClient.newCall(request).execute();
} catch (NullPointerException | IllegalStateException ex) {
return null;
}
}
/**
* buildHeaders
*
* @param builder builder
* @param headers headers
*/
private void buildHeaders(Request.Builder builder, Map<String, String> headers) {
if (MapUtils.isEmpty(headers)) {
return;
}
Iterator<Map.Entry<String, String>> iterator = headers.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> next = iterator.next();
builder.header(next.getKey(), next.getValue());
}
}
}