Skip to content

Commit

Permalink
Support for UserAgent trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
frwiqueueit committed Aug 14, 2017
1 parent d604bdb commit 289dbac
Show file tree
Hide file tree
Showing 6 changed files with 525 additions and 31 deletions.
5 changes: 3 additions & 2 deletions SDK/src/queueit/knownuserv3/sdk/KnownUser.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package queueit.knownuserv3.sdk;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import queueit.knownuserv3.sdk.integrationconfig.*;


public class KnownUser {

public static final String QueueITTokenKey = "queueittoken";
Expand Down Expand Up @@ -38,8 +38,9 @@ public static RequestValidationResult validateRequestByIntegrationConfig(String
}
Cookie[] cookies = request != null ? request.getCookies() : new Cookie[0];
IntegrationEvaluator configEvaluater = new IntegrationEvaluator();
String userAgent = request.getHeader("User-Agent");
IntegrationConfigModel matchedConfig = configEvaluater.getMatchedIntegrationConfig(
customerIntegrationInfo, currentUrlWithoutQueueITToken, cookies);
customerIntegrationInfo, currentUrlWithoutQueueITToken, cookies, userAgent != null ? userAgent : "");
if (matchedConfig == null) {
return new RequestValidationResult(null, null, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ final class ValidatorType {

public static final String URL_VALIDATOR = "UrlValidator";
public static final String COOKIE_VALIDATOR = "CookieValidator";
public static final String USERAGENT_VALIDATOR = "UserAgentValidator";
}

final class UrlPartType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ interface IIntegrationEvaluator {
IntegrationConfigModel getMatchedIntegrationConfig(
CustomerIntegration customerIntegration,
String currentPageUrl,
Cookie[] cookies);
Cookie[] cookies,
String userAgent);
}

public class IntegrationEvaluator implements IIntegrationEvaluator {
Expand All @@ -18,10 +19,11 @@ public class IntegrationEvaluator implements IIntegrationEvaluator {
public IntegrationConfigModel getMatchedIntegrationConfig(
CustomerIntegration customerIntegration,
String currentPageUrl,
Cookie[] cookies) {
Cookie[] cookies,
String userAgent) {
for (IntegrationConfigModel integration : customerIntegration.Integrations) {
for (TriggerModel trigger : integration.Triggers) {
if (evaluateTrigger(trigger, currentPageUrl, cookies)) {
if (evaluateTrigger(trigger, currentPageUrl, cookies, userAgent)) {
return integration;
}
}
Expand All @@ -32,17 +34,18 @@ public IntegrationConfigModel getMatchedIntegrationConfig(
private boolean evaluateTrigger(
TriggerModel trigger,
String currentPageUrl,
Cookie[] cookies) {
Cookie[] cookies,
String userAgent) {
if (trigger.LogicalOperator.equals(LogicalOperatorType.OR)) {
for (TriggerPart part : trigger.TriggerParts) {
if (evaluateTriggerPart(part, currentPageUrl, cookies)) {
if (evaluateTriggerPart(part, currentPageUrl, cookies, userAgent)) {
return true;
}
}
return false;
} else {
for (TriggerPart part : trigger.TriggerParts) {
if (!evaluateTriggerPart(part, currentPageUrl, cookies)) {
if (!evaluateTriggerPart(part, currentPageUrl, cookies, userAgent)) {
return false;
}
}
Expand All @@ -53,12 +56,15 @@ private boolean evaluateTrigger(
private boolean evaluateTriggerPart(
TriggerPart triggerPart,
String currentPageUrl,
Cookie[] cookies) {
Cookie[] cookies,
String userAgent) {
switch (triggerPart.ValidatorType) {
case ValidatorType.URL_VALIDATOR:
return UrlValidatorHelper.evaluate(triggerPart, currentPageUrl);
case ValidatorType.COOKIE_VALIDATOR:
return CookieValidatorHelper.evaluate(triggerPart, cookies);
case ValidatorType.USERAGENT_VALIDATOR:
return UserAgentValidatorHelper.evaluate(triggerPart, userAgent);
default:
return false;
}
Expand Down Expand Up @@ -135,6 +141,16 @@ private static String getCookie(String cookieName, Cookie[] cookieCollection) {
}
}

final class UserAgentValidatorHelper {

public static boolean evaluate(TriggerPart triggerPart, String userAgent) {
return ComparisonOperatorHelper.evaluate(triggerPart.Operator,
triggerPart.IsNegative,
triggerPart.IsIgnoreCase,
userAgent,
triggerPart.ValueToCompare);
}
}
final class ComparisonOperatorHelper {

public static boolean evaluate(String opt, boolean isNegative, boolean isIgnoreCase, String left, String right) {
Expand Down
Loading

0 comments on commit 289dbac

Please sign in to comment.