Skip to content

Commit

Permalink
Merge branch 'master' into WebHooks
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasponce committed Oct 27, 2015
2 parents ec3b371 + 589a6e2 commit c515521
Show file tree
Hide file tree
Showing 19 changed files with 1,033 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* 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 org.hawkular.alerts.actions.api;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Define a plugin as global.
* Global plugins process all messages without filtering per plugin name.
* Global annotation is used in combination with Plugin annotation.
*
* @author Lucas Ponce
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Global {
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hawkular.alerts.actions.api.ActionPluginListener;
import org.hawkular.alerts.actions.api.ActionPluginSender;
import org.hawkular.alerts.actions.api.Global;
import org.hawkular.alerts.actions.api.Plugin;
import org.hawkular.alerts.actions.api.Sender;
import org.jboss.vfs.VirtualFile;
Expand All @@ -41,6 +44,7 @@
public class ActionPlugins {
private static ActionPlugins instance;
private Map<String, ActionPluginListener> plugins;
private Set<String> globals;
private Map<String, ActionPluginSender> senders;

public static synchronized Map<String, ActionPluginListener> getPlugins() {
Expand All @@ -50,6 +54,13 @@ public static synchronized Map<String, ActionPluginListener> getPlugins() {
return Collections.unmodifiableMap(instance.plugins);
}

public static synchronized Set<String> getGlobals() {
if (instance == null) {
instance = new ActionPlugins();
}
return Collections.unmodifiableSet(instance.globals);
}

public static synchronized Map<String, ActionPluginSender> getSenders() {
if (instance == null) {
instance = new ActionPlugins();
Expand All @@ -60,6 +71,7 @@ public static synchronized Map<String, ActionPluginSender> getSenders() {
private ActionPlugins() {
try {
plugins = new HashMap<>();
globals = new HashSet<>();
senders = new HashMap<>();
URL webInfUrl = getWebInfUrl();
List<Class> pluginClasses = findAnnotationInClasses(webInfUrl, Plugin.class);
Expand All @@ -72,6 +84,9 @@ private ActionPlugins() {
ActionPluginListener pluginInstance = (ActionPluginListener)newInstance;
injectActionPluginSender(name, pluginInstance);
plugins.put(name, pluginInstance);
if (pluginClass.isAnnotationPresent(Global.class)) {
globals.add(name);
}
} else {
throw new IllegalStateException("Plugin [" + name + "] is not instance of " +
"ActionPluginListener");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.hawkular.alerts.actions.bus;

import java.util.Collection;
import java.util.Set;

import javax.annotation.PreDestroy;
import javax.ejb.ActivationConfigProperty;
Expand Down Expand Up @@ -58,14 +59,26 @@ protected void onBasicMessage(BusActionMessage basicMessage) {
}
String actionPlugin = basicMessage.getAction().getActionPlugin();
ActionPluginListener plugin = ActionPlugins.getPlugins().get(actionPlugin);
if (plugin == null) {
Set<String> globals = ActionPlugins.getGlobals();
if (plugin == null && ActionPlugins.getGlobals().isEmpty()) {
log.debug("Received action [" + actionPlugin + "] but no ActionPluginListener found on this deployment");
return;
}
try {
plugin.process(basicMessage);
log.debugf("Plugin [%s] has received a action message: [%s]", actionPlugin,
basicMessage.getMessageId().getId());
if (plugin != null) {
plugin.process(basicMessage);
log.debugf("Plugin [%s] has received a action message: [%s]", actionPlugin,
basicMessage.getMessageId().getId());
}
// Check if the plugin is executed twice
if (!globals.contains(actionPlugin)) {
for (String global : globals) {
ActionPluginListener globalPlugin = ActionPlugins.getPlugins().get(global);
globalPlugin.process(basicMessage);
log.debugf("Global plugin [%s] has received a action message: [%s]", global,
basicMessage.getMessageId().getId());
}
}
} catch (Exception e) {
msgLog.error("Plugin [" + actionPlugin + "] processing error", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2015 Red Hat, Inc. and/or its affiliates
and other contributors as indicated by the @author tags.
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.
-->
<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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.hawkular.alerts</groupId>
<artifactId>hawkular-alerts-actions-plugins</artifactId>
<version>0.6.0-SNAPSHOT</version>
</parent>

<artifactId>hawkular-alerts-actions-webhook</artifactId>
<packaging>war</packaging>

<name>Hawkular Alerts Action Webhook Plugin</name>

<profiles>

<profile>
<id>bus</id>
<dependencies>

<!-- Hawkular Accounts dependencies -->
<dependency>
<groupId>org.hawkular.accounts</groupId>
<artifactId>hawkular-accounts-api</artifactId>
<version>${version.org.hawkular.accounts}</version>
</dependency>

</dependencies>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>

<profile>
<id>standalone</id>
<build>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<excludes>
<exclude>org/hawkular/alerts/actions/webhook/accounts/*.java</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<executions>
<execution>
<id>bus-war-package</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
<configuration>
<warSourceDirectory>src/main/webapp-standalone</warSourceDirectory>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>
</profile>

</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* 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 org.hawkular.alerts.actions.webhook;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;

import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;

import org.hawkular.alerts.actions.api.MsgLogger;
import org.jboss.logging.Logger;

/**
* Base class for REST WebHook plugin
* @author Jay Shaughnessy
* @author Lucas Ponce
*/
@ApplicationPath("/")
public class WebHookApp extends Application {
private final MsgLogger msgLog = MsgLogger.LOGGER;
private static final Logger log = Logger.getLogger(WebHookApp.class);

public static final String TENANT_HEADER_NAME = "Hawkular-Tenant";

public WebHookApp() {
log.debugf("Hawkular Alerts WebHook starting...");
}

public static Response internalError(String message) {
Map<String, String> errors = new HashMap<>();
errors.put("errorMsg", "Internal error: " + message);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(errors).type(APPLICATION_JSON_TYPE).build();
}

public static Response ok(Object entity) {
return Response.status(Response.Status.OK).entity(entity).type(APPLICATION_JSON_TYPE).build();
}

public static Response ok() {
return Response.status(Response.Status.OK).type(APPLICATION_JSON_TYPE).build();
}

public static Response badRequest(String message) {
Map<String, String> errors = new HashMap<>();
errors.put("errorMsg", "Bad request: " + message);
return Response.status(Response.Status.BAD_REQUEST)
.entity(errors).type(APPLICATION_JSON_TYPE).build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* 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 org.hawkular.alerts.actions.webhook;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;

import static org.hawkular.alerts.actions.webhook.WebHookApp.TENANT_HEADER_NAME;

import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import org.jboss.logging.Logger;

/**
* @author Jay Shaughnessy
* @author Lucas Ponce
*/
@Path("/")
public class WebHookHandler {
private static final Logger log = Logger.getLogger(WebHookHandler.class);

@HeaderParam(TENANT_HEADER_NAME)
String tenantId;

@GET
@Path("/")
@Produces(APPLICATION_JSON)
public Response findWebHooks() {
return WebHookApp.ok(WebHooks.getWebHooks(tenantId));
}

@PUT
@Path("/")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
public Response registerWebHook(final Map<String, String> webhook) {
if (webhook == null || webhook.isEmpty()) {
return WebHookApp.badRequest("webhook must be not null");
}
if (!webhook.containsKey("url")) {
return WebHookApp.badRequest("webhook must contain an url");
}
String url = webhook.get("url");
String filter = webhook.containsKey("filter") ? webhook.get("filter") : null;
try {
WebHooks.addWebHook(tenantId, filter, url);
return WebHookApp.ok();
} catch (Exception e) {
log.debugf(e.getMessage(), e);
return WebHookApp.internalError(e.getMessage());
}
}

@DELETE
@Path("/{url}")
public Response unregisterWebHook(
@PathParam("url")
final String url) {
if (url == null || url.isEmpty()) {
return WebHookApp.badRequest("webhook url must be not null");
}
try {
WebHooks.removeWebHook(tenantId, url);
return WebHookApp.ok();
} catch (Exception e) {
log.debugf(e.getMessage(), e);
return WebHookApp.internalError(e.getMessage());
}
}
}

0 comments on commit c515521

Please sign in to comment.