31
31
import org .springframework .http .HttpMethod ;
32
32
import org .springframework .http .ResponseEntity ;
33
33
import org .springframework .util .StringUtils ;
34
- import org .springframework .web .client .RestClientException ;
35
34
import org .springframework .web .client .RestTemplate ;
36
35
import org .springframework .web .util .UriComponents ;
37
36
import org .springframework .web .util .UriComponentsBuilder ;
@@ -47,9 +46,9 @@ public class ContainerRegistryService {
47
46
48
47
private static final Logger logger = LoggerFactory .getLogger (ContainerRegistryService .class );
49
48
50
- private static final List <String > SUPPORTED_MANIFEST_MEDIA_TYPES = Collections
51
- .unmodifiableList (Arrays .asList (ContainerRegistryProperties .OCI_IMAGE_MANIFEST_MEDIA_TYPE ,
52
- ContainerRegistryProperties .DOCKER_IMAGE_MANIFEST_MEDIA_TYPE ));
49
+ private static final List <String > SUPPORTED_MANIFEST_MEDIA_TYPES =
50
+ Collections .unmodifiableList (Arrays .asList (ContainerRegistryProperties .OCI_IMAGE_MANIFEST_MEDIA_TYPE ,
51
+ ContainerRegistryProperties .DOCKER_IMAGE_MANIFEST_MEDIA_TYPE ));
53
52
54
53
private static final String HTTPS_SCHEME = "https" ;
55
54
@@ -89,38 +88,35 @@ public Map<String, ContainerRegistryConfiguration> getContainerRegistryConfigura
89
88
}
90
89
91
90
/**
92
- * Get the tag information for the given container image identified by its repository
93
- * and registry. The registry information is expected to be set via container registry
94
- * configuration in SCDF.
91
+ * Get the tag information for the given container image identified by its repository and registry.
92
+ * The registry information is expected to be set via container registry configuration in SCDF.
95
93
* @param registryName the container registry name
96
94
* @param repositoryName the image repository name
97
95
* @return the list of tags for the image
98
96
*/
99
97
public List <String > getTags (String registryName , String repositoryName ) {
100
98
try {
101
- ContainerRegistryConfiguration containerRegistryConfiguration = this .registryConfigurations
102
- .get (registryName );
99
+ ContainerRegistryConfiguration containerRegistryConfiguration = this .registryConfigurations .get (registryName );
103
100
Map <String , String > properties = new HashMap <>();
104
101
properties .put (DockerOAuth2RegistryAuthorizer .DOCKER_REGISTRY_REPOSITORY_FIELD_KEY , repositoryName );
105
- HttpHeaders httpHeaders = new HttpHeaders (
106
- this .registryAuthorizerMap .get (containerRegistryConfiguration .getAuthorizationType ())
107
- .getAuthorizationHeaders (containerRegistryConfiguration , properties ));
102
+ HttpHeaders httpHeaders = new HttpHeaders (this .registryAuthorizerMap .get (containerRegistryConfiguration .getAuthorizationType ()).getAuthorizationHeaders (
103
+ containerRegistryConfiguration , properties ));
108
104
httpHeaders .set (HttpHeaders .ACCEPT , "application/json" );
109
105
110
106
UriComponents manifestUriComponents = UriComponentsBuilder .newInstance ()
111
- .scheme (HTTPS_SCHEME )
112
- .host (containerRegistryConfiguration .getRegistryHost ())
113
- .path (TAGS_LIST_PATH )
114
- .build ()
115
- .expand (repositoryName );
107
+ .scheme (HTTPS_SCHEME )
108
+ .host (containerRegistryConfiguration .getRegistryHost ())
109
+ .path (TAGS_LIST_PATH )
110
+ .build ().expand (repositoryName );
116
111
117
112
RestTemplate requestRestTemplate = this .containerImageRestTemplateFactory .getContainerRestTemplate (
118
113
containerRegistryConfiguration .isDisableSslVerification (),
119
- containerRegistryConfiguration .isUseHttpProxy (), containerRegistryConfiguration .getExtra ());
114
+ containerRegistryConfiguration .isUseHttpProxy (),
115
+ containerRegistryConfiguration .getExtra ());
120
116
121
- ResponseEntity <Map > manifest = requestRestTemplate .exchange (manifestUriComponents .toUri (), HttpMethod . GET ,
122
- new HttpEntity <>(httpHeaders ), Map .class );
123
- return (List <String >) manifest .getBody ().get (TAGS_FIELD );
117
+ ResponseEntity <Map > manifest = requestRestTemplate .exchange (manifestUriComponents .toUri (),
118
+ HttpMethod . GET , new HttpEntity <>(httpHeaders ), Map .class );
119
+ return (List <String >) manifest .getBody ().get (TAGS_FIELD );
124
120
}
125
121
catch (Exception e ) {
126
122
logger .error ("Exception getting tag information for the {} from {}" , repositoryName , registryName );
@@ -136,25 +132,28 @@ public List<String> getTags(String registryName, String repositoryName) {
136
132
public Map getRepositories (String registryName ) {
137
133
try {
138
134
ContainerRegistryConfiguration containerRegistryConfiguration = this .registryConfigurations
139
- .get (registryName );
135
+ .get (registryName );
140
136
Map <String , String > properties = new HashMap <>();
141
137
properties .put (DockerOAuth2RegistryAuthorizer .DOCKER_REGISTRY_REPOSITORY_FIELD_KEY , registryName );
142
138
HttpHeaders httpHeaders = new HttpHeaders (
143
139
this .registryAuthorizerMap .get (containerRegistryConfiguration .getAuthorizationType ())
144
- .getAuthorizationHeaders (containerRegistryConfiguration , properties ));
140
+ .getAuthorizationHeaders (
141
+ containerRegistryConfiguration , properties ));
145
142
httpHeaders .set (HttpHeaders .ACCEPT , "application/json" );
146
143
UriComponents manifestUriComponents = UriComponentsBuilder .newInstance ()
147
- .scheme (HTTPS_SCHEME )
148
- .host (containerRegistryConfiguration .getRegistryHost ())
149
- .path (CATALOG_LIST_PATH )
150
- .build ();
144
+ .scheme (HTTPS_SCHEME )
145
+ .host (containerRegistryConfiguration .getRegistryHost ())
146
+ .path (CATALOG_LIST_PATH )
147
+ .build ();
148
+
151
149
152
150
RestTemplate requestRestTemplate = this .containerImageRestTemplateFactory .getContainerRestTemplate (
153
151
containerRegistryConfiguration .isDisableSslVerification (),
154
- containerRegistryConfiguration .isUseHttpProxy (), containerRegistryConfiguration .getExtra ());
152
+ containerRegistryConfiguration .isUseHttpProxy (),
153
+ containerRegistryConfiguration .getExtra ());
155
154
156
- ResponseEntity <Map > manifest = requestRestTemplate .exchange (manifestUriComponents .toUri (), HttpMethod . GET ,
157
- new HttpEntity <>(httpHeaders ), Map .class );
155
+ ResponseEntity <Map > manifest = requestRestTemplate .exchange (manifestUriComponents .toUri (),
156
+ HttpMethod . GET , new HttpEntity <>(httpHeaders ), Map .class );
158
157
return manifest .getBody ();
159
158
}
160
159
catch (Exception e ) {
@@ -207,20 +206,18 @@ public <T> T getImageManifest(ContainerRegistryRequest registryRequest, Class<T>
207
206
// Docker Registry HTTP V2 API pull manifest
208
207
ContainerImage containerImage = registryRequest .getContainerImage ();
209
208
UriComponents manifestUriComponents = UriComponentsBuilder .newInstance ()
210
- .scheme (HTTPS_SCHEME )
211
- .host (containerImage .getHostname ())
212
- .port (StringUtils .hasText (containerImage .getPort ()) ? containerImage .getPort () : null )
213
- .path (IMAGE_MANIFEST_REFERENCE_PATH )
214
- .build ()
215
- .expand (containerImage .getRepository (), containerImage .getRepositoryReference ());
209
+ .scheme (HTTPS_SCHEME )
210
+ .host (containerImage .getHostname ())
211
+ .port (StringUtils .hasText (containerImage .getPort ()) ? containerImage .getPort () : null )
212
+ .path (IMAGE_MANIFEST_REFERENCE_PATH )
213
+ .build ().expand (containerImage .getRepository (), containerImage .getRepositoryReference ());
216
214
217
- ResponseEntity <T > manifest = registryRequest .getRestTemplate ()
218
- . exchange ( manifestUriComponents . toUri (), HttpMethod .GET , new HttpEntity <>(httpHeaders ), responseClassType );
215
+ ResponseEntity <T > manifest = registryRequest .getRestTemplate (). exchange ( manifestUriComponents . toUri (),
216
+ HttpMethod .GET , new HttpEntity <>(httpHeaders ), responseClassType );
219
217
return manifest .getBody ();
220
218
}
221
219
222
- public <T > T getImageBlob (ContainerRegistryRequest registryRequest , String configDigest ,
223
- Class <T > responseClassType ) {
220
+ public <T > T getImageBlob (ContainerRegistryRequest registryRequest , String configDigest , Class <T > responseClassType ) {
224
221
ContainerImage containerImage = registryRequest .getContainerImage ();
225
222
HttpHeaders httpHeaders = new HttpHeaders (registryRequest .getAuthHttpHeaders ());
226
223
@@ -230,19 +227,11 @@ public <T> T getImageBlob(ContainerRegistryRequest registryRequest, String confi
230
227
.host (containerImage .getHostname ())
231
228
.port (StringUtils .hasText (containerImage .getPort ()) ? containerImage .getPort () : null )
232
229
.path (IMAGE_BLOB_DIGEST_PATH )
233
- .build ()
234
- .expand (containerImage .getRepository (), configDigest );
235
- try {
236
- logger .info ("getImageBlob:request:{},{}" , blobUriComponents .toUri (), httpHeaders );
237
- ResponseEntity <T > blob = registryRequest .getRestTemplate ()
238
- .exchange (blobUriComponents .toUri (), HttpMethod .GET , new HttpEntity <>(httpHeaders ), responseClassType );
230
+ .build ().expand (containerImage .getRepository (), configDigest );
239
231
240
- return blob .getStatusCode ().is2xxSuccessful () ? blob .getBody () : null ;
241
- }
242
- catch (RestClientException x ) {
243
- logger .error ("getImageBlob:exception:" + x , x );
244
- return null ;
245
- }
246
- }
232
+ ResponseEntity <T > blob = registryRequest .getRestTemplate ().exchange (blobUriComponents .toUri (),
233
+ HttpMethod .GET , new HttpEntity <>(httpHeaders ), responseClassType );
247
234
235
+ return blob .getBody ();
236
+ }
248
237
}
0 commit comments