Skip to content

Commit

Permalink
Release 3.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Salman-Queueit committed Feb 28, 2018
1 parent 8f40c87 commit 7b5b054
Show file tree
Hide file tree
Showing 4 changed files with 424 additions and 170 deletions.
167 changes: 95 additions & 72 deletions SDK/src/queueit/knownuserv3/sdk/KnownUser.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package queueit.knownuserv3.sdk;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
Expand All @@ -11,9 +12,10 @@
import queueit.knownuserv3.sdk.integrationconfig.*;

public class KnownUser {

public static final String QueueITTokenKey = "queueittoken";
public static final String QueueITDebugKey = "queueitdebug";
public static final String QueueITAjaxHeaderKey = "x-queueit-ajaxpageurl";
private static IUserInQueueService _userInQueueService;

private static IUserInQueueService getUserInQueueService(HttpServletRequest request, HttpServletResponse response) {
Expand All @@ -37,7 +39,7 @@ public static RequestValidationResult validateRequestByIntegrationConfig(String
HttpServletResponse response, String secretKey) throws Exception {

Map<String, String> debugEntries = new HashMap<>();

try {
boolean isDebug = getIsDebug(queueitToken, secretKey);
if (isDebug) {
Expand All @@ -55,7 +57,7 @@ public static RequestValidationResult validateRequestByIntegrationConfig(String
throw new KnowUserException("customerIntegrationInfo can not be null.");
}

Cookie[] cookies = request != null ? request.getCookies() : new Cookie[0];
Cookie[] cookies = request != null ? request.getCookies() : new Cookie[0];

IntegrationEvaluator configEvaluater = new IntegrationEvaluator();

Expand All @@ -64,26 +66,23 @@ public static RequestValidationResult validateRequestByIntegrationConfig(String

if (isDebug) {
String matchedConfigName = (matchedConfig != null) ? matchedConfig.Name : "NULL";
debugEntries.put("MatchedConfig", matchedConfigName);
debugEntries.put("MatchedConfig", matchedConfigName);
}

if (matchedConfig == null) {
return new RequestValidationResult(null, null, null, null, null);
}

// unspecified or 'Queue' specified
if(Utils.isNullOrWhiteSpace(matchedConfig.ActionType) || ActionType.QUEUE_ACTION.equals(matchedConfig.ActionType)) {
if (Utils.isNullOrWhiteSpace(matchedConfig.ActionType) || ActionType.QUEUE_ACTION.equals(matchedConfig.ActionType)) {
return handleQueueAction(matchedConfig, currentUrlWithoutQueueITToken, customerIntegrationInfo, queueitToken, customerId, request, response, secretKey, debugEntries);
}
else if (ActionType.CANCEL_ACTION.equals(matchedConfig.ActionType)){
} else if (ActionType.CANCEL_ACTION.equals(matchedConfig.ActionType)) {
return handleCancelAction(matchedConfig, customerIntegrationInfo, currentUrlWithoutQueueITToken, queueitToken, customerId, request, response, secretKey, debugEntries);
}
// for all unknown types default to 'Ignore'
} // for all unknown types default to 'Ignore'
else {
return handleIgnoreAction(request, response);
}
}
finally {
} finally {
setDebugCookie(debugEntries, request, response);
}
}
Expand All @@ -92,24 +91,23 @@ public static RequestValidationResult cancelRequestByLocalConfig(
String targetUrl, String queueitToken, CancelEventConfig cancelConfig,
String customerId, HttpServletRequest request,
HttpServletResponse response, String secretKey) throws Exception {

Map<String, String> debugEntries = new HashMap<>();

try {
return cancelRequestByLocalConfig(
targetUrl, queueitToken, cancelConfig, customerId, request, response, secretKey, debugEntries);
}
finally {
targetUrl = generateTargetUrl(targetUrl, request);
return cancelRequestByLocalConfig(targetUrl, queueitToken, cancelConfig, customerId, request, response, secretKey, debugEntries);
} finally {
setDebugCookie(debugEntries, request, response);
}
}

private static RequestValidationResult cancelRequestByLocalConfig(
String targetUrl, String queueitToken, CancelEventConfig cancelConfig,
String customerId, HttpServletRequest request,
HttpServletResponse response, String secretKey,
HttpServletResponse response, String secretKey,
Map<String, String> debugEntries) throws Exception {

boolean isDebug = getIsDebug(queueitToken, secretKey);
if (isDebug) {
debugEntries.put("TargetUrl", targetUrl);
Expand All @@ -118,7 +116,7 @@ private static RequestValidationResult cancelRequestByLocalConfig(
debugEntries.put("OriginalUrl", getOriginalUrl(request));
logMoreRequestDetails(debugEntries, request);
}

if (Utils.isNullOrWhiteSpace(targetUrl)) {
throw new Exception("targetUrl can not be null or empty.");
}
Expand All @@ -137,33 +135,35 @@ private static RequestValidationResult cancelRequestByLocalConfig(
if (Utils.isNullOrWhiteSpace(cancelConfig.getQueueDomain())) {
throw new Exception("QueueDomain from cancelConfig can not be null or empty.");
}

IUserInQueueService userInQueueService = getUserInQueueService(request, response);
return userInQueueService.validateCancelRequest(targetUrl, cancelConfig, customerId, secretKey);
RequestValidationResult result = userInQueueService.validateCancelRequest(targetUrl, cancelConfig, customerId, secretKey);
result.isAjaxResult = isQueueAjaxCall(request);

return result;
}

public static RequestValidationResult resolveQueueRequestByLocalConfig(
String targetUrl, String queueitToken, QueueEventConfig queueConfig,
String customerId, HttpServletRequest request,
HttpServletResponse response, String secretKey) throws Exception {

Map<String, String> debugEntries = new HashMap<>();

try {
return resolveQueueRequestByLocalConfig(
targetUrl, queueitToken, queueConfig, customerId, request, response, secretKey, debugEntries);
}
finally {
targetUrl = generateTargetUrl(targetUrl, request);
return resolveQueueRequestByLocalConfig(targetUrl, queueitToken, queueConfig, customerId, request, response, secretKey, debugEntries);
} finally {
setDebugCookie(debugEntries, request, response);
}
}

private static RequestValidationResult resolveQueueRequestByLocalConfig(
String targetUrl, String queueitToken, QueueEventConfig queueConfig,
String customerId, HttpServletRequest request,
HttpServletResponse response, String secretKey,
HttpServletResponse response, String secretKey,
Map<String, String> debugEntries) throws Exception {

boolean isDebug = getIsDebug(queueitToken, secretKey);
if (isDebug) {
debugEntries.put("TargetUrl", targetUrl);
Expand All @@ -172,7 +172,7 @@ private static RequestValidationResult resolveQueueRequestByLocalConfig(
debugEntries.put("OriginalUrl", getOriginalUrl(request));
logMoreRequestDetails(debugEntries, request);
}

if (Utils.isNullOrWhiteSpace(customerId)) {
throw new Exception("customerId can not be null or empty.");
}
Expand All @@ -196,7 +196,10 @@ private static RequestValidationResult resolveQueueRequestByLocalConfig(
}

IUserInQueueService userInQueueService = getUserInQueueService(request, response);
return userInQueueService.validateQueueRequest(targetUrl, queueitToken, queueConfig, customerId, secretKey);
RequestValidationResult result = userInQueueService.validateQueueRequest(targetUrl, queueitToken, queueConfig, customerId, secretKey);
result.isAjaxResult = isQueueAjaxCall(request);

return result;
}

public static void extendQueueCookie(String eventId,
Expand All @@ -205,7 +208,7 @@ public static void extendQueueCookie(String eventId,
HttpServletRequest request,
HttpServletResponse response,
String secretKey) throws Exception {

if (Utils.isNullOrWhiteSpace(eventId)) {
throw new Exception("eventId can not be null or empty.");
}
Expand All @@ -215,52 +218,52 @@ public static void extendQueueCookie(String eventId,
if (Utils.isNullOrWhiteSpace(secretKey)) {
throw new Exception("secretKey can not be null or empty.");
}

IUserInQueueService userInQueueService = getUserInQueueService(request, response);
userInQueueService.extendQueueCookie(eventId, cookieValidityMinute, cookieDomain, secretKey);
}

private static void setDebugCookie(Map<String, String> debugEntries, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
if(debugEntries.isEmpty())
if (debugEntries.isEmpty()) {
return;

}

ICookieManager cookieManager = new CookieManager(request, response);
String cookieValue = "";
for (Map.Entry<String, String> entry : debugEntries.entrySet()) {
cookieValue += (entry.getKey() + "=" + entry.getValue() + "|");
cookieValue += (entry.getKey() + "=" + entry.getValue() + "|");
}
if(!"".equals(cookieValue))
if (!"".equals(cookieValue)) {
cookieValue = cookieValue.substring(0, cookieValue.length() - 1); // remove trailing char

}
cookieManager.setCookie(QueueITDebugKey, cookieValue, null, null);
}

private static void logMoreRequestDetails(Map<String, String> debugEntries, HttpServletRequest request) {
debugEntries.put("ServerUtcTime", Instant.now().toString());
debugEntries.put("RequestIP", request.getRemoteAddr());
debugEntries.put("RequestHttpHeader_Via", request.getHeader("via") != null ? request.getHeader("via") : "");
debugEntries.put("RequestHttpHeader_Forwarded", request.getHeader("forwarded") != null ? request.getHeader("forwarded") : "");
debugEntries.put("RequestHttpHeader_XForwardedFor", request.getHeader("x-forwarded-for") != null ? request.getHeader("x-forwarded-for") : "");
debugEntries.put("RequestHttpHeader_XForwardedHost", request.getHeader("x-forwarded-host") != null ? request.getHeader("x-forwarded-host") : "");
debugEntries.put("RequestHttpHeader_XForwardedProto", request.getHeader("x-forwarded-proto") != null ? request.getHeader("x-forwarded-proto") : "");
debugEntries.put("RequestHttpHeader_XForwardedProto", request.getHeader("x-forwarded-proto") != null ? request.getHeader("x-forwarded-proto") : "");
}

private static boolean getIsDebug(String queueitToken, String secretKey) throws Exception
{

private static boolean getIsDebug(String queueitToken, String secretKey) throws Exception {
QueueUrlParams qParams = QueueParameterHelper.extractQueueParams(queueitToken);
if (qParams != null && qParams.getRedirectType() != null && "debug".equals(qParams.getRedirectType().toLowerCase())) {
String hash = HashHelper.generateSHA256Hash(secretKey, qParams.getQueueITTokenWithoutHash());
return qParams.getHashCode().equals(hash);
}
return false;
}
private static String getOriginalUrl(HttpServletRequest request){
return (request.getQueryString() != null)
? String.join("", request.getRequestURL(), "?",request.getQueryString())
: request.getRequestURL().toString();

private static String getOriginalUrl(HttpServletRequest request) {
return (request.getQueryString() != null)
? String.join("", request.getRequestURL(), "?", request.getQueryString())
: request.getRequestURL().toString();
}

private static RequestValidationResult handleQueueAction(IntegrationConfigModel matchedConfig, String currentUrlWithoutQueueITToken, CustomerIntegration customerIntegrationInfo, String queueitToken, String customerId, HttpServletRequest request, HttpServletResponse response, String secretKey, Map<String, String> debugEntries) throws Exception {
String targetUrl;
switch (matchedConfig.RedirectLogic) {
Expand All @@ -272,10 +275,10 @@ private static RequestValidationResult handleQueueAction(IntegrationConfigModel
targetUrl = "";
break;
default:
targetUrl = currentUrlWithoutQueueITToken;
targetUrl = generateTargetUrl(currentUrlWithoutQueueITToken, request);
break;
}

QueueEventConfig queueConfig = new QueueEventConfig();
queueConfig.setQueueDomain(matchedConfig.QueueDomain);
queueConfig.setCulture(matchedConfig.Culture);
Expand All @@ -285,9 +288,8 @@ private static RequestValidationResult handleQueueAction(IntegrationConfigModel
queueConfig.setCookieValidityMinute(matchedConfig.CookieValidityMinute);
queueConfig.setCookieDomain(matchedConfig.CookieDomain);
queueConfig.setVersion(customerIntegrationInfo.Version);

return resolveQueueRequestByLocalConfig(
targetUrl, queueitToken, queueConfig, customerId, request, response, secretKey, debugEntries);

return resolveQueueRequestByLocalConfig(targetUrl, queueitToken, queueConfig, customerId, request, response, secretKey, debugEntries);
}

private static RequestValidationResult handleCancelAction(IntegrationConfigModel matchedConfig, CustomerIntegration customerIntegrationInfo, String currentUrlWithoutQueueITToken, String queueitToken, String customerId, HttpServletRequest request, HttpServletResponse response, String secretKey, Map<String, String> debugEntries) throws Exception {
Expand All @@ -296,20 +298,37 @@ private static RequestValidationResult handleCancelAction(IntegrationConfigModel
cancelConfig.setEventId(matchedConfig.EventId);
cancelConfig.setCookieDomain(matchedConfig.CookieDomain);
cancelConfig.setVersion(customerIntegrationInfo.Version);
return cancelRequestByLocalConfig(
currentUrlWithoutQueueITToken, queueitToken, cancelConfig, customerId, request, response, secretKey, debugEntries);

String targetUrl = generateTargetUrl(currentUrlWithoutQueueITToken, request);
return cancelRequestByLocalConfig(targetUrl, queueitToken, cancelConfig, customerId, request, response, secretKey, debugEntries);
}

private static RequestValidationResult handleIgnoreAction(HttpServletRequest request, HttpServletResponse response) {
IUserInQueueService userInQueueService = getUserInQueueService(request, response);
return userInQueueService.getIgnoreActionResult();
RequestValidationResult result = userInQueueService.getIgnoreActionResult();
result.isAjaxResult = isQueueAjaxCall(request);
return result;
}

private static String generateTargetUrl(String originalTargetUrl, HttpServletRequest request) {
try {
return !isQueueAjaxCall(request)
? originalTargetUrl
: URLDecoder.decode(request.getHeader(QueueITAjaxHeaderKey), "UTF-8");
} catch (UnsupportedEncodingException e) {
}
return "";
}

private static boolean isQueueAjaxCall(HttpServletRequest request) {
return !Utils.isNullOrWhiteSpace(request.getHeader(QueueITAjaxHeaderKey));
}
}

interface ICookieManager {

void setCookie(String cookieName, String cookieValue, Integer expiration, String cookieDomain);

String getCookie(String cookieName);
}

Expand All @@ -318,26 +337,28 @@ class CookieManager implements ICookieManager {
HttpServletRequest request;
HttpServletResponse response;

public CookieManager(HttpServletRequest request,
HttpServletResponse response) {
public CookieManager(HttpServletRequest request, HttpServletResponse response) {
this.request = request;
this.response = response;
}

@Override
public void setCookie(String cookieName, String cookieValue, Integer expiration, String cookieDomain) {
if(response == null)
if (response == null) {
return;

}

Cookie cookie = new Cookie(cookieName, cookieValue);
if (cookieValue == null) {
cookieValue = "";
}
try {
cookie.setValue(URLEncoder.encode(cookieValue, "UTF-8"));
} catch (UnsupportedEncodingException ex) {}
if(expiration != null)
} catch (UnsupportedEncodingException ex) {
}
if (expiration != null) {
cookie.setMaxAge(expiration);
}
cookie.setPath("/");
if (!Utils.isNullOrWhiteSpace(cookieDomain)) {
cookie.setDomain(cookieDomain);
Expand All @@ -348,9 +369,10 @@ public void setCookie(String cookieName, String cookieValue, Integer expiration,

@Override
public String getCookie(String cookieName) {
if(request == null)
if (request == null) {
return null;

}

Cookie[] cookies = request.getCookies();
if (cookies == null) {
return null;
Expand All @@ -360,7 +382,8 @@ public String getCookie(String cookieName) {
if (cookie.getName().equals(cookieName)) {
try {
return URLDecoder.decode(cookie.getValue(), "UTF-8");
} catch (UnsupportedEncodingException ex) { }
} catch (UnsupportedEncodingException ex) {
}
}
}
return null;
Expand Down
Loading

0 comments on commit 7b5b054

Please sign in to comment.