-
Notifications
You must be signed in to change notification settings - Fork 40.2k
/
SdkmanService.java
141 lines (108 loc) · 3.99 KB
/
SdkmanService.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.concourse.releasescripts.sdkman;
import java.net.URI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
/**
* Central class for interacting with SDKMAN's API.
*
* @author Madhura Bhave
*/
@Component
public class SdkmanService {
private static final Logger logger = LoggerFactory.getLogger(SdkmanService.class);
private static final String SDKMAN_URL = "https://vendors.sdkman.io/";
private static final String DOWNLOAD_URL = "https://repo.spring.io/simple/libs-release-local/org/springframework/boot/spring-boot-cli/"
+ "%s/spring-boot-cli-%s-bin.zip";
private static final String SPRING_BOOT = "springboot";
private final RestTemplate restTemplate;
public SdkmanService(RestTemplateBuilder builder, SdkmanProperties properties) {
String consumerKey = properties.getConsumerKey();
String consumerToken = properties.getConsumerToken();
if (StringUtils.hasLength(consumerKey)) {
builder = builder.basicAuthentication(consumerKey, consumerToken);
}
this.restTemplate = builder.build();
}
public void publish(String version, boolean makeDefault) {
release(version);
if (makeDefault) {
makeDefault(version);
}
broadcast(version);
}
private void broadcast(String version) {
BroadcastRequest broadcastRequest = new BroadcastRequest(version);
RequestEntity<BroadcastRequest> broadcastEntity = RequestEntity.post(URI.create(SDKMAN_URL + "announce/struct"))
.contentType(MediaType.APPLICATION_JSON).body(broadcastRequest);
this.restTemplate.exchange(broadcastEntity, String.class);
logger.debug("Broadcast complete");
}
private void makeDefault(String version) {
logger.debug("Making this version the default");
Request request = new Request(version);
RequestEntity<Request> requestEntity = RequestEntity.post(URI.create(SDKMAN_URL + "default"))
.contentType(MediaType.APPLICATION_JSON).body(request);
this.restTemplate.exchange(requestEntity, String.class);
logger.debug("Make default complete");
}
private void release(String version) {
ReleaseRequest releaseRequest = new ReleaseRequest(version, String.format(DOWNLOAD_URL, version, version));
RequestEntity<ReleaseRequest> releaseEntity = RequestEntity.post(URI.create(SDKMAN_URL + "release"))
.contentType(MediaType.APPLICATION_JSON).body(releaseRequest);
this.restTemplate.exchange(releaseEntity, String.class);
logger.debug("Release complete");
}
static class Request {
private final String candidate = SPRING_BOOT;
private final String version;
Request(String version) {
this.version = version;
}
public String getCandidate() {
return this.candidate;
}
public String getVersion() {
return this.version;
}
}
static class ReleaseRequest extends Request {
private final String url;
ReleaseRequest(String version, String url) {
super(version);
this.url = url;
}
public String getUrl() {
return this.url;
}
}
static class BroadcastRequest extends Request {
private final String hashtag = SPRING_BOOT;
BroadcastRequest(String version) {
super(version);
}
public String getHashtag() {
return this.hashtag;
}
}
}