2 * # ============LICENSE_START=======================================================
3 * # Copyright (c) 2021 AT&T Intellectual Property. All rights reserved.
4 * # ================================================================================
5 * # Licensed under the Apache License, Version 2.0 (the "License");
6 * # you may not use this file except in compliance with the License.
7 * # You may obtain a copy of the License at
9 * # http://www.apache.org/licenses/LICENSE-2.0
11 * # Unless required by applicable law or agreed to in writing, software
12 * # distributed under the License is distributed on an "AS IS" BASIS,
13 * # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * # See the License for the specific language governing permissions and
15 * # limitations under the License.
16 * # ============LICENSE_END=========================================================
19 package org.onap.dcaegen2.platform.helmchartgenerator.distribution;
21 import lombok.extern.slf4j.Slf4j;
22 import okhttp3.Credentials;
23 import okhttp3.MediaType;
24 import okhttp3.OkHttpClient;
25 import okhttp3.Request;
26 import okhttp3.RequestBody;
27 import okhttp3.Response;
28 import org.springframework.beans.factory.annotation.Value;
29 import org.springframework.stereotype.Component;
32 import java.io.IOException;
33 import java.util.Objects;
36 * Distributes helm chart to Chart Museum through REST
37 * @author Dhrumin Desai
41 public class ChartMuseumDistributor implements ChartDistributor {
43 @Value("${chartmuseum.baseurl}")
44 private String chartMuseumUrl;
46 @Value("${chartmuseum.auth.basic.username}")
47 private String username;
49 @Value("${chartmuseum.auth.basic.password}")
50 private String password;
53 * distributes chart to Chart Museum
54 * @param chartFile packaged helm chart tgz file
57 public void distribute(File chartFile) {
58 OkHttpClient client = new OkHttpClient().newBuilder().build();
59 Request request = createRequestBody(chartFile);
61 Response response = client.newCall(request).execute();
62 log.info(Objects.requireNonNull(response.body()).string());
63 } catch (IOException e) {
64 throw new RuntimeException(e.getMessage());
68 private Request createRequestBody(File chartFile) {
69 String credential = Credentials.basic(username, password);
70 MediaType mediaType = MediaType.parse("application/octet-stream");
71 RequestBody body = RequestBody.create(chartFile, mediaType);
72 return new Request.Builder()
75 .addHeader("Content-Type", "application/octet-stream")
76 .addHeader("Authorization", credential)