io.syndesis.connector.slack.SlackVerifierExtension Maven / Gradle / Ivy
/*
* Copyright (C) 2016 Red Hat, Inc.
*
* 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
*
* http://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.syndesis.connector.slack;
import java.util.HashMap;
import java.util.Map;
import org.apache.camel.CamelContext;
import org.apache.camel.component.extension.verifier.DefaultComponentVerifierExtension;
import org.apache.camel.component.extension.verifier.ResultBuilder;
import org.apache.camel.component.extension.verifier.ResultErrorBuilder;
import org.apache.camel.component.extension.verifier.ResultErrorHelper;
import org.apache.camel.component.slack.helper.SlackMessage;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.json.simple.JSONObject;
public class SlackVerifierExtension extends DefaultComponentVerifierExtension {
protected SlackVerifierExtension(String defaultScheme, CamelContext context) {
super(defaultScheme, context);
}
// *********************************
// Parameters validation
//
@Override
protected Result verifyParameters(Map parameters) {
ResultBuilder builder = ResultBuilder.withStatusAndScope(Result.Status.OK, Scope.PARAMETERS)
.error(ResultErrorHelper.requiresOption("webhookUrl", parameters));
if (builder.build().getErrors().isEmpty()) {
verifyCredentials(builder, parameters);
}
return builder.build();
}
// *********************************
// Connectivity validation
// *********************************
@Override
protected Result verifyConnectivity(Map parameters) {
return ResultBuilder.withStatusAndScope(Result.Status.OK, Scope.CONNECTIVITY)
.error(parameters, this::verifyCredentials).build();
}
@SuppressWarnings("PMD.AvoidCatchingGenericException")
private void verifyCredentials(ResultBuilder builder, Map parameters) {
String webhookUrl = (String) parameters.get("webhookUrl");
try {
HttpClient client = HttpClientBuilder.create().useSystemProperties().build();
HttpPost httpPost = new HttpPost(webhookUrl);
// Build Helper object
SlackMessage slackMessage;
slackMessage = new SlackMessage();
slackMessage.setText("Test connection");
// Set the post body
String json = asJson(slackMessage);
StringEntity body = new StringEntity(json);
// Do the post
httpPost.setEntity(body);
HttpResponse response = client.execute(httpPost);
// 2xx is OK, anything else we regard as failure
if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() > 299) {
builder.error(ResultErrorBuilder
.withCodeAndDescription(VerificationError.StandardCode.AUTHENTICATION,
"Invalid webhookUrl")
.parameterKey("webhookUrl").build());
}
} catch (Exception e) {
builder.error(ResultErrorBuilder
.withCodeAndDescription(VerificationError.StandardCode.AUTHENTICATION,
"Invalid webhookUrl")
.parameterKey("webhookUrl").build());
}
}
protected String asJson(SlackMessage message) {
Map jsonMap = new HashMap<>();
// Put the values in a map
jsonMap.put("text", message.getText());
// Generate a JSONObject
JSONObject jsonObject = new JSONObject(jsonMap);
// Return the string based on the JSON Object
return JSONObject.toJSONString(jsonObject);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy