samz406
4 years ago
committed by
GitHub
7 changed files with 413 additions and 0 deletions
@ -0,0 +1,56 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" |
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||||
|
<parent> |
||||||
|
<artifactId>dolphinscheduler-alert-plugin</artifactId> |
||||||
|
<groupId>org.apache.dolphinscheduler</groupId> |
||||||
|
<version>1.3.2-SNAPSHOT</version> |
||||||
|
</parent> |
||||||
|
<modelVersion>4.0.0</modelVersion> |
||||||
|
|
||||||
|
<artifactId>dolphinscheduler-alert-http</artifactId> |
||||||
|
|
||||||
|
<dependencies> |
||||||
|
<dependency> |
||||||
|
<groupId>org.apache.dolphinscheduler</groupId> |
||||||
|
<artifactId>dolphinscheduler-spi</artifactId> |
||||||
|
<scope>provided</scope> |
||||||
|
</dependency> |
||||||
|
|
||||||
|
<dependency> |
||||||
|
<groupId>com.google.guava</groupId> |
||||||
|
<artifactId>guava</artifactId> |
||||||
|
</dependency> |
||||||
|
|
||||||
|
<dependency> |
||||||
|
<groupId>ch.qos.logback</groupId> |
||||||
|
<artifactId>logback-classic</artifactId> |
||||||
|
</dependency> |
||||||
|
|
||||||
|
<dependency> |
||||||
|
<groupId>org.apache.httpcomponents</groupId> |
||||||
|
<artifactId>httpclient</artifactId> |
||||||
|
</dependency> |
||||||
|
|
||||||
|
<dependency> |
||||||
|
<groupId>com.fasterxml.jackson.core</groupId> |
||||||
|
<artifactId>jackson-databind</artifactId> |
||||||
|
<scope>provided</scope> |
||||||
|
</dependency> |
||||||
|
|
||||||
|
<dependency> |
||||||
|
<groupId>junit</groupId> |
||||||
|
<artifactId>junit</artifactId> |
||||||
|
<scope>test</scope> |
||||||
|
</dependency> |
||||||
|
|
||||||
|
<dependency> |
||||||
|
<groupId>org.mockito</groupId> |
||||||
|
<artifactId>mockito-core</artifactId> |
||||||
|
<type>jar</type> |
||||||
|
<scope>test</scope> |
||||||
|
</dependency> |
||||||
|
</dependencies> |
||||||
|
|
||||||
|
</project> |
@ -0,0 +1,42 @@ |
|||||||
|
/* |
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||||
|
* contributor license agreements. See the NOTICE file distributed with |
||||||
|
* this work for additional information regarding copyright ownership. |
||||||
|
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.plugin.alert.http; |
||||||
|
|
||||||
|
import org.apache.dolphinscheduler.spi.alert.AlertChannel; |
||||||
|
import org.apache.dolphinscheduler.spi.alert.AlertData; |
||||||
|
import org.apache.dolphinscheduler.spi.alert.AlertInfo; |
||||||
|
import org.apache.dolphinscheduler.spi.alert.AlertResult; |
||||||
|
import org.apache.dolphinscheduler.spi.params.PluginParamsTransfer; |
||||||
|
|
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* http alert channel,use sms message to seed the alertInfo |
||||||
|
*/ |
||||||
|
public class HttpAlertChannel implements AlertChannel { |
||||||
|
@Override |
||||||
|
public AlertResult process(AlertInfo alertInfo) { |
||||||
|
|
||||||
|
AlertData alertData = alertInfo.getAlertData(); |
||||||
|
String alertParams = alertInfo.getAlertParams(); |
||||||
|
Map<String, String> paramsMap = PluginParamsTransfer.getPluginParamsMap(alertParams); |
||||||
|
|
||||||
|
return new HttpSender(paramsMap).send(alertData.getContent()); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,78 @@ |
|||||||
|
/* |
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||||
|
* contributor license agreements. See the NOTICE file distributed with |
||||||
|
* this work for additional information regarding copyright ownership. |
||||||
|
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.plugin.alert.http; |
||||||
|
|
||||||
|
import org.apache.dolphinscheduler.spi.alert.AlertChannel; |
||||||
|
import org.apache.dolphinscheduler.spi.alert.AlertChannelFactory; |
||||||
|
import org.apache.dolphinscheduler.spi.params.InputParam; |
||||||
|
import org.apache.dolphinscheduler.spi.params.base.PluginParams; |
||||||
|
import org.apache.dolphinscheduler.spi.params.base.Validate; |
||||||
|
|
||||||
|
import java.util.Arrays; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
/** |
||||||
|
* http alert factory |
||||||
|
*/ |
||||||
|
public class HttpAlertChannelFactory implements AlertChannelFactory { |
||||||
|
@Override |
||||||
|
public String getName() { |
||||||
|
return "http alert"; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public List<PluginParams> getParams() { |
||||||
|
|
||||||
|
InputParam url = InputParam.newBuilder(HttpAlertConstants.URL, HttpAlertConstants.URL) |
||||||
|
.addValidate(Validate.newBuilder() |
||||||
|
.setRequired(true) |
||||||
|
.build()) |
||||||
|
.build(); |
||||||
|
|
||||||
|
InputParam headerParams = InputParam.newBuilder(HttpAlertConstants.HEADER_PARAMS, HttpAlertConstants.HEADER_PARAMS) |
||||||
|
.addValidate(Validate.newBuilder() |
||||||
|
.setRequired(true) |
||||||
|
.build()) |
||||||
|
.build(); |
||||||
|
|
||||||
|
InputParam bodyParams = InputParam.newBuilder(HttpAlertConstants.BODY_PARAMS, HttpAlertConstants.BODY_PARAMS) |
||||||
|
.addValidate(Validate.newBuilder() |
||||||
|
.setRequired(true) |
||||||
|
.build()) |
||||||
|
.build(); |
||||||
|
|
||||||
|
InputParam contentField = InputParam.newBuilder(HttpAlertConstants.CONTENT_FIELD, HttpAlertConstants.CONTENT_FIELD) |
||||||
|
.addValidate(Validate.newBuilder() |
||||||
|
.setRequired(true) |
||||||
|
.build()) |
||||||
|
.build(); |
||||||
|
|
||||||
|
InputParam requestType = InputParam.newBuilder(HttpAlertConstants.REQUEST_TYPE, HttpAlertConstants.REQUEST_TYPE) |
||||||
|
.addValidate(Validate.newBuilder() |
||||||
|
.setRequired(true) |
||||||
|
.build()) |
||||||
|
.build(); |
||||||
|
|
||||||
|
return Arrays.asList(url, requestType, headerParams, bodyParams, contentField); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public AlertChannel create() { |
||||||
|
return new HttpAlertChannel(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,33 @@ |
|||||||
|
/* |
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||||
|
* contributor license agreements. See the NOTICE file distributed with |
||||||
|
* this work for additional information regarding copyright ownership. |
||||||
|
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.plugin.alert.http; |
||||||
|
|
||||||
|
public class HttpAlertConstants { |
||||||
|
|
||||||
|
|
||||||
|
public static final String URL = "url"; |
||||||
|
|
||||||
|
public static final String HEADER_PARAMS = "headerParams"; |
||||||
|
|
||||||
|
public static final String BODY_PARAMS = "bodyParams"; |
||||||
|
|
||||||
|
public static final String CONTENT_FIELD = "contentField"; |
||||||
|
|
||||||
|
public static final String REQUEST_TYPE = "requestType"; |
||||||
|
|
||||||
|
} |
@ -0,0 +1,33 @@ |
|||||||
|
/* |
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||||
|
* contributor license agreements. See the NOTICE file distributed with |
||||||
|
* this work for additional information regarding copyright ownership. |
||||||
|
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.plugin.alert.http; |
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableList; |
||||||
|
import org.apache.dolphinscheduler.spi.DolphinSchedulerPlugin; |
||||||
|
import org.apache.dolphinscheduler.spi.alert.AlertChannelFactory; |
||||||
|
|
||||||
|
/** |
||||||
|
* http alertPlugins |
||||||
|
*/ |
||||||
|
public class HttpAlertPlugin implements DolphinSchedulerPlugin { |
||||||
|
|
||||||
|
@Override |
||||||
|
public Iterable<AlertChannelFactory> getAlertChannelFactorys() { |
||||||
|
return ImmutableList.of(new HttpAlertChannelFactory()); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,170 @@ |
|||||||
|
/* |
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||||
|
* contributor license agreements. See the NOTICE file distributed with |
||||||
|
* this work for additional information regarding copyright ownership. |
||||||
|
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.plugin.alert.http; |
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.node.ObjectNode; |
||||||
|
import org.apache.dolphinscheduler.spi.alert.AlertResult; |
||||||
|
import org.apache.dolphinscheduler.spi.utils.JSONUtils; |
||||||
|
import org.apache.dolphinscheduler.spi.utils.StringUtils; |
||||||
|
import org.apache.http.HttpEntity; |
||||||
|
import org.apache.http.client.methods.CloseableHttpResponse; |
||||||
|
import org.apache.http.client.methods.HttpGet; |
||||||
|
import org.apache.http.client.methods.HttpPost; |
||||||
|
import org.apache.http.client.methods.HttpRequestBase; |
||||||
|
import org.apache.http.entity.StringEntity; |
||||||
|
import org.apache.http.impl.client.CloseableHttpClient; |
||||||
|
import org.apache.http.impl.client.HttpClientBuilder; |
||||||
|
import org.apache.http.util.EntityUtils; |
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
|
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
/** |
||||||
|
*http send message |
||||||
|
*/ |
||||||
|
public class HttpSender { |
||||||
|
|
||||||
|
public static final Logger logger = LoggerFactory.getLogger(HttpSender.class); |
||||||
|
|
||||||
|
private String url; |
||||||
|
|
||||||
|
private String headerParams; |
||||||
|
|
||||||
|
private String bodyParams; |
||||||
|
|
||||||
|
private String contentField; |
||||||
|
|
||||||
|
private String requestType; |
||||||
|
|
||||||
|
private HttpRequestBase httpRequest; |
||||||
|
|
||||||
|
|
||||||
|
private static final String URL_SPLICE_CHAR = "?"; |
||||||
|
|
||||||
|
/** |
||||||
|
* request type post |
||||||
|
*/ |
||||||
|
private static final String REQUEST_TYPE_POST = "POST"; |
||||||
|
|
||||||
|
/** |
||||||
|
* request type get |
||||||
|
*/ |
||||||
|
private static final String REQUEST_TYPE_GET = "GET"; |
||||||
|
|
||||||
|
private static final String DEFAULT_CHARSET = "utf-8"; |
||||||
|
|
||||||
|
public HttpSender(Map<String, String> paramsMap){ |
||||||
|
|
||||||
|
url = paramsMap.get(HttpAlertConstants.URL); |
||||||
|
headerParams = paramsMap.get(HttpAlertConstants.HEADER_PARAMS); |
||||||
|
bodyParams = paramsMap.get(HttpAlertConstants.BODY_PARAMS); |
||||||
|
contentField = paramsMap.get(HttpAlertConstants.CONTENT_FIELD); |
||||||
|
requestType = paramsMap.get(HttpAlertConstants.REQUEST_TYPE); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public AlertResult send(String msg) { |
||||||
|
|
||||||
|
AlertResult alertResult = new AlertResult(); |
||||||
|
|
||||||
|
getHttpRequest(msg); |
||||||
|
|
||||||
|
if (httpRequest == null){ |
||||||
|
alertResult.setStatus("false"); |
||||||
|
alertResult.setMessage("Request types are not supported"); |
||||||
|
return alertResult; |
||||||
|
} |
||||||
|
|
||||||
|
try { |
||||||
|
CloseableHttpClient httpClient = HttpClientBuilder.create().build(); |
||||||
|
CloseableHttpResponse response = httpClient.execute(httpRequest); |
||||||
|
HttpEntity entity = response.getEntity(); |
||||||
|
String resp = EntityUtils.toString(entity, DEFAULT_CHARSET); |
||||||
|
alertResult.setStatus("true"); |
||||||
|
alertResult.setMessage(resp); |
||||||
|
}catch (Exception e) { |
||||||
|
logger.error("send sms alert msg exception : {}", e.getMessage()); |
||||||
|
alertResult.setStatus("false"); |
||||||
|
alertResult.setMessage("send http request alert fail."); |
||||||
|
} |
||||||
|
|
||||||
|
return alertResult; |
||||||
|
} |
||||||
|
|
||||||
|
private void getHttpRequest(String msg){ |
||||||
|
|
||||||
|
if (REQUEST_TYPE_POST.equals(requestType)){ |
||||||
|
httpRequest = new HttpPost(url); |
||||||
|
//POST request add param in request body
|
||||||
|
setMsgInRequestBody(msg); |
||||||
|
}else if (REQUEST_TYPE_GET.equals(requestType)){ |
||||||
|
//GET request add param in url
|
||||||
|
setMsgInUrl(msg); |
||||||
|
httpRequest = new HttpGet(url); |
||||||
|
} |
||||||
|
setHeader(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* add msg param in url |
||||||
|
* @param msg |
||||||
|
*/ |
||||||
|
private void setMsgInUrl(String msg) { |
||||||
|
|
||||||
|
if (StringUtils.isNotBlank(contentField)){ |
||||||
|
String type = "&"; |
||||||
|
//check splice char is & or ?
|
||||||
|
if (!url.contains(URL_SPLICE_CHAR)){ |
||||||
|
type = URL_SPLICE_CHAR; |
||||||
|
} |
||||||
|
url = String.format("%s%s%s=%s", url, type, contentField, msg); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* set header params |
||||||
|
*/ |
||||||
|
private void setHeader(){ |
||||||
|
|
||||||
|
if (httpRequest == null){ |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
HashMap<String,Object> map = JSONUtils.parseObject(headerParams, HashMap.class); |
||||||
|
for (Map.Entry<String,Object> entry : map.entrySet()) { |
||||||
|
httpRequest.setHeader(entry.getKey(), String.valueOf(entry.getValue())); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
*set body params |
||||||
|
* @param msg |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
private String setMsgInRequestBody(String msg){ |
||||||
|
ObjectNode objectNode = JSONUtils.parseObject(bodyParams); |
||||||
|
//set msg content field
|
||||||
|
objectNode.put(contentField,msg); |
||||||
|
return objectNode.toString(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue