Created
November 28, 2019 10:13
-
-
Save bartveenstra/c5fcc03628bceab794ab53dafa6b83aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Registration Mojo that sends a REST request to a Spring Cloud Data Flow to register the application. | |
*/ | |
@Mojo(name = "register", requiresProject = true, threadSafe = true) | |
@Slf4j | |
public class RegisterMojo extends AbstractMojo { | |
@Parameter | |
protected String name; | |
/** | |
* Type of application to be registered. Must be one of SOURCE, PROCESSOR or SINK | |
*/ | |
@Parameter | |
protected Type type; | |
/** | |
* Address of the Data Flow Server [default: http://localhost:9393]. | |
*/ | |
@Parameter(defaultValue = "http://localhost:9393") | |
protected URL dataflowUrl; | |
/** | |
* Force registration of App even if same version exists. | |
*/ | |
@Parameter(defaultValue = "true") | |
protected boolean force; | |
/** | |
* Username of the Data Flow Server [no default]. | |
*/ | |
@Parameter | |
protected String dataflowUsername; | |
/** | |
* Password of the Data Flow Server [no default]. | |
*/ | |
@Parameter | |
protected String dataflowPassword; | |
/** | |
* Default App URL. Defaults to maven Artifact. | |
*/ | |
@Parameter(defaultValue = "maven://${project.groupId}/${project.artifactId}:${project.version}") | |
protected String appUri; | |
/** | |
* App Metadata Artifact. | |
*/ | |
@Parameter(defaultValue = "maven://${project.groupId}:${project.artifactId}:jar:metadata:${project.version}") | |
protected String appMetadata; | |
@Parameter(defaultValue = "${project}", readonly = true) | |
protected MavenProject project; | |
/** | |
* Method executed by Maven by invoking mvn scdf:register. | |
* | |
* @throws MojoExecutionException When it fails to r | |
*/ | |
public void execute() throws MojoExecutionException { | |
if (name == null || type == null) { | |
log.info("Skipping execution. No name of type defined for project: {}", project.getName()); | |
return; | |
} | |
log.info("Register app: {} with metadata: {}", appUri, appMetadata); | |
DataOutputStream out; | |
HttpURLConnection urlConnection; | |
int status; | |
try { | |
Map<String, String> parameters = new HashMap<>(); | |
parameters.put("uri", appUri); | |
parameters.put("force", Boolean.toString(force)); | |
parameters.put("metadata-uri", appMetadata); | |
String paramsString = ParameterStringBuilder.getParamsString(parameters); | |
String spec = dataflowUrl.toString() + "/apps" | |
+ "/" + type.name().toLowerCase() | |
+ "/" + name | |
+ "/" + project.getVersion(); | |
log.info("Register app {} to: {} with body: \n{}", name, spec, paramsString); | |
URL url = new URL(spec); | |
urlConnection = (HttpURLConnection) url.openConnection(); | |
urlConnection.setDoOutput(true); | |
out = new DataOutputStream(urlConnection.getOutputStream()); | |
out.writeBytes(paramsString); | |
out.flush(); | |
out.close(); | |
status = urlConnection.getResponseCode(); | |
String content = readResponse(urlConnection, status); | |
if (status == 201) { | |
log.info("Registered application: {} response: [{}]\n{}", appUri, status, content); | |
} else { | |
log.warn("Failed to register application: {} response: [{}] \n{}", appUri, status, content); | |
} | |
} catch (MalformedURLException e) { | |
throw new MojoExecutionException("Cannot create data flow url", e); | |
} catch (IOException e) { | |
throw new MojoExecutionException("Failed to read content for error response from server", e); | |
} | |
} | |
/** | |
* Read the response from the HTTP Connection. | |
* | |
* @param urlConnection The URL Connection | |
* @param status THe HTTP Response | |
* @return The response as a String | |
* @throws IOException The exception if the request failed. | |
*/ | |
private String readResponse(HttpURLConnection urlConnection, int status) throws IOException { | |
Reader streamReader; | |
if (status > 299) { | |
streamReader = new InputStreamReader(urlConnection.getErrorStream()); | |
} else { | |
streamReader = new InputStreamReader(urlConnection.getInputStream()); | |
} | |
BufferedReader in = new BufferedReader(streamReader); | |
String inputLine; | |
StringBuilder content = new StringBuilder(); | |
while ((inputLine = in.readLine()) != null) { | |
content.append(inputLine); | |
} | |
in.close(); | |
return content.toString(); | |
} | |
/** | |
* Type of application to register. | |
*/ | |
public enum Type { | |
APP, SOURCE, PROCESSOR, SINK, TASK | |
} | |
/** | |
* Convenience method to build request body. | |
*/ | |
private static class ParameterStringBuilder { | |
static String getParamsString(Map<String, String> params) | |
throws UnsupportedEncodingException { | |
StringBuilder result = new StringBuilder(); | |
for (Map.Entry<String, String> entry : params.entrySet()) { | |
result.append(URLEncoder.encode(entry.getKey(), "UTF-8")); | |
result.append("="); | |
result.append(URLEncoder.encode(entry.getValue(), "UTF-8")); | |
result.append("&"); | |
} | |
String resultString = result.toString(); | |
return resultString.length() > 0 | |
? resultString.substring(0, resultString.length() - 1) | |
: resultString; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment