Skip to content

Commit

Permalink
HWKALERTS-68 Move Persona into a filter
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasponce committed Jul 31, 2015
1 parent 40c5638 commit 311a552
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 245 deletions.
Expand Up @@ -18,18 +18,19 @@

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

import static org.hawkular.alerts.rest.HawkularAlertsApp.TENANT_HEADER_NAME;

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

import javax.ejb.EJB;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import org.hawkular.accounts.api.model.Persona;
import org.hawkular.alerts.api.services.DefinitionsService;
import org.jboss.logging.Logger;

Expand All @@ -50,8 +51,8 @@
public class ActionPluginHandler {
private final Logger log = Logger.getLogger(ActionPluginHandler.class);

@Inject
Persona persona;
@HeaderParam(TENANT_HEADER_NAME)
String tenantId;

@EJB
DefinitionsService definitions;
Expand All @@ -69,9 +70,6 @@ public ActionPluginHandler() {
@ApiResponse(code = 200, message = "Success."),
@ApiResponse(code = 500, message = "Internal server error")})
public Response findActionPlugins() {
if (!checkPersona()) {
return ResponseUtil.internalError("No persona found");
}
try {
Collection<String> actionPlugins = definitions.getActionPlugins();
log.debugf("ActionPlugins: %s ", actionPlugins);
Expand All @@ -95,9 +93,6 @@ public Response findActionPlugins() {
public Response getActionPlugin(@ApiParam(value = "Action plugin to query", required = true)
@PathParam ("actionPlugin")
final String actionPlugin) {
if (!checkPersona()) {
return ResponseUtil.internalError("No persona found");
}
try {
Set<String> actionPluginProps = definitions.getActionPlugin(actionPlugin);
log.debugf("ActionPlugin: %s - Properties: %s ", actionPlugin, actionPluginProps);
Expand All @@ -111,22 +106,6 @@ public Response getActionPlugin(@ApiParam(value = "Action plugin to query", requ
}
}

private boolean checkPersona() {
if (persona == null) {
log.warn("Persona is null. Possible issue with accounts integration ? ");
return false;
}
if (isEmpty(persona.getId())) {
log.warn("Persona is empty. Possible issue with accounts integration ? ");
return false;
}
return true;
}

private boolean isEmpty(String s) {
return s == null || s.trim().isEmpty();
}

private boolean isEmpty(Collection collection) {
return collection == null || collection.isEmpty();
}
Expand Down
Expand Up @@ -18,23 +18,24 @@

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

import static org.hawkular.alerts.rest.HawkularAlertsApp.TENANT_HEADER_NAME;

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

import javax.ejb.EJB;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
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.hawkular.accounts.api.model.Persona;
import org.hawkular.alerts.api.services.ActionsService;
import org.hawkular.alerts.api.services.DefinitionsService;
import org.jboss.logging.Logger;
Expand All @@ -55,8 +56,8 @@
public class ActionsHandler {
private final Logger log = Logger.getLogger(ActionsHandler.class);

@Inject
Persona persona;
@HeaderParam(TENANT_HEADER_NAME)
String tenantId;

@EJB
DefinitionsService definitions;
Expand All @@ -77,11 +78,8 @@ public ActionsHandler() {
@ApiResponse(code = 200, message = "Success."),
@ApiResponse(code = 500, message = "Internal server error") })
public Response findActions() {
if (!checkPersona()) {
return ResponseUtil.internalError("No persona found");
}
try {
Map<String, Set<String>> actions = definitions.getActions(persona.getId());
Map<String, Set<String>> actions = definitions.getActions(tenantId);
log.debugf("Actions: ", actions);
return ResponseUtil.ok(actions);
} catch (Exception e) {
Expand All @@ -102,11 +100,8 @@ public Response findActionsByPlugin(@ApiParam(value = "Action plugin to filter q
required = true)
@PathParam("actionPlugin")
final String actionPlugin) {
if (!checkPersona()) {
return ResponseUtil.internalError("No persona found");
}
try {
Collection<String> actions = definitions.getActions(persona.getId(), actionPlugin);
Collection<String> actions = definitions.getActions(tenantId, actionPlugin);
log.debugf("Actions: %s ", actions);
return ResponseUtil.ok(actions);
} catch (Exception e) {
Expand All @@ -132,9 +127,6 @@ public Response createAction(@ApiParam(value = "Action properties. Properties de
name = "actionProperties",
required = true)
final Map<String, String> actionProperties) {
if (!checkPersona()) {
return ResponseUtil.internalError("No persona found");
}
String actionPlugin = actionProperties.get("actionPlugin");
String actionId = actionProperties.get("actionId");
if (isEmpty(actionPlugin)) {
Expand All @@ -144,10 +136,10 @@ public Response createAction(@ApiParam(value = "Action properties. Properties de
return ResponseUtil.badRequest("actionId must be not null");
}
try {
if (definitions.getAction(persona.getId(), actionPlugin, actionId) != null) {
if (definitions.getAction(tenantId, actionPlugin, actionId) != null) {
return ResponseUtil.badRequest("Existing action: " + actionId);
} else {
definitions.addAction(persona.getId(), actionPlugin, actionId, actionProperties);
definitions.addAction(tenantId, actionPlugin, actionId, actionProperties);
log.debugf("ActionId: %s - Properties: %s ", actionId, actionProperties);
return ResponseUtil.ok(actionProperties);
}
Expand All @@ -174,11 +166,8 @@ public Response getAction(@ApiParam(value = "Action plugin", required = true)
@ApiParam(value = "Action id to be retrieved", required = true)
@PathParam("actionId")
final String actionId) {
if (!checkPersona()) {
return ResponseUtil.internalError("No persona found");
}
try {
Map<String, String> actionProperties = definitions.getAction(persona.getId(), actionPlugin, actionId);
Map<String, String> actionProperties = definitions.getAction(tenantId, actionPlugin, actionId);
log.debugf("ActionId: %s - Properties: %s ", actionId, actionProperties);
if (isEmpty(actionProperties)) {
return ResponseUtil.notFound("Not action found for actionPlugin: " + actionPlugin + " and actionId: "
Expand Down Expand Up @@ -212,11 +201,8 @@ public Response updateAction(@ApiParam(value = "Action plugin", required = true)
@ApiParam(value = "Action properties. Properties depend of specific ActionPlugin.", required = true)
final Map<String, String> actionProperties) {
try {
if (!checkPersona()) {
return ResponseUtil.internalError("No persona found");
}
if (definitions.getAction(persona.getId(), actionPlugin, actionId) != null) {
definitions.updateAction(persona.getId(), actionPlugin, actionId, actionProperties);
if (definitions.getAction(tenantId, actionPlugin, actionId) != null) {
definitions.updateAction(tenantId, actionPlugin, actionId, actionProperties);
log.debugf("ActionId: %s - Properties: %s ", actionId, actionProperties);
return ResponseUtil.ok(actionProperties);
} else {
Expand All @@ -242,11 +228,8 @@ public Response deleteAction(@ApiParam(value = "Action plugin", required = true)
@PathParam("actionId")
final String actionId) {
try {
if (!checkPersona()) {
return ResponseUtil.internalError("No persona found");
}
if (definitions.getAction(persona.getId(), actionPlugin, actionId) != null) {
definitions.removeAction(persona.getId(), actionPlugin, actionId);
if (definitions.getAction(tenantId, actionPlugin, actionId) != null) {
definitions.removeAction(tenantId, actionPlugin, actionId);
log.debugf("ActionId: %s ", actionId);
return ResponseUtil.ok();
} else {
Expand All @@ -258,26 +241,10 @@ public Response deleteAction(@ApiParam(value = "Action plugin", required = true)
}
}

private boolean checkPersona() {
if (persona == null) {
log.warn("Persona is null. Possible issue with accounts integration ? ");
return false;
}
if (isEmpty(persona.getId())) {
log.warn("Persona is empty. Possible issue with accounts integration ? ");
return false;
}
return true;
}

private boolean isEmpty(Map map) {
return map == null || map.isEmpty();
}

private boolean isEmpty(Collection collection) {
return collection == null || collection.isEmpty();
}

private boolean isEmpty(String s) {
return s == null || s.trim().isEmpty();
}
Expand Down

0 comments on commit 311a552

Please sign in to comment.