-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
237 additions
and
12 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
...va/org/craftercms/deployer/impl/processors/notification/WebhookNotificationProcessor.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package org.craftercms.deployer.impl.processors.notification; | ||
|
||
import org.apache.commons.configuration2.Configuration; | ||
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.HttpUriRequest; | ||
import org.apache.http.entity.ContentType; | ||
import org.apache.http.entity.StringEntity; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClients; | ||
import org.craftercms.commons.config.ConfigurationException; | ||
import org.craftercms.deployer.api.Deployment; | ||
import org.craftercms.deployer.api.exceptions.DeployerException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.io.UnsupportedEncodingException; | ||
|
||
public class WebhookNotificationProcessor extends NotificationProcessor<WebhookNotificationProcessor.WebhookMessage> { | ||
private static final Logger logger = LoggerFactory.getLogger(WebhookNotificationProcessor.class); | ||
|
||
private static final String URL_CONFIG_KEY = "url"; | ||
private static final String METHOD_CONFIG_KEY = "method"; | ||
private static final String CONTENT_TYPE_CONFIG_KEY = "contentType"; | ||
|
||
private String defaultMethod; | ||
private String defaultContentType; | ||
|
||
private String method; | ||
private String url; | ||
private String contentType; | ||
private CloseableHttpClient httpClient; | ||
|
||
|
||
@Override | ||
public void doInit(Configuration config) throws ConfigurationException, DeployerException { | ||
super.doInit(config); | ||
method = config.getString(METHOD_CONFIG_KEY, defaultMethod); | ||
contentType = config.getString(CONTENT_TYPE_CONFIG_KEY, defaultContentType); | ||
url = config.getString(URL_CONFIG_KEY); | ||
|
||
httpClient = HttpClients.createDefault(); | ||
} | ||
|
||
@Override | ||
protected WebhookMessage doCreateMessage(Deployment deployment) { | ||
return new WebhookMessage(); | ||
} | ||
|
||
@Override | ||
protected void doNotify(WebhookMessage message) throws DeployerException { | ||
logger.info("Sending webhook notification to {} with method {}", url, method); | ||
try { | ||
HttpUriRequest request = createRequest(message); | ||
try (CloseableHttpResponse response = httpClient.execute(request)) { | ||
logger.info("Webhook notification sent with status {}", response.getStatusLine()); | ||
} | ||
} catch (IOException e) { | ||
logger.error("Error sending webhook notification", e); | ||
throw new DeployerException(e); | ||
} | ||
} | ||
|
||
private HttpUriRequest createRequest(NotificationMessage message) throws DeployerException, UnsupportedEncodingException { | ||
if (method.equalsIgnoreCase("get")) { | ||
return new HttpGet(url); | ||
} | ||
if (method.equalsIgnoreCase("post")) { | ||
HttpPost request = new HttpPost(url); | ||
request.setEntity(new StringEntity(message.getBody(), ContentType.getByMimeType(contentType))); | ||
return request; | ||
} | ||
throw new DeployerException("HTTP method '" + method + " not supported"); | ||
} | ||
|
||
public void setDefaultMethod(String defaultMethod) { | ||
this.defaultMethod = defaultMethod; | ||
} | ||
|
||
public void setDefaultContentType(String defaultContentType) { | ||
this.defaultContentType = defaultContentType; | ||
} | ||
|
||
public class WebhookMessage extends NotificationMessage { | ||
} | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
{ | ||
"blocks": [ | ||
{ | ||
"type": "rich_text", | ||
"elements": [ | ||
{ | ||
"type": "rich_text_section", | ||
"elements": [ | ||
{ | ||
"type": "text", | ||
"text": "A deployment in server ${serverName} has just finished:\n" | ||
} | ||
] | ||
}, | ||
{ | ||
"type": "rich_text_list", | ||
"style": "bullet", | ||
"indent": 0, | ||
"border": 1, | ||
"elements": [ | ||
{ | ||
"type": "rich_text_section", | ||
"elements": [ | ||
{ | ||
"type": "text", | ||
"text": "Target ID", | ||
"style": { | ||
"bold": true | ||
} | ||
}, | ||
{ | ||
"type": "text", | ||
"text": ": ${targetId}" | ||
} | ||
] | ||
}, | ||
{ | ||
"type": "rich_text_section", | ||
"elements": [ | ||
{ | ||
"type": "text", | ||
"text": "Start", | ||
"style": { | ||
"bold": true | ||
} | ||
}, | ||
{ | ||
"type": "text", | ||
"text": ": ${start}" | ||
} | ||
] | ||
}, | ||
{ | ||
"type": "rich_text_section", | ||
"elements": [ | ||
{ | ||
"type": "text", | ||
"text": "End", | ||
"style": { | ||
"bold": true | ||
} | ||
}, | ||
{ | ||
"type": "text", | ||
"text": ": ${end}" | ||
} | ||
] | ||
}, | ||
{ | ||
"type": "rich_text_section", | ||
"elements": [ | ||
{ | ||
"type": "text", | ||
"text": "Status", | ||
"style": { | ||
"bold": true | ||
} | ||
}, | ||
{ | ||
"type": "text", | ||
"text": ": ${status}" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} |